mirror of
https://github.com/predis/predis.git
synced 2026-08-25 14:39:40 +00:00
36c9bb047e
- Make use of more typehints for function parameters - Make use of typehints for function return values - Use @var where needed to give proper hints to IDEs and avoid warnings - Replace MockObject::setMethods() with addMethods() and onlyMethods() - Rewording of some phpdocs
124 lines
3.9 KiB
PHP
124 lines
3.9 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;
|
|
use Predis\Protocol\Text\Handler\ResponseHandlerInterface;
|
|
|
|
/**
|
|
*
|
|
*/
|
|
class ResponseReaderTest extends PredisTestCase
|
|
{
|
|
/**
|
|
* @group disconnected
|
|
*/
|
|
public function testDefaultHandlers(): void
|
|
{
|
|
$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(): void
|
|
{
|
|
/** @var ResponseHandlerInterface */
|
|
$handler = $this->getMockBuilder('Predis\Protocol\Text\Handler\ResponseHandlerInterface')->getMock();
|
|
|
|
$reader = new ResponseReader();
|
|
$reader->setHandler('+', $handler);
|
|
|
|
$this->assertSame($handler, $reader->getHandler('+'));
|
|
}
|
|
|
|
/**
|
|
* @group disconnected
|
|
*/
|
|
public function testReadResponse(): void
|
|
{
|
|
$connection = $this->getMockConnectionOfType('Predis\Connection\CompositeConnectionInterface');
|
|
$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'));
|
|
|
|
$reader = new ResponseReader();
|
|
|
|
$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
|
|
*/
|
|
public function testEmptyResponseHeader(): void
|
|
{
|
|
$this->expectException('Predis\Protocol\ProtocolException');
|
|
$this->expectExceptionMessage('Unexpected empty reponse header [tcp://127.0.0.1:6379]');
|
|
|
|
$connection = $this->getMockConnectionOfType('Predis\Connection\CompositeConnectionInterface', 'tcp://127.0.0.1:6379');
|
|
$connection
|
|
->expects($this->once())
|
|
->method('readLine')
|
|
->will($this->returnValue(''));
|
|
|
|
$reader = new ResponseReader();
|
|
$reader->read($connection);
|
|
}
|
|
|
|
/**
|
|
* @group disconnected
|
|
*/
|
|
public function testUnknownResponsePrefix(): void
|
|
{
|
|
$this->expectException('Predis\Protocol\ProtocolException');
|
|
$this->expectExceptionMessage("Unknown response prefix: '!' [tcp://127.0.0.1:6379]");
|
|
|
|
$connection = $this->getMockConnectionOfType('Predis\Connection\CompositeConnectionInterface', 'tcp://127.0.0.1:6379');
|
|
$connection
|
|
->expects($this->once())
|
|
->method('readLine')
|
|
->will($this->returnValue('!'));
|
|
|
|
$reader = new ResponseReader();
|
|
$reader->read($connection);
|
|
}
|
|
}
|