CommandsTest.php 5.6 KB

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