CRC16Test.php 4.0 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 Predis\Cluster\Hash;
  12. use PredisTestCase;
  13. /**
  14. *
  15. */
  16. class CRC16Test extends PredisTestCase
  17. {
  18. /**
  19. * @group disconnected
  20. */
  21. public function testDefaultOptionValue()
  22. {
  23. $option = new CRC16();
  24. $options = $this->getMock('Predis\Configuration\OptionsInterface');
  25. $hashGenerator = $option->getDefault($options);
  26. $this->assertInstanceOf('Predis\Cluster\Hash\HashGeneratorInterface', $hashGenerator);
  27. if (function_exists('phpiredis_utils_crc16')) {
  28. $this->assertInstanceOf('Predis\Cluster\Hash\PhpiredisCRC16', $hashGenerator);
  29. } else {
  30. $this->assertInstanceOf('Predis\Cluster\Hash\CRC16', $hashGenerator);
  31. }
  32. }
  33. /**
  34. * @group disconnected
  35. */
  36. public function testAcceptsHashGeneratorInstance()
  37. {
  38. $option = new CRC16();
  39. $options = $this->getMock('Predis\Configuration\OptionsInterface');
  40. $hashGenerator = $this->getMock('Predis\Cluster\Hash\HashGeneratorInterface');
  41. $this->assertSame($hashGenerator, $option->filter($options, $hashGenerator));
  42. }
  43. /**
  44. * @group disconnected
  45. */
  46. public function testAcceptsCallableAsHashGeneratorInitializer()
  47. {
  48. $option = new CRC16();
  49. $options = $this->getMock('Predis\Configuration\OptionsInterface');
  50. $hashGenerator = $this->getMock('Predis\Cluster\Hash\HashGeneratorInterface');
  51. $callable = $this->getMock('stdClass', array('__invoke'));
  52. $callable
  53. ->expects($this->once())
  54. ->method('__invoke')
  55. ->with($this->isInstanceOf('Predis\Configuration\OptionsInterface'))
  56. ->will($this->returnValue($hashGenerator));
  57. $this->assertSame($hashGenerator, $option->filter($options, $callable));
  58. }
  59. /**
  60. * @group disconnected
  61. * @expectedException \InvalidArgumentException
  62. * @expectedExceptionMessage Predis\Configuration\Option\CRC16 expects a valid hash generator
  63. */
  64. public function testThrowsExceptionOnInvalidReturnTypeOfHashGeneratorInitializer()
  65. {
  66. $option = new CRC16();
  67. $options = $this->getMock('Predis\Configuration\OptionsInterface');
  68. $wrongValue = $this->getMock('stdClass');
  69. $callable = $this->getMock('stdClass', array('__invoke'));
  70. $callable
  71. ->expects($this->once())
  72. ->method('__invoke')
  73. ->with($this->isInstanceOf('Predis\Configuration\OptionsInterface'))
  74. ->will($this->returnValue($wrongValue));
  75. $option->filter($options, $callable);
  76. }
  77. /**
  78. * @group disconnected
  79. */
  80. public function testAcceptsShortNameStringPredis()
  81. {
  82. $option = new CRC16();
  83. $options = $this->getMock('Predis\Configuration\OptionsInterface');
  84. $this->assertInstanceOf('Predis\Cluster\Hash\CRC16', $option->filter($options, 'predis'));
  85. }
  86. /**
  87. * @group disconnected
  88. * @group ext-phpiredis
  89. * @requires extension phpiredis
  90. * @requires function phpiredis_utils_crc16
  91. */
  92. public function testAcceptsShortNameStringPhpiredis()
  93. {
  94. $option = new CRC16();
  95. $options = $this->getMock('Predis\Configuration\OptionsInterface');
  96. $this->assertInstanceOf('Predis\Cluster\Hash\PhpiredisCRC16', $option->filter($options, 'phpiredis'));
  97. }
  98. /**
  99. * @group disconnected
  100. * @expectedException \InvalidArgumentException
  101. * @expectedExceptionMessage String value for the crc16 option must be either `predis` or `phpiredis`
  102. */
  103. public function testThrowsExceptionOnInvalidShortNameString()
  104. {
  105. $option = new CRC16();
  106. $options = $this->getMock('Predis\Configuration\OptionsInterface');
  107. $option->filter($options, 'unknown');
  108. }
  109. }