CommandTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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\Command;
  11. use PHPUnit_Framework_TestCase as StandardTestCase;
  12. /**
  13. *
  14. */
  15. class CommandTest extends StandardTestCase
  16. {
  17. /**
  18. * @group disconnected
  19. */
  20. public function testImplementsCorrectInterface()
  21. {
  22. $command = $this->getMockForAbstractClass('Predis\Command\Command');
  23. $this->assertInstanceOf('Predis\Command\CommandInterface', $command);
  24. }
  25. /**
  26. * @group disconnected
  27. */
  28. public function testGetEmptyArguments()
  29. {
  30. $command = $this->getMockForAbstractClass('Predis\Command\Command');
  31. $this->assertEmpty($command->getArguments());
  32. }
  33. /**
  34. * @group disconnected
  35. */
  36. public function testSetRawArguments()
  37. {
  38. $arguments = array('1st', '2nd', '3rd');
  39. $command = $this->getMockForAbstractClass('Predis\Command\Command');
  40. $command->setRawArguments($arguments);
  41. $this->assertEquals($arguments, $command->getArguments());
  42. }
  43. /**
  44. * @group disconnected
  45. *
  46. * @todo We cannot set an expectation for Command::filterArguments when we
  47. * invoke Command::setArguments() because it is protected. I wonder
  48. * how we can do that.
  49. */
  50. public function testSetArguments()
  51. {
  52. $arguments = array('1st', '2nd', '3rd');
  53. $command = $this->getMockForAbstractClass('Predis\Command\Command');
  54. $command->setArguments($arguments);
  55. $this->assertEquals($arguments, $command->getArguments());
  56. }
  57. /**
  58. * @group disconnected
  59. */
  60. public function testGetArgumentAtIndex()
  61. {
  62. $arguments = array('1st', '2nd', '3rd');
  63. $command = $this->getMockForAbstractClass('Predis\Command\Command');
  64. $command->setArguments($arguments);
  65. $this->assertEquals($arguments[0], $command->getArgument(0));
  66. $this->assertEquals($arguments[2], $command->getArgument(2));
  67. $this->assertNull($command->getArgument(10));
  68. }
  69. /**
  70. * @group disconnected
  71. */
  72. public function testParseResponse()
  73. {
  74. $response = 'response-buffer';
  75. $command = $this->getMockForAbstractClass('Predis\Command\Command');
  76. $this->assertEquals($response, $command->parseResponse($response));
  77. }
  78. /**
  79. * @group disconnected
  80. */
  81. public function testSetAndGetHash()
  82. {
  83. $hash = "key-hash";
  84. $command = $this->getMockForAbstractClass('Predis\Command\Command');
  85. $command->setRawArguments(array('key'));
  86. $this->assertNull($command->getHash());
  87. $command->setHash($hash);
  88. $this->assertSame($hash, $command->getHash());
  89. }
  90. /**
  91. * @group disconnected
  92. */
  93. public function testToString()
  94. {
  95. $expected = 'SET key value';
  96. $arguments = array('key', 'value');
  97. $command = $this->getMockForAbstractClass('Predis\Command\Command');
  98. $command->expects($this->once())->method('getId')->will($this->returnValue('SET'));
  99. $command->setRawArguments($arguments);
  100. $this->assertEquals($expected, (string) $command);
  101. }
  102. /**
  103. * @group disconnected
  104. */
  105. public function testToStringWithLongArguments()
  106. {
  107. $expected = 'SET key abcdefghijklmnopqrstuvwxyz012345[...]';
  108. $arguments = array('key', 'abcdefghijklmnopqrstuvwxyz0123456789');
  109. $command = $this->getMockForAbstractClass('Predis\Command\Command');
  110. $command->expects($this->once())->method('getId')->will($this->returnValue('SET'));
  111. $command->setRawArguments($arguments);
  112. $this->assertEquals($expected, (string) $command);
  113. }
  114. /**
  115. * @group disconnected
  116. */
  117. public function testNormalizeArguments()
  118. {
  119. $arguments = array('arg1', 'arg2', 'arg3', 'arg4');
  120. $this->assertSame($arguments, Command::normalizeArguments($arguments));
  121. $this->assertSame($arguments, Command::normalizeArguments(array($arguments)));
  122. $arguments = array(array(), array());
  123. $this->assertSame($arguments, Command::normalizeArguments($arguments));
  124. $arguments = array(new \stdClass());
  125. $this->assertSame($arguments, Command::normalizeArguments($arguments));
  126. }
  127. /**
  128. * @group disconnected
  129. */
  130. public function testNormalizeVariadic()
  131. {
  132. $arguments = array('key', 'value1', 'value2', 'value3');
  133. $this->assertSame($arguments, Command::normalizeVariadic($arguments));
  134. $this->assertSame($arguments, Command::normalizeVariadic(array('key', array('value1', 'value2', 'value3'))));
  135. $arguments = array(new \stdClass());
  136. $this->assertSame($arguments, Command::normalizeVariadic($arguments));
  137. }
  138. }