RawCommandTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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($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($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. * The signature of RawCommand::create() requires one argument which is the
  52. * ID of the command (other arguments are fetched dinamically). If the first
  53. * argument is missing, PHP emits an E_WARNING.
  54. *
  55. * @group disconnected
  56. */
  57. public function testPHPWarningOnMissingCommandIDWithStaticCreate()
  58. {
  59. if (version_compare(PHP_VERSION, "7.1", '>')) {
  60. $this->markTestSkipped('only for PHP < 7.1');
  61. }
  62. $this->setExpectedException('PHPUnit_Framework_Error_Warning');
  63. RawCommand::create();
  64. }
  65. /**
  66. * The signature of RawCommand::create() requires one argument which is the
  67. * ID of the command (other arguments are fetched dinamically). If the first
  68. * argument is missing, PHP 7.1 throw an exception
  69. *
  70. * @group disconnected
  71. */
  72. public function testPHPWarningOnMissingCommandIDWithStaticCreate71()
  73. {
  74. if (version_compare(PHP_VERSION, "7.1", '<')) {
  75. $this->markTestSkipped('only for PHP > 7.1');
  76. }
  77. $this->setExpectedException('ArgumentCountError');
  78. RawCommand::create();
  79. }
  80. /**
  81. * @group disconnected
  82. */
  83. public function testSetArguments()
  84. {
  85. $commandID = 'SET';
  86. $command = new RawCommand($commandID);
  87. $command->setArguments($commandArgs = array('foo', 'bar'));
  88. $this->assertSame($commandArgs, $command->getArguments());
  89. $command->setArguments($commandArgs = array('hoge', 'piyo'));
  90. $this->assertSame($commandArgs, $command->getArguments());
  91. }
  92. /**
  93. * @group disconnected
  94. */
  95. public function testSetRawArguments()
  96. {
  97. $commandID = 'SET';
  98. $command = new RawCommand($commandID);
  99. $command->setRawArguments($commandArgs = array('foo', 'bar'));
  100. $this->assertSame($commandArgs, $command->getArguments());
  101. $command->setRawArguments($commandArgs = array('hoge', 'piyo'));
  102. $this->assertSame($commandArgs, $command->getArguments());
  103. }
  104. /**
  105. * @group disconnected
  106. */
  107. public function testGetArgumentAtIndex()
  108. {
  109. $command = new RawCommand('GET', array('key'));
  110. $this->assertSame('key', $command->getArgument(0));
  111. $this->assertNull($command->getArgument(1));
  112. }
  113. /**
  114. * @group disconnected
  115. */
  116. public function testSetAndGetHash()
  117. {
  118. $slot = 1024;
  119. $arguments = array('key', 'value');
  120. $command = new RawCommand('SET', $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('set', array('key', 'value'));
  133. $this->assertSame('SET', $command->getId());
  134. }
  135. }