ConnectionFactoryOptionTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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;
  11. use PredisTestCase;
  12. /**
  13. *
  14. */
  15. class ConnectionFactoryOptionTest extends PredisTestCase
  16. {
  17. /**
  18. * @group disconnected
  19. */
  20. public function testDefaultOptionValue()
  21. {
  22. $option = new ConnectionFactoryOption();
  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->expects($this->exactly(2))
  36. ->method('define')
  37. ->with($this->matchesRegularExpression('/^tcp|redis$/'), $class);
  38. $option = $this->getMock('Predis\Configuration\ConnectionFactoryOption', array('getDefault'));
  39. $option->expects($this->once())
  40. ->method('getDefault')
  41. ->with($options)
  42. ->will($this->returnValue($default));
  43. $this->assertInstanceOf('Predis\Connection\FactoryInterface', $factory = $option->filter($options, $value));
  44. $this->assertSame($default, $factory);
  45. }
  46. /**
  47. * @group disconnected
  48. */
  49. public function testUsesParametersOptionToSetDefaultParameters()
  50. {
  51. $parameters = array('database' => 5, 'password' => 'mypassword');
  52. $default = $this->getMock('Predis\Connection\Factory');
  53. $options = $this->getMock('Predis\Configuration\OptionsInterface');
  54. $options->expects($this->once())
  55. ->method('defined')
  56. ->with('parameters')
  57. ->will($this->returnValue(true));
  58. $options->expects($this->once())
  59. ->method('__get')
  60. ->with('parameters')
  61. ->will($this->returnValue($parameters));
  62. $option = new ConnectionFactoryOption();
  63. $factory = $option->getDefault($options);
  64. $this->assertSame($parameters, $factory->getDefaultParameters());
  65. }
  66. /**
  67. * @group disconnected
  68. */
  69. public function testAcceptsConnectionFactoryInstance()
  70. {
  71. $options = $this->getMock('Predis\Configuration\OptionsInterface');
  72. $value = $this->getMock('Predis\Connection\FactoryInterface');
  73. $option = $this->getMock('Predis\Configuration\ConnectionFactoryOption', array('getDefault'));
  74. $option->expects($this->never())->method('getDefault');
  75. $this->assertSame($value, $option->filter($options, $value));
  76. }
  77. /**
  78. * @group disconnected
  79. * @expectedException InvalidArgumentException
  80. */
  81. public function testThrowsExceptionOnInvalidArguments()
  82. {
  83. $option = new ConnectionFactoryOption();
  84. $options = $this->getMock('Predis\Configuration\OptionsInterface');
  85. $option->filter($options, new \stdClass());
  86. }
  87. }