RawCommandTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. * @expectedException \PHPUnit_Framework_Error_Warning
  57. */
  58. public function testPHPWarningOnMissingCommandIDWithStaticCreate()
  59. {
  60. RawCommand::create();
  61. }
  62. /**
  63. * @group disconnected
  64. */
  65. public function testSetArguments()
  66. {
  67. $commandID = 'SET';
  68. $command = new RawCommand($commandID);
  69. $command->setArguments($commandArgs = array('foo', 'bar'));
  70. $this->assertSame($commandArgs, $command->getArguments());
  71. $command->setArguments($commandArgs = array('hoge', 'piyo'));
  72. $this->assertSame($commandArgs, $command->getArguments());
  73. }
  74. /**
  75. * @group disconnected
  76. */
  77. public function testSetRawArguments()
  78. {
  79. $commandID = 'SET';
  80. $command = new RawCommand($commandID);
  81. $command->setRawArguments($commandArgs = array('foo', 'bar'));
  82. $this->assertSame($commandArgs, $command->getArguments());
  83. $command->setRawArguments($commandArgs = array('hoge', 'piyo'));
  84. $this->assertSame($commandArgs, $command->getArguments());
  85. }
  86. /**
  87. * @group disconnected
  88. */
  89. public function testGetArgumentAtIndex()
  90. {
  91. $command = new RawCommand('GET', array('key'));
  92. $this->assertSame('key', $command->getArgument(0));
  93. $this->assertNull($command->getArgument(1));
  94. }
  95. /**
  96. * @group disconnected
  97. */
  98. public function testSetAndGetHash()
  99. {
  100. $slot = 1024;
  101. $arguments = array('key', 'value');
  102. $command = new RawCommand('SET', $arguments);
  103. $this->assertNull($command->getSlot());
  104. $command->setSlot($slot);
  105. $this->assertSame($slot, $command->getSlot());
  106. $command->setArguments(array('hoge', 'piyo'));
  107. $this->assertNull($command->getSlot());
  108. }
  109. /**
  110. * @group disconnected
  111. */
  112. public function testNormalizesCommandIdentifiersToUppercase()
  113. {
  114. $command = new RawCommand('set', array('key', 'value'));
  115. $this->assertSame('SET', $command->getId());
  116. }
  117. }