123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293 |
- <?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\Profile;
- use PredisTestCase;
- use Predis\Command\Processor\ProcessorChain;
- /**
- *
- */
- class ServerProfileTest extends PredisTestCase
- {
- const DEFAULT_PROFILE_VERSION = '2.8';
- const DEVELOPMENT_PROFILE_VERSION = '3.0';
- /**
- * @group disconnected
- */
- public function testGetVersion()
- {
- $profile = ServerProfile::get('2.0');
- $this->assertInstanceOf('Predis\Profile\ServerProfileInterface', $profile);
- $this->assertEquals('2.0', $profile->getVersion());
- }
- /**
- * @group disconnected
- */
- public function testGetDefault()
- {
- $profile1 = ServerProfile::get(self::DEFAULT_PROFILE_VERSION);
- $profile2 = ServerProfile::get('default');
- $profile3 = ServerProfile::getDefault();
- $this->assertInstanceOf('Predis\Profile\ServerProfileInterface', $profile1);
- $this->assertInstanceOf('Predis\Profile\ServerProfileInterface', $profile2);
- $this->assertInstanceOf('Predis\Profile\ServerProfileInterface', $profile3);
- $this->assertEquals($profile1->getVersion(), $profile2->getVersion());
- $this->assertEquals($profile2->getVersion(), $profile3->getVersion());
- }
- /**
- * @group disconnected
- */
- public function testGetDevelopment()
- {
- $profile1 = ServerProfile::get('dev');
- $profile2 = ServerProfile::getDevelopment();
- $this->assertInstanceOf('Predis\Profile\ServerProfileInterface', $profile1);
- $this->assertInstanceOf('Predis\Profile\ServerProfileInterface', $profile2);
- $this->assertEquals(self::DEVELOPMENT_PROFILE_VERSION, $profile2->getVersion());
- }
- /**
- * @group disconnected
- * @expectedException Predis\ClientException
- * @expectedExceptionMessage Unknown server profile: 1.0
- */
- public function testGetUndefinedProfile()
- {
- ServerProfile::get('1.0');
- }
- /**
- * @group disconnected
- */
- public function testDefineProfile()
- {
- $profileClass = get_class($this->getMock('Predis\Profile\ServerProfileInterface'));
- ServerProfile::define('mock', $profileClass);
- $this->assertInstanceOf($profileClass, ServerProfile::get('mock'));
- }
- /**
- * @group disconnected
- * @expectedException InvalidArgumentException
- * @expectedExceptionMessage Cannot register 'stdClass' as it is not a valid profile class
- */
- public function testDefineInvalidProfile()
- {
- ServerProfile::define('bogus', 'stdClass');
- }
- /**
- * @group disconnected
- */
- public function testToString()
- {
- $this->assertEquals('2.0', (string) ServerProfile::get('2.0'));
- }
- /**
- * @group disconnected
- */
- public function testSupportCommand()
- {
- $profile = ServerProfile::getDefault();
- $this->assertTrue($profile->supportsCommand('info'));
- $this->assertTrue($profile->supportsCommand('INFO'));
- $this->assertFalse($profile->supportsCommand('unknown'));
- $this->assertFalse($profile->supportsCommand('UNKNOWN'));
- }
- /**
- * @group disconnected
- */
- public function testSupportCommands()
- {
- $profile = ServerProfile::getDefault();
- $this->assertTrue($profile->supportsCommands(array('get', 'set')));
- $this->assertTrue($profile->supportsCommands(array('GET', 'SET')));
- $this->assertFalse($profile->supportsCommands(array('get', 'unknown')));
- $this->assertFalse($profile->supportsCommands(array('unknown1', 'unknown2')));
- }
- /**
- * @group disconnected
- */
- public function testGetCommandClass()
- {
- $profile = ServerProfile::getDefault();
- $this->assertSame('Predis\Command\ConnectionPing', $profile->getCommandClass('ping'));
- $this->assertSame('Predis\Command\ConnectionPing', $profile->getCommandClass('PING'));
- $this->assertNull($profile->getCommandClass('unknown'));
- $this->assertNull($profile->getCommandClass('UNKNOWN'));
- }
- /**
- * @group disconnected
- */
- public function testDefineCommand()
- {
- $profile = ServerProfile::getDefault();
- $command = $this->getMock('Predis\Command\CommandInterface');
- $profile->defineCommand('mock', get_class($command));
- $this->assertTrue($profile->supportsCommand('mock'));
- $this->assertTrue($profile->supportsCommand('MOCK'));
- $this->assertSame(get_class($command), $profile->getCommandClass('mock'));
- }
- /**
- * @group disconnected
- * @expectedException InvalidArgumentException
- * @expectedExceptionMessage Cannot register 'stdClass' as it is not a valid Redis command
- */
- public function testDefineInvalidCommand()
- {
- $profile = ServerProfile::getDefault();
- $profile->defineCommand('mock', 'stdClass');
- }
- /**
- * @group disconnected
- */
- public function testCreateCommandWithoutArguments()
- {
- $profile = ServerProfile::getDefault();
- $command = $profile->createCommand('info');
- $this->assertInstanceOf('Predis\Command\CommandInterface', $command);
- $this->assertEquals('INFO', $command->getId());
- $this->assertEquals(array(), $command->getArguments());
- }
- /**
- * @group disconnected
- */
- public function testCreateCommandWithArguments()
- {
- $profile = ServerProfile::getDefault();
- $arguments = array('foo', 'bar');
- $command = $profile->createCommand('set', $arguments);
- $this->assertInstanceOf('Predis\Command\CommandInterface', $command);
- $this->assertEquals('SET', $command->getId());
- $this->assertEquals($arguments, $command->getArguments());
- }
- /**
- * @group disconnected
- * @expectedException Predis\ClientException
- * @expectedExceptionMessage 'unknown' is not a registered Redis command
- */
- public function testCreateUndefinedCommand()
- {
- $profile = ServerProfile::getDefault();
- $profile->createCommand('unknown');
- }
- /**
- * @group disconnected
- */
- public function testGetDefaultProcessor()
- {
- $profile = ServerProfile::getDefault();
- $this->assertNull($profile->getProcessor());
- }
- /**
- * @group disconnected
- */
- public function testSetProcessor()
- {
- $processor = $this->getMock('Predis\Command\Processor\CommandProcessorInterface');
- $profile = ServerProfile::getDefault();
- $profile->setProcessor($processor);
- $this->assertSame($processor, $profile->getProcessor());
- }
- /**
- * @group disconnected
- */
- public function testSetAndUnsetProcessor()
- {
- $processor = $this->getMock('Predis\Command\Processor\CommandProcessorInterface');
- $profile = ServerProfile::getDefault();
- $profile->setProcessor($processor);
- $this->assertSame($processor, $profile->getProcessor());
- $profile->setProcessor(null);
- $this->assertNull($profile->getProcessor());
- }
- /**
- * @group disconnected
- * @todo Could it be that objects passed to the return callback of a mocked
- * method are cloned instead of being passed by reference?
- */
- public function testSingleProcessor()
- {
- $argsRef = null;
- $processor = $this->getMock('Predis\Command\Processor\CommandProcessorInterface');
- $processor->expects($this->once())
- ->method('process')
- ->with($this->isInstanceOf('Predis\Command\CommandInterface'))
- ->will($this->returnCallback(function ($cmd) use (&$argsRef) {
- $cmd->setRawArguments($argsRef = array_map('strtoupper', $cmd->getArguments()));
- }));
- $profile = ServerProfile::getDefault();
- $profile->setProcessor($processor);
- $command = $profile->createCommand('set', array('foo', 'bar'));
- $this->assertSame(array('FOO', 'BAR'), $argsRef);
- }
- /**
- * @group disconnected
- */
- public function testChainOfProcessors()
- {
- $processor = $this->getMock('Predis\Command\Processor\CommandProcessorInterface');
- $processor->expects($this->exactly(2))
- ->method('process');
- $chain = new ProcessorChain();
- $chain->add($processor);
- $chain->add($processor);
- $profile = ServerProfile::getDefault();
- $profile->setProcessor($chain);
- $profile->createCommand('info');
- }
- }
|