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