CommandTest.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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\Commands;
  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\Commands\Command');
  23. $this->assertInstanceOf('Predis\Commands\ICommand', $command);
  24. }
  25. /**
  26. * @group disconnected
  27. */
  28. public function testGetEmptyArguments()
  29. {
  30. $command = $this->getMockForAbstractClass('Predis\Commands\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\Commands\Command');
  40. $command->setRawArguments($arguments);
  41. $this->assertEquals($arguments, $command->getArguments());
  42. }
  43. /**
  44. * @group disconnected
  45. *
  46. * @todo Since Command::filterArguments is protected we cannot set an expectation
  47. * for it when Command::setArguments() is invoked. I wonder how we can do that.
  48. */
  49. public function testSetArguments()
  50. {
  51. $arguments = array('1st', '2nd', '3rd');
  52. $command = $this->getMockForAbstractClass('Predis\Commands\Command');
  53. $command->setArguments($arguments);
  54. $this->assertEquals($arguments, $command->getArguments());
  55. }
  56. /**
  57. * @group disconnected
  58. */
  59. public function testGetArgumentAtIndex()
  60. {
  61. $arguments = array('1st', '2nd', '3rd');
  62. $command = $this->getMockForAbstractClass('Predis\Commands\Command');
  63. $command->setArguments($arguments);
  64. $this->assertEquals($arguments[0], $command->getArgument(0));
  65. $this->assertEquals($arguments[2], $command->getArgument(2));
  66. $this->assertNull($command->getArgument(10));
  67. }
  68. /**
  69. * @group disconnected
  70. */
  71. public function testParseResponse()
  72. {
  73. $response = 'response-buffer';
  74. $command = $this->getMockForAbstractClass('Predis\Commands\Command');
  75. $this->assertEquals($response, $command->parseResponse($response));
  76. }
  77. /**
  78. * @group disconnected
  79. * @protected
  80. */
  81. public function testCheckSameHashForKeys()
  82. {
  83. $command = $this->getMockForAbstractClass('Predis\Commands\Command');
  84. $checkSameHashForKeys = new \ReflectionMethod($command, 'checkSameHashForKeys');
  85. $checkSameHashForKeys->setAccessible(true);
  86. $this->assertTrue($checkSameHashForKeys->invoke($command, array('foo', '{foo}:bar')));
  87. $this->assertFalse($checkSameHashForKeys->invoke($command, array('foo', '{foo}:bar', 'foo:bar')));
  88. }
  89. /**
  90. * @group disconnected
  91. * @protected
  92. */
  93. public function testCanBeHashed()
  94. {
  95. $command = $this->getMockForAbstractClass('Predis\Commands\Command');
  96. $canBeHashed = new \ReflectionMethod($command, 'canBeHashed');
  97. $canBeHashed->setAccessible(true);
  98. $this->assertFalse($canBeHashed->invoke($command));
  99. $command->setRawArguments(array('key'));
  100. $this->assertTrue($canBeHashed->invoke($command));
  101. }
  102. /**
  103. * @group disconnected
  104. */
  105. public function testDoesNotReturnAnHashByDefault()
  106. {
  107. $distributor = $this->getMock('Predis\Distribution\INodeKeyGenerator');
  108. $distributor->expects($this->never())->method('generateKey');
  109. $command = $this->getMockForAbstractClass('Predis\Commands\Command');
  110. $command->getHash($distributor);
  111. }
  112. /**
  113. * @group disconnected
  114. */
  115. public function testReturnAnHashWhenCanBeHashedAndCachesIt()
  116. {
  117. $key = 'key';
  118. $hash = "$key-hash";
  119. $distributor = $this->getMock('Predis\Distribution\INodeKeyGenerator');
  120. $distributor->expects($this->once())
  121. ->method('generateKey')
  122. ->with($key)
  123. ->will($this->returnValue($hash));
  124. $command = $this->getMockForAbstractClass('Predis\Commands\Command');
  125. $command->setRawArguments(array($key));
  126. $this->assertEquals($hash, $command->getHash($distributor));
  127. $this->assertEquals($hash, $command->getHash($distributor));
  128. $this->assertEquals($hash, $command->getHash($distributor));
  129. }
  130. /**
  131. * @group disconnected
  132. */
  133. public function testExtractsKeyTagsBeforeHashing()
  134. {
  135. $tag = 'key';
  136. $key = "{{$tag}}:ignore";
  137. $hash = "$tag-hash";
  138. $distributor = $this->getMock('Predis\Distribution\INodeKeyGenerator');
  139. $distributor->expects($this->once())
  140. ->method('generateKey')
  141. ->with($tag)
  142. ->will($this->returnValue($hash));
  143. $command = $this->getMockForAbstractClass('Predis\Commands\Command');
  144. $command->setRawArguments(array($key));
  145. $this->assertEquals($hash, $command->getHash($distributor));
  146. }
  147. /**
  148. * @group disconnected
  149. */
  150. public function testToString()
  151. {
  152. $expected = 'SET key value';
  153. $arguments = array('key', 'value');
  154. $command = $this->getMockForAbstractClass('Predis\Commands\Command');
  155. $command->expects($this->once())->method('getId')->will($this->returnValue('SET'));
  156. $command->setRawArguments($arguments);
  157. $this->assertEquals($expected, (string) $command);
  158. }
  159. /**
  160. * @group disconnected
  161. */
  162. public function testToStringWithLongArguments()
  163. {
  164. $expected = 'SET key abcdefghijklmnopqrstuvwxyz012345[...]';
  165. $arguments = array('key', 'abcdefghijklmnopqrstuvwxyz0123456789');
  166. $command = $this->getMockForAbstractClass('Predis\Commands\Command');
  167. $command->expects($this->once())->method('getId')->will($this->returnValue('SET'));
  168. $command->setRawArguments($arguments);
  169. $this->assertEquals($expected, (string) $command);
  170. }
  171. }