ProtocolProcessorTest.php 3.4 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 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. public function testConnectionRead()
  41. {
  42. $protocol = new ProtocolProcessor();
  43. $connection = $this->getMock('Predis\Connection\CompositeConnectionInterface');
  44. $connection->expects($this->at(0))
  45. ->method('readLine')
  46. ->will($this->returnValue('+OK'));
  47. $connection->expects($this->at(1))
  48. ->method('readLine')
  49. ->will($this->returnValue('-ERR error message'));
  50. $connection->expects($this->at(2))
  51. ->method('readLine')
  52. ->will($this->returnValue(':2'));
  53. $connection->expects($this->at(3))
  54. ->method('readLine')
  55. ->will($this->returnValue('$-1'));
  56. $connection->expects($this->at(4))
  57. ->method('readLine')
  58. ->will($this->returnValue('*-1'));
  59. $this->assertEquals('OK', $protocol->read($connection));
  60. $this->assertEquals('ERR error message', $protocol->read($connection));
  61. $this->assertSame(2, $protocol->read($connection));
  62. $this->assertNull($protocol->read($connection));
  63. $this->assertNull($protocol->read($connection));
  64. }
  65. /**
  66. * @group disconnected
  67. */
  68. public function testIterableMultibulkSupport()
  69. {
  70. $protocol = new ProtocolProcessor();
  71. $protocol->useIterableMultibulk(true);
  72. $connection = $this->getMock('Predis\Connection\CompositeConnectionInterface');
  73. $connection->expects($this->once(4))
  74. ->method('readLine')
  75. ->will($this->returnValue('*1'));
  76. $this->assertInstanceOf('Predis\Response\Iterator\MultiBulk', $protocol->read($connection));
  77. }
  78. /**
  79. * @group disconnected
  80. * @expectedException \Predis\Protocol\ProtocolException
  81. * @expectedExceptionMessage Unknown response prefix: '!'.
  82. */
  83. public function testUnknownResponsePrefix()
  84. {
  85. $protocol = new ProtocolProcessor();
  86. $connection = $this->getMock('Predis\Connection\CompositeConnectionInterface');
  87. $connection->expects($this->once())
  88. ->method('readLine')
  89. ->will($this->returnValue('!'));
  90. $protocol->read($connection);
  91. }
  92. }