ResponseReaderTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. /*
  3. * This file is part of the Predis package.
  4. *
  5. * (c) Daniele Alessandri <suppakilla@gmail.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Predis\Protocol\Text;
  11. use PredisTestCase;
  12. /**
  13. *
  14. */
  15. class ResponseReaderTest extends PredisTestCase
  16. {
  17. /**
  18. * @group disconnected
  19. */
  20. public function testDefaultHandlers()
  21. {
  22. $reader = new ResponseReader();
  23. $this->assertInstanceOf('Predis\Protocol\Text\Handler\StatusResponse', $reader->getHandler('+'));
  24. $this->assertInstanceOf('Predis\Protocol\Text\Handler\ErrorResponse', $reader->getHandler('-'));
  25. $this->assertInstanceOf('Predis\Protocol\Text\Handler\IntegerResponse', $reader->getHandler(':'));
  26. $this->assertInstanceOf('Predis\Protocol\Text\Handler\BulkResponse', $reader->getHandler('$'));
  27. $this->assertInstanceOf('Predis\Protocol\Text\Handler\MultiBulkResponse', $reader->getHandler('*'));
  28. $this->assertNull($reader->getHandler('!'));
  29. }
  30. /**
  31. * @group disconnected
  32. */
  33. public function testReplaceHandler()
  34. {
  35. $handler = $this->getMock('Predis\Protocol\Text\Handler\ResponseHandlerInterface');
  36. $reader = new ResponseReader();
  37. $reader->setHandler('+', $handler);
  38. $this->assertSame($handler, $reader->getHandler('+'));
  39. }
  40. /**
  41. * @group disconnected
  42. */
  43. public function testReadResponse()
  44. {
  45. $connection = $this->getMockConnectionOfType('Predis\Connection\CompositeConnectionInterface');
  46. $connection
  47. ->expects($this->at(0))
  48. ->method('readLine')
  49. ->will($this->returnValue('+OK'));
  50. $connection
  51. ->expects($this->at(1))
  52. ->method('readLine')
  53. ->will($this->returnValue('-ERR error message'));
  54. $connection
  55. ->expects($this->at(2))
  56. ->method('readLine')
  57. ->will($this->returnValue(':2'));
  58. $connection
  59. ->expects($this->at(3))
  60. ->method('readLine')
  61. ->will($this->returnValue('$-1'));
  62. $connection
  63. ->expects($this->at(4))
  64. ->method('readLine')
  65. ->will($this->returnValue('*-1'));
  66. $reader = new ResponseReader();
  67. $this->assertEquals('OK', $reader->read($connection));
  68. $this->assertEquals('ERR error message', $reader->read($connection));
  69. $this->assertSame(2, $reader->read($connection));
  70. $this->assertNull($reader->read($connection));
  71. $this->assertNull($reader->read($connection));
  72. }
  73. /**
  74. * @group disconnected
  75. * @expectedException \Predis\Protocol\ProtocolException
  76. * @expectedExceptionMessage Unexpected empty reponse header [tcp://127.0.0.1:6379]
  77. */
  78. public function testEmptyResponseHeader()
  79. {
  80. $connection = $this->getMockConnectionOfType('Predis\Connection\CompositeConnectionInterface', 'tcp://127.0.0.1:6379');
  81. $connection
  82. ->expects($this->once())
  83. ->method('readLine')
  84. ->will($this->returnValue(''));
  85. $reader = new ResponseReader();
  86. $reader->read($connection);
  87. }
  88. /**
  89. * @group disconnected
  90. * @expectedException \Predis\Protocol\ProtocolException
  91. * @expectedExceptionMessage Unknown response prefix: '!' [tcp://127.0.0.1:6379]
  92. */
  93. public function testUnknownResponsePrefix()
  94. {
  95. $connection = $this->getMockConnectionOfType('Predis\Connection\CompositeConnectionInterface', 'tcp://127.0.0.1:6379');
  96. $connection
  97. ->expects($this->once())
  98. ->method('readLine')
  99. ->will($this->returnValue('!'));
  100. $reader = new ResponseReader();
  101. $reader->read($connection);
  102. }
  103. }