BulkResponseTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 BulkResponseTest extends PredisTestCase
  16. {
  17. /**
  18. * @group disconnected
  19. */
  20. public function testZeroLengthBulk()
  21. {
  22. $connection = $this->getMockConnectionOfType('Predis\Connection\CompositeConnectionInterface');
  23. $connection
  24. ->expects($this->never())
  25. ->method('readLine');
  26. $connection
  27. ->expects($this->once())
  28. ->method('readBuffer')
  29. ->with($this->equalTo(2))
  30. ->will($this->returnValue("\r\n"));
  31. $handler = new Handler\BulkResponse();
  32. $this->assertSame('', $handler->handle($connection, '0'));
  33. }
  34. /**
  35. * @group disconnected
  36. */
  37. public function testBulk()
  38. {
  39. $bulk = 'This is a bulk string.';
  40. $bulkLengh = strlen($bulk);
  41. $connection = $this->getMockConnectionOfType('Predis\Connection\CompositeConnectionInterface');
  42. $connection
  43. ->expects($this->never())
  44. ->method('readLine');
  45. $connection
  46. ->expects($this->once())
  47. ->method('readBuffer')
  48. ->with($this->equalTo($bulkLengh + 2))
  49. ->will($this->returnValue("$bulk\r\n"));
  50. $handler = new Handler\BulkResponse();
  51. $this->assertSame($bulk, $handler->handle($connection, (string) $bulkLengh));
  52. }
  53. /**
  54. * @group disconnected
  55. */
  56. public function testNull()
  57. {
  58. $connection = $this->getMockConnectionOfType('Predis\Connection\CompositeConnectionInterface');
  59. $connection
  60. ->expects($this->never())
  61. ->method('readLine');
  62. $connection
  63. ->expects($this->never())
  64. ->method('readBuffer');
  65. $handler = new Handler\BulkResponse();
  66. $this->assertNull($handler->handle($connection, '-1'));
  67. }
  68. /**
  69. * @group disconnected
  70. * @expectedException \Predis\Protocol\ProtocolException
  71. * @expectedExceptionMessage Cannot parse 'invalid' as a valid length for a bulk response [tcp://127.0.0.1:6379]
  72. */
  73. public function testInvalidLengthString()
  74. {
  75. $connection = $this->getMockConnectionOfType('Predis\Connection\CompositeConnectionInterface', 'tcp://127.0.0.1:6379');
  76. $connection
  77. ->expects($this->never())
  78. ->method('readLine');
  79. $connection
  80. ->expects($this->never())
  81. ->method('readBuffer');
  82. $handler = new Handler\BulkResponse();
  83. $handler->handle($connection, 'invalid');
  84. }
  85. /**
  86. * @group disconnected
  87. * @expectedException \Predis\Protocol\ProtocolException
  88. * @expectedExceptionMessage Value '-5' is not a valid length for a bulk response [tcp://127.0.0.1:6379]
  89. */
  90. public function testInvalidLengthInteger()
  91. {
  92. $connection = $this->getMockConnectionOfType('Predis\Connection\CompositeConnectionInterface', 'tcp://127.0.0.1:6379');
  93. $connection
  94. ->expects($this->never())
  95. ->method('readLine');
  96. $connection
  97. ->expects($this->never())
  98. ->method('readBuffer');
  99. $handler = new Handler\BulkResponse();
  100. $handler->handle($connection, '-5');
  101. }
  102. }