ReplicationTest.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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\Option;
  11. use PredisTestCase;
  12. /**
  13. *
  14. */
  15. class ReplicationTest extends PredisTestCase
  16. {
  17. /**
  18. * @group disconnected
  19. */
  20. public function testDefaultOptionValue()
  21. {
  22. $option = new Replication();
  23. $options = $this->getMock('Predis\Configuration\OptionsInterface');
  24. $this->assertInstanceOf('Closure', $initializer = $option->getDefault($options));
  25. $this->assertInstanceOf('Predis\Connection\Replication\MasterSlaveReplication', $initializer($options));
  26. }
  27. /**
  28. * @group disconnected
  29. */
  30. public function testConfiguresAutomaticDiscoveryWhenAutodiscoveryOptionIsPresent()
  31. {
  32. $option = new Replication();
  33. $connectionFactory = $this->getMock('Predis\Connection\FactoryInterface');
  34. $options = $this->getMock('Predis\Configuration\OptionsInterface');
  35. $options
  36. ->expects($this->at(0))
  37. ->method('__get')
  38. ->with('autodiscovery')
  39. ->will($this->returnValue(true));
  40. $options
  41. ->expects($this->at(1))
  42. ->method('__get')
  43. ->with('connections')
  44. ->will($this->returnValue($connectionFactory));
  45. $this->assertInstanceOf('Closure', $initializer = $option->getDefault($options));
  46. $this->assertInstanceOf('Predis\Connection\Replication\MasterSlaveReplication', $connection = $initializer($options));
  47. // TODO: I know, I know...
  48. $reflection = new \ReflectionProperty($connection, 'autoDiscovery');
  49. $reflection->setAccessible(true);
  50. $this->assertTrue($reflection->getValue($connection));
  51. }
  52. /**
  53. * @group disconnected
  54. */
  55. public function testAcceptsCallableAsConnectionInitializer()
  56. {
  57. $option = new Replication();
  58. $options = $this->getMock('Predis\Configuration\OptionsInterface');
  59. $connection = $this->getMock('Predis\Connection\AggregateConnectionInterface');
  60. $callable = $this->getMock('stdClass', array('__invoke'));
  61. $callable
  62. ->expects($this->once())
  63. ->method('__invoke')
  64. ->with($this->isInstanceOf('Predis\Configuration\OptionsInterface'))
  65. ->will($this->returnValue($connection));
  66. $this->assertInstanceOf('Closure', $initializer = $option->filter($options, $callable));
  67. $this->assertSame($connection, $initializer($parameters = array()));
  68. }
  69. /**
  70. * @group disconnected
  71. * @expectedException \InvalidArgumentException
  72. * @expectedExceptionMessage Predis\Configuration\Option\Replication expects a valid connection type returned by callable initializer
  73. */
  74. public function testThrowsExceptionOnInvalidReturnTypeOfConnectionInitializer()
  75. {
  76. $option = new Replication();
  77. $options = $this->getMock('Predis\Configuration\OptionsInterface');
  78. $connection = $this->getMock('Predis\Connection\NodeConnectionInterface');
  79. $callable = $this->getMock('stdClass', array('__invoke'));
  80. $callable
  81. ->expects($this->once())
  82. ->method('__invoke')
  83. ->with($this->isInstanceOf('Predis\Configuration\OptionsInterface'))
  84. ->will($this->returnValue($connection));
  85. $this->assertInstanceOf('Closure', $initializer = $option->filter($options, $callable));
  86. $initializer($parameters = array());
  87. }
  88. /**
  89. * @group disconnected
  90. */
  91. public function testAcceptsShortNameStringPredis()
  92. {
  93. $option = new Replication();
  94. $options = $this->getMock('Predis\Configuration\OptionsInterface');
  95. $this->assertInstanceOf('Closure', $initializer = $option->filter($options, 'predis'));
  96. $this->assertInstanceOf('Predis\Connection\Replication\MasterSlaveReplication', $initializer($parameters = array()));
  97. }
  98. /**
  99. * @group disconnected
  100. */
  101. public function testAcceptsShortNameStringRedis()
  102. {
  103. $option = new Replication();
  104. $options = $this->getMock('Predis\Configuration\OptionsInterface');
  105. $options
  106. ->expects($this->at(0))
  107. ->method('__get')
  108. ->with('service')
  109. ->will($this->returnValue('mymaster'));
  110. $options
  111. ->expects($this->at(1))
  112. ->method('__get')
  113. ->with('connections')
  114. ->will($this->returnValue(
  115. $this->getMock('Predis\Connection\FactoryInterface')
  116. ));
  117. $parameters = array(
  118. $this->getMock('Predis\Connection\NodeConnectionInterface'),
  119. );
  120. $this->assertInstanceOf('Closure', $initializer = $option->filter($options, 'sentinel'));
  121. $this->assertInstanceOf('Predis\Connection\Replication\SentinelReplication', $connection = $initializer($parameters));
  122. $this->assertSame($parameters[0], $connection->getSentinelConnection());
  123. }
  124. /**
  125. * @group disconnected
  126. * @expectedException \InvalidArgumentException
  127. * @expectedExceptionMessage String value for the replication option must be either `predis` or `sentinel`
  128. */
  129. public function testThrowsExceptionOnInvalidShortNameString()
  130. {
  131. $option = new Replication();
  132. $options = $this->getMock('Predis\Configuration\OptionsInterface');
  133. $option->filter($options, 'unknown');
  134. }
  135. /**
  136. * @group disconnected
  137. * @expectedException \InvalidArgumentException
  138. * @expectedExceptionMessage Predis\Configuration\Option\Replication expects a valid callable
  139. */
  140. public function testThrowsExceptionOnBooleanValue()
  141. {
  142. $option = new Replication();
  143. $options = $this->getMock('Predis\Configuration\OptionsInterface');
  144. $option->filter($options, true);
  145. }
  146. /**
  147. * @group disconnected
  148. * @expectedException \InvalidArgumentException
  149. * @expectedExceptionMessage Predis\Configuration\Option\Replication expects a valid callable
  150. */
  151. public function testThrowsExceptionOnInstanceOfReplicationInterface()
  152. {
  153. $option = new Replication();
  154. $options = $this->getMock('Predis\Configuration\OptionsInterface');
  155. $connection = $this->getMock('Predis\Connection\Cluster\ClusterInterface');
  156. $option->filter($options, $connection);
  157. }
  158. }