ReplicationTest.php 6.4 KB

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