ResponseBulkHandlerTest.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 ResponseBulkHandlerTest extends StandardTestCase
  16. {
  17. /**
  18. * @group disconnected
  19. */
  20. public function testZeroLengthBulk()
  21. {
  22. $handler = new ResponseBulkHandler();
  23. $connection = $this->getMock('Predis\Connection\ComposableConnectionInterface');
  24. $connection->expects($this->never())->method('readLine');
  25. $connection->expects($this->once())
  26. ->method('readBytes')
  27. ->with($this->equalTo(2))
  28. ->will($this->returnValue("\r\n"));
  29. $this->assertSame('', $handler->handle($connection, '0'));
  30. }
  31. /**
  32. * @group disconnected
  33. */
  34. public function testBulk()
  35. {
  36. $bulk = "This is a bulk string.";
  37. $bulkLengh = (string) strlen($bulk);
  38. $handler = new ResponseBulkHandler();
  39. $connection = $this->getMock('Predis\Connection\ComposableConnectionInterface');
  40. $connection->expects($this->never())->method('readLine');
  41. $connection->expects($this->once())
  42. ->method('readBytes')
  43. ->with($this->equalTo($bulkLengh + 2))
  44. ->will($this->returnValue("$bulk\r\n"));
  45. $this->assertSame($bulk, $handler->handle($connection, $bulkLengh));
  46. }
  47. /**
  48. * @group disconnected
  49. */
  50. public function testNull()
  51. {
  52. $handler = new ResponseBulkHandler();
  53. $connection = $this->getMock('Predis\Connection\ComposableConnectionInterface');
  54. $connection->expects($this->never())->method('readLine');
  55. $connection->expects($this->never())->method('readBytes');
  56. $this->assertNull($handler->handle($connection, '-1'));
  57. }
  58. /**
  59. * @group disconnected
  60. * @expectedException Predis\Protocol\ProtocolException
  61. * @expectedExceptionMessage Cannot parse 'invalid' as bulk length
  62. */
  63. public function testInvalidLength()
  64. {
  65. $handler = new ResponseBulkHandler();
  66. $connection = $this->getMock('Predis\Connection\ComposableConnectionInterface');
  67. $connection->expects($this->never())->method('readLine');
  68. $connection->expects($this->never())->method('readBytes');
  69. $handler->handle($connection, 'invalid');
  70. }
  71. }