ProtocolProcessorTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 ProtocolProcessorTest extends PredisTestCase
  16. {
  17. /**
  18. * @group disconnected
  19. */
  20. public function testConnectionWrite()
  21. {
  22. $serialized = "*1\r\n$4\r\nPING\r\n";
  23. $protocol = new ProtocolProcessor();
  24. $command = $this->getMock('Predis\Command\CommandInterface');
  25. $command->expects($this->once())
  26. ->method('getId')
  27. ->will($this->returnValue('PING'));
  28. $command->expects($this->once())
  29. ->method('getArguments')
  30. ->will($this->returnValue(array()));
  31. $connection = $this->getMock('Predis\Connection\CompositeConnectionInterface');
  32. $connection->expects($this->once())
  33. ->method('writeBuffer')
  34. ->with($this->equalTo($serialized));
  35. $protocol->write($connection, $command);
  36. }
  37. /**
  38. * @group disconnected
  39. *
  40. * @todo Improve test coverage
  41. */
  42. public function testConnectionRead()
  43. {
  44. $protocol = new ProtocolProcessor();
  45. $connection = $this->getMock('Predis\Connection\CompositeConnectionInterface');
  46. $connection->expects($this->at(0))
  47. ->method('readLine')
  48. ->will($this->returnValue("+OK"));
  49. $connection->expects($this->at(1))
  50. ->method('readLine')
  51. ->will($this->returnValue("-ERR error message"));
  52. $connection->expects($this->at(2))
  53. ->method('readLine')
  54. ->will($this->returnValue(":2"));
  55. $connection->expects($this->at(3))
  56. ->method('readLine')
  57. ->will($this->returnValue("$-1"));
  58. $connection->expects($this->at(4))
  59. ->method('readLine')
  60. ->will($this->returnValue("*-1"));
  61. $this->assertEquals('OK', $protocol->read($connection));
  62. $this->assertEquals("ERR error message", $protocol->read($connection));
  63. $this->assertSame(2, $protocol->read($connection));
  64. $this->assertNull($protocol->read($connection));
  65. $this->assertNull($protocol->read($connection));
  66. }
  67. /**
  68. * @group disconnected
  69. */
  70. public function testIterableMultibulkSupport()
  71. {
  72. $protocol = new ProtocolProcessor();
  73. $protocol->useIterableMultibulk(true);
  74. $connection = $this->getMock('Predis\Connection\CompositeConnectionInterface');
  75. $connection->expects($this->once(4))
  76. ->method('readLine')
  77. ->will($this->returnValue("*1"));
  78. $this->assertInstanceOf('Predis\Response\Iterator\MultiBulk', $protocol->read($connection));
  79. }
  80. /**
  81. * @group disconnected
  82. * @expectedException \Predis\Protocol\ProtocolException
  83. * @expectedExceptionMessage Unknown response prefix: '!'.
  84. */
  85. public function testUnknownResponsePrefix()
  86. {
  87. $protocol = new ProtocolProcessor();
  88. $connection = $this->getMock('Predis\Connection\CompositeConnectionInterface');
  89. $connection->expects($this->once())
  90. ->method('readLine')
  91. ->will($this->returnValue('!'));
  92. $protocol->read($connection);
  93. }
  94. }