CommandTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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\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 testSetAndGetSlot()
  82. {
  83. $slot = 1024;
  84. $command = $this->getMockForAbstractClass('Predis\Command\Command');
  85. $command->setRawArguments(array('key'));
  86. $this->assertNull($command->getSlot());
  87. $command->setSlot($slot);
  88. $this->assertSame($slot, $command->getSlot());
  89. $command->setArguments(array('key'));
  90. $this->assertNull($command->getSlot());
  91. $command->setSlot($slot);
  92. $command->setRawArguments(array('key'));
  93. $this->assertNull($command->getSlot());
  94. }
  95. /**
  96. * @group disconnected
  97. */
  98. public function testNormalizeArguments()
  99. {
  100. $arguments = array('arg1', 'arg2', 'arg3', 'arg4');
  101. $this->assertSame($arguments, Command::normalizeArguments($arguments));
  102. $this->assertSame($arguments, Command::normalizeArguments(array($arguments)));
  103. $arguments = array(array(), array());
  104. $this->assertSame($arguments, Command::normalizeArguments($arguments));
  105. $arguments = array(new \stdClass());
  106. $this->assertSame($arguments, Command::normalizeArguments($arguments));
  107. }
  108. /**
  109. * @group disconnected
  110. */
  111. public function testNormalizeVariadic()
  112. {
  113. $arguments = array('key', 'value1', 'value2', 'value3');
  114. $this->assertSame($arguments, Command::normalizeVariadic($arguments));
  115. $this->assertSame($arguments, Command::normalizeVariadic(array('key', array('value1', 'value2', 'value3'))));
  116. $arguments = array(new \stdClass());
  117. $this->assertSame($arguments, Command::normalizeVariadic($arguments));
  118. }
  119. }