CommandTest.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 PredisTestCase;
  12. /**
  13. *
  14. */
  15. class CommandTest extends PredisTestCase
  16. {
  17. /**
  18. * @group disconnected
  19. */
  20. public function testImplementsCorrectInterface()
  21. {
  22. $command = $this->getMockForAbstractClass('Predis\Command\AbstractCommand');
  23. $this->assertInstanceOf('Predis\Command\CommandInterface', $command);
  24. }
  25. /**
  26. * @group disconnected
  27. */
  28. public function testGetEmptyArguments()
  29. {
  30. $command = $this->getMockForAbstractClass('Predis\Command\AbstractCommand');
  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\AbstractCommand');
  40. $command->setRawArguments($arguments);
  41. $this->assertEquals($arguments, $command->getArguments());
  42. }
  43. /**
  44. * @group disconnected
  45. *
  46. * @todo Since AbstractCommand::filterArguments is protected we cannot set an expectation
  47. * for it when AbstractCommand::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\Command\AbstractCommand');
  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\Command\AbstractCommand');
  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\Command\AbstractCommand');
  75. $this->assertEquals($response, $command->parseResponse($response));
  76. }
  77. /**
  78. * @group disconnected
  79. */
  80. public function testSetAndGetHash()
  81. {
  82. $hash = "key-hash";
  83. $command = $this->getMockForAbstractClass('Predis\Command\AbstractCommand');
  84. $command->setRawArguments(array('key'));
  85. $this->assertNull($command->getHash());
  86. $command->setHash($hash);
  87. $this->assertSame($hash, $command->getHash());
  88. $command->setArguments(array('key'));
  89. $this->assertNull($command->getHash());
  90. $command->setHash($hash);
  91. $command->setRawArguments(array('key'));
  92. $this->assertNull($command->getHash());
  93. }
  94. /**
  95. * @group disconnected
  96. */
  97. public function testToString()
  98. {
  99. $expected = 'SET key value';
  100. $arguments = array('key', 'value');
  101. $command = $this->getMockForAbstractClass('Predis\Command\AbstractCommand');
  102. $command->expects($this->once())->method('getId')->will($this->returnValue('SET'));
  103. $command->setRawArguments($arguments);
  104. $this->assertEquals($expected, (string) $command);
  105. }
  106. /**
  107. * @group disconnected
  108. */
  109. public function testToStringWithLongArguments()
  110. {
  111. $expected = 'SET key abcdefghijklmnopqrstuvwxyz012345[...]';
  112. $arguments = array('key', 'abcdefghijklmnopqrstuvwxyz0123456789');
  113. $command = $this->getMockForAbstractClass('Predis\Command\AbstractCommand');
  114. $command->expects($this->once())->method('getId')->will($this->returnValue('SET'));
  115. $command->setRawArguments($arguments);
  116. $this->assertEquals($expected, (string) $command);
  117. }
  118. /**
  119. * @group disconnected
  120. */
  121. public function testNormalizeArguments()
  122. {
  123. $arguments = array('arg1', 'arg2', 'arg3', 'arg4');
  124. $this->assertSame($arguments, AbstractCommand::normalizeArguments($arguments));
  125. $this->assertSame($arguments, AbstractCommand::normalizeArguments(array($arguments)));
  126. $arguments = array(array(), array());
  127. $this->assertSame($arguments, AbstractCommand::normalizeArguments($arguments));
  128. $arguments = array(new \stdClass());
  129. $this->assertSame($arguments, AbstractCommand::normalizeArguments($arguments));
  130. }
  131. /**
  132. * @group disconnected
  133. */
  134. public function testNormalizeVariadic()
  135. {
  136. $arguments = array('key', 'value1', 'value2', 'value3');
  137. $this->assertSame($arguments, AbstractCommand::normalizeVariadic($arguments));
  138. $this->assertSame($arguments, AbstractCommand::normalizeVariadic(array('key', array('value1', 'value2', 'value3'))));
  139. $arguments = array(new \stdClass());
  140. $this->assertSame($arguments, AbstractCommand::normalizeVariadic($arguments));
  141. }
  142. }