RawCommandTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 RawCommandTest extends PredisTestCase
  16. {
  17. /**
  18. * @group disconnected
  19. */
  20. public function testConstructorWithCommandID()
  21. {
  22. $commandID = 'PING';
  23. $command = new RawCommand(array($commandID));
  24. $this->assertSame($commandID, $command->getId());
  25. $this->assertEmpty($command->getArguments());
  26. }
  27. /**
  28. * @group disconnected
  29. */
  30. public function testConstructorWithCommandIDAndArguments()
  31. {
  32. $commandID = 'SET';
  33. $commandArgs = array('foo', 'bar');
  34. $command = new RawCommand(array_merge((array) $commandID, $commandArgs));
  35. $this->assertSame($commandID, $command->getId());
  36. $this->assertSame($commandArgs, $command->getArguments());
  37. }
  38. /**
  39. * @group disconnected
  40. */
  41. public function testStaticCreate()
  42. {
  43. $command = RawCommand::create('SET');
  44. $this->assertSame('SET', $command->getId());
  45. $this->assertEmpty($command->getArguments());
  46. $command = RawCommand::create('SET', 'foo', 'bar');
  47. $this->assertSame('SET', $command->getId());
  48. $this->assertSame(array('foo', 'bar'), $command->getArguments());
  49. }
  50. /**
  51. * @group disconnected
  52. * @expectedException \InvalidArgumentException
  53. * @expectedExceptionMessage The arguments array must contain at least the command ID.
  54. */
  55. public function testExceptionOnMissingCommandID()
  56. {
  57. new RawCommand(array());
  58. }
  59. /**
  60. * The signature of RawCommand::create() requires one argument which is the
  61. * ID of the command (other arguments are fetched dinamically). If the first
  62. * argument is missing, PHP emits an E_WARNING.
  63. *
  64. * @group disconnected
  65. */
  66. public function testPHPWarningOnMissingCommandIDWithStaticCreate()
  67. {
  68. if (version_compare(PHP_VERSION, "7.1", '>')) {
  69. $this->markTestSkipped('only for PHP < 7.1');
  70. }
  71. $this->setExpectedException('PHPUnit_Framework_Error_Warning');
  72. RawCommand::create();
  73. }
  74. /**
  75. * The signature of RawCommand::create() requires one argument which is the
  76. * ID of the command (other arguments are fetched dinamically). If the first
  77. * argument is missing, PHP 7.1 throw an exception
  78. *
  79. * @group disconnected
  80. */
  81. public function testPHPWarningOnMissingCommandIDWithStaticCreate71()
  82. {
  83. if (version_compare(PHP_VERSION, "7.1", '<')) {
  84. $this->markTestSkipped('only for PHP > 7.1');
  85. }
  86. $this->setExpectedException('ArgumentCountError');
  87. RawCommand::create();
  88. }
  89. /**
  90. * @group disconnected
  91. */
  92. public function testSetArguments()
  93. {
  94. $commandID = 'SET';
  95. $command = new RawCommand(array($commandID));
  96. $command->setArguments($commandArgs = array('foo', 'bar'));
  97. $this->assertSame($commandArgs, $command->getArguments());
  98. $command->setArguments($commandArgs = array('hoge', 'piyo'));
  99. $this->assertSame($commandArgs, $command->getArguments());
  100. }
  101. /**
  102. * @group disconnected
  103. */
  104. public function testSetRawArguments()
  105. {
  106. $commandID = 'SET';
  107. $command = new RawCommand(array($commandID));
  108. $command->setRawArguments($commandArgs = array('foo', 'bar'));
  109. $this->assertSame($commandArgs, $command->getArguments());
  110. $command->setRawArguments($commandArgs = array('hoge', 'piyo'));
  111. $this->assertSame($commandArgs, $command->getArguments());
  112. }
  113. /**
  114. * @group disconnected
  115. */
  116. public function testSetAndGetHash()
  117. {
  118. $slot = 1024;
  119. $arguments = array('SET', 'key', 'value');
  120. $command = new RawCommand($arguments);
  121. $this->assertNull($command->getSlot());
  122. $command->setSlot($slot);
  123. $this->assertSame($slot, $command->getSlot());
  124. $command->setArguments(array('hoge', 'piyo'));
  125. $this->assertNull($command->getSlot());
  126. }
  127. /**
  128. * @group disconnected
  129. */
  130. public function testNormalizesCommandIdentifiersToUppercase()
  131. {
  132. $command = new RawCommand(array('set', 'key', 'value'));
  133. $this->assertSame('SET', $command->getId());
  134. }
  135. }