OptionsTest.php 6.2 KB

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