diff options
Diffstat (limited to 'vendor/ruflin/elastica/test/lib/Elastica/Test/Connection')
7 files changed, 571 insertions, 0 deletions
diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Connection/ConnectionPoolTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Connection/ConnectionPoolTest.php new file mode 100644 index 00000000..ee03587a --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Connection/ConnectionPoolTest.php @@ -0,0 +1,112 @@ +<?php +namespace Elastica\Test\Connection; + +use Elastica\Connection; +use Elastica\Connection\ConnectionPool; +use Elastica\Connection\Strategy\StrategyFactory; +use Elastica\Test\Base as BaseTest; + +/** + * @author chabior + */ +class ConnectionPoolTest extends BaseTest +{ + /** + * @group unit + */ + public function testConstruct() + { + $pool = $this->createPool(); + + $this->assertEquals($this->getConnections(), $pool->getConnections()); + } + + /** + * @group unit + */ + public function testSetConnections() + { + $pool = $this->createPool(); + + $connections = $this->getConnections(5); + + $pool->setConnections($connections); + + $this->assertEquals($connections, $pool->getConnections()); + + $this->assertInstanceOf('Elastica\Connection\ConnectionPool', $pool->setConnections($connections)); + } + + /** + * @group unit + */ + public function testAddConnection() + { + $pool = $this->createPool(); + $pool->setConnections(array()); + + $connections = $this->getConnections(5); + + foreach ($connections as $connection) { + $pool->addConnection($connection); + } + + $this->assertEquals($connections, $pool->getConnections()); + + $this->assertInstanceOf('Elastica\Connection\ConnectionPool', $pool->addConnection($connections[0])); + } + + /** + * @group unit + */ + public function testHasConnection() + { + $pool = $this->createPool(); + + $this->assertTrue($pool->hasConnection()); + } + + /** + * @group unit + */ + public function testFailHasConnections() + { + $pool = $this->createPool(); + + $pool->setConnections(array()); + + $this->assertFalse($pool->hasConnection()); + } + + /** + * @group unit + */ + public function testGetConnection() + { + $pool = $this->createPool(); + + $this->assertInstanceOf('Elastica\Connection', $pool->getConnection()); + } + + protected function getConnections($quantity = 1) + { + $params = array(); + $connections = array(); + + for ($i = 0; $i < $quantity; $i++) { + $connections[] = new Connection($params); + } + + return $connections; + } + + protected function createPool() + { + $connections = $this->getConnections(); + $strategy = StrategyFactory::create('Simple'); + + $pool = new ConnectionPool($connections, $strategy); + + return $pool; + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Connection/Strategy/CallbackStrategyTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Connection/Strategy/CallbackStrategyTest.php new file mode 100644 index 00000000..de8290ae --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Connection/Strategy/CallbackStrategyTest.php @@ -0,0 +1,97 @@ +<?php +namespace Elastica\Test\Connection\Strategy; + +use Elastica\Connection\Strategy\CallbackStrategy; +use Elastica\Test\Base; + +/** + * Description of CallbackStrategyTest. + * + * @author chabior + */ +class CallbackStrategyTest extends Base +{ + /** + * @group unit + */ + public function testInvoke() + { + $count = 0; + + $callback = function ($connections) use (&$count) { + $count++; + }; + + $strategy = new CallbackStrategy($callback); + $strategy->getConnection(array()); + + $this->assertEquals(1, $count); + } + + /** + * @group unit + */ + public function testIsValid() + { + // closure is valid + $isValid = CallbackStrategy::isValid(function () {}); + $this->assertTrue($isValid); + + // object implementing __invoke + $isValid = CallbackStrategy::isValid(new CallbackStrategyTestHelper()); + $this->assertTrue($isValid); + + // static method as string + $isValid = CallbackStrategy::isValid('Elastica\Test\Connection\Strategy\CallbackStrategyTestHelper::getFirstConnectionStatic'); + $this->assertTrue($isValid); + + // static method as array + $isValid = CallbackStrategy::isValid(array('Elastica\Test\Connection\Strategy\CallbackStrategyTestHelper', 'getFirstConnectionStatic')); + $this->assertTrue($isValid); + + // object method + $isValid = CallbackStrategy::isValid(array(new CallbackStrategyTestHelper(), 'getFirstConnectionStatic')); + $this->assertTrue($isValid); + + // function name + $isValid = CallbackStrategy::isValid('array_pop'); + $this->assertTrue($isValid); + } + + /** + * @group unit + */ + public function testFailIsValid() + { + $isValid = CallbackStrategy::isValid(new \stdClass()); + $this->assertFalse($isValid); + + $isValid = CallbackStrategy::isValid('array_pop_pop_pop_pop_pop_pop'); + $this->assertFalse($isValid); + } + + /** + * @group functional + */ + public function testConnection() + { + $count = 0; + + $config = array('connectionStrategy' => function ($connections) use (&$count) { + ++$count; + + return current($connections); + }); + + $client = $this->_getClient($config); + $response = $client->request('/_aliases'); + + $this->assertEquals(1, $count); + + $this->assertTrue($response->isOk()); + + $strategy = $client->getConnectionStrategy(); + + $this->assertInstanceOf('Elastica\Connection\Strategy\CallbackStrategy', $strategy); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Connection/Strategy/CallbackStrategyTestHelper.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Connection/Strategy/CallbackStrategyTestHelper.php new file mode 100644 index 00000000..b15efe91 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Connection/Strategy/CallbackStrategyTestHelper.php @@ -0,0 +1,20 @@ +<?php +namespace Elastica\Test\Connection\Strategy; + +class CallbackStrategyTestHelper +{ + public function __invoke($connections) + { + return $connections[0]; + } + + public function getFirstConnection($connections) + { + return $connections[0]; + } + + public static function getFirstConnectionStatic($connections) + { + return $connections[0]; + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Connection/Strategy/EmptyStrategy.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Connection/Strategy/EmptyStrategy.php new file mode 100644 index 00000000..8d2aa5df --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Connection/Strategy/EmptyStrategy.php @@ -0,0 +1,17 @@ +<?php +namespace Elastica\Test\Connection\Strategy; + +use Elastica\Connection\Strategy\StrategyInterface; + +/** + * Description of EmptyStrategy. + * + * @author chabior + */ +class EmptyStrategy implements StrategyInterface +{ + public function getConnection($connections) + { + return; + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Connection/Strategy/RoundRobinTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Connection/Strategy/RoundRobinTest.php new file mode 100644 index 00000000..0dedbc22 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Connection/Strategy/RoundRobinTest.php @@ -0,0 +1,128 @@ +<?php +namespace Elastica\Test\Connection\Strategy; + +use Elastica\Connection; +use Elastica\Connection\Strategy\RoundRobin; +use Elastica\Exception\ConnectionException; +use Elastica\Response; +use Elastica\Test\Base; + +/** + * Description of RoundRobinTest. + * + * @author chabior + */ +class RoundRobinTest extends Base +{ + /** + * @var int Number of seconds to wait before timeout is called. Is set low for tests to have fast tests. + */ + protected $_timeout = 1; + + /** + * @group functional + */ + public function testConnection() + { + $config = array('connectionStrategy' => 'RoundRobin'); + $client = $this->_getClient($config); + $response = $client->request('/_aliases'); + /* @var $response Response */ + + $this->_checkResponse($response); + + $this->_checkStrategy($client); + } + + /** + * @group unit + */ + public function testOldStrategySetted() + { + $config = array('roundRobin' => true); + $client = $this->_getClient($config); + + $this->_checkStrategy($client); + } + + /** + * @group functional + * @expectedException \Elastica\Exception\ConnectionException + */ + public function testFailConnection() + { + $config = array('connectionStrategy' => 'RoundRobin', 'host' => '255.255.255.0', 'timeout' => $this->_timeout); + $client = $this->_getClient($config); + + $this->_checkStrategy($client); + + $client->request('/_aliases'); + } + + /** + * @group functional + */ + public function testWithOneFailConnection() + { + $connections = array( + new Connection(array('host' => '255.255.255.0', 'timeout' => $this->_timeout)), + new Connection(array('host' => $this->_getHost(), 'timeout' => $this->_timeout)), + ); + + $count = 0; + $callback = function ($connection, $exception, $client) use (&$count) { + ++$count; + }; + + $client = $this->_getClient(array('connectionStrategy' => 'RoundRobin'), $callback); + $client->setConnections($connections); + + $response = $client->request('/_aliases'); + /* @var $response Response */ + + $this->_checkResponse($response); + + $this->_checkStrategy($client); + + $this->assertLessThan(count($connections), $count); + } + + /** + * @group functional + */ + public function testWithNoValidConnection() + { + $connections = array( + new Connection(array('host' => '255.255.255.0', 'timeout' => $this->_timeout)), + new Connection(array('host' => '45.45.45.45', 'port' => '80', 'timeout' => $this->_timeout)), + new Connection(array('host' => '10.123.213.123', 'timeout' => $this->_timeout)), + ); + + $count = 0; + $client = $this->_getClient(array('roundRobin' => true), function () use (&$count) { + ++$count; + }); + + $client->setConnections($connections); + + try { + $client->request('/_aliases'); + $this->fail('Should throw exception as no connection valid'); + } catch (ConnectionException $e) { + $this->assertEquals(count($connections), $count); + $this->_checkStrategy($client); + } + } + + protected function _checkStrategy($client) + { + $strategy = $client->getConnectionStrategy(); + + $this->assertInstanceOf('Elastica\Connection\Strategy\RoundRobin', $strategy); + } + + protected function _checkResponse($response) + { + $this->assertTrue($response->isOk()); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Connection/Strategy/SimpleTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Connection/Strategy/SimpleTest.php new file mode 100644 index 00000000..3b6ac89d --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Connection/Strategy/SimpleTest.php @@ -0,0 +1,113 @@ +<?php +namespace Elastica\Test\Connection\Strategy; + +use Elastica\Connection; +use Elastica\Exception\ConnectionException; +use Elastica\Test\Base; + +/** + * Description of SimplyTest. + * + * @author chabior + */ +class SimpleTest extends Base +{ + /** + * @var int Number of seconds to wait before timeout is called. Is set low for tests to have fast tests. + */ + protected $_timeout = 1; + + /** + * @group functional + */ + public function testConnection() + { + $client = $this->_getClient(); + $response = $client->request('/_aliases'); + /* @var $response \Elastica\Response */ + + $this->_checkResponse($response); + + $this->_checkStrategy($client); + } + + /** + * @group functional + * @expectedException \Elastica\Exception\ConnectionException + */ + public function testFailConnection() + { + $config = array('host' => '255.255.255.0', 'timeout' => $this->_timeout); + $client = $this->_getClient($config); + + $this->_checkStrategy($client); + + $client->request('/_aliases'); + } + + /** + * @group functional + */ + public function testWithOneFailConnection() + { + $connections = array( + new Connection(array('host' => '255.255.255.0', 'timeout' => $this->_timeout)), + new Connection(array('host' => $this->_getHost(), 'timeout' => $this->_timeout)), + ); + + $count = 0; + $callback = function ($connection, $exception, $client) use (&$count) { + ++$count; + }; + + $client = $this->_getClient(array(), $callback); + $client->setConnections($connections); + + $response = $client->request('/_aliases'); + /* @var $response Response */ + + $this->_checkResponse($response); + + $this->_checkStrategy($client); + + $this->assertLessThan(count($connections), $count); + } + + /** + * @group functional + */ + public function testWithNoValidConnection() + { + $connections = array( + new Connection(array('host' => '255.255.255.0', 'timeout' => $this->_timeout)), + new Connection(array('host' => '45.45.45.45', 'port' => '80', 'timeout' => $this->_timeout)), + new Connection(array('host' => '10.123.213.123', 'timeout' => $this->_timeout)), + ); + + $count = 0; + $client = $this->_getClient(array(), function () use (&$count) { + ++$count; + }); + + $client->setConnections($connections); + + try { + $client->request('/_aliases'); + $this->fail('Should throw exception as no connection valid'); + } catch (ConnectionException $e) { + $this->assertEquals(count($connections), $count); + } + } + + protected function _checkStrategy($client) + { + $strategy = $client->getConnectionStrategy(); + + $this->assertInstanceOf('Elastica\Connection\Strategy\Simple', $strategy); + } + + protected function _checkResponse($response) + { + $this->assertTrue($response->isOk()); + } +} diff --git a/vendor/ruflin/elastica/test/lib/Elastica/Test/Connection/Strategy/StrategyFactoryTest.php b/vendor/ruflin/elastica/test/lib/Elastica/Test/Connection/Strategy/StrategyFactoryTest.php new file mode 100644 index 00000000..978f8fd5 --- /dev/null +++ b/vendor/ruflin/elastica/test/lib/Elastica/Test/Connection/Strategy/StrategyFactoryTest.php @@ -0,0 +1,84 @@ +<?php +namespace Elastica\Test\Connection\Strategy; + +use Elastica\Connection\Strategy\StrategyFactory; +use Elastica\Test\Base; + +/** + * Description of StrategyFactoryTest. + * + * @author chabior + */ +class StrategyFactoryTest extends Base +{ + /** + * @group unit + */ + public function testCreateCallbackStrategy() + { + $callback = function ($connections) { + }; + + $strategy = StrategyFactory::create($callback); + + $this->assertInstanceOf('Elastica\Connection\Strategy\CallbackStrategy', $strategy); + } + + /** + * @group unit + */ + public function testCreateByName() + { + $strategyName = 'Simple'; + + $strategy = StrategyFactory::create($strategyName); + + $this->assertInstanceOf('Elastica\Connection\Strategy\Simple', $strategy); + } + + /** + * @group unit + */ + public function testCreateByClass() + { + $strategy = new EmptyStrategy(); + + $this->assertEquals($strategy, StrategyFactory::create($strategy)); + } + + /** + * @group unit + */ + public function testCreateByClassName() + { + $strategyName = '\\Elastica\Test\Connection\Strategy\\EmptyStrategy'; + + $strategy = StrategyFactory::create($strategyName); + + $this->assertInstanceOf($strategyName, $strategy); + } + + /** + * @group unit + * @expectedException \InvalidArgumentException + */ + public function testFailCreate() + { + $strategy = new \stdClass(); + + StrategyFactory::create($strategy); + } + + /** + * @group unit + */ + public function testNoCollisionWithGlobalNamespace() + { + // create collision + if (!class_exists('Simple')) { + class_alias('Elastica\Util', 'Simple'); + } + $strategy = StrategyFactory::create('Simple'); + $this->assertInstanceOf('Elastica\Connection\Strategy\Simple', $strategy); + } +} |