ClientConnectionFactoryTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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\Option;
  11. use PredisTestCase;
  12. use Predis\Connection\ConnectionFactory;
  13. /**
  14. *
  15. */
  16. class ClientConnectionFactoryTest extends PredisTestCase
  17. {
  18. /**
  19. * @group disconnected
  20. */
  21. public function testValidationReturnsDefaultFactoryWithSchemeDefinitionsArray()
  22. {
  23. $connectionClass = get_class($this->getMock('Predis\Connection\SingleConnectionInterface'));
  24. $value = array('tcp' => $connectionClass, 'redis' => $connectionClass);
  25. $options = $this->getMock('Predis\Option\ClientOptionsInterface');
  26. $default = $this->getMock('Predis\Connection\ConnectionFactoryInterface');
  27. $default->expects($this->exactly(2))
  28. ->method('define')
  29. ->with($this->matchesRegularExpression('/^tcp|redis$/'), $connectionClass);
  30. $option = $this->getMock('Predis\Option\ClientConnectionFactory', array('getDefault'));
  31. $option->expects($this->once())
  32. ->method('getDefault')
  33. ->with($options)
  34. ->will($this->returnValue($default));
  35. $factory = $option->filter($options, $value);
  36. $this->assertInstanceOf('Predis\Connection\ConnectionFactoryInterface', $factory);
  37. $this->assertSame($default, $factory);
  38. }
  39. /**
  40. * @group disconnected
  41. */
  42. public function testValidationAcceptsFactoryInstancesAsValue()
  43. {
  44. $value = $this->getMock('Predis\Connection\ConnectionFactoryInterface');
  45. $options = $this->getMock('Predis\Option\ClientOptionsInterface');
  46. $option = $this->getMock('Predis\Option\ClientConnectionFactory', array('getDefault'));
  47. $option->expects($this->never())->method('getDefault');
  48. $this->assertSame($value, $option->filter($options, $value));
  49. }
  50. /**
  51. * @group disconnected
  52. */
  53. public function testValidationAcceptsCallableObjectAsInitializers()
  54. {
  55. $value = $this->getMock('Predis\Connection\ConnectionFactoryInterface');
  56. $options = $this->getMock('Predis\Option\ClientOptionsInterface');
  57. $option = new ClientConnectionFactory();
  58. $initializer = $this->getMock('stdClass', array('__invoke'));
  59. $initializer->expects($this->once())
  60. ->method('__invoke')
  61. ->with($this->isInstanceOf('Predis\Option\ClientOptionsInterface'), $option)
  62. ->will($this->returnValue($value));
  63. $cluster = $option->filter($options, $initializer, $option);
  64. $this->assertInstanceOf('Predis\Connection\ConnectionFactoryInterface', $cluster);
  65. $this->assertSame($value, $cluster);
  66. }
  67. /**
  68. * @group disconnected
  69. */
  70. public function testValidationAcceptsStringAsValue()
  71. {
  72. $factory = 'Predis\Connection\ConnectionFactory';
  73. $options = $this->getMock('Predis\Option\ClientOptionsInterface');
  74. $option = $this->getMock('Predis\Option\ClientConnectionFactory', array('getDefault'));
  75. $option->expects($this->never())->method('getDefault');
  76. $this->assertInstanceOf($factory, $option->filter($options, $factory));
  77. }
  78. /**
  79. * @group disconnected
  80. */
  81. public function testValidationThrowsExceptionOnWrongInvalidArguments()
  82. {
  83. $this->setExpectedException('InvalidArgumentException');
  84. $options = $this->getMock('Predis\Option\ClientOptionsInterface');
  85. $option = new ClientConnectionFactory();
  86. $option->filter($options, new \stdClass());
  87. }
  88. /**
  89. * @group disconnected
  90. */
  91. public function testInvokeReturnsSpecifiedFactoryOrDefault()
  92. {
  93. $value = $this->getMock('Predis\Connection\ConnectionFactoryInterface');
  94. $options = $this->getMock('Predis\Option\ClientOptionsInterface');
  95. $option = $this->getMock('Predis\Option\ClientConnectionFactory', array('filter', 'getDefault'));
  96. $option->expects($this->once())
  97. ->method('filter')
  98. ->with($options, $value)
  99. ->will($this->returnValue($value));
  100. $option->expects($this->never())->method('getDefault');
  101. $this->assertInstanceOf('Predis\Connection\ConnectionFactoryInterface', $option($options, $value));
  102. $option = $this->getMock('Predis\Option\ClientConnectionFactory', array('filter', 'getDefault'));
  103. $option->expects($this->never())->method('filter');
  104. $option->expects($this->once())
  105. ->method('getDefault')
  106. ->with($options)
  107. ->will($this->returnValue($value));
  108. $this->assertInstanceOf('Predis\Connection\ConnectionFactoryInterface', $option($options, null));
  109. }
  110. }