mirror of
https://github.com/predis/predis.git
synced 2026-10-10 13:57:22 +00:00
fb2d8a37c1
Supporting this feature has been problematic and leaded to some ugly code to make abstractions such as pipelines and transactions aware of these kind of response objects. Furthermore, it was not possible to add them to all the connection classes due to implementation limits. For such reasons Predis do not support them globally anymore, but the actual classes are still shipped within the library so that they can be used to build custom stuff at a level lower than client (that is, unless we decide to remove them for good before going stable).
155 lines
5.1 KiB
PHP
155 lines
5.1 KiB
PHP
<?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 SplQueue;
|
|
use Predis\ResponseError;
|
|
use Predis\ResponseObjectInterface;
|
|
use Predis\ResponseQueued;
|
|
use Predis\Profile\ServerProfile;
|
|
|
|
/**
|
|
*
|
|
*/
|
|
class MultiExecExecutorTest extends StandardTestCase
|
|
{
|
|
/**
|
|
* @group disconnected
|
|
*/
|
|
public function testExecutorWithSingleConnection()
|
|
{
|
|
$executor = new MultiExecExecutor();
|
|
$pipeline = $this->getCommandsQueue();
|
|
$queued = new ResponseQueued();
|
|
|
|
$connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
|
|
$connection->expects($this->exactly(2))
|
|
->method('executeCommand')
|
|
->will($this->onConsecutiveCalls(true, array('PONG', 'PONG', 'PONG')));
|
|
$connection->expects($this->exactly(3))
|
|
->method('writeCommand');
|
|
$connection->expects($this->at(3))
|
|
->method('readResponse')
|
|
->will($this->onConsecutiveCalls($queued, $queued, $queued));
|
|
|
|
$replies = $executor->execute($connection, $pipeline);
|
|
|
|
$this->assertTrue($pipeline->isEmpty());
|
|
$this->assertSame(array(true, true, true), $replies);
|
|
}
|
|
|
|
/**
|
|
* @group disconnected
|
|
* @expectedException Predis\ClientException
|
|
* @expectedExceptionMessage The underlying transaction has been aborted by the server
|
|
*/
|
|
public function testExecutorWithAbortedTransaction()
|
|
{
|
|
$executor = new MultiExecExecutor();
|
|
$pipeline = $this->getCommandsQueue();
|
|
|
|
$connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
|
|
$connection->expects($this->exactly(2))
|
|
->method('executeCommand')
|
|
->will($this->onConsecutiveCalls(true, null));
|
|
|
|
$executor->execute($connection, $pipeline);
|
|
}
|
|
|
|
/**
|
|
* @group disconnected
|
|
* @expectedException Predis\ServerException
|
|
* @expectedExceptionMessage ERR Test error
|
|
*/
|
|
public function testExecutorWithErrorInTransaction()
|
|
{
|
|
$executor = new MultiExecExecutor();
|
|
$pipeline = $this->getCommandsQueue();
|
|
$queued = new ResponseQueued();
|
|
$error = new ResponseError('ERR Test error');
|
|
|
|
$connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
|
|
$connection->expects($this->at(0))
|
|
->method('executeCommand')
|
|
->will($this->returnValue(true));
|
|
$connection->expects($this->exactly(3))
|
|
->method('readResponse')
|
|
->will($this->onConsecutiveCalls($queued, $queued, $error));
|
|
$connection->expects($this->at(7))
|
|
->method('executeCommand')
|
|
->with($this->isInstanceOf('Predis\Command\TransactionDiscard'));
|
|
|
|
$executor->execute($connection, $pipeline);
|
|
}
|
|
|
|
/**
|
|
* @group disconnected
|
|
*/
|
|
public function testExecutorWithErrorInCommandResponse()
|
|
{
|
|
$executor = new MultiExecExecutor();
|
|
$pipeline = $this->getCommandsQueue();
|
|
$queued = new ResponseQueued();
|
|
$error = new ResponseError('ERR Test error');
|
|
|
|
$connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
|
|
$connection->expects($this->exactly(3))
|
|
->method('readResponse')
|
|
->will($this->onConsecutiveCalls($queued, $queued, $queued));
|
|
$connection->expects($this->at(7))
|
|
->method('executeCommand')
|
|
->will($this->returnValue(array('PONG', 'PONG', $error)));
|
|
|
|
$replies = $executor->execute($connection, $pipeline);
|
|
|
|
$this->assertSame(array(true, true, $error), $replies);
|
|
}
|
|
|
|
/**
|
|
* @group disconnected
|
|
* @expectedException Predis\ClientException
|
|
* @expectedExceptionMessage Predis\Pipeline\MultiExecExecutor can be used only with single connections
|
|
*/
|
|
public function testExecutorWithAggregatedConnection()
|
|
{
|
|
$executor = new MultiExecExecutor();
|
|
$pipeline = $this->getCommandsQueue();
|
|
|
|
$connection = $this->getMock('Predis\Connection\ReplicationConnectionInterface');
|
|
|
|
$replies = $executor->execute($connection, $pipeline);
|
|
}
|
|
|
|
// ******************************************************************** //
|
|
// ---- HELPER METHODS ------------------------------------------------ //
|
|
// ******************************************************************** //
|
|
|
|
/**
|
|
* Returns a list of queued command instances.
|
|
*
|
|
* @return SplQueue
|
|
*/
|
|
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;
|
|
}
|
|
}
|