RequestSerializerTest.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 RequestSerializerTest extends PredisTestCase
  16. {
  17. /**
  18. * @group disconnected
  19. */
  20. public function testSerializerIdWithNoArguments()
  21. {
  22. $serializer = new RequestSerializer();
  23. $command = $this->getMock('Predis\Command\CommandInterface');
  24. $command
  25. ->expects($this->once())
  26. ->method('getId')
  27. ->will($this->returnValue('PING'));
  28. $command
  29. ->expects($this->once())
  30. ->method('getArguments')
  31. ->will($this->returnValue(array()));
  32. $result = $serializer->serialize($command);
  33. $this->assertSame("*1\r\n$4\r\nPING\r\n", $result);
  34. }
  35. /**
  36. * @group disconnected
  37. */
  38. public function testSerializerIdWithArguments()
  39. {
  40. $serializer = new RequestSerializer();
  41. $command = $this->getMock('Predis\Command\CommandInterface');
  42. $command
  43. ->expects($this->once())
  44. ->method('getId')
  45. ->will($this->returnValue('SET'));
  46. $command
  47. ->expects($this->once())
  48. ->method('getArguments')
  49. ->will($this->returnValue(array('key', 'value')));
  50. $result = $serializer->serialize($command);
  51. $this->assertSame("*3\r\n$3\r\nSET\r\n$3\r\nkey\r\n$5\r\nvalue\r\n", $result);
  52. }
  53. /**
  54. * @group disconnected
  55. */
  56. public function testSerializerDoesNotBreakOnArgumentsWithHoles()
  57. {
  58. $serializer = new RequestSerializer();
  59. $command = $this->getMock('Predis\Command\CommandInterface');
  60. $command
  61. ->expects($this->once())
  62. ->method('getId')
  63. ->will($this->returnValue('DEL'));
  64. $command
  65. ->expects($this->once())
  66. ->method('getArguments')
  67. ->will($this->returnValue(array(0 => 'key:1', 2 => 'key:2')));
  68. $result = $serializer->serialize($command);
  69. $this->assertSame("*3\r\n$3\r\nDEL\r\n$5\r\nkey:1\r\n$5\r\nkey:2\r\n", $result);
  70. }
  71. }