TextProtocolTest.php 3.5 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 \PHPUnit_Framework_TestCase as StandardTestCase;
  12. /**
  13. *
  14. */
  15. class TextProtocolTest extends StandardTestCase
  16. {
  17. /**
  18. * @group disconnected
  19. */
  20. public function testConnectionWrite()
  21. {
  22. $serialized = "*1\r\n$4\r\nPING\r\n";
  23. $protocol = new TextProtocol();
  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\ComposableConnectionInterface');
  32. $connection->expects($this->once())
  33. ->method('writeBytes')
  34. ->with($this->equalTo($serialized));
  35. $protocol->write($connection, $command);
  36. }
  37. /**
  38. * @todo Improve test coverage
  39. * @group disconnected
  40. */
  41. public function testConnectionRead()
  42. {
  43. $protocol = new TextProtocol();
  44. $connection = $this->getMock('Predis\Connection\ComposableConnectionInterface');
  45. $connection->expects($this->at(0))
  46. ->method('readLine')
  47. ->will($this->returnValue("+OK"));
  48. $connection->expects($this->at(1))
  49. ->method('readLine')
  50. ->will($this->returnValue("-ERR error message"));
  51. $connection->expects($this->at(2))
  52. ->method('readLine')
  53. ->will($this->returnValue(":2"));
  54. $connection->expects($this->at(3))
  55. ->method('readLine')
  56. ->will($this->returnValue("$-1"));
  57. $connection->expects($this->at(4))
  58. ->method('readLine')
  59. ->will($this->returnValue("*-1"));
  60. $this->assertTrue($protocol->read($connection));
  61. $this->assertEquals("ERR error message", $protocol->read($connection));
  62. $this->assertSame(2, $protocol->read($connection));
  63. $this->assertNull($protocol->read($connection));
  64. $this->assertNull($protocol->read($connection));
  65. }
  66. /**
  67. * @group disconnected
  68. */
  69. public function testIterableMultibulkSupport()
  70. {
  71. $protocol = new TextProtocol();
  72. $protocol->setOption('iterable_multibulk', true);
  73. $connection = $this->getMock('Predis\Connection\ComposableConnectionInterface');
  74. $connection->expects($this->once(4))
  75. ->method('readLine')
  76. ->will($this->returnValue("*1"));
  77. $this->assertInstanceOf('Predis\Iterator\MultiBulkResponseSimple', $protocol->read($connection));
  78. }
  79. /**
  80. * @group disconnected
  81. * @expectedException Predis\Protocol\ProtocolException
  82. * @expectedExceptionMessage Unknown prefix: '!'
  83. */
  84. public function testUnknownResponsePrefix()
  85. {
  86. $protocol = new TextProtocol();
  87. $connection = $this->getMock('Predis\Connection\ComposableConnectionInterface');
  88. $connection->expects($this->once())
  89. ->method('readLine')
  90. ->will($this->returnValue('!'));
  91. $protocol->read($connection);
  92. }
  93. }