RawCommandTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 Arguments array is missing the command ID
  54. */
  55. public function testExceptionOnMissingCommandID()
  56. {
  57. $command = new RawCommand(array());
  58. }
  59. /**
  60. * @group disconnected
  61. * @expectedException PHPUnit_Framework_Error_Warning
  62. * @expectedExceptionMessage Missing argument 1 for Predis\Command\RawCommand::create()
  63. */
  64. public function testPHPWarningOnMissingCommandIDWithStaticCreate()
  65. {
  66. RawCommand::create();
  67. }
  68. /**
  69. * @group disconnected
  70. */
  71. public function testSetArguments()
  72. {
  73. $commandID = 'SET';
  74. $command = new RawCommand(array($commandID));
  75. $command->setArguments($commandArgs = array('foo', 'bar'));
  76. $this->assertSame($commandArgs, $command->getArguments());
  77. $command->setArguments($commandArgs = array('hoge', 'piyo'));
  78. $this->assertSame($commandArgs, $command->getArguments());
  79. }
  80. /**
  81. * @group disconnected
  82. */
  83. public function testSetRawArguments()
  84. {
  85. $commandID = 'SET';
  86. $command = new RawCommand(array($commandID));
  87. $command->setRawArguments($commandArgs = array('foo', 'bar'));
  88. $this->assertSame($commandArgs, $command->getArguments());
  89. $command->setRawArguments($commandArgs = array('hoge', 'piyo'));
  90. $this->assertSame($commandArgs, $command->getArguments());
  91. }
  92. /**
  93. * @group disconnected
  94. */
  95. public function testSetAndGetHash()
  96. {
  97. $hash = "key-hash";
  98. $arguments = array('SET', 'key', 'value');
  99. $command = new RawCommand($arguments);
  100. $this->assertNull($command->getHash());
  101. $command->setHash($hash);
  102. $this->assertSame($hash, $command->getHash());
  103. $command->setArguments(array('hoge', 'piyo'));
  104. $this->assertNull($command->getHash());
  105. }
  106. /**
  107. * @group disconnected
  108. */
  109. public function testToString()
  110. {
  111. $arguments = array('SET', 'key', 'value');
  112. $expected = implode(' ', $arguments);
  113. $command = new RawCommand($arguments);
  114. $this->assertEquals($expected, (string) $command);
  115. }
  116. }