Files
predis/tests/Predis/Pipeline/AtomicTest.php
T
Daniele Alessandri 373d30b070 Use Predis\Response\Status to identify all kinds of status responses.
Status response objects are needed mostly to make it possible from the
client perspective to differentiate a status response with the payload
"OK" from a normale bulk reply containing "OK".

The biggest change is for commands returning +OK responses: these were
previously translated to TRUE (bool value), but they are now returned
as instances of Predis\Response\Status. Just to illustrate an example
of the possibilities with this change we will use SET since it is the
most widely used command returning +OK:

  $response = $client->set('foo', 'bar');

  echo $response;         // 'OK'
  $response == 'OK';      // TRUE
  isset($response->ok);   // TRUE
  $response == true;      // TRUE
  $response === true;     // FALSE
  $response instanceof Predis\Response\ObjectInterface;     // TRUE
  $response instanceof Predis\Response\Status;              // TRUE

For those checking responses returned by commands such as SET or PONG,
the breaking change basically lies in the usage of strict comparison:
doing $response === true will now evaluate to FALSE instead of TRUE.

By default Predis caches common status responses such as OK or QUEUED
to lower the memory usage when using pipelines or transactions.
2013-12-07 15:15:23 +01:00

165 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 PredisTestCase;
use SplQueue;
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\SingleConnectionInterface');
$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\SingleConnectionInterface');
$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\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'));
$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\SingleConnectionInterface');
$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\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)));
$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 Predis\Pipeline\Atomic can be used only with connections to single nodes
*/
public function testExecutorWithAggregatedConnection()
{
$connection = $this->getMock('Predis\Connection\ClusterConnectionInterface');
$pipeline = new Atomic(new Client($connection));
$pipeline->ping();
$pipeline->execute();
}
}