ConnectionsTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 ConnectionsTest extends PredisTestCase
  16. {
  17. /**
  18. * @group disconnected
  19. */
  20. public function testDefaultOptionValue()
  21. {
  22. $option = new Connections();
  23. $options = $this->getMock('Predis\Configuration\OptionsInterface');
  24. $this->assertInstanceOf('Predis\Connection\Factory', $option->getDefault($options));
  25. }
  26. /**
  27. * @group disconnected
  28. */
  29. public function testAcceptsNamedArrayWithSchemeToConnectionClassMappings()
  30. {
  31. $options = $this->getMock('Predis\Configuration\OptionsInterface');
  32. $class = get_class($this->getMock('Predis\Connection\NodeConnectionInterface'));
  33. $value = array('tcp' => $class, 'redis' => $class);
  34. $default = $this->getMock('Predis\Connection\FactoryInterface');
  35. $default
  36. ->expects($this->exactly(2))
  37. ->method('define')
  38. ->with($this->matchesRegularExpression('/^tcp|redis$/'), $class);
  39. $option = $this->getMock('Predis\Configuration\Option\Connections', array('getDefault'));
  40. $option
  41. ->expects($this->once())
  42. ->method('getDefault')
  43. ->with($options)
  44. ->will($this->returnValue($default));
  45. $this->assertInstanceOf('Predis\Connection\FactoryInterface', $factory = $option->filter($options, $value));
  46. $this->assertSame($default, $factory);
  47. }
  48. /**
  49. * @group disconnected
  50. */
  51. public function testUsesParametersOptionToSetDefaultParameters()
  52. {
  53. $parameters = array('database' => 5, 'password' => 'mypassword');
  54. $default = $this->getMock('Predis\Connection\Factory');
  55. $options = $this->getMock('Predis\Configuration\OptionsInterface');
  56. $options
  57. ->expects($this->once())
  58. ->method('defined')
  59. ->with('parameters')
  60. ->will($this->returnValue(true));
  61. $options
  62. ->expects($this->once())
  63. ->method('__get')
  64. ->with('parameters')
  65. ->will($this->returnValue($parameters));
  66. $option = new Connections();
  67. $factory = $option->getDefault($options);
  68. $this->assertSame($parameters, $factory->getDefaultParameters());
  69. }
  70. /**
  71. * @group disconnected
  72. */
  73. public function testAcceptsConnectionFactoryInstance()
  74. {
  75. $option = $this->getMock('Predis\Configuration\Option\Connections', array('getDefault'));
  76. $option
  77. ->expects($this->never())
  78. ->method('getDefault');
  79. $options = $this->getMock('Predis\Configuration\OptionsInterface');
  80. $factory = $this->getMock('Predis\Connection\FactoryInterface');
  81. $this->assertSame($factory, $option->filter($options, $factory));
  82. }
  83. /**
  84. * @group disconnected
  85. */
  86. public function testAcceptsCallableReturningConnectionFactoryInstance()
  87. {
  88. $option = new Connections();
  89. $options = $this->getMock('Predis\Configuration\OptionsInterface');
  90. $callable = $this->getMock('stdClass', array('__invoke'));
  91. $callable
  92. ->expects($this->once())
  93. ->method('__invoke')
  94. ->with($this->isInstanceOf('Predis\Configuration\OptionsInterface'))
  95. ->will($this->returnValue(
  96. $factory = $this->getMock('Predis\Connection\FactoryInterface')
  97. ));
  98. $this->assertSame($factory, $option->filter($options, $callable));
  99. }
  100. /**
  101. * @group disconnected
  102. * @expectedException \InvalidArgumentException
  103. * @expectedException Predis\Configuration\Option\Connections expects a valid command factory
  104. */
  105. public function testThrowsExceptionOnInvalidArguments()
  106. {
  107. $option = new Connections();
  108. $options = $this->getMock('Predis\Configuration\OptionsInterface');
  109. $option->filter($options, new \stdClass());
  110. }
  111. }