CommandsTest.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 Predis\Command\Processor\KeyPrefixProcessor;
  12. use Predis\Command\RedisFactory;
  13. use PredisTestCase;
  14. /**
  15. *
  16. */
  17. class CommandsTest extends PredisTestCase
  18. {
  19. /**
  20. * @group disconnected
  21. */
  22. public function testDefaultOptionValue()
  23. {
  24. $option = new Commands();
  25. $options = $this->getMock('Predis\Configuration\OptionsInterface');
  26. $commands = $option->getDefault($options);
  27. $this->assertInstanceOf('Predis\Command\FactoryInterface', $commands);
  28. $this->assertNull($commands->getProcessor());
  29. }
  30. /**
  31. * @group disconnected
  32. */
  33. public function testAppliesPrefixOnDefaultOptionValue()
  34. {
  35. $option = new Commands();
  36. $options = $this->getMock('Predis\Configuration\OptionsInterface');
  37. $options->expects($this->once())
  38. ->method('__isset')
  39. ->with('prefix')
  40. ->will($this->returnValue(true));
  41. $options->expects($this->once())
  42. ->method('__get')
  43. ->with('prefix')
  44. ->will($this->returnValue(new KeyPrefixProcessor('prefix:')));
  45. $commands = $option->getDefault($options);
  46. $this->assertInstanceOf('Predis\Command\FactoryInterface', $commands);
  47. $this->assertInstanceOf('Predis\Command\Processor\KeyPrefixProcessor', $commands->getProcessor());
  48. $this->assertSame('prefix:', $commands->getProcessor()->getPrefix());
  49. }
  50. /**
  51. * @group disconnected
  52. */
  53. public function testAcceptsCommandFactoryInstanceAsValue()
  54. {
  55. $option = new Commands();
  56. $options = $this->getMock('Predis\Configuration\OptionsInterface');
  57. $input = new RedisFactory();
  58. $commands = $option->filter($options, $input);
  59. $this->assertSame($commands, $input);
  60. $this->assertNull($commands->getProcessor());
  61. }
  62. /**
  63. * @group disconnected
  64. */
  65. public function testAcceptsDictionaryOfCommandsAsValue()
  66. {
  67. $option = new Commands();
  68. $options = $this->getMock('Predis\Configuration\OptionsInterface');
  69. $input = array(
  70. 'FOO' => 'Predis\Command\RawCommand',
  71. 'BAR' => 'Predis\Command\RawCommand',
  72. );
  73. $commands = $option->filter($options, $input);
  74. $this->assertInstanceOf('Predis\Command\FactoryInterface', $commands);
  75. $this->assertSame('Predis\Command\RawCommand', $commands->getCommandClass('FOO'));
  76. $this->assertSame('Predis\Command\RawCommand', $commands->getCommandClass('BAR'));
  77. }
  78. /**
  79. * @group disconnected
  80. */
  81. public function testAcceptsCallableReturningCommandFactoryInstance()
  82. {
  83. $option = new Commands();
  84. $options = $this->getMock('Predis\Configuration\OptionsInterface');
  85. $commands = $this->getMock('Predis\Command\FactoryInterface');
  86. $callable = $this->getMock('stdClass', array('__invoke'));
  87. $callable->expects($this->once())
  88. ->method('__invoke')
  89. ->with($this->isInstanceOf('Predis\Configuration\OptionsInterface'))
  90. ->will($this->returnValue($commands));
  91. $this->assertSame($commands, $option->filter($options, $callable));
  92. }
  93. /**
  94. * @group disconnected
  95. */
  96. public function testAcceptsCallableReturningDictionaryOfCommandsAsValue()
  97. {
  98. $option = new Commands();
  99. $options = $this->getMock('Predis\Configuration\OptionsInterface');
  100. $dictionary = array(
  101. 'FOO' => 'Predis\Command\RawCommand',
  102. 'BAR' => 'Predis\Command\RawCommand',
  103. );
  104. $callable = $this->getMock('stdClass', array('__invoke'));
  105. $callable->expects($this->once())
  106. ->method('__invoke')
  107. ->with($this->isInstanceOf('Predis\Configuration\OptionsInterface'))
  108. ->will($this->returnValue($dictionary));
  109. $commands = $option->filter($options, $callable);
  110. $this->assertInstanceOf('Predis\Command\FactoryInterface', $commands);
  111. $this->assertSame('Predis\Command\RawCommand', $commands->getCommandClass('FOO'));
  112. $this->assertSame('Predis\Command\RawCommand', $commands->getCommandClass('BAR'));
  113. }
  114. /**
  115. * @group disconnected
  116. * @expectedException \InvalidArgumentException
  117. * @expectedExceptionMessage Predis\Configuration\Option\Commands expects a valid command factory
  118. */
  119. public function testThrowsExceptionOnInvalidTypeReturnedByCallable()
  120. {
  121. $option = new Commands();
  122. $options = $this->getMock('Predis\Configuration\OptionsInterface');
  123. $callable = $this->getMock('stdClass', array('__invoke'));
  124. $callable->expects($this->once())
  125. ->method('__invoke')
  126. ->with($this->isInstanceOf('Predis\Configuration\OptionsInterface'))
  127. ->will($this->returnValue(new \stdClass()));
  128. $option->filter($options, $callable);
  129. }
  130. /**
  131. * @group disconnected
  132. * @expectedException \InvalidArgumentException
  133. * @expectedExceptionMessage Predis\Configuration\Option\Commands expects a valid command factory
  134. */
  135. public function testThrowsExceptionOnInvalidValue()
  136. {
  137. $option = new Commands();
  138. $options = $this->getMock('Predis\Configuration\OptionsInterface');
  139. $option->filter($options, new \stdClass());
  140. }
  141. }