summaryrefslogtreecommitdiff
path: root/vendor/ruflin/elastica/test/lib/Elastica/Test/Transport/ThriftTest.php
blob: b73ef4f7cec00228796f01641fd182943fcd7dbd (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
<?php
namespace Elastica\Test\Transport;

use Elastica\Connection;
use Elastica\Document;
use Elastica\Index;
use Elastica\Test\Base as BaseTest;

class ThriftTest extends BaseTest
{
    public static function setUpBeforeClass()
    {
        if (!class_exists('Elasticsearch\\RestClient')) {
            self::markTestSkipped('munkie/elasticsearch-thrift-php package should be installed to run thrift transport tests');
        }
    }

    /**
     * @group unit
     */
    public function testConstruct()
    {
        $host = $this->_getHost();
        $port = 9500;
        $client = $this->_getClient(array('host' => $host, 'port' => $port, 'transport' => 'Thrift'));

        $this->assertEquals($host, $client->getConnection()->getHost());
        $this->assertEquals($port, $client->getConnection()->getPort());
    }

    /**
     * @group functional
     * @dataProvider configProvider
     */
    public function testSearchRequest($config)
    {
        $this->_checkPlugin();

        // Creates a new index 'xodoa' and a type 'user' inside this index
        $client = $this->_getClient($config);

        $index = $client->getIndex('elastica_test1');
        $index->create(array(), true);

        $type = $index->getType('user');

        // Adds 1 document to the index
        $doc1 = new Document(1,
            array('username' => 'hans', 'test' => array('2', '3', '5'))
        );
        $doc1->setVersion(0);
        $type->addDocument($doc1);

        // Adds a list of documents with _bulk upload to the index
        $docs = array();
        $docs[] = new Document(2,
            array('username' => 'john', 'test' => array('1', '3', '6'))
        );
        $docs[] = new Document(3,
            array('username' => 'rolf', 'test' => array('2', '3', '7'))
        );
        $type->addDocuments($docs);

        // Refresh index
        $index->refresh();
        $resultSet = $type->search('rolf');

        $this->assertEquals(1, $resultSet->getTotalHits());
    }

    /**
     * @group unit
     * @expectedException \Elastica\Exception\ConnectionException
     */
    public function testInvalidHostRequest()
    {
        $this->_checkPlugin();

        $client = $this->_getClient(array('host' => 'unknown', 'port' => 9555, 'transport' => 'Thrift'));
        $client->getStatus();
    }

    /**
     * @group functional
     * @expectedException \Elastica\Exception\ResponseException
     */
    public function testInvalidElasticRequest()
    {
        $this->_checkPlugin();

        $connection = new Connection();
        $connection->setHost($this->_getHost());
        $connection->setPort(9500);
        $connection->setTransport('Thrift');

        $client = $this->_getClient();
        $client->addConnection($connection);

        $index = new Index($client, 'missing_index');
        $index->getStatus();
    }

    public function configProvider()
    {
        return array(
            array(
                array(
                    'host' => $this->_getHost(),
                    'port' => 9500,
                    'transport' => 'Thrift',
                ),
            ),
            array(
                array(
                    'host' => $this->_getHost(),
                    'port' => 9500,
                    'transport' => 'Thrift',
                    'config' => array(
                        'framedTransport' => false,
                        'sendTimeout' => 10000,
                        'recvTimeout' => 20000,
                    ),
                ),
            ),
        );
    }

    protected function _checkPlugin()
    {
        $nodes = $this->_getClient()->getCluster()->getNodes();
        if (!$nodes[0]->getInfo()->hasPlugin('transport-thrift')) {
            $this->markTestSkipped('transport-thrift plugin not installed.');
        }
    }
}