OptionsTest.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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;
  11. use PredisTestCase;
  12. /**
  13. * @todo Use mock objects to test the inner workings of the Options class.
  14. */
  15. class OptionsTest extends PredisTestCase
  16. {
  17. /**
  18. * @group disconnected
  19. */
  20. public function testConstructorWithoutArguments()
  21. {
  22. $options = new Options();
  23. $this->assertTrue($options->exceptions);
  24. $this->assertNull($options->prefix);
  25. $this->assertNull($options->aggregate);
  26. $this->assertInstanceOf('Closure', $options->cluster);
  27. $this->assertInstanceOf('Closure', $options->replication);
  28. $this->assertInstanceOf('Predis\Command\FactoryInterface', $options->commands);
  29. $this->assertInstanceOf('Predis\Connection\FactoryInterface', $options->connections);
  30. }
  31. /**
  32. * @group disconnected
  33. */
  34. public function testConstructorWithArrayArgument()
  35. {
  36. $connection = $this->getMock('Predis\Connection\AggregateConnectionInterface');
  37. $callable = $this->getMock('stdClass', array('__invoke'));
  38. $callable->expects($this->any())
  39. ->method('__invoke')
  40. ->with($this->isInstanceOf('Predis\Configuration\OptionsInterface'))
  41. ->will($this->returnValue($connection));
  42. $options = new Options(array(
  43. 'exceptions' => false,
  44. 'prefix' => 'prefix:',
  45. 'commands' => $this->getMock('Predis\Command\FactoryInterface'),
  46. 'connections' => $this->getMock('Predis\Connection\FactoryInterface'),
  47. 'cluster' => $callable,
  48. 'replication' => $callable,
  49. 'aggregate' => $callable,
  50. ));
  51. $this->assertInternalType('bool', $options->exceptions);
  52. $this->assertInstanceOf('Predis\Command\Processor\ProcessorInterface', $options->prefix);
  53. $this->assertInstanceOf('Predis\Command\FactoryInterface', $options->commands);
  54. $this->assertInstanceOf('Predis\Connection\FactoryInterface', $options->connections);
  55. $this->assertInstanceOf('Closure', $initializer = $options->aggregate);
  56. $this->assertSame($connection, $initializer($options, array()));
  57. $this->assertInstanceOf('Closure', $initializer = $options->cluster);
  58. $this->assertSame($connection, $initializer($options, array()));
  59. $this->assertInstanceOf('Closure', $initializer = $options->replication);
  60. $this->assertSame($connection, $initializer($options, array()));
  61. }
  62. /**
  63. * @group disconnected
  64. */
  65. public function testSupportsCustomOptions()
  66. {
  67. $options = new Options(array(
  68. 'custom' => 'foobar',
  69. ));
  70. $this->assertSame('foobar', $options->custom);
  71. }
  72. /**
  73. * @group disconnected
  74. */
  75. public function testUndefinedOptionsReturnNull()
  76. {
  77. $options = new Options();
  78. $this->assertFalse($options->defined('unknown'));
  79. $this->assertFalse(isset($options->unknown));
  80. $this->assertNull($options->unknown);
  81. }
  82. /**
  83. * @group disconnected
  84. */
  85. public function testCanCheckOptionsIfDefinedByUser()
  86. {
  87. $options = new Options(array(
  88. 'prefix' => 'prefix:',
  89. 'custom' => 'foobar',
  90. 'void' => null,
  91. ));
  92. $this->assertTrue($options->defined('prefix'));
  93. $this->assertTrue($options->defined('custom'));
  94. $this->assertTrue($options->defined('void'));
  95. $this->assertFalse($options->defined('commands'));
  96. }
  97. /**
  98. * @group disconnected
  99. */
  100. public function testIsSetReplicatesPHPBehavior()
  101. {
  102. $options = new Options(array(
  103. 'prefix' => 'prefix:',
  104. 'custom' => 'foobar',
  105. 'void' => null,
  106. ));
  107. $this->assertTrue(isset($options->prefix));
  108. $this->assertTrue(isset($options->custom));
  109. $this->assertFalse(isset($options->void));
  110. $this->assertFalse(isset($options->commands));
  111. }
  112. /**
  113. * @group disconnected
  114. */
  115. public function testReturnsDefaultValueOfSpecifiedOption()
  116. {
  117. $options = new Options();
  118. $this->assertInstanceOf('Predis\Command\FactoryInterface', $options->getDefault('commands'));
  119. }
  120. /**
  121. * @group disconnected
  122. */
  123. public function testReturnsNullAsDefaultValueForUndefinedOption()
  124. {
  125. $options = new Options();
  126. $this->assertNull($options->getDefault('unknown'));
  127. }
  128. /**
  129. * @group disconnected
  130. */
  131. public function testLazilyInitializesOptionValueUsingObjectWithInvokeMagicMethod()
  132. {
  133. $commands = $this->getMock('Predis\Command\FactoryInterface');
  134. // NOTE: closure values are covered by this test since they define __invoke().
  135. $callable = $this->getMock('stdClass', array('__invoke'));
  136. $callable->expects($this->once())
  137. ->method('__invoke')
  138. ->with($this->isInstanceOf('Predis\Configuration\OptionsInterface'))
  139. ->will($this->returnValue($commands));
  140. $options = new Options(array(
  141. 'commands' => $callable,
  142. ));
  143. $this->assertSame($commands, $options->commands);
  144. $this->assertSame($commands, $options->commands);
  145. }
  146. /**
  147. * @group disconnected
  148. */
  149. public function testLazilyInitializesCustomOptionValueUsingObjectWithInvokeMagicMethod()
  150. {
  151. $custom = new \stdClass();
  152. // NOTE: closure values are covered by this test since they define __invoke().
  153. $callable = $this->getMock('stdClass', array('__invoke'));
  154. $callable->expects($this->once())
  155. ->method('__invoke')
  156. ->with($this->isInstanceOf('Predis\Configuration\OptionsInterface'))
  157. ->will($this->returnValue($custom));
  158. $options = new Options(array(
  159. 'custom' => $callable,
  160. ));
  161. $this->assertSame($custom, $options->custom);
  162. $this->assertSame($custom, $options->custom);
  163. }
  164. /**
  165. * @group disconnected
  166. */
  167. public function testChecksForInvokeMagicMethodDoesNotTriggerAutoloader()
  168. {
  169. $trigger = $this->getMock('stdClass', array('autoload'));
  170. $trigger->expects($this->never())->method('autoload');
  171. spl_autoload_register($autoload = function ($class) use ($trigger) {
  172. $trigger->autoload($class);
  173. }, true, false);
  174. try {
  175. $options = new Options(array('custom' => 'value'));
  176. $pfx = $options->prefix;
  177. } catch (\Exception $_) {
  178. spl_autoload_unregister($autoload);
  179. }
  180. }
  181. }