diff options
author | Pierre Schmitz <pierre@archlinux.de> | 2015-12-20 09:00:55 +0100 |
---|---|---|
committer | Pierre Schmitz <pierre@archlinux.de> | 2015-12-20 09:00:55 +0100 |
commit | a2190ac74dd4d7080b12bab90e552d7aa81209ef (patch) | |
tree | 8b31f38de9882d18df54cf8d9e0de74167a094eb /vendor/ruflin/elastica/test/lib/Elastica/Test/ClientTest.php | |
parent | 15e69f7b20b6596b9148030acce5b59993b95a45 (diff) | |
parent | 257401d8b2cf661adf36c84b0e3fd1cf85e33c22 (diff) |
Merge branch 'mw-1.26'
Diffstat (limited to 'vendor/ruflin/elastica/test/lib/Elastica/Test/ClientTest.php')
-rw-r--r-- | vendor/ruflin/elastica/test/lib/Elastica/Test/ClientTest.php | 402 |
1 files changed, 277 insertions, 125 deletions
diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/ClientTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/ClientTest.php index 94ee2ead..a5f2b46f 100644 --- a/vendor/ruflin/elastica/test/lib/Elastica/Test/ClientTest.php +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/ClientTest.php @@ -1,31 +1,34 @@ <?php - namespace Elastica\Test; -use Elastica\Client; use Elastica\Connection; use Elastica\Document; -use Elastica\Exception\ClientException; use Elastica\Exception\Connection\HttpException; -use Elastica\Script; +use Elastica\Exception\InvalidException; use Elastica\Index; use Elastica\Request; +use Elastica\Script; use Elastica\Test\Base as BaseTest; use Elastica\Type; class ClientTest extends BaseTest { - + /** + * @group unit + */ public function testConstruct() { $client = $this->_getClient(); $this->assertCount(1, $client->getConnections()); } + /** + * @group functional + */ public function testConnectionsArray() { // Creates a new index 'xodoa' and a type 'user' inside this index - $client = new Client(array('connections' => array(array('host' => 'localhost', 'port' => 9200)))); + $client = $this->_getClient(array('connections' => array(array('host' => $this->_getHost(), 'port' => 9200)))); $index = $client->getIndex('elastica_test1'); $index->create(array(), true); @@ -53,12 +56,15 @@ class ClientTest extends BaseTest $resultSet = $type->search('rolf'); } + /** + * @group functional + */ public function testTwoServersSame() { // Creates a new index 'xodoa' and a type 'user' inside this index - $client = new Client(array('connections' => array( - array('host' => 'localhost', 'port' => 9200), - array('host' => 'localhost', 'port' => 9200), + $client = $this->_getClient(array('connections' => array( + array('host' => $this->_getHost(), 'port' => 9200), + array('host' => $this->_getHost(), 'port' => 9200), ))); $index = $client->getIndex('elastica_test1'); $index->create(array(), true); @@ -67,17 +73,17 @@ class ClientTest extends BaseTest // Adds 1 document to the index $doc1 = new Document(1, - array('username' => 'hans', 'test' => array('2', '3', '5')) + array('username' => 'hans', 'test' => array('2', '3', '5')) ); $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')) + array('username' => 'john', 'test' => array('1', '3', '6')) ); $docs[] = new Document(3, - array('username' => 'rolf', 'test' => array('2', '3', '7')) + array('username' => 'rolf', 'test' => array('2', '3', '7')) ); $type->addDocuments($docs); @@ -87,30 +93,45 @@ class ClientTest extends BaseTest $resultSet = $type->search('rolf'); } + /** + * @group unit + */ public function testConnectionParamsArePreparedForConnectionsOption() { - $client = new Client(array('connections' => array(array('url' => 'https://localhost:9200')))); + $url = 'https://'.$this->_getHost().':9200'; + $client = $this->_getClient(array('connections' => array(array('url' => $url)))); $connection = $client->getConnection(); - $this->assertEquals('https://localhost:9200', $connection->getConfig('url')); + $this->assertEquals($url, $connection->getConfig('url')); } + /** + * @group unit + */ public function testConnectionParamsArePreparedForServersOption() { - $client = new Client(array('servers' => array(array('url' => 'https://localhost:9200')))); + $url = 'https://'.$this->_getHost().':9200'; + $client = $this->_getClient(array('servers' => array(array('url' => $url)))); $connection = $client->getConnection(); - $this->assertEquals('https://localhost:9200', $connection->getConfig('url')); + $this->assertEquals($url, $connection->getConfig('url')); } + /** + * @group unit + */ public function testConnectionParamsArePreparedForDefaultOptions() { - $client = new Client(array('url' => 'https://localhost:9200')); + $url = 'https://'.$this->_getHost().':9200'; + $client = $this->_getClient(array('url' => $url)); $connection = $client->getConnection(); - $this->assertEquals('https://localhost:9200', $connection->getConfig('url')); + $this->assertEquals($url, $connection->getConfig('url')); } + /** + * @group functional + */ public function testBulk() { $client = $this->_getClient(); @@ -125,6 +146,9 @@ class ClientTest extends BaseTest $client->bulk($params); } + /** + * @group functional + */ public function testOptimizeAll() { $client = $this->_getClient(); @@ -134,6 +158,7 @@ class ClientTest extends BaseTest } /** + * @group unit * @expectedException \Elastica\Exception\InvalidException */ public function testAddDocumentsEmpty() @@ -143,7 +168,9 @@ class ClientTest extends BaseTest } /** - * Test bulk operations on Index + * Test bulk operations on Index. + * + * @group functional */ public function testBulkIndex() { @@ -159,7 +186,7 @@ class ClientTest extends BaseTest $index->updateDocuments(array( new Document(1, array('name' => 'AnonCoin'), 'altcoin'), - new Document(2, array('name' => 'iXcoin'), 'altcoin') + new Document(2, array('name' => 'iXcoin'), 'altcoin'), )); $this->assertEquals('AnonCoin', $index->getType('altcoin')->getDocument(1)->get('name')); @@ -174,7 +201,9 @@ class ClientTest extends BaseTest } /** - * Test bulk operations on Type + * Test bulk operations on Type. + * + * @group functional */ public function testBulkType() { @@ -190,7 +219,7 @@ class ClientTest extends BaseTest $type->updateDocuments(array( new Document(1, array('name' => 'LiteCoin')), - new Document(2, array('name' => 'NameCoin')) + new Document(2, array('name' => 'NameCoin')), )); $this->assertEquals('LiteCoin', $type->getDocument(1)->get('name')); @@ -204,6 +233,9 @@ class ClientTest extends BaseTest $type->getDocument(2); } + /** + * @group functional + */ public function testUpdateDocuments() { $indexName = 'test'; @@ -243,27 +275,29 @@ class ClientTest extends BaseTest } /** - * Test deleteIds method using string parameters - * - * This test ensures that the deleteIds method of - * the \Elastica\Client can properly accept and use - * an $index parameter and $type parameter that are - * strings - * - * This test is a bit more verbose than just sending the - * values to deleteIds and checking for exceptions or - * warnings. - * - * It will add a document, search for it, then delete it - * using the parameter types we are interested in, and then - * re-search to verify that they have been deleted - */ + * Test deleteIds method using string parameters. + * + * This test ensures that the deleteIds method of + * the \Elastica\Client can properly accept and use + * an $index parameter and $type parameter that are + * strings + * + * This test is a bit more verbose than just sending the + * values to deleteIds and checking for exceptions or + * warnings. + * + * It will add a document, search for it, then delete it + * using the parameter types we are interested in, and then + * re-search to verify that they have been deleted + * + * @group functional + */ public function testDeleteIdsIdxStringTypeString() { $data = array('username' => 'hans'); $userSearch = 'username:hans'; - $index = $this->_createIndex('test', true, 2); + $index = $this->_createIndex(null, true, 2); // Create the index, deleting it first if it already exists $index->create(array(), true); @@ -289,8 +323,8 @@ class ClientTest extends BaseTest // deleteIds are the type we are testing for $idxString = $index->getName(); $typeString = $type->getName(); - $this->assertEquals(true, is_string($idxString)); - $this->assertEquals(true, is_string($typeString)); + $this->assertTrue(is_string($idxString)); + $this->assertTrue(is_string($typeString)); // Try to delete doc with a routing value which hashes to // a different shard then the id. @@ -318,22 +352,24 @@ class ClientTest extends BaseTest } /** - * Test deleteIds method using string parameter for $index - * and object parameter for $type - * - * This test ensures that the deleteIds method of - * the \Elastica\Client can properly accept and use - * an $index parameter that is a string and a $type - * parameter that is of type \Elastica\Type - * - * This test is a bit more verbose than just sending the - * values to deleteIds and checking for exceptions or - * warnings. - * - * It will add a document, search for it, then delete it - * using the parameter types we are interested in, and then - * re-search to verify that they have been deleted - */ + * Test deleteIds method using string parameter for $index + * and object parameter for $type. + * + * This test ensures that the deleteIds method of + * the \Elastica\Client can properly accept and use + * an $index parameter that is a string and a $type + * parameter that is of type \Elastica\Type + * + * This test is a bit more verbose than just sending the + * values to deleteIds and checking for exceptions or + * warnings. + * + * It will add a document, search for it, then delete it + * using the parameter types we are interested in, and then + * re-search to verify that they have been deleted + * + * @group functional + */ public function testDeleteIdsIdxStringTypeObject() { $data = array('username' => 'hans'); @@ -363,8 +399,8 @@ class ClientTest extends BaseTest // And verify that the variables we are doing to send to // deleteIds are the type we are testing for $idxString = $index->getName(); - $this->assertEquals(true, is_string($idxString)); - $this->assertEquals(true, ($type instanceof Type)); + $this->assertTrue(is_string($idxString)); + $this->assertInstanceOf('Elastica\Type', $type); // Using the existing $index and $type variables which // are \Elastica\Index and \Elastica\Type objects respectively @@ -380,22 +416,24 @@ class ClientTest extends BaseTest } /** - * Test deleteIds method using object parameter for $index - * and string parameter for $type - * - * This test ensures that the deleteIds method of - * the \Elastica\Client can properly accept and use - * an $index parameter that is of type Elasitca_Index - * and a $type parameter that is a string - * - * This test is a bit more verbose than just sending the - * values to deleteIds and checking for exceptions or - * warnings. - * - * It will add a document, search for it, then delete it - * using the parameter types we are interested in, and then - * re-search to verify that they have been deleted - */ + * Test deleteIds method using object parameter for $index + * and string parameter for $type. + * + * This test ensures that the deleteIds method of + * the \Elastica\Client can properly accept and use + * an $index parameter that is of type Elasitca_Index + * and a $type parameter that is a string + * + * This test is a bit more verbose than just sending the + * values to deleteIds and checking for exceptions or + * warnings. + * + * It will add a document, search for it, then delete it + * using the parameter types we are interested in, and then + * re-search to verify that they have been deleted + * + * @group functional + */ public function testDeleteIdsIdxObjectTypeString() { $data = array('username' => 'hans'); @@ -425,8 +463,8 @@ class ClientTest extends BaseTest // And verify that the variables we are doing to send to // deleteIds are the type we are testing for $typeString = $type->getName(); - $this->assertEquals(true, ($index instanceof Index)); - $this->assertEquals(true, is_string($typeString)); + $this->assertInstanceOf('Elastica\Index', $index); + $this->assertTrue(is_string($typeString)); // Using the existing $index and $type variables which // are \Elastica\Index and \Elastica\Type objects respectively @@ -442,22 +480,24 @@ class ClientTest extends BaseTest } /** - * Test deleteIds method using object parameter for $index - * and object parameter for $type - * - * This test ensures that the deleteIds method of - * the \Elastica\Client can properly accept and use - * an $index parameter that is an object and a $type - * parameter that is of type \Elastica\Type - * - * This test is a bit more verbose than just sending the - * values to deleteIds and checking for exceptions or - * warnings. - * - * It will add a document, search for it, then delete it - * using the parameter types we are interested in, and then - * re-search to verify that they have been deleted - */ + * Test deleteIds method using object parameter for $index + * and object parameter for $type. + * + * This test ensures that the deleteIds method of + * the \Elastica\Client can properly accept and use + * an $index parameter that is an object and a $type + * parameter that is of type \Elastica\Type + * + * This test is a bit more verbose than just sending the + * values to deleteIds and checking for exceptions or + * warnings. + * + * It will add a document, search for it, then delete it + * using the parameter types we are interested in, and then + * re-search to verify that they have been deleted + * + * @group functional + */ public function testDeleteIdsIdxObjectTypeObject() { $data = array('username' => 'hans'); @@ -486,8 +526,8 @@ class ClientTest extends BaseTest // And verify that the variables we are doing to send to // deleteIds are the type we are testing for - $this->assertEquals(true, ($index instanceof Index)); - $this->assertEquals(true, ($type instanceof Type)); + $this->assertInstanceOf('Elastica\Index', $index); + $this->assertInstanceOf('Elastica\Type', $type); // Using the existing $index and $type variables which // are \Elastica\Index and \Elastica\Type objects respectively @@ -502,13 +542,16 @@ class ClientTest extends BaseTest $this->assertEquals(0, $totalHits); } + /** + * @group functional + */ public function testOneInvalidConnection() { $client = $this->_getClient(); // First connection work, second should not work - $connection1 = new Connection(array('port' => '9100', 'timeout' => 2)); - $connection2 = new Connection(array('port' => '9200', 'timeout' => 2)); + $connection1 = new Connection(array('port' => '9100', 'timeout' => 2, 'host' => $this->_getHost())); + $connection2 = new Connection(array('port' => '9200', 'timeout' => 2, 'host' => $this->_getHost())); $client->setConnections(array($connection1, $connection2)); @@ -523,6 +566,9 @@ class ClientTest extends BaseTest $this->assertTrue($connections[0]->isEnabled() == false || $connections[1]->isEnabled() == false); } + /** + * @group functional + */ public function testTwoInvalidConnection() { $client = $this->_getClient(); @@ -537,7 +583,6 @@ class ClientTest extends BaseTest $client->request('_status', Request::GET); $this->fail('Should throw exception as no connection valid'); } catch (HttpException $e) { - $this->assertTrue(true); } $connections = $client->getConnections(); @@ -550,7 +595,9 @@ class ClientTest extends BaseTest } /** - * Tests if the callback works in case a connection is down + * Tests if the callback works in case a connection is down. + * + * @group functional */ public function testCallback() { @@ -558,7 +605,7 @@ class ClientTest extends BaseTest $object = $this; // Callback function which verifies that disabled connection objects are returned - $callback = function($connection, $exception, $client) use (&$object, &$count) { + $callback = function ($connection, $exception, $client) use (&$object, &$count) { $object->assertInstanceOf('Elastica\Connection', $connection); $object->assertInstanceOf('Elastica\Exception\ConnectionException', $exception); $object->assertInstanceOf('Elastica\Client', $client); @@ -566,7 +613,7 @@ class ClientTest extends BaseTest $count++; }; - $client = new Client(array(), $callback); + $client = $this->_getClient(array(), $callback); // First connection work, second should not work $connection1 = new Connection(array('port' => '9101', 'timeout' => 2)); @@ -587,17 +634,23 @@ class ClientTest extends BaseTest $this->assertEquals(2, $count); } + /** + * @group functional + */ public function testUrlConstructor() { - $url = 'http://localhost:9200/'; + $url = 'http://'.$this->_getHost().':9200/'; // Url should overwrite invalid host - $client = new Client(array('url' => $url, 'port' => '9101', 'timeout' => 2)); + $client = $this->_getClient(array('url' => $url, 'port' => '9101', 'timeout' => 2)); $response = $client->request('_status'); $this->assertInstanceOf('Elastica\Response', $response); } + /** + * @group functional + */ public function testUpdateDocumentByDocument() { $index = $this->_createIndex(); @@ -622,6 +675,9 @@ class ClientTest extends BaseTest $this->assertEquals('value3added', $data['field3']); } + /** + * @group functional + */ public function testUpdateDocumentByScript() { $index = $this->_createIndex(); @@ -647,6 +703,9 @@ class ClientTest extends BaseTest $this->assertArrayNotHasKey('field3', $data); } + /** + * @group functional + */ public function testUpdateDocumentByScriptWithUpsert() { $index = $this->_createIndex(); @@ -689,6 +748,9 @@ class ClientTest extends BaseTest $this->assertArrayNotHasKey('field3', $data); } + /** + * @group functional + */ public function testUpdateDocumentByRawData() { $index = $this->_createIndex(); @@ -701,7 +763,7 @@ class ClientTest extends BaseTest $rawData = array( 'doc' => array( 'field2' => 'value2', - ) + ), ); $response = $client->updateDocument(1, $rawData, $index->getName(), $type->getName(), array('retry_on_conflict' => 1)); @@ -717,6 +779,9 @@ class ClientTest extends BaseTest $this->assertEquals('value2', $data['field2']); } + /** + * @group functional + */ public function testUpdateDocumentByDocumentWithUpsert() { $index = $this->_createIndex(); @@ -748,6 +813,9 @@ class ClientTest extends BaseTest $this->assertEquals('value2updated', $data['field2']); } + /** + * @group functional + */ public function testDocAsUpsert() { $index = $this->_createIndex(); @@ -775,6 +843,9 @@ class ClientTest extends BaseTest $this->assertEquals('value2', $data['field2']); } + /** + * @group functional + */ public function testUpdateWithInvalidType() { $index = $this->_createIndex(); @@ -792,6 +863,9 @@ class ClientTest extends BaseTest } } + /** + * @group functional + */ public function testDeleteDocuments() { $index = $this->_createIndex(); @@ -834,9 +908,12 @@ class ClientTest extends BaseTest $this->assertEquals(1, $type->count()); } + /** + * @group functional + */ public function testLastRequestResponse() { - $client = new Client(); + $client = $this->_getClient(); $response = $client->request('_status'); $this->assertInstanceOf('Elastica\Response', $response); @@ -851,6 +928,9 @@ class ClientTest extends BaseTest $this->assertSame($response, $lastResponse); } + /** + * @group functional + */ public function testUpdateDocumentPopulateFields() { $index = $this->_createIndex(); @@ -907,6 +987,9 @@ class ClientTest extends BaseTest $this->assertArrayNotHasKey('field4', $data); } + /** + * @group functional + */ public function testAddDocumentsWithoutIds() { $docs = array(); @@ -933,6 +1016,9 @@ class ClientTest extends BaseTest } } + /** + * @group unit + */ public function testConfigValue() { $config = array( @@ -940,11 +1026,11 @@ class ClientTest extends BaseTest 'level2' => array( 'level3' => 'value3', ), - 'level21' => 'value21' + 'level21' => 'value21', ), - 'level11' => 'value11' + 'level11' => 'value11', ); - $client = new Client($config); + $client = $this->_getClient($config); $this->assertNull($client->getConfigValue('level12')); $this->assertFalse($client->getConfigValue('level12', false)); @@ -958,51 +1044,117 @@ class ClientTest extends BaseTest $this->assertEquals('value3', $client->getConfigValue(array('level1', 'level2', 'level3'))); $this->assertInternalType('array', $client->getConfigValue(array('level1', 'level2'))); } - - + + /** + * @group functional + */ public function testArrayQuery() { - $client = new Client(); - + $client = $this->_getClient(); + $index = $client->getIndex('test'); $index->create(array(), true); $type = $index->getType('test'); $type->addDocument(new Document(1, array('username' => 'ruflin'))); $index->refresh(); - + $query = array( 'query' => array( 'query_string' => array( 'query' => 'ruflin', - ) - ) + ), + ), ); - - $path = $index->getName() . '/' . $type->getName() . '/_search'; - + + $path = $index->getName().'/'.$type->getName().'/_search'; + $response = $client->request($path, Request::GET, $query); $responseArray = $response->getData(); - + $this->assertEquals(1, $responseArray['hits']['total']); } - + + /** + * @group functional + */ public function testJSONQuery() { - $client = new Client(); - + $client = $this->_getClient(); + $index = $client->getIndex('test'); $index->create(array(), true); $type = $index->getType('test'); $type->addDocument(new Document(1, array('username' => 'ruflin'))); $index->refresh(); - + $query = '{"query":{"query_string":{"query":"ruflin"}}}'; - $path = $index->getName() . '/' . $type->getName() . '/_search'; - + $path = $index->getName().'/'.$type->getName().'/_search'; + $response = $client->request($path, Request::GET, $query); $responseArray = $response->getData(); - + $this->assertEquals(1, $responseArray['hits']['total']); } + + /** + * @group unit + */ + public function testAddHeader() + { + $client = $this->_getClient(); + + // add one header + $client->addHeader('foo', 'bar'); + $this->assertEquals(array('foo' => 'bar'), $client->getConfigValue('headers')); + + // check class + $this->assertInstanceOf('Elastica\Client', $client->addHeader('foo', 'bar')); + + // check invalid parameters + try { + $client->addHeader(new \stdClass(), 'foo'); + $this->fail('Header name is not a string but exception not thrown'); + } catch (InvalidException $ex) { + } + + try { + $client->addHeader('foo', new \stdClass()); + $this->fail('Header value is not a string but exception not thrown'); + } catch (InvalidException $ex) { + } + } + + /** + * @group unit + */ + public function testRemoveHeader() + { + $client = $this->_getClient(); + + // set headers + $headers = array( + 'first' => 'first value', + 'second' => 'second value', + ); + foreach ($headers as $key => $value) { + $client->addHeader($key, $value); + } + $this->assertEquals($headers, $client->getConfigValue('headers')); + + // remove one + $client->removeHeader('first'); + unset($headers['first']); + $this->assertEquals($headers, $client->getConfigValue('headers')); + + // check class + $this->assertInstanceOf('Elastica\Client', $client->removeHeader('second')); + + // check invalid parameter + try { + $client->removeHeader(new \stdClass()); + $this->fail('Header name is not a string but exception not thrown'); + } catch (InvalidException $ex) { + } + } } |