ClusterTest.php 4.6 KB

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