CustomOptionTest.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. /**
  13. *
  14. */
  15. class CustomOptionTest extends StandardTestCase
  16. {
  17. /**
  18. * @group disconnected
  19. * @expectedException InvalidArgumentException
  20. */
  21. public function testConstructorAcceptsOnlyCallablesForFilter()
  22. {
  23. $option = new CustomOption(array('filter' => new \stdClass()));
  24. }
  25. /**
  26. * @group disconnected
  27. * @expectedException InvalidArgumentException
  28. */
  29. public function testConstructorAcceptsOnlyCallablesForDefault()
  30. {
  31. $option = new CustomOption(array('default' => new \stdClass()));
  32. }
  33. /**
  34. * @group disconnected
  35. */
  36. public function testConstructorIgnoresUnrecognizedParameters()
  37. {
  38. $option = new CustomOption(array('unknown' => new \stdClass()));
  39. $this->assertNotNull($option);
  40. }
  41. /**
  42. * @group disconnected
  43. */
  44. public function testFilterWithoutCallbackReturnsValue()
  45. {
  46. $options = $this->getMock('Predis\Options\IClientOptions');
  47. $option = new CustomOption();
  48. $this->assertEquals('test', $option->filter($options, 'test'));
  49. }
  50. /**
  51. * @group disconnected
  52. */
  53. public function testDefaultWithoutCallbackReturnsNull()
  54. {
  55. $options = $this->getMock('Predis\Options\IClientOptions');
  56. $option = new CustomOption();
  57. $this->assertNull($option->getDefault($options));
  58. }
  59. /**
  60. * @group disconnected
  61. */
  62. public function testInvokeCallsFilterCallback()
  63. {
  64. $value = 'test';
  65. $options = $this->getMock('Predis\Options\IClientOptions');
  66. $filter = $this->getMock('stdClass', array('__invoke'));
  67. $filter->expects($this->once())
  68. ->method('__invoke')
  69. ->with($this->isInstanceOf('Predis\Options\IClientOptions'), $value)
  70. ->will($this->returnValue(true));
  71. $default = $this->getMock('stdClass', array('__invoke'));
  72. $default->expects($this->never())->method('__invoke');
  73. $option = new CustomOption(array('filter' => $filter, 'default' => $default));
  74. $this->assertTrue($option($options, $value));
  75. }
  76. /**
  77. * @group disconnected
  78. */
  79. public function testInvokeCallsDefaultCallback()
  80. {
  81. $options = $this->getMock('Predis\Options\IClientOptions');
  82. $filter = $this->getMock('stdClass', array('__invoke'));
  83. $filter->expects($this->never())->method('__invoke');
  84. $default = $this->getMock('stdClass', array('__invoke'));
  85. $default->expects($this->once())
  86. ->method('__invoke')
  87. ->with($this->isInstanceOf('Predis\Options\IClientOptions'))
  88. ->will($this->returnValue(true));
  89. $option = new CustomOption(array('filter' => $filter, 'default' => $default));
  90. $this->assertTrue($option($options, null));
  91. }
  92. }