CommandTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 \PHPUnit_Framework_TestCase as StandardTestCase;
  12. /**
  13. *
  14. */
  15. class CommandTest extends StandardTestCase
  16. {
  17. /**
  18. * @group disconnected
  19. */
  20. public function testImplementsCorrectInterface()
  21. {
  22. $command = $this->getMockForAbstractClass('Predis\Command\AbstractCommand');
  23. $this->assertInstanceOf('Predis\Command\CommandInterface', $command);
  24. }
  25. /**
  26. * @group disconnected
  27. */
  28. public function testGetEmptyArguments()
  29. {
  30. $command = $this->getMockForAbstractClass('Predis\Command\AbstractCommand');
  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\AbstractCommand');
  40. $command->setRawArguments($arguments);
  41. $this->assertEquals($arguments, $command->getArguments());
  42. }
  43. /**
  44. * @group disconnected
  45. *
  46. * @todo Since AbstractCommand::filterArguments is protected we cannot set an expectation
  47. * for it when AbstractCommand::setArguments() is invoked. I wonder how we can do that.
  48. */
  49. public function testSetArguments()
  50. {
  51. $arguments = array('1st', '2nd', '3rd');
  52. $command = $this->getMockForAbstractClass('Predis\Command\AbstractCommand');
  53. $command->setArguments($arguments);
  54. $this->assertEquals($arguments, $command->getArguments());
  55. }
  56. /**
  57. * @group disconnected
  58. */
  59. public function testGetArgumentAtIndex()
  60. {
  61. $arguments = array('1st', '2nd', '3rd');
  62. $command = $this->getMockForAbstractClass('Predis\Command\AbstractCommand');
  63. $command->setArguments($arguments);
  64. $this->assertEquals($arguments[0], $command->getArgument(0));
  65. $this->assertEquals($arguments[2], $command->getArgument(2));
  66. $this->assertNull($command->getArgument(10));
  67. }
  68. /**
  69. * @group disconnected
  70. */
  71. public function testParseResponse()
  72. {
  73. $response = 'response-buffer';
  74. $command = $this->getMockForAbstractClass('Predis\Command\AbstractCommand');
  75. $this->assertEquals($response, $command->parseResponse($response));
  76. }
  77. /**
  78. * @group disconnected
  79. */
  80. public function testSetAndGetHash()
  81. {
  82. $hash = "key-hash";
  83. $command = $this->getMockForAbstractClass('Predis\Command\AbstractCommand');
  84. $command->setRawArguments(array('key'));
  85. $this->assertNull($command->getHash());
  86. $command->setHash($hash);
  87. $this->assertSame($hash, $command->getHash());
  88. }
  89. /**
  90. * @group disconnected
  91. */
  92. public function testToString()
  93. {
  94. $expected = 'SET key value';
  95. $arguments = array('key', 'value');
  96. $command = $this->getMockForAbstractClass('Predis\Command\AbstractCommand');
  97. $command->expects($this->once())->method('getId')->will($this->returnValue('SET'));
  98. $command->setRawArguments($arguments);
  99. $this->assertEquals($expected, (string) $command);
  100. }
  101. /**
  102. * @group disconnected
  103. */
  104. public function testToStringWithLongArguments()
  105. {
  106. $expected = 'SET key abcdefghijklmnopqrstuvwxyz012345[...]';
  107. $arguments = array('key', 'abcdefghijklmnopqrstuvwxyz0123456789');
  108. $command = $this->getMockForAbstractClass('Predis\Command\AbstractCommand');
  109. $command->expects($this->once())->method('getId')->will($this->returnValue('SET'));
  110. $command->setRawArguments($arguments);
  111. $this->assertEquals($expected, (string) $command);
  112. }
  113. }