OptionsTest.php 6.7 KB

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