TextResponseReaderTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 \PHPUnit_Framework_TestCase as StandardTestCase;
  12. /**
  13. *
  14. */
  15. class TextResponseReaderTest extends StandardTestCase
  16. {
  17. /**
  18. * @group disconnected
  19. */
  20. public function testDefaultHandlers()
  21. {
  22. $reader = new TextResponseReader();
  23. $this->assertInstanceOf('Predis\Protocol\Text\ResponseStatusHandler', $reader->getHandler('+'));
  24. $this->assertInstanceOf('Predis\Protocol\Text\ResponseErrorHandler', $reader->getHandler('-'));
  25. $this->assertInstanceOf('Predis\Protocol\Text\ResponseIntegerHandler', $reader->getHandler(':'));
  26. $this->assertInstanceOf('Predis\Protocol\Text\ResponseBulkHandler', $reader->getHandler('$'));
  27. $this->assertInstanceOf('Predis\Protocol\Text\ResponseMultiBulkHandler', $reader->getHandler('*'));
  28. $this->assertNull($reader->getHandler('!'));
  29. }
  30. /**
  31. * @group disconnected
  32. */
  33. public function testReplaceHandler()
  34. {
  35. $handler = $this->getMock('Predis\Protocol\ResponseHandlerInterface');
  36. $reader = new TextResponseReader();
  37. $reader->setHandler('+', $handler);
  38. $this->assertSame($handler, $reader->getHandler('+'));
  39. }
  40. /**
  41. * @group disconnected
  42. */
  43. public function testReadResponse()
  44. {
  45. $reader = new TextResponseReader();
  46. $protocol = new ComposableTextProtocol();
  47. $protocol->setReader($reader);
  48. $connection = $this->getMock('Predis\Connection\ComposableConnectionInterface');
  49. $connection->expects($this->at(0))
  50. ->method('readLine')
  51. ->will($this->returnValue("+OK"));
  52. $connection->expects($this->at(1))
  53. ->method('readLine')
  54. ->will($this->returnValue("-ERR error message"));
  55. $connection->expects($this->at(2))
  56. ->method('readLine')
  57. ->will($this->returnValue(":2"));
  58. $connection->expects($this->at(3))
  59. ->method('readLine')
  60. ->will($this->returnValue("$-1"));
  61. $connection->expects($this->at(4))
  62. ->method('readLine')
  63. ->will($this->returnValue("*-1"));
  64. $this->assertTrue($reader->read($connection));
  65. $this->assertEquals("ERR error message", $reader->read($connection));
  66. $this->assertSame(2, $reader->read($connection));
  67. $this->assertNull($reader->read($connection));
  68. $this->assertNull($reader->read($connection));
  69. }
  70. /**
  71. * @group disconnected
  72. * @expectedException Predis\Protocol\ProtocolException
  73. * @expectedExceptionMessage Unexpected empty header
  74. */
  75. public function testEmptyResponseHeader()
  76. {
  77. $reader = new TextResponseReader();
  78. $connection = $this->getMock('Predis\Connection\ComposableConnectionInterface');
  79. $connection->expects($this->once())
  80. ->method('readLine')
  81. ->will($this->returnValue(''));
  82. $reader->read($connection);
  83. }
  84. /**
  85. * @group disconnected
  86. * @expectedException Predis\Protocol\ProtocolException
  87. * @expectedExceptionMessage Unknown prefix: '!'
  88. */
  89. public function testUnknownResponsePrefix()
  90. {
  91. $reader = new TextResponseReader();
  92. $connection = $this->getMock('Predis\Connection\ComposableConnectionInterface');
  93. $connection->expects($this->once())
  94. ->method('readLine')
  95. ->will($this->returnValue('!'));
  96. $reader->read($connection);
  97. }
  98. }