summaryrefslogtreecommitdiff
path: root/vendor/ruflin/elastica/test/lib/Elastica/Test/Transport/MemcacheTest.php
blob: 308970733fa67b338f94c545ac957b31b4b8d501 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php
namespace Elastica\Test\Transport;

use Elastica\Document;
use Elastica\Query;
use Elastica\Query\QueryString;
use Elastica\Request;
use Elastica\Test\Base as BaseTest;

class MemcacheTest extends BaseTest
{
    public static function setUpBeforeClass()
    {
        if (!extension_loaded('Memcache')) {
            self::markTestSkipped('pecl/memcache must be installed to run this test case');
        }
    }

    protected function _getMemcacheClient()
    {
        return $this->_getClient(array(
            'host' => $this->_getHost(),
            'port' => 11211,
            'transport' => 'Memcache',
        ));
    }

    /**
     * @group functional
     */
    public function testConstruct()
    {
        $client = $this->_getMemcacheClient();
        $this->assertEquals($this->_getHost(), $client->getConnection()->getHost());
        $this->assertEquals(11211, $client->getConnection()->getPort());
    }

    /**
     * @group functional
     */
    public function testCreateDocument()
    {
        $index = $this->_createIndex();
        $this->_waitForAllocation($index);
        $type = $index->getType('foo');

        // Create document
        $document = new Document(1, array('username' => 'John Doe'));
        $type->addDocument($document);
        $index->refresh();

        // Check it was saved
        $document = $type->getDocument(1);
        $this->assertEquals('John Doe', $document->get('username'));
    }

    /**
     * @group functional
     * @expectedException Elastica\Exception\NotFoundException
     */
    public function testDeleteDocument()
    {
        $index = $this->_createIndex();
        $this->_waitForAllocation($index);
        $type = $index->getType('foo');

        // Create document
        $document = new Document(1, array('username' => 'John Doe'));
        $type->addDocument($document);
        $index->refresh();

        // Delete document
        $type->deleteById(1);

        // Check if document is not exists
        $document = $type->getDocument(1);
    }

    /**
     * @group functional
     */
    public function testUpdateDocument()
    {
        $index = $this->_createIndex();
        $this->_waitForAllocation($index);
        $type = $index->getType('foo');

        // Create document
        $document = new Document(1, array('username' => 'John Doe'));
        $type->addDocument($document);
        $index->refresh();

        // Check it was saved
        $savedDocument = $type->getDocument(1);
        $this->assertEquals('John Doe', $savedDocument->get('username'));

        // Update document
        $newDocument = new Document(1, array('username' => 'Doe John'));
        $type->updateDocument($newDocument);
        $index->refresh();

        // Check it was updated
        $newSavedDocument = $type->getDocument(1);
        $this->assertEquals('Doe John', $newSavedDocument->get('username'));
    }

    /**
     * @group functional
     */
    public function testSearchDocument()
    {
        $index = $this->_createIndex();
        $this->_waitForAllocation($index);
        $type = $index->getType('fruits');

        // Create documents
        $docs = array(
            new Document(1, array('name' => 'banana')),
            new Document(2, array('name' => 'apple')),
            new Document(3, array('name' => 'orange')),
        );
        $type->addDocuments($docs);
        $index->refresh();

        // Search documents
        $queryString = new QueryString('orange');
        $query = new Query($queryString);
        $resultSet = $type->search($query);

        // Check if correct document was found
        $this->assertEquals(1, $resultSet->getTotalHits());
        $this->assertEquals(3, $resultSet[0]->getId());
        $data = $resultSet[0]->getData();
        $this->assertEquals('orange', $data['name']);
    }

    /**
     * @group functional
     * @expectedException Elastica\Exception\InvalidException
     * @expectedExceptionMessage is not supported in memcache transport
     */
    public function testHeadRequest()
    {
        $client = $this->_getMemcacheClient();
        $client->request('foo', Request::HEAD);
    }

    /**
     * @group functional
     * @expectedException Elastica\Exception\InvalidException
     * @expectedExceptionMessage is not supported in memcache transport
     */
    public function testInvalidRequest()
    {
        $client = $this->_getMemcacheClient();
        $client->request('foo', 'its_fail');
    }

    /**
     * @group functional
     * @expectedException Elastica\Exception\Connection\MemcacheException
     * @expectedExceptionMessage is too long
     */
    public function testRequestWithLongPath()
    {
        $client = $this->_getMemcacheClient();
        $index = $client->getIndex('memcache-test');
        $index->create();

        $this->_waitForAllocation($index);

        $queryString = new QueryString(str_repeat('z', 300));
        $query = new Query($queryString);
        $index->search($query);
    }
}