mirror of
https://github.com/predis/predis.git
synced 2026-08-25 19:39:39 +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.
122 lines
3.7 KiB
PHP
122 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\Response\Iterator;
|
|
|
|
use PredisTestCase;
|
|
use Predis\Client;
|
|
use Predis\Connection\ComposableStreamConnection;
|
|
use Predis\Connection\ConnectionParameters;
|
|
use Predis\Protocol\Text\ProtocolProcessor as TextProtocolProcessor;
|
|
|
|
/**
|
|
* @group realm-iterators
|
|
*/
|
|
class MultiBulkTupleTest extends PredisTestCase
|
|
{
|
|
/**
|
|
* @group disconnected
|
|
* @expectedException RuntimeException
|
|
* @expectedExceptionMessage Cannot initialize a tuple iterator with an already initiated iterator
|
|
*/
|
|
public function testInitiatedMultiBulkIteratorsAreNotValid()
|
|
{
|
|
$connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
|
|
$iterator = new MultiBulk($connection, 2);
|
|
$iterator->next();
|
|
|
|
new MultiBulkTuple($iterator);
|
|
}
|
|
|
|
/**
|
|
* @group disconnected
|
|
* @expectedException UnexpectedValueException
|
|
* @expectedExceptionMessage Invalid response size for a tuple iterator [3]
|
|
*/
|
|
public function testMultiBulkWithOddSizesAreInvalid()
|
|
{
|
|
$connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
|
|
$iterator = new MultiBulk($connection, 3);
|
|
|
|
new MultiBulkTuple($iterator);
|
|
}
|
|
|
|
/**
|
|
* @group connected
|
|
*/
|
|
public function testIterableMultibulk()
|
|
{
|
|
$client = $this->getClient();
|
|
$client->zadd('metavars', 1, 'foo', 2, 'hoge', 3, 'lol');
|
|
|
|
$this->assertInstanceOf('OuterIterator', $iterator = $client->zrange('metavars', 0, -1, 'withscores')->asTuple());
|
|
$this->assertInstanceOf('Predis\Response\Iterator\MultiBulkTuple', $iterator);
|
|
$this->assertInstanceOf('Predis\Response\Iterator\MultiBulk', $iterator->getInnerIterator());
|
|
$this->assertTrue($iterator->valid());
|
|
$this->assertSame(3, $iterator->count());
|
|
|
|
$this->assertSame(array('foo', '1'), $iterator->current());
|
|
$this->assertSame(1, $iterator->next());
|
|
$this->assertTrue($iterator->valid());
|
|
|
|
$this->assertSame(array('hoge', '2'), $iterator->current());
|
|
$this->assertSame(2, $iterator->next());
|
|
$this->assertTrue($iterator->valid());
|
|
|
|
$this->assertSame(array('lol', '3'), $iterator->current());
|
|
$this->assertSame(3, $iterator->next());
|
|
$this->assertFalse($iterator->valid());
|
|
|
|
$this->assertEquals('PONG', $client->ping());
|
|
}
|
|
|
|
/**
|
|
* @group connected
|
|
*/
|
|
public function testGarbageCollectorDropsUnderlyingConnection()
|
|
{
|
|
$client = $this->getClient();
|
|
$client->zadd('metavars', 1, 'foo', 2, 'hoge', 3, 'lol');
|
|
|
|
$iterator = $client->zrange('metavars', 0, -1, 'withscores')->asTuple();
|
|
|
|
unset($iterator);
|
|
|
|
$this->assertFalse($client->isConnected());
|
|
$this->assertEquals('PONG', $client->ping());
|
|
}
|
|
|
|
// ******************************************************************** //
|
|
// ---- HELPER METHODS ------------------------------------------------ //
|
|
// ******************************************************************** //
|
|
|
|
/**
|
|
* Returns a new client instance.
|
|
*
|
|
* @return Client
|
|
*/
|
|
protected function getClient()
|
|
{
|
|
$parameters = $this->getParameters(array('read_write_timeout' => 2));
|
|
|
|
$protocol = new TextProtocolProcessor();
|
|
$protocol->useIterableMultibulk(true);
|
|
|
|
$connection = new ComposableStreamConnection($parameters, $protocol);
|
|
|
|
$client = new Client($connection);
|
|
$client->connect();
|
|
$client->flushdb();
|
|
|
|
return $client;
|
|
}
|
|
}
|