Files
predis/tests/Predis/Configuration/Option/CRC16Test.php
T
Daniele Alessandri e38df19fac Implement "crc16" client option for redis-cluster.
By default Predis chooses automatically which hash generator to use:
if a suitable version of ext-phpiredis is loaded (that is, a version
that implements the phpiredis_utils_crc16() function) it uses the new
Predis\Cluster\Hash\PhpiredisCRC16 generator, otherwise it falls back
to the usual pure-PHP Predis\Cluster\Hash\CRC16 generator.
2016-06-17 12:32:29 +02:00

137 lines
4.0 KiB
PHP

<?php
/*
* This file is part of the Predis package.
*
* (c) Daniele Alessandri <suppakilla@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Predis\Configuration\Option;
use Predis\Cluster\Hash;
use PredisTestCase;
/**
*
*/
class CRC16Test extends PredisTestCase
{
/**
* @group disconnected
*/
public function testDefaultOptionValue()
{
$option = new CRC16();
$options = $this->getMock('Predis\Configuration\OptionsInterface');
$hashGenerator = $option->getDefault($options);
$this->assertInstanceOf('Predis\Cluster\Hash\HashGeneratorInterface', $hashGenerator);
if (function_exists('phpiredis_utils_crc16')) {
$this->assertInstanceOf('Predis\Cluster\Hash\PhpiredisCRC16', $hashGenerator);
} else {
$this->assertInstanceOf('Predis\Cluster\Hash\CRC16', $hashGenerator);
}
}
/**
* @group disconnected
*/
public function testAcceptsHashGeneratorInstance()
{
$option = new CRC16();
$options = $this->getMock('Predis\Configuration\OptionsInterface');
$hashGenerator = $this->getMock('Predis\Cluster\Hash\HashGeneratorInterface');
$this->assertSame($hashGenerator, $option->filter($options, $hashGenerator));
}
/**
* @group disconnected
*/
public function testAcceptsCallableAsHashGeneratorInitializer()
{
$option = new CRC16();
$options = $this->getMock('Predis\Configuration\OptionsInterface');
$hashGenerator = $this->getMock('Predis\Cluster\Hash\HashGeneratorInterface');
$callable = $this->getMock('stdClass', array('__invoke'));
$callable
->expects($this->once())
->method('__invoke')
->with($this->isInstanceOf('Predis\Configuration\OptionsInterface'))
->will($this->returnValue($hashGenerator));
$this->assertSame($hashGenerator, $option->filter($options, $callable));
}
/**
* @group disconnected
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Predis\Configuration\Option\CRC16 expects a valid hash generator
*/
public function testThrowsExceptionOnInvalidReturnTypeOfHashGeneratorInitializer()
{
$option = new CRC16();
$options = $this->getMock('Predis\Configuration\OptionsInterface');
$wrongValue = $this->getMock('stdClass');
$callable = $this->getMock('stdClass', array('__invoke'));
$callable
->expects($this->once())
->method('__invoke')
->with($this->isInstanceOf('Predis\Configuration\OptionsInterface'))
->will($this->returnValue($wrongValue));
$option->filter($options, $callable);
}
/**
* @group disconnected
*/
public function testAcceptsShortNameStringPredis()
{
$option = new CRC16();
$options = $this->getMock('Predis\Configuration\OptionsInterface');
$this->assertInstanceOf('Predis\Cluster\Hash\CRC16', $option->filter($options, 'predis'));
}
/**
* @group disconnected
* @group ext-phpiredis
* @requires extension phpiredis
* @requires function phpiredis_utils_crc16
*/
public function testAcceptsShortNameStringPhpiredis()
{
$option = new CRC16();
$options = $this->getMock('Predis\Configuration\OptionsInterface');
$this->assertInstanceOf('Predis\Cluster\Hash\PhpiredisCRC16', $option->filter($options, 'phpiredis'));
}
/**
* @group disconnected
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage String value for the crc16 option must be either `predis` or `phpiredis`
*/
public function testThrowsExceptionOnInvalidShortNameString()
{
$option = new CRC16();
$options = $this->getMock('Predis\Configuration\OptionsInterface');
$option->filter($options, 'unknown');
}
}