diff options
Diffstat (limited to 'vendor/ruflin/elastica/test/lib/Elastica/Test/Transport')
8 files changed, 649 insertions, 184 deletions
diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Transport/AbstractTransportTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Transport/AbstractTransportTest.php index 4f1c7114..20573cc7 100644 --- a/vendor/ruflin/elastica/test/lib/Elastica/Test/Transport/AbstractTransportTest.php +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Transport/AbstractTransportTest.php @@ -1,16 +1,14 @@ <?php - namespace Elastica\Test\Transport; +use Elastica\Connection; use Elastica\Transport\AbstractTransport; use Elastica\Transport\Http; -use Elastica\Connection; -use Elastica\Exception\InvalidException; class AbstractTransportTest extends \PHPUnit_Framework_TestCase { /** - * Return transport configuration and the expected HTTP method + * Return transport configuration and the expected HTTP method. * * @return array[] */ @@ -27,6 +25,7 @@ class AbstractTransportTest extends \PHPUnit_Framework_TestCase } /** + * @group unit * @dataProvider getValidDefinitions */ public function testCanCreateTransportInstances($transport) @@ -47,6 +46,7 @@ class AbstractTransportTest extends \PHPUnit_Framework_TestCase } /** + * @group unit * @dataProvider getInvalidDefinitions * @expectedException Elastica\Exception\InvalidException * @expectedExceptionMessage Invalid transport @@ -56,6 +56,9 @@ class AbstractTransportTest extends \PHPUnit_Framework_TestCase AbstractTransport::create($transport, new Connection()); } + /** + * @group unit + */ public function testCanInjectParamsWhenUsingArray() { $connection = new Connection(); diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Transport/GuzzleTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Transport/GuzzleTest.php index b2e385bb..04e7ee2d 100644 --- a/vendor/ruflin/elastica/test/lib/Elastica/Test/Transport/GuzzleTest.php +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Transport/GuzzleTest.php @@ -1,13 +1,10 @@ <?php - namespace Elastica\Test\Transport; -use Elastica\Client; use Elastica\Document; use Elastica\Query; use Elastica\ResultSet; use Elastica\Test\Base as BaseTest; -use Elastica\Exception\ResponseException; class GuzzleTest extends BaseTest { @@ -18,19 +15,8 @@ class GuzzleTest extends BaseTest } } - public function setUp() - { - if (defined('DEBUG') && !DEBUG) { - $this->markTestSkipped('The DEBUG constant must be set to true for this test to run'); - } - - if (!defined('DEBUG')) { - define('DEBUG', true); - } - } - /** - * Return transport configuration and the expected HTTP method + * Return transport configuration and the expected HTTP method. * * @return array[] */ @@ -38,26 +24,27 @@ class GuzzleTest extends BaseTest { return array( array( - array('transport' => 'Guzzle'), - 'GET' + array('persistent' => false, 'transport' => 'Guzzle'), + 'GET', ), array( - array('transport' => array('type' => 'Guzzle', 'postWithRequestBody' => false)), - 'GET' + array('persistent' => false, 'transport' => array('type' => 'Guzzle', 'postWithRequestBody' => false)), + 'GET', ), array( - array('transport' => array('type' => 'Guzzle', 'postWithRequestBody' => true)), - 'POST' + array('persistent' => false, 'transport' => array('type' => 'Guzzle', 'postWithRequestBody' => true)), + 'POST', ), ); } /** + * @group functional * @dataProvider getConfig */ public function testDynamicHttpMethodBasedOnConfigParameter(array $config, $httpMethod) { - $client = new Client($config); + $client = $this->_getClient($config); $index = $client->getIndex('dynamic_http_method_test'); $index->create(array(), true); @@ -70,22 +57,26 @@ class GuzzleTest extends BaseTest } /** + * @group functional * @dataProvider getConfig */ public function testDynamicHttpMethodOnlyAffectsRequestsWithBody(array $config, $httpMethod) { - $client = new Client($config); + $client = $this->_getClient($config); $status = $client->getStatus(); $info = $status->getResponse()->getTransferInfo(); $this->assertStringStartsWith('GET', $info['request_header']); } + /** + * @group functional + */ public function testWithEnvironmentalProxy() { - putenv('http_proxy=http://127.0.0.1:12345/'); + putenv('http_proxy='.$this->_getProxyUrl().'/'); - $client = new \Elastica\Client(array('transport' => 'Guzzle')); + $client = $this->_getClient(array('transport' => 'Guzzle', 'persistent' => false)); $transferInfo = $client->request('/_nodes')->getTransferInfo(); $this->assertEquals(200, $transferInfo['http_code']); @@ -96,16 +87,18 @@ class GuzzleTest extends BaseTest putenv('http_proxy='); } + /** + * @group functional + */ public function testWithEnabledEnvironmentalProxy() { - putenv('http_proxy=http://127.0.0.1:12346/'); - - $client = new \Elastica\Client(array('transport' => 'Guzzle')); + putenv('http_proxy='.$this->_getProxyUrl403().'/'); + $client = $this->_getClient(array('transport' => 'Guzzle', 'persistent' => false)); $transferInfo = $client->request('/_nodes')->getTransferInfo(); $this->assertEquals(403, $transferInfo['http_code']); - $client = new \Elastica\Client(); + $client = $this->_getClient(array('transport' => 'Guzzle', 'persistent' => false)); $client->getConnection()->setProxy(''); $transferInfo = $client->request('/_nodes')->getTransferInfo(); $this->assertEquals(200, $transferInfo['http_code']); @@ -113,31 +106,40 @@ class GuzzleTest extends BaseTest putenv('http_proxy='); } + /** + * @group functional + */ public function testWithProxy() { - $client = new \Elastica\Client(array('transport' => 'Guzzle')); - $client->getConnection()->setProxy('http://127.0.0.1:12345'); + $client = $this->_getClient(array('transport' => 'Guzzle', 'persistent' => false)); + $client->getConnection()->setProxy($this->_getProxyUrl()); $transferInfo = $client->request('/_nodes')->getTransferInfo(); $this->assertEquals(200, $transferInfo['http_code']); } + /** + * @group functional + */ public function testWithoutProxy() { - $client = new \Elastica\Client(array('transport' => 'Guzzle')); + $client = $this->_getClient(array('transport' => 'Guzzle', 'persistent' => false)); $client->getConnection()->setProxy(''); $transferInfo = $client->request('/_nodes')->getTransferInfo(); $this->assertEquals(200, $transferInfo['http_code']); } + /** + * @group functional + */ public function testBodyReuse() { - $client = new Client(array('transport' => 'Guzzle')); + $client = $this->_getClient(array('transport' => 'Guzzle', 'persistent' => false)); $index = $client->getIndex('elastica_body_reuse_test'); - $index->create(array(), true); + $this->_waitForAllocation($index); $type = $index->getType('test'); $type->addDocument(new Document(1, array('test' => 'test'))); @@ -160,4 +162,19 @@ class GuzzleTest extends BaseTest $this->assertEquals(1, $resultSet->getTotalHits()); } + /** + * @group unit + * @expectedException Elastica\Exception\Connection\GuzzleException + */ + public function testInvalidConnection() + { + $client = $this->_getClient(array('transport' => 'Guzzle', 'port' => 4500, 'persistent' => false)); + $response = $client->request('_status', 'GET'); + } + + protected function tearDown() + { + parent::tearDown(); + putenv('http_proxy='); + } } diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Transport/HttpTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Transport/HttpTest.php index 88c93bea..53ee105f 100644 --- a/vendor/ruflin/elastica/test/lib/Elastica/Test/Transport/HttpTest.php +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Transport/HttpTest.php @@ -1,29 +1,15 @@ <?php - namespace Elastica\Test\Transport; -use Elastica\Client; use Elastica\Document; use Elastica\Query; use Elastica\ResultSet; use Elastica\Test\Base as BaseTest; -use Elastica\Exception\ResponseException; class HttpTest extends BaseTest { - public function setUp() - { - if (defined('DEBUG') && !DEBUG) { - $this->markTestSkipped('The DEBUG constant must be set to true for this test to run'); - } - - if (!defined('DEBUG')) { - define('DEBUG', true); - } - } - /** - * Return transport configuration and the expected HTTP method + * Return transport configuration and the expected HTTP method. * * @return array[] */ @@ -31,30 +17,31 @@ class HttpTest extends BaseTest { return array( array( - array('transport' => 'Http'), - 'GET' + array('transport' => 'Http', 'curl' => array(CURLINFO_HEADER_OUT => true)), + 'GET', ), array( - array('transport' => array('type' => 'Http', 'postWithRequestBody' => false)), - 'GET' + array('transport' => array('type' => 'Http', 'postWithRequestBody' => false, 'curl' => array(CURLINFO_HEADER_OUT => true))), + 'GET', ), array( - array('transport' => array('type' => 'Http', 'postWithRequestBody' => true)), - 'POST' + array('transport' => array('type' => 'Http', 'postWithRequestBody' => true, 'curl' => array(CURLINFO_HEADER_OUT => true))), + 'POST', ), ); } /** + * @group functional * @dataProvider getConfig */ public function testDynamicHttpMethodBasedOnConfigParameter(array $config, $httpMethod) { - $client = new Client($config); + $client = $this->_getClient($config); $index = $client->getIndex('dynamic_http_method_test'); - $index->create(array(), true); + $this->_waitForAllocation($index); $type = $index->getType('test'); $type->addDocument(new Document(1, array('test' => 'test'))); @@ -68,23 +55,29 @@ class HttpTest extends BaseTest } /** + * @group functional * @dataProvider getConfig */ public function testDynamicHttpMethodOnlyAffectsRequestsWithBody(array $config, $httpMethod) { - $client = new Client($config); + $client = $this->_getClient($config); $status = $client->getStatus(); $info = $status->getResponse()->getTransferInfo(); $this->assertStringStartsWith('GET', $info['request_header']); } + /** + * @group functional + */ public function testCurlNobodyOptionIsResetAfterHeadRequest() { - $client = new \Elastica\Client(); + $client = $this->_getClient(); $index = $client->getIndex('curl_test'); - $type = $index->getType('item'); + $index->create(array(), true); + $this->_waitForAllocation($index); + $type = $index->getType('item'); // Force HEAD request to set CURLOPT_NOBODY = true $index->exists(); @@ -103,10 +96,16 @@ class HttpTest extends BaseTest $this->assertEquals($id, $doc->getId()); } + /** + * @group functional + */ public function testUnicodeData() { - $client = new \Elastica\Client(); + $client = $this->_getClient(); $index = $client->getIndex('curl_test'); + $index->create(array(), true); + $this->_waitForAllocation($index); + $type = $index->getType('item'); // Force HEAD request to set CURLOPT_NOBODY = true @@ -132,11 +131,14 @@ class HttpTest extends BaseTest $this->assertEquals($id, $doc->getId()); } + /** + * @group functional + */ public function testWithEnvironmentalProxy() { - putenv('http_proxy=http://127.0.0.1:12345/'); + putenv('http_proxy='.$this->_getProxyUrl().'/'); - $client = new \Elastica\Client(); + $client = $this->_getClient(); $transferInfo = $client->request('/_nodes')->getTransferInfo(); $this->assertEquals(200, $transferInfo['http_code']); @@ -147,48 +149,56 @@ class HttpTest extends BaseTest putenv('http_proxy='); } + /** + * @group functional + */ public function testWithEnabledEnvironmentalProxy() { - putenv('http_proxy=http://127.0.0.1:12346/'); - - $client = new \Elastica\Client(); - + putenv('http_proxy='.$this->_getProxyUrl403().'/'); + $client = $this->_getClient(); $transferInfo = $client->request('/_nodes')->getTransferInfo(); $this->assertEquals(403, $transferInfo['http_code']); - - $client = new \Elastica\Client(); + $client = $this->_getClient(); $client->getConnection()->setProxy(''); $transferInfo = $client->request('/_nodes')->getTransferInfo(); $this->assertEquals(200, $transferInfo['http_code']); - putenv('http_proxy='); } + /** + * @group functional + */ public function testWithProxy() { - $client = new \Elastica\Client(); - $client->getConnection()->setProxy('http://127.0.0.1:12345'); + $client = $this->_getClient(); + $client->getConnection()->setProxy($this->_getProxyUrl()); $transferInfo = $client->request('/_nodes')->getTransferInfo(); $this->assertEquals(200, $transferInfo['http_code']); } + /** + * @group functional + */ public function testWithoutProxy() { - $client = new \Elastica\Client(); + $client = $this->_getClient(); $client->getConnection()->setProxy(''); $transferInfo = $client->request('/_nodes')->getTransferInfo(); $this->assertEquals(200, $transferInfo['http_code']); } + /** + * @group functional + */ public function testBodyReuse() { - $client = new Client(); + $client = $this->_getClient(); $index = $client->getIndex('elastica_body_reuse_test'); - $index->create(array(), true); + $this->_waitForAllocation($index); $type = $index->getType('test'); $type->addDocument(new Document(1, array('test' => 'test'))); @@ -211,12 +221,16 @@ class HttpTest extends BaseTest $this->assertEquals(1, $resultSet->getTotalHits()); } + /** + * @group functional + */ public function testPostWith0Body() { - $client = new Client(); + $client = $this->_getClient(); $index = $client->getIndex('elastica_0_body'); $index->create(array(), true); + $this->_waitForAllocation($index); $index->refresh(); $tokens = $index->analyze('0'); @@ -224,4 +238,9 @@ class HttpTest extends BaseTest $this->assertNotEmpty($tokens); } + protected function tearDown() + { + parent::tearDown(); + putenv('http_proxy='); + } } diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Transport/MemcacheTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Transport/MemcacheTest.php index 17d46d88..30897073 100644 --- a/vendor/ruflin/elastica/test/lib/Elastica/Test/Transport/MemcacheTest.php +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Transport/MemcacheTest.php @@ -1,51 +1,176 @@ <?php - namespace Elastica\Test\Transport; -use Elastica\Client; use Elastica\Document; +use Elastica\Query; +use Elastica\Query\QueryString; +use Elastica\Request; use Elastica\Test\Base as BaseTest; class MemcacheTest extends BaseTest { - public function setUp() + public static function setUpBeforeClass() { if (!extension_loaded('Memcache')) { - $this->markTestSkipped('pecl/memcache must be installed to run this test case'); + self::markTestSkipped('pecl/memcache must be installed to run this test case'); } } - public function testExample() + protected function _getMemcacheClient() { - // Creates a new index 'xodoa' and a type 'user' inside this index - $host = 'localhost'; - $port = 11211; - $client = new Client(array('host' => $host, 'port' => $port, 'transport' => 'Memcache')); + return $this->_getClient(array( + 'host' => $this->_getHost(), + 'port' => 11211, + 'transport' => 'Memcache', + )); + } - $index = $client->getIndex('elastica_test1'); - $index->create(array(), true); + /** + * @group functional + */ + public function testConstruct() + { + $client = $this->_getMemcacheClient(); + $this->assertEquals($this->_getHost(), $client->getConnection()->getHost()); + $this->assertEquals(11211, $client->getConnection()->getPort()); + } - $type = $index->getType('user'); + /** + * @group functional + */ + public function testCreateDocument() + { + $index = $this->_createIndex(); + $this->_waitForAllocation($index); + $type = $index->getType('foo'); - // Adds 1 document to the index - $doc1 = new Document(1, - array('username' => 'hans', 'test' => array('2', '3', '5')) - ); - $type->addDocument($doc1); + // Create document + $document = new Document(1, array('username' => 'John Doe')); + $type->addDocument($document); + $index->refresh(); - // 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')) + // 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); - - // Refresh index $index->refresh(); - $this->markTestIncomplete('Memcache implementation is not finished yet'); - $resultSet = $type->search('rolf'); + + // 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); } } diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Transport/NullTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Transport/NullTest.php deleted file mode 100644 index c07f5da1..00000000 --- a/vendor/ruflin/elastica/test/lib/Elastica/Test/Transport/NullTest.php +++ /dev/null @@ -1,59 +0,0 @@ -<?php - -namespace Elastica\Test\Transport; - -use Elastica\Client; -use Elastica\Connection; -use Elastica\Query; -use Elastica\Test\Base as BaseTest; - -/** - * Elastica Null Transport Test - * - * @package Elastica - * @author James Boehmer <james.boehmer@jamesboehmer.com> - */ -class NullTest extends BaseTest -{ - - public function testEmptyResult() - { - // Creates a client with any destination, and verify it returns a response object when executed - $client = $this->_getClient(); - $connection = new Connection(array('transport' => 'Null')); - $client->setConnections(array($connection)); - - $index = $client->getIndex('elasticaNullTransportTest1'); - - $resultSet = $index->search(new Query()); - $this->assertNotNull($resultSet); - - $response = $resultSet->getResponse(); - $this->assertNotNull($response); - - // Validate most of the expected fields in the response data. Consumers of the response - // object have a reasonable expectation of finding "hits", "took", etc - $responseData = $response->getData(); - $this->assertContains("took", $responseData); - $this->assertEquals(0, $responseData["took"]); - $this->assertContains("_shards", $responseData); - $this->assertContains("hits", $responseData); - $this->assertContains("total", $responseData["hits"]); - $this->assertEquals(0, $responseData["hits"]["total"]); - $this->assertContains("params", $responseData); - - $took = $response->getEngineTime(); - $this->assertEquals(0, $took); - - $errorString = $response->getError(); - $this->assertEmpty($errorString); - - $shards = $response->getShardsStatistics(); - $this->assertContains("total", $shards); - $this->assertEquals(0, $shards["total"]); - $this->assertContains("successful", $shards); - $this->assertEquals(0, $shards["successful"]); - $this->assertContains("failed", $shards); - $this->assertEquals(0, $shards["failed"]); - } -} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Transport/NullTransportTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Transport/NullTransportTest.php new file mode 100644 index 00000000..cea3e3ba --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Transport/NullTransportTest.php @@ -0,0 +1,96 @@ +<?php +namespace Elastica\Test\Transport; + +use Elastica\Connection; +use Elastica\Query; +use Elastica\Request; +use Elastica\Test\Base as BaseTest; +use Elastica\Transport\NullTransport; + +/** + * Elastica Null Transport Test. + * + * @author James Boehmer <james.boehmer@jamesboehmer.com> + */ +class NullTransportTest extends BaseTest +{ + /** + * @group functional + */ + public function testEmptyResult() + { + // Creates a client with any destination, and verify it returns a response object when executed + $client = $this->_getClient(); + $connection = new Connection(array('transport' => 'NullTransport')); + $client->setConnections(array($connection)); + + $index = $client->getIndex('elasticaNullTransportTest1'); + + $resultSet = $index->search(new Query()); + $this->assertNotNull($resultSet); + + $response = $resultSet->getResponse(); + $this->assertNotNull($response); + + // Validate most of the expected fields in the response data. Consumers of the response + // object have a reasonable expectation of finding "hits", "took", etc + $responseData = $response->getData(); + $this->assertContains('took', $responseData); + $this->assertEquals(0, $responseData['took']); + $this->assertContains('_shards', $responseData); + $this->assertContains('hits', $responseData); + $this->assertContains('total', $responseData['hits']); + $this->assertEquals(0, $responseData['hits']['total']); + $this->assertContains('params', $responseData); + + $took = $response->getEngineTime(); + $this->assertEquals(0, $took); + + $errorString = $response->getError(); + $this->assertEmpty($errorString); + + $shards = $response->getShardsStatistics(); + $this->assertContains('total', $shards); + $this->assertEquals(0, $shards['total']); + $this->assertContains('successful', $shards); + $this->assertEquals(0, $shards['successful']); + $this->assertContains('failed', $shards); + $this->assertEquals(0, $shards['failed']); + } + + /** + * @group functional + */ + public function testExec() + { + $request = new Request('/test'); + $params = array('name' => 'ruflin'); + $transport = new NullTransport(); + $response = $transport->exec($request, $params); + + $this->assertInstanceOf('\Elastica\Response', $response); + + $data = $response->getData(); + $this->assertEquals($params, $data['params']); + } + + /** + * @group functional + */ + public function testOldObject() + { + if (version_compare(phpversion(), 7, '>=')) { + self::markTestSkipped('These objects are not supported in PHP 7'); + } + + $request = new Request('/test'); + $params = array('name' => 'ruflin'); + $transport = new \Elastica\Transport\Null(); + $response = $transport->exec($request, $params); + + $this->assertInstanceOf('\Elastica\Response', $response); + + $data = $response->getData(); + $this->assertEquals($params, $data['params']); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Transport/ThriftTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Transport/ThriftTest.php index f1698ff6..b73ef4f7 100644 --- a/vendor/ruflin/elastica/test/lib/Elastica/Test/Transport/ThriftTest.php +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Transport/ThriftTest.php @@ -1,12 +1,9 @@ <?php - namespace Elastica\Test\Transport; -use Elastica\Client; use Elastica\Connection; use Elastica\Document; use Elastica\Index; -use Elastica\Query; use Elastica\Test\Base as BaseTest; class ThriftTest extends BaseTest @@ -18,17 +15,21 @@ class ThriftTest extends BaseTest } } + /** + * @group unit + */ public function testConstruct() { - $host = 'localhost'; + $host = $this->_getHost(); $port = 9500; - $client = new Client(array('host' => $host, 'port' => $port, 'transport' => 'Thrift')); + $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) @@ -36,7 +37,7 @@ class ThriftTest extends BaseTest $this->_checkPlugin(); // Creates a new index 'xodoa' and a type 'user' inside this index - $client = new Client($config); + $client = $this->_getClient($config); $index = $client->getIndex('elastica_test1'); $index->create(array(), true); @@ -68,17 +69,19 @@ class ThriftTest extends BaseTest } /** + * @group unit * @expectedException \Elastica\Exception\ConnectionException */ public function testInvalidHostRequest() { $this->_checkPlugin(); - $client = new Client(array('host' => 'unknown', 'port' => 9555, 'transport' => 'Thrift')); + $client = $this->_getClient(array('host' => 'unknown', 'port' => 9555, 'transport' => 'Thrift')); $client->getStatus(); } /** + * @group functional * @expectedException \Elastica\Exception\ResponseException */ public function testInvalidElasticRequest() @@ -86,11 +89,11 @@ class ThriftTest extends BaseTest $this->_checkPlugin(); $connection = new Connection(); - $connection->setHost('localhost'); + $connection->setHost($this->_getHost()); $connection->setPort(9500); $connection->setTransport('Thrift'); - $client = new Client(); + $client = $this->_getClient(); $client->addConnection($connection); $index = new Index($client, 'missing_index'); @@ -102,23 +105,23 @@ class ThriftTest extends BaseTest return array( array( array( - 'host' => 'localhost', + 'host' => $this->_getHost(), 'port' => 9500, - 'transport' => 'Thrift' - ) + 'transport' => 'Thrift', + ), ), array( array( - 'host' => 'localhost', + 'host' => $this->_getHost(), 'port' => 9500, 'transport' => 'Thrift', 'config' => array( 'framedTransport' => false, 'sendTimeout' => 10000, 'recvTimeout' => 20000, - ) - ) - ) + ), + ), + ), ); } @@ -126,7 +129,7 @@ class ThriftTest extends BaseTest { $nodes = $this->_getClient()->getCluster()->getNodes(); if (!$nodes[0]->getInfo()->hasPlugin('transport-thrift')) { - $this->markTestSkipped("transport-thrift plugin not installed."); + $this->markTestSkipped('transport-thrift plugin not installed.'); } } } diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Transport/TransportBenchmarkTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Transport/TransportBenchmarkTest.php new file mode 100644 index 00000000..11a16a34 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Transport/TransportBenchmarkTest.php @@ -0,0 +1,261 @@ +<?php +namespace Elastica\Test\Transport; + +use Elastica\Document; +use Elastica\Index; +use Elastica\Query; +use Elastica\Test\Base as BaseTest; + +class TransportBenchmarkTest extends BaseTest +{ + protected $_max = 1000; + + protected $_maxData = 20; + + protected static $_results = array(); + + public static function tearDownAfterClass() + { + self::printResults(); + } + + /** + * @param array $config + * + * @return \Elastica\Type + */ + protected function getType(array $config) + { + $client = $this->_getClient($config); + $index = $client->getIndex('benchmark'.uniqid()); + $index->create(array('index' => array('number_of_shards' => 1, 'number_of_replicas' => 0)), true); + + return $index->getType('benchmark'); + } + + /** + * @dataProvider providerTransport + * @group benchmark + */ + public function testAddDocument(array $config, $transport) + { + $this->_checkThrift($transport); + + $type = $this->getType($config); + $index = $type->getIndex(); + $index->create(array(), true); + + $times = array(); + for ($i = 0; $i < $this->_max; $i++) { + $data = $this->getData($i); + $doc = new Document($i, $data); + $result = $type->addDocument($doc); + $times[] = $result->getQueryTime(); + $this->assertTrue($result->isOk()); + } + + $index->refresh(); + + self::logResults('insert', $transport, $times); + } + + /** + * @depends testAddDocument + * @dataProvider providerTransport + * @group benchmark + */ + public function testRandomRead(array $config, $transport) + { + $this->_checkThrift($transport); + + $type = $this->getType($config); + + $type->search('test'); + + $times = array(); + for ($i = 0; $i < $this->_max; $i++) { + $test = rand(1, $this->_max); + $query = new Query(); + $query->setQuery(new \Elastica\Query\MatchAll()); + $query->setPostFilter(new \Elastica\Filter\Term(array('test' => $test))); + $result = $type->search($query); + $times[] = $result->getResponse()->getQueryTime(); + } + + self::logResults('random read', $transport, $times); + } + + /** + * @depends testAddDocument + * @dataProvider providerTransport + * @group benchmark + */ + public function testBulk(array $config, $transport) + { + $type = $this->getType($config); + + $times = array(); + for ($i = 0; $i < $this->_max; $i++) { + $docs = array(); + for ($j = 0; $j < 10; $j++) { + $data = $this->getData($i.$j); + $docs[] = new Document($i, $data); + } + + $result = $type->addDocuments($docs); + $times[] = $result->getQueryTime(); + } + + self::logResults('bulk', $transport, $times); + } + + /** + * @dataProvider providerTransport + * @group benchmark + */ + public function testGetMapping(array $config, $transport) + { + $client = $this->_getClient($config); + $index = $client->getIndex('benchmark'); + $index->create(array(), true); + $type = $index->getType('mappingTest'); + + // Define mapping + $mapping = new \Elastica\Type\Mapping(); + $mapping->setParam('_boost', array('name' => '_boost', 'null_value' => 1.0)); + $mapping->setProperties(array( + 'id' => array('type' => 'integer', 'include_in_all' => false), + 'user' => array( + 'type' => 'object', + 'properties' => array( + 'name' => array('type' => 'string', 'include_in_all' => true), + 'fullName' => array('type' => 'string', 'include_in_all' => true), + ), + ), + 'msg' => array('type' => 'string', 'include_in_all' => true), + 'tstamp' => array('type' => 'date', 'include_in_all' => false), + 'location' => array('type' => 'geo_point', 'include_in_all' => false), + '_boost' => array('type' => 'float', 'include_in_all' => false), + )); + + $type->setMapping($mapping); + $index->refresh(); + + $times = array(); + for ($i = 0; $i < $this->_max; $i++) { + $response = $type->request('_mapping', \Elastica\Request::GET); + $times[] = $response->getQueryTime(); + } + self::logResults('get mapping', $transport, $times); + } + + public function providerTransport() + { + return array( + array( + array( + 'transport' => 'Http', + 'host' => $this->_getHost(), + 'port' => $this->_getPort(), + 'persistent' => false, + ), + 'Http:NotPersistent', + ), + array( + array( + 'transport' => 'Http', + 'host' => $this->_getHost(), + 'port' => $this->_getPort(), + 'persistent' => true, + ), + 'Http:Persistent', + ), + array( + array( + 'transport' => 'Thrift', + 'host' => $this->_getHost(), + 'port' => 9500, + 'config' => array( + 'framedTransport' => false, + ), + ), + 'Thrift:Buffered', + ), + ); + } + + /** + * @param string $test + * + * @return array + */ + protected function getData($test) + { + $data = array( + 'test' => $test, + 'name' => array(), + ); + for ($i = 0; $i < $this->_maxData; $i++) { + $data['name'][] = uniqid(); + } + + return $data; + } + + /** + * @param $name + * @param $transport + * @param array $times + */ + protected static function logResults($name, $transport, array $times) + { + self::$_results[$name][$transport] = array( + 'count' => count($times), + 'max' => max($times) * 1000, + 'min' => min($times) * 1000, + 'mean' => (array_sum($times) / count($times)) * 1000, + ); + } + + protected static function printResults() + { + echo sprintf( + "\n%-12s | %-20s | %-12s | %-12s | %-12s | %-12s\n\n", + 'NAME', + 'TRANSPORT', + 'COUNT', + 'MAX', + 'MIN', + 'MEAN', + '%' + ); + foreach (self::$_results as $name => $values) { + $means = array(); + foreach ($values as $times) { + $means[] = $times['mean']; + } + $minMean = min($means); + foreach ($values as $transport => $times) { + $perc = (($times['mean'] - $minMean) / $minMean) * 100; + echo sprintf( + "%-12s | %-20s | %-12d | %-12.2f | %-12.2f | %-12.2f | %+03.2f\n", + $name, + $transport, + $times['count'], + $times['max'], + $times['min'], + $times['mean'], + $perc + ); + } + echo "\n"; + } + } + + protected function _checkThrift($transport) + { + if (strpos($transport, 'Thrift') !== false && !class_exists('Elasticsearch\\RestClient')) { + self::markTestSkipped('munkie/elasticsearch-thrift-php package should be installed to run thrift transport tests'); + } + } +} |