ClusterTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 ClusterTest extends PredisTestCase
  16. {
  17. /**
  18. * @group disconnected
  19. */
  20. public function testDefaultOptionValue()
  21. {
  22. $option = new Cluster();
  23. $options = $this->getMock('Predis\Configuration\OptionsInterface');
  24. $this->assertInstanceOf('Closure', $initializer = $option->getDefault($options));
  25. $this->assertInstanceOf('Predis\Connection\Cluster\PredisCluster', $initializer($options));
  26. }
  27. /**
  28. * @group disconnected
  29. */
  30. public function testAcceptsCallableAsConnectionInitializer()
  31. {
  32. $option = new Cluster();
  33. $options = $this->getMock('Predis\Configuration\OptionsInterface');
  34. $connection = $this->getMock('Predis\Connection\AggregateConnectionInterface');
  35. $callable = $this->getMock('stdClass', array('__invoke'));
  36. $callable->expects($this->once())
  37. ->method('__invoke')
  38. ->with($this->isInstanceOf('Predis\Configuration\OptionsInterface'))
  39. ->will($this->returnValue($connection));
  40. $this->assertInstanceOf('Closure', $initializer = $option->filter($options, $callable));
  41. $this->assertSame($connection, $initializer($parameters = array()));
  42. }
  43. /**
  44. * @group disconnected
  45. * @expectedException \InvalidArgumentException
  46. * @expectedExceptionMessage Predis\Configuration\Option\Cluster expects a valid connection type returned by callable initializer
  47. */
  48. public function testThrowsExceptionOnInvalidReturnTypeOfConnectionInitializer()
  49. {
  50. $option = new Cluster();
  51. $options = $this->getMock('Predis\Configuration\OptionsInterface');
  52. $connection = $this->getMock('Predis\Connection\NodeConnectionInterface');
  53. $callable = $this->getMock('stdClass', array('__invoke'));
  54. $callable->expects($this->once())
  55. ->method('__invoke')
  56. ->with($this->isInstanceOf('Predis\Configuration\OptionsInterface'))
  57. ->will($this->returnValue($connection));
  58. $this->assertInstanceOf('Closure', $initializer = $option->filter($options, $callable));
  59. $initializer($parameters = array());
  60. }
  61. /**
  62. * @group disconnected
  63. */
  64. public function testAcceptsShortNameStringPredis()
  65. {
  66. $option = new Cluster();
  67. $options = $this->getMock('Predis\Configuration\OptionsInterface');
  68. $options->expects($this->never())
  69. ->method('__get')
  70. ->with('connections');
  71. $this->assertInstanceOf('Closure', $initializer = $option->filter($options, 'predis'));
  72. $this->assertInstanceOf('Predis\Connection\Cluster\PredisCluster', $initializer($parameters = array()));
  73. }
  74. /**
  75. * @group disconnected
  76. */
  77. public function testAcceptsShortNameStringRedis()
  78. {
  79. $option = new Cluster();
  80. $options = $this->getMock('Predis\Configuration\OptionsInterface');
  81. $options->expects($this->once())
  82. ->method('__get')
  83. ->with('connections')
  84. ->will($this->returnValue(
  85. $this->getMock('Predis\Connection\FactoryInterface')
  86. ));
  87. $this->assertInstanceOf('Closure', $initializer = $option->filter($options, 'redis'));
  88. $this->assertInstanceOf('Predis\Connection\Cluster\RedisCluster', $initializer($parameters = array()));
  89. }
  90. /**
  91. * @group disconnected
  92. * @expectedException \InvalidArgumentException
  93. * @expectedExceptionMessage String value for the cluster option must be either `predis` or `redis`
  94. */
  95. public function testThrowsExceptionOnInvalidShortNameString()
  96. {
  97. $option = new Cluster();
  98. $options = $this->getMock('Predis\Configuration\OptionsInterface');
  99. $option->filter($options, 'unknown');
  100. }
  101. /**
  102. * @group disconnected
  103. * @expectedException \InvalidArgumentException
  104. * @expectedExceptionMessage Predis\Configuration\Option\Cluster expects a valid callable
  105. */
  106. public function testThrowsExceptionOnInstanceOfClusterInterface()
  107. {
  108. $option = new Cluster();
  109. $options = $this->getMock('Predis\Configuration\OptionsInterface');
  110. $connection = $this->getMock('Predis\Connection\Cluster\ClusterInterface');
  111. $option->filter($options, $connection);
  112. }
  113. }