OptionsTest.php 5.7 KB

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