123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- <?php
- namespace Predis\Pipeline;
- use PHPUnit_Framework_TestCase as StandardTestCase;
- use SplQueue;
- use Predis\Profile\ServerProfile;
- use Predis\Response;
- class StandardExecutorTest extends StandardTestCase
- {
-
- public function testExecutorWithSingleConnection()
- {
- $executor = new StandardExecutor();
- $pipeline = $this->getCommandsQueue();
- $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
- $connection->expects($this->exactly(3))
- ->method('writeCommand');
- $connection->expects($this->exactly(3))
- ->method('readResponse')
- ->will($this->returnValue('PONG'));
- $replies = $executor->execute($connection, $pipeline);
- $this->assertTrue($pipeline->isEmpty());
- $this->assertSame(array(true, true, true), $replies);
- }
-
- public function testExecutorWithReplicationConnection()
- {
- $executor = new StandardExecutor();
- $pipeline = $this->getCommandsQueue();
- $connection = $this->getMock('Predis\Connection\ReplicationConnectionInterface');
- $connection->expects($this->once())
- ->method('switchTo')
- ->with('master');
- $connection->expects($this->exactly(3))
- ->method('writeCommand');
- $connection->expects($this->exactly(3))
- ->method('readResponse')
- ->will($this->returnValue('PONG'));
- $replies = $executor->execute($connection, $pipeline);
- $this->assertTrue($pipeline->isEmpty());
- $this->assertSame(array(true, true, true), $replies);
- }
-
- public function testExecutorDoesNotParseResponseObjects()
- {
- $executor = new StandardExecutor();
- $response = $this->getMock('Predis\Response\ObjectInterface');
- $this->simpleResponseObjectTest($executor, $response);
- }
-
- public function testExecutorCanReturnRedisErrors()
- {
- $executor = new StandardExecutor(false);
- $response = $this->getMock('Predis\Response\ErrorInterface');
- $this->simpleResponseObjectTest($executor, $response);
- }
-
- public function testExecutorCanThrowExceptions()
- {
- $executor = new StandardExecutor(true);
- $pipeline = $this->getCommandsQueue();
- $error = new Response\Error('ERR Test error');
- $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
- $connection->expects($this->once())
- ->method('readResponse')
- ->will($this->returnValue($error));
- $executor->execute($connection, $pipeline);
- }
-
-
-
-
- protected function simpleResponseObjectTest(PipelineExecutorInterface $executor, Response\ObjectInterface $response)
- {
- $pipeline = new SplQueue();
- $command = $this->getMock('Predis\Command\CommandInterface');
- $command->expects($this->never())
- ->method('parseResponse');
- $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
- $connection->expects($this->once())
- ->method('writeCommand');
- $connection->expects($this->once())
- ->method('readResponse')
- ->will($this->returnValue($response));
- $pipeline->enqueue($command);
- $replies = $executor->execute($connection, $pipeline);
- $this->assertTrue($pipeline->isEmpty());
- $this->assertSame(array($response), $replies);
- }
-
- protected function getCommandsQueue()
- {
- $profile = ServerProfile::getDevelopment();
- $pipeline = new SplQueue();
- $pipeline->enqueue($profile->createCommand('ping'));
- $pipeline->enqueue($profile->createCommand('ping'));
- $pipeline->enqueue($profile->createCommand('ping'));
- return $pipeline;
- }
- }
|