PrefixTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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\Configuration\Option;
  11. use PredisTestCase;
  12. /**
  13. *
  14. */
  15. class PrefixTest extends PredisTestCase
  16. {
  17. /**
  18. * @group disconnected
  19. */
  20. public function testDefaultOptionValue()
  21. {
  22. $option = new Prefix();
  23. $options = $this->getMock('Predis\Configuration\OptionsInterface');
  24. $this->assertNull($option->getDefault($options));
  25. }
  26. /**
  27. * @group disconnected
  28. */
  29. public function testAcceptsStringAndReturnsCommandProcessor()
  30. {
  31. $option = new Prefix();
  32. $options = $this->getMock('Predis\Configuration\OptionsInterface');
  33. $prefix = $option->filter($options, $value = 'prefix:');
  34. $this->assertInstanceOf('Predis\Command\Processor\ProcessorInterface', $prefix);
  35. $this->assertInstanceOf('Predis\Command\Processor\KeyPrefixProcessor', $prefix);
  36. $this->assertSame($value, $prefix->getPrefix());
  37. }
  38. /**
  39. * @group disconnected
  40. */
  41. public function testAcceptsCommandProcessorInstance()
  42. {
  43. $option = new Prefix();
  44. $options = $this->getMock('Predis\Configuration\OptionsInterface');
  45. $processor = $this->getMock('Predis\Command\Processor\ProcessorInterface');
  46. $prefix = $option->filter($options, $processor);
  47. $this->assertSame($processor, $prefix);
  48. }
  49. /**
  50. * @group disconnected
  51. */
  52. public function testAcceptsCallableReturningProcessorInterface()
  53. {
  54. $option = new Prefix();
  55. $options = $this->getMock('Predis\Configuration\OptionsInterface');
  56. $callable = $this->getMock('stdClass', array('__invoke'));
  57. $callable
  58. ->expects($this->once())
  59. ->method('__invoke')
  60. ->with($this->isInstanceOf('Predis\Configuration\OptionsInterface'))
  61. ->will($this->returnValue(
  62. $processor = $this->getMock('Predis\Command\Processor\ProcessorInterface')
  63. ));
  64. $prefix = $option->filter($options, $callable);
  65. $this->assertSame($processor, $prefix);
  66. }
  67. /**
  68. * @group disconnected
  69. */
  70. public function testAcceptsCallableReturningStringPrefix()
  71. {
  72. $option = new Prefix();
  73. $options = $this->getMock('Predis\Configuration\OptionsInterface');
  74. $callable = $this->getMock('stdClass', array('__invoke'));
  75. $callable
  76. ->expects($this->once())
  77. ->method('__invoke')
  78. ->with($this->isInstanceOf('Predis\Configuration\OptionsInterface'))
  79. ->will($this->returnValue('pfx:'));
  80. $prefix = $option->filter($options, $callable);
  81. $this->assertInstanceOf('Predis\Command\Processor\ProcessorInterface', $prefix);
  82. $this->assertInstanceOf('Predis\Command\Processor\KeyPrefixProcessor', $prefix);
  83. $this->assertSame('pfx:', $prefix->getPrefix());
  84. }
  85. /**
  86. * @group disconnected
  87. */
  88. public function testAcceptsObjectAsPrefixAndCastsToString()
  89. {
  90. $option = new Prefix();
  91. $options = $this->getMock('Predis\Configuration\OptionsInterface');
  92. $input = $this->getMock('stdClass', array('__toString'));
  93. $input
  94. ->expects($this->once())
  95. ->method('__toString')
  96. ->will($this->returnValue('pfx:'));
  97. $prefix = $option->filter($options, $input);
  98. $this->assertInstanceOf('Predis\Command\Processor\ProcessorInterface', $prefix);
  99. $this->assertInstanceOf('Predis\Command\Processor\KeyPrefixProcessor', $prefix);
  100. $this->assertSame('pfx:', $prefix->getPrefix());
  101. }
  102. }