KeyPrefixProcessorTest.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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\Processor;
  11. use PredisTestCase;
  12. /**
  13. *
  14. */
  15. class KeyPrefixProcessorTest extends PredisTestCase
  16. {
  17. /**
  18. * @group disconnected
  19. */
  20. public function testConstructorWithPrefix()
  21. {
  22. $prefix = 'prefix:';
  23. $processor = new KeyPrefixProcessor($prefix);
  24. $this->assertInstanceOf('Predis\Command\Processor\CommandProcessorInterface', $processor);
  25. $this->assertEquals($prefix, $processor->getPrefix());
  26. }
  27. /**
  28. * @group disconnected
  29. */
  30. public function testChangePrefix()
  31. {
  32. $prefix1 = 'prefix:';
  33. $prefix2 = 'prefix:new:';
  34. $processor = new KeyPrefixProcessor($prefix1);
  35. $this->assertEquals($prefix1, $processor->getPrefix());
  36. $processor->setPrefix($prefix2);
  37. $this->assertEquals($prefix2, $processor->getPrefix());
  38. }
  39. /**
  40. * @group disconnected
  41. */
  42. public function testProcessPrefixableCommands()
  43. {
  44. $prefix = 'prefix:';
  45. $command = $this->getMock('Predis\Command\PrefixableCommand');
  46. $command->expects($this->once())
  47. ->method('prefixKeys')
  48. ->with($prefix);
  49. $command->expects($this->once())
  50. ->method('getArguments')
  51. ->will($this->returnValue('key'));
  52. $processor = new KeyPrefixProcessor($prefix);
  53. $processor->process($command);
  54. }
  55. /**
  56. * @group disconnected
  57. */
  58. public function testSkipPrefixableCommandsWithNoArguments()
  59. {
  60. $prefix = 'prefix:';
  61. $command = $this->getMock('Predis\Command\PrefixableCommand');
  62. $command->expects($this->never())
  63. ->method('prefixKeys');
  64. $processor = new KeyPrefixProcessor($prefix);
  65. $processor->process($command);
  66. }
  67. /**
  68. * @group disconnected
  69. */
  70. public function testSkipNotPrefixableCommands()
  71. {
  72. $prefix = 'prefix:';
  73. $unprefixed = 'key';
  74. $expected = "$prefix$unprefixed";
  75. $command = $this->getMock('Predis\Command\CommandInterface');
  76. $command->expects($this->never())->method('prefixKeys');
  77. $processor = new KeyPrefixProcessor($prefix);
  78. $processor->process($command);
  79. }
  80. /**
  81. * @group disconnected
  82. */
  83. public function testInstanceCanBeCastedToString()
  84. {
  85. $prefix = 'prefix:';
  86. $processor = new KeyPrefixProcessor($prefix);
  87. $this->assertEquals($prefix, (string) $processor);
  88. }
  89. }