mirror of
https://github.com/predis/predis.git
synced 2026-09-28 08:05:40 +00:00
abd284c972
* Renamed SingleConnectionInterface to NodeConnectionInterface since this name is better and makes even more sense in the context of cluster and replication scenarios. * Moved specialized aggregate connections (the ones implementing both predis and redis cluster and master/slave replication) in a newly created Predis\Connection\Aggregate sub-namespace. * Removed the "Connection" part from names of aggregate connection interfaces in the Predis\Connection\Aggregate sub-namespace. * Changed "Composable" to "Composite" in the name of interfaces and classes that can use pluggable protocol processors.
164 lines
5.0 KiB
PHP
164 lines
5.0 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 PredisTestCase;
|
|
use Predis\Client;
|
|
use Predis\Response;
|
|
|
|
/**
|
|
*
|
|
*/
|
|
class AtomicTest extends PredisTestCase
|
|
{
|
|
/**
|
|
* @group disconnected
|
|
*/
|
|
public function testPipelineWithSingleConnection()
|
|
{
|
|
$pong = new Response\Status('PONG');
|
|
$queued = new Response\Status('QUEUED');
|
|
|
|
$connection = $this->getMock('Predis\Connection\NodeConnectionInterface');
|
|
$connection->expects($this->exactly(2))
|
|
->method('executeCommand')
|
|
->will($this->onConsecutiveCalls(true, array($pong, $pong, $pong)));
|
|
$connection->expects($this->exactly(3))
|
|
->method('writeRequest');
|
|
$connection->expects($this->at(3))
|
|
->method('readResponse')
|
|
->will($this->onConsecutiveCalls($queued, $queued, $queued));
|
|
|
|
$pipeline = new Atomic(new Client($connection));
|
|
|
|
$pipeline->ping();
|
|
$pipeline->ping();
|
|
$pipeline->ping();
|
|
|
|
$this->assertSame(array($pong, $pong, $pong), $pipeline->execute());
|
|
}
|
|
|
|
/**
|
|
* @group disconnected
|
|
* @expectedException Predis\ClientException
|
|
* @expectedExceptionMessage The underlying transaction has been aborted by the server.
|
|
*/
|
|
public function testThrowsExceptionOnAbortedTransaction()
|
|
{
|
|
$connection = $this->getMock('Predis\Connection\NodeConnectionInterface');
|
|
$connection->expects($this->exactly(2))
|
|
->method('executeCommand')
|
|
->will($this->onConsecutiveCalls(true, null));
|
|
|
|
$pipeline = new Atomic(new Client($connection));
|
|
|
|
$pipeline->ping();
|
|
$pipeline->ping();
|
|
$pipeline->ping();
|
|
|
|
$pipeline->execute();
|
|
}
|
|
|
|
/**
|
|
* @group disconnected
|
|
* @expectedException Predis\Response\ServerException
|
|
* @expectedExceptionMessage ERR Test error
|
|
*/
|
|
public function testPipelineWithErrorInTransaction()
|
|
{
|
|
$queued = new Response\Status('QUEUED');
|
|
$error = new Response\Error('ERR Test error');
|
|
|
|
$connection = $this->getMock('Predis\Connection\NodeConnectionInterface');
|
|
$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'));
|
|
|
|
$pipeline = new Atomic(new Client($connection));
|
|
|
|
$pipeline->ping();
|
|
$pipeline->ping();
|
|
$pipeline->ping();
|
|
|
|
$pipeline->execute();
|
|
}
|
|
|
|
/**
|
|
* @group disconnected
|
|
* @expectedException Predis\Response\ServerException
|
|
* @expectedExceptionMessage ERR Test error
|
|
*/
|
|
public function testThrowsServerExceptionOnResponseErrorByDefault()
|
|
{
|
|
$error = new Response\Error('ERR Test error');
|
|
|
|
$connection = $this->getMock('Predis\Connection\NodeConnectionInterface');
|
|
$connection->expects($this->once())
|
|
->method('readResponse')
|
|
->will($this->returnValue($error));
|
|
|
|
$pipeline = new Atomic(new Client($connection));
|
|
|
|
$pipeline->ping();
|
|
$pipeline->ping();
|
|
|
|
$pipeline->execute();
|
|
}
|
|
|
|
/**
|
|
* @group disconnected
|
|
*/
|
|
public function testReturnsResponseErrorWithClientExceptionsSetToFalse()
|
|
{
|
|
$pong = new Response\Status('PONG');
|
|
$queued = new Response\Status('QUEUED');
|
|
$error = new Response\Error('ERR Test error');
|
|
|
|
$connection = $this->getMock('Predis\Connection\NodeConnectionInterface');
|
|
$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)));
|
|
|
|
$pipeline = new Atomic(new Client($connection, array('exceptions' => false)));
|
|
|
|
$pipeline->ping();
|
|
$pipeline->ping();
|
|
$pipeline->ping();
|
|
|
|
$this->assertSame(array($pong, $pong, $error), $pipeline->execute());
|
|
}
|
|
|
|
/**
|
|
* @group disconnected
|
|
* @expectedException Predis\ClientException
|
|
* @expectedExceptionMessage The class 'Predis\Pipeline\Atomic' does not support aggregate connections.
|
|
*/
|
|
public function testExecutorWithAggregateConnection()
|
|
{
|
|
$connection = $this->getMock('Predis\Connection\Aggregate\ClusterInterface');
|
|
$pipeline = new Atomic(new Client($connection));
|
|
|
|
$pipeline->ping();
|
|
|
|
$pipeline->execute();
|
|
}
|
|
}
|