Skip to content
  • P
    Projects
  • G
    Groups
  • S
    Snippets
  • Help

孙龙 / note-library

  • This project
    • Loading...
  • Sign in
Go to a project
  • Project
  • Repository
  • Issues 0
  • Merge Requests 0
  • Pipelines
  • Wiki
  • Snippets
  • Settings
  • Activity
  • Graph
  • Charts
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
  • Files
  • Commits
  • Branches
  • Tags
  • Contributors
  • Graph
  • Compare
  • Charts
Find file
BlameHistoryPermalink
Switch branch/tag
  • note-library
  • vendor
  • predis
  • tests
  • PHPUnit
  • PredisProfileTestCase.php
  • 孙龙's avatar
    init · 1f46a6ed
    孙龙 committed 5 years ago
    1f46a6ed
PredisProfileTestCase.php 7.72 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
<?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 Predis\Command\CommandInterface;
use Predis\Command\Processor\ProcessorChain;
use PredisTestCase;

/**
 *
 */
abstract class PredisProfileTestCase extends PredisTestCase
{
    /**
     * Returns a new instance of the tested profile.
     *
     * @param string $version Version of Redis.
     *
     * @return ProfileInterface
     */
    protected function getProfile($version = null)
    {
        $this->markTestIncomplete('Server profile must be defined in '.get_class($this));
    }

    /**
     * Returns the expected version string for the tested profile.
     *
     * @return string Version string.
     */
    abstract protected function getExpectedVersion();

    /**
     * Returns the expected list of commands supported by the tested profile.
     *
     * @return array List of supported commands.
     */
    abstract protected function getExpectedCommands();

    /**
     * Returns the list of commands supported by the current
     * server profile.
     *
     * @param ProfileInterface $profile Server profile instance.
     *
     * @return array
     */
    protected function getCommands(ProfileInterface $profile)
    {
        $commands = $profile->getSupportedCommands();

        return array_keys($commands);
    }

    /**
     * @group disconnected
     */
    public function testGetVersion()
    {
        $profile = $this->getProfile();

        $this->assertEquals($this->getExpectedVersion(), $profile->getVersion());
    }

    /**
     * @group disconnected
     */
    public function testSupportedCommands()
    {
        $profile = $this->getProfile();
        $expected = $this->getExpectedCommands();
        $commands = $this->getCommands($profile);

        $this->assertSame($expected, $commands);
    }

    /**
     * @group disconnected
     */
    public function testToString()
    {
        $this->assertEquals($this->getExpectedVersion(), $this->getProfile());
    }

    /**
     * @group disconnected
     */
    public function testSupportCommand()
    {
        $profile = $this->getProfile();

        $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 = $this->getProfile();

        $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 = $this->getProfile();

        $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 = $this->getProfile();
        $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 The class 'stdClass' is not a valid command class.
     */
    public function testDefineInvalidCommand()
    {
        $profile = $this->getProfile();

        $profile->defineCommand('mock', 'stdClass');
    }

    /**
     * @group disconnected
     */
    public function testCreateCommandWithoutArguments()
    {
        $profile = $this->getProfile();

        $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 = $this->getProfile();
        $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 Command 'UNKNOWN' is not a registered Redis command.
     */
    public function testCreateUndefinedCommand()
    {
        $profile = $this->getProfile();
        $profile->createCommand('unknown');
    }

    /**
     * @group disconnected
     */
    public function testGetDefaultProcessor()
    {
        $profile = $this->getProfile();

        $this->assertNull($profile->getProcessor());
    }

    /**
     * @group disconnected
     */
    public function testSetProcessor()
    {
        $processor = $this->getMock('Predis\Command\Processor\ProcessorInterface');

        $profile = $this->getProfile();
        $profile->setProcessor($processor);

        $this->assertSame($processor, $profile->getProcessor());
    }

    /**
     * @group disconnected
     */
    public function testSetAndUnsetProcessor()
    {
        $processor = $this->getMock('Predis\Command\Processor\ProcessorInterface');
        $profile = $this->getProfile();

        $profile->setProcessor($processor);
        $this->assertSame($processor, $profile->getProcessor());

        $profile->setProcessor(null);
        $this->assertNull($profile->getProcessor());
    }

    /**
     * @group disconnected
     */
    public function testSingleProcessor()
    {
        // Could it be that objects passed to the return callback of a mocked
        // method are cloned instead of being passed by reference?
        $argsRef = null;

        $processor = $this->getMock('Predis\Command\Processor\ProcessorInterface');
        $processor->expects($this->once())
                  ->method('process')
                  ->with($this->isInstanceOf('Predis\Command\CommandInterface'))
                  ->will($this->returnCallback(function (CommandInterface $cmd) use (&$argsRef) {
                        $cmd->setRawArguments($argsRef = array_map('strtoupper', $cmd->getArguments()));
                    }));

        $profile = $this->getProfile();
        $profile->setProcessor($processor);
        $profile->createCommand('set', array('foo', 'bar'));

        $this->assertSame(array('FOO', 'BAR'), $argsRef);
    }

    /**
     * @group disconnected
     */
    public function testChainOfProcessors()
    {
        $processor = $this->getMock('Predis\Command\Processor\ProcessorInterface');
        $processor->expects($this->exactly(2))
                  ->method('process');

        $chain = new ProcessorChain();
        $chain->add($processor);
        $chain->add($processor);

        $profile = $this->getProfile();
        $profile->setProcessor($chain);
        $profile->createCommand('info');
    }
}