OptionsTest.php 6.1 KB

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