123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435 |
- <?php
- /*
- * This file is part of the Predis package.
- *
- * (c) Daniele Alessandri <suppakilla@gmail.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Predis\Pipeline;
- use \PHPUnit_Framework_TestCase as StandardTestCase;
- use Predis\Client;
- use Predis\ClientException;
- use Predis\Profile\ServerProfile;
- /**
- *
- */
- class PipelineContextTest extends StandardTestCase
- {
- /**
- * @group disconnected
- */
- public function testConstructorWithoutOptions()
- {
- $client = new Client();
- $pipeline = new PipelineContext($client);
- $this->assertSame($client, $pipeline->getClient());
- $this->assertInstanceOf('Predis\Pipeline\StandardExecutor', $pipeline->getExecutor());
- }
- /**
- * @group disconnected
- */
- public function testConstructorWithExecutorArgument()
- {
- $client = new Client();
- $executor = $this->getMock('Predis\Pipeline\PipelineExecutorInterface');
- $pipeline = new PipelineContext($client, $executor);
- $this->assertSame($executor, $pipeline->getExecutor());
- }
- /**
- * @group disconnected
- */
- public function testCallDoesNotSendCommandsWithoutExecute()
- {
- $executor = $this->getMock('Predis\Pipeline\PipelineExecutorInterface');
- $executor->expects($this->never())->method('executor');
- $pipeline = new PipelineContext(new Client(), $executor);
- $pipeline->echo('one');
- $pipeline->echo('two');
- $pipeline->echo('three');
- }
- /**
- * @group disconnected
- */
- public function testCallReturnsPipelineForFluentInterface()
- {
- $executor = $this->getMock('Predis\Pipeline\PipelineExecutorInterface');
- $executor->expects($this->never())->method('executor');
- $pipeline = new PipelineContext(new Client(), $executor);
- $this->assertSame($pipeline, $pipeline->echo('one'));
- $this->assertSame($pipeline, $pipeline->echo('one')->echo('two')->echo('three'));
- }
- /**
- * @group disconnected
- */
- public function testExecuteCommandDoesNotSendCommandsWithoutExecute()
- {
- $profile = ServerProfile::getDefault();
- $executor = $this->getMock('Predis\Pipeline\PipelineExecutorInterface');
- $executor->expects($this->never())->method('executor');
- $pipeline = new PipelineContext(new Client(), $executor);
- $pipeline->executeCommand($profile->createCommand('echo', array('one')));
- $pipeline->executeCommand($profile->createCommand('echo', array('two')));
- $pipeline->executeCommand($profile->createCommand('echo', array('three')));
- }
- /**
- * @group disconnected
- */
- public function testExecuteWithEmptyBuffer()
- {
- $executor = $this->getMock('Predis\Pipeline\PipelineExecutorInterface');
- $executor->expects($this->never())->method('executor');
- $pipeline = new PipelineContext(new Client(), $executor);
- $this->assertSame(array(), $pipeline->execute());
- }
- /**
- * @group disconnected
- */
- public function testExecuteWithFilledBuffer()
- {
- $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
- $connection->expects($this->exactly(3))
- ->method('writeCommand');
- $connection->expects($this->exactly(3))
- ->method('readResponse')
- ->will($this->returnCallback($this->getReadCallback()));
- $pipeline = new PipelineContext(new Client($connection));
- $pipeline->echo('one');
- $pipeline->echo('two');
- $pipeline->echo('three');
- $pipeline->flushPipeline();
- $this->assertSame(array('one', 'two', 'three'), $pipeline->execute());
- }
- /**
- * @group disconnected
- */
- public function testFlushWithFalseArgumentDiscardsBuffer()
- {
- $executor = $this->getMock('Predis\Pipeline\PipelineExecutorInterface');
- $executor->expects($this->never())->method('executor');
- $pipeline = new PipelineContext(new Client(), $executor);
- $pipeline->echo('one');
- $pipeline->echo('two');
- $pipeline->echo('three');
- $pipeline->flushPipeline(false);
- $this->assertSame(array(), $pipeline->execute());
- }
- /**
- * @group disconnected
- */
- public function testFlushHandlesPartialBuffers()
- {
- $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
- $connection->expects($this->exactly(4))
- ->method('writeCommand');
- $connection->expects($this->exactly(4))
- ->method('readResponse')
- ->will($this->returnCallback($this->getReadCallback()));
- $pipeline = new PipelineContext(new Client($connection));
- $pipeline->echo('one');
- $pipeline->echo('two');
- $pipeline->flushPipeline();
- $pipeline->echo('three');
- $pipeline->echo('four');
- $this->assertSame(array('one', 'two', 'three', 'four'), $pipeline->execute());
- }
- /**
- * @group disconnected
- */
- public function testExecuteAcceptsCallableArgument()
- {
- $test = $this;
- $pipeline = new PipelineContext(new Client());
- $callable = function ($pipe) use ($test, $pipeline) {
- $test->assertSame($pipeline, $pipe);
- $pipe->flushPipeline(false);
- };
- $pipeline->execute($callable);
- }
- /**
- * @group disconnected
- * @expectedException InvalidArgumentException
- */
- public function testExecuteDoesNotAcceptNonCallableArgument()
- {
- $noncallable = new \stdClass();
- $pipeline = new PipelineContext(new Client());
- $pipeline->execute($noncallable);
- }
- /**
- * @group disconnected
- * @expectedException Predis\ClientException
- */
- public function testExecuteInsideCallableArgumentThrowsException()
- {
- $pipeline = new PipelineContext(new Client());
- $pipeline->execute(function ($pipe) {
- $pipe->execute();
- });
- }
- /**
- * @group disconnected
- */
- public function testExecuteWithCallableArgumentRunsPipelineInCallable()
- {
- $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
- $connection->expects($this->exactly(4))
- ->method('writeCommand');
- $connection->expects($this->exactly(4))
- ->method('readResponse')
- ->will($this->returnCallback($this->getReadCallback()));
- $pipeline = new PipelineContext(new Client($connection));
- $replies = $pipeline->execute(function ($pipe) {
- $pipe->echo('one');
- $pipe->echo('two');
- $pipe->echo('three');
- $pipe->echo('four');
- });
- $this->assertSame(array('one', 'two', 'three', 'four'), $replies);
- }
- /**
- * @group disconnected
- */
- public function testExecuteWithCallableArgumentHandlesExceptions()
- {
- $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
- $connection->expects($this->never())->method('writeCommand');
- $connection->expects($this->never())->method('readResponse');
- $pipeline = new PipelineContext(new Client($connection));
- $exception = null;
- $replies = null;
- try {
- $replies = $pipeline->execute(function ($pipe) {
- $pipe->echo('one');
- throw new ClientException('TEST');
- $pipe->echo('two');
- });
- } catch (\Exception $ex) {
- $exception = $ex;
- }
- $this->assertInstanceOf('Predis\ClientException', $exception);
- $this->assertSame('TEST', $exception->getMessage());
- $this->assertNull($replies);
- }
- // ******************************************************************** //
- // ---- INTEGRATION TESTS --------------------------------------------- //
- // ******************************************************************** //
- /**
- * @group connected
- */
- public function testIntegrationWithFluentInterface()
- {
- $pipeline = $this->getClient()->pipeline();
- $results = $pipeline->echo('one')
- ->echo('two')
- ->echo('three')
- ->execute();
- $this->assertSame(array('one', 'two', 'three'), $results);
- }
- /**
- * @group connected
- */
- public function testIntegrationWithCallableBlock()
- {
- $client = $this->getClient();
- $results = $client->pipeline(function ($pipe) {
- $pipe->set('foo', 'bar');
- $pipe->get('foo');
- });
- $this->assertSame(array(true, 'bar'), $results);
- $this->assertTrue($client->exists('foo'));
- }
- /**
- * @group connected
- */
- public function testOutOfBandMessagesInsidePipeline()
- {
- $oob = null;
- $client = $this->getClient();
- $results = $client->pipeline(function ($pipe) use (&$oob) {
- $pipe->set('foo', 'bar');
- $oob = $pipe->getClient()->echo('oob message');
- $pipe->get('foo');
- });
- $this->assertSame(array(true, 'bar'), $results);
- $this->assertSame('oob message', $oob);
- $this->assertTrue($client->exists('foo'));
- }
- /**
- * @group connected
- */
- public function testIntegrationWithClientExceptionInCallableBlock()
- {
- $client = $this->getClient();
- try {
- $client->pipeline(function ($pipe) {
- $pipe->set('foo', 'bar');
- throw new ClientException('TEST');
- });
- } catch (\Exception $ex) {
- $exception = $ex;
- }
- $this->assertInstanceOf('Predis\ClientException', $exception);
- $this->assertSame('TEST', $exception->getMessage());
- $this->assertFalse($client->exists('foo'));
- }
- /**
- * @group connected
- */
- public function testIntegrationWithServerExceptionInCallableBlock()
- {
- $client = $this->getClient();
- try {
- $client->pipeline(function ($pipe) {
- $pipe->set('foo', 'bar');
- // LPUSH on a string key fails, but won't stop
- // the pipeline to send the commands.
- $pipe->lpush('foo', 'bar');
- $pipe->set('hoge', 'piyo');
- });
- } catch (\Exception $ex) {
- $exception = $ex;
- }
- $this->assertInstanceOf('Predis\ServerException', $exception);
- $this->assertTrue($client->exists('foo'));
- $this->assertTrue($client->exists('hoge'));
- }
- /**
- * @group connected
- */
- public function testIntegrationWithServerErrorInCallableBlock()
- {
- $client = $this->getClient(array(), array('exceptions' => false));
- $results = $client->pipeline(function ($pipe) {
- $pipe->set('foo', 'bar');
- $pipe->lpush('foo', 'bar'); // LPUSH on a string key fails.
- $pipe->get('foo');
- });
- $this->assertTrue($results[0]);
- $this->assertInstanceOf('Predis\ResponseError', $results[1]);
- $this->assertSame('bar', $results[2]);
- }
- // ******************************************************************** //
- // ---- HELPER METHODS ------------------------------------------------ //
- // ******************************************************************** //
- /**
- * Returns a client instance connected to the specified Redis
- * server instance to perform integration tests.
- *
- * @return array Additional connection parameters.
- * @return array Additional client options.
- * @return Client New client instance.
- */
- protected function getClient(Array $parameters = array(), Array $options = array())
- {
- $parameters = array_merge(array(
- 'scheme' => 'tcp',
- 'host' => REDIS_SERVER_HOST,
- 'port' => REDIS_SERVER_PORT,
- 'database' => REDIS_SERVER_DBNUM,
- ), $parameters);
- $options = array_merge(array(
- 'profile' => REDIS_SERVER_VERSION,
- ), $options);
- $client = new Client($parameters, $options);
- $client->connect();
- $client->flushdb();
- return $client;
- }
- /**
- * Helper method that returns a callback used to emulate a reply
- * to an ECHO command.
- *
- * @return \Closure
- */
- protected function getReadCallback()
- {
- return function ($command) {
- if (($id = $command->getId()) !== 'ECHO') {
- throw new \InvalidArgumentException("Expected ECHO, got {$id}");
- }
- list($echoed) = $command->getArguments();
- return $echoed;
- };
- }
- }
|