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