mirror of
https://github.com/predis/predis.git
synced 2026-08-26 11:49:51 +00:00
373d30b070
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.
124 lines
3.7 KiB
PHP
124 lines
3.7 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\Protocol\Text;
|
|
|
|
use PredisTestCase;
|
|
|
|
/**
|
|
*
|
|
*/
|
|
class ResponseReaderTest extends PredisTestCase
|
|
{
|
|
/**
|
|
* @group disconnected
|
|
*/
|
|
public function testDefaultHandlers()
|
|
{
|
|
$reader = new ResponseReader();
|
|
|
|
$this->assertInstanceOf('Predis\Protocol\Text\Handler\StatusResponse', $reader->getHandler('+'));
|
|
$this->assertInstanceOf('Predis\Protocol\Text\Handler\ErrorResponse', $reader->getHandler('-'));
|
|
$this->assertInstanceOf('Predis\Protocol\Text\Handler\IntegerResponse', $reader->getHandler(':'));
|
|
$this->assertInstanceOf('Predis\Protocol\Text\Handler\BulkResponse', $reader->getHandler('$'));
|
|
$this->assertInstanceOf('Predis\Protocol\Text\Handler\MultiBulkResponse', $reader->getHandler('*'));
|
|
|
|
$this->assertNull($reader->getHandler('!'));
|
|
}
|
|
|
|
/**
|
|
* @group disconnected
|
|
*/
|
|
public function testReplaceHandler()
|
|
{
|
|
$handler = $this->getMock('Predis\Protocol\Text\Handler\ResponseHandlerInterface');
|
|
|
|
$reader = new ResponseReader();
|
|
$reader->setHandler('+', $handler);
|
|
|
|
$this->assertSame($handler, $reader->getHandler('+'));
|
|
}
|
|
|
|
/**
|
|
* @group disconnected
|
|
*/
|
|
public function testReadResponse()
|
|
{
|
|
$reader = new ResponseReader();
|
|
|
|
$protocol = new ComposableProtocolProcessor();
|
|
$protocol->setResponseReader($reader);
|
|
|
|
$connection = $this->getMock('Predis\Connection\ComposableConnectionInterface');
|
|
|
|
$connection->expects($this->at(0))
|
|
->method('readLine')
|
|
->will($this->returnValue("+OK"));
|
|
|
|
$connection->expects($this->at(1))
|
|
->method('readLine')
|
|
->will($this->returnValue("-ERR error message"));
|
|
|
|
$connection->expects($this->at(2))
|
|
->method('readLine')
|
|
->will($this->returnValue(":2"));
|
|
|
|
$connection->expects($this->at(3))
|
|
->method('readLine')
|
|
->will($this->returnValue("$-1"));
|
|
|
|
$connection->expects($this->at(4))
|
|
->method('readLine')
|
|
->will($this->returnValue("*-1"));
|
|
|
|
$this->assertEquals('OK', $reader->read($connection));
|
|
$this->assertEquals('ERR error message', $reader->read($connection));
|
|
$this->assertSame(2, $reader->read($connection));
|
|
$this->assertNull($reader->read($connection));
|
|
$this->assertNull($reader->read($connection));
|
|
}
|
|
|
|
/**
|
|
* @group disconnected
|
|
* @expectedException Predis\Protocol\ProtocolException
|
|
* @expectedExceptionMessage Unexpected empty header
|
|
*/
|
|
public function testEmptyResponseHeader()
|
|
{
|
|
$reader = new ResponseReader();
|
|
|
|
$connection = $this->getMock('Predis\Connection\ComposableConnectionInterface');
|
|
|
|
$connection->expects($this->once())
|
|
->method('readLine')
|
|
->will($this->returnValue(''));
|
|
|
|
$reader->read($connection);
|
|
}
|
|
/**
|
|
* @group disconnected
|
|
* @expectedException Predis\Protocol\ProtocolException
|
|
* @expectedExceptionMessage Unknown prefix: '!'
|
|
*/
|
|
public function testUnknownResponsePrefix()
|
|
{
|
|
$reader = new ResponseReader();
|
|
|
|
$connection = $this->getMock('Predis\Connection\ComposableConnectionInterface');
|
|
|
|
$connection->expects($this->once())
|
|
->method('readLine')
|
|
->will($this->returnValue('!'));
|
|
|
|
$reader->read($connection);
|
|
}
|
|
}
|