ClientConnectionFactoryTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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\Options;
  11. use \PHPUnit_Framework_TestCase as StandardTestCase;
  12. use Predis\ConnectionFactory;
  13. /**
  14. *
  15. */
  16. class ClientConnectionFactoryTest extends StandardTestCase
  17. {
  18. /**
  19. * @group disconnected
  20. */
  21. public function testValidationReturnsDefaultFactoryWithSchemeDefinitionsArray()
  22. {
  23. $connectionClass = get_class($this->getMock('Predis\Network\IConnectionSingle'));
  24. $value = array('tcp' => $connectionClass, 'redis' => $connectionClass);
  25. $options = $this->getMock('Predis\Options\IClientOptions');
  26. $default = $this->getMock('Predis\IConnectionFactory');
  27. $default->expects($this->exactly(2))
  28. ->method('define')
  29. ->with($this->matchesRegularExpression('/^tcp|redis$/'), $connectionClass);
  30. $option = $this->getMock('Predis\Options\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\IConnectionFactory', $factory);
  37. $this->assertSame($default, $factory);
  38. }
  39. /**
  40. * @group disconnected
  41. */
  42. public function testValidationAcceptsFactoryInstancesAsValue()
  43. {
  44. $value = $this->getMock('Predis\IConnectionFactory');
  45. $options = $this->getMock('Predis\Options\IClientOptions');
  46. $option = $this->getMock('Predis\Options\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 testValidationAcceptsStringAsValue()
  54. {
  55. $factory = 'Predis\ConnectionFactory';
  56. $options = $this->getMock('Predis\Options\IClientOptions');
  57. $option = $this->getMock('Predis\Options\ClientConnectionFactory', array('getDefault'));
  58. $option->expects($this->never())->method('getDefault');
  59. $this->assertInstanceOf($factory, $option->filter($options, $factory));
  60. }
  61. /**
  62. * @group disconnected
  63. */
  64. public function testValidationThrowsExceptionOnWrongInvalidArguments()
  65. {
  66. $this->setExpectedException('InvalidArgumentException');
  67. $options = $this->getMock('Predis\Options\IClientOptions');
  68. $option = new ClientConnectionFactory();
  69. $option->filter($options, new \stdClass());
  70. }
  71. /**
  72. * @group disconnected
  73. */
  74. public function testInvokeReturnsSpecifiedFactoryOrDefault()
  75. {
  76. $value = $this->getMock('Predis\IConnectionFactory');
  77. $options = $this->getMock('Predis\Options\IClientOptions');
  78. $option = $this->getMock('Predis\Options\ClientConnectionFactory', array('filter', 'getDefault'));
  79. $option->expects($this->once())
  80. ->method('filter')
  81. ->with($options, $value)
  82. ->will($this->returnValue($value));
  83. $option->expects($this->never())->method('getDefault');
  84. $this->assertInstanceOf('Predis\IConnectionFactory', $option($options, $value));
  85. $option = $this->getMock('Predis\Options\ClientConnectionFactory', array('filter', 'getDefault'));
  86. $option->expects($this->never())->method('filter');
  87. $option->expects($this->once())
  88. ->method('getDefault')
  89. ->with($options)
  90. ->will($this->returnValue($value));
  91. $this->assertInstanceOf('Predis\IConnectionFactory', $option($options, null));
  92. }
  93. }