Example #1
0
 /**
  * {@inheritdoc}
  */
 public function getDefault(IClientOptions $options)
 {
     $profile = ServerProfile::getDefault();
     if (isset($options->prefix)) {
         $profile->setProcessor($options->prefix);
     }
     return $profile;
 }
 /**
  * @group disconnected
  */
 public function testConstructorOpensContext()
 {
     $cmdMonitor = ServerProfile::getDefault()->createCommand('monitor');
     $connection = $this->getMock('Predis\\Network\\IConnectionSingle');
     $client = $this->getMock('Predis\\Client', array('createCommand', 'executeCommand'), array($connection));
     $client->expects($this->once())->method('createCommand')->with('monitor', array())->will($this->returnValue($cmdMonitor));
     $client->expects($this->once())->method('executeCommand')->with($cmdMonitor);
     $monitor = new MonitorContext($client);
 }
 /**
  * @group disconnected
  */
 public function testExecuteCommandDoesNotSendCommandsWithoutExecute()
 {
     $profile = ServerProfile::getDefault();
     $executor = $this->getMock('Predis\\Pipeline\\IPipelineExecutor');
     $executor->expects($this->never())->method('executor');
     $pipeline = new PipelineContext(new Client(), array('executor' => $executor));
     $pipeline->executeCommand($profile->createCommand('echo', array('one')));
     $pipeline->executeCommand($profile->createCommand('echo', array('two')));
     $pipeline->executeCommand($profile->createCommand('echo', array('three')));
 }
Example #4
0
 /**
  * Creates an instance of Predis\Options\ClientOptions from various types of
  * parameters (string, array, Predis\Profiles\ServerProfile) or returns the
  * passed object if its an instance of Predis\Options\ClientOptions.
  *
  * @param mixed $options Client options.
  * @return ClientOptions
  */
 private function filterOptions($options)
 {
     if ($options === null) {
         return new ClientOptions();
     }
     if (is_array($options)) {
         return new ClientOptions($options);
     }
     if ($options instanceof ClientOptions) {
         return $options;
     }
     if ($options instanceof IServerProfile) {
         return new ClientOptions(array('profile' => $options));
     }
     if (is_string($options)) {
         return new ClientOptions(array('profile' => ServerProfile::get($options)));
     }
     throw new \InvalidArgumentException("Invalid type for client options");
 }
Example #5
0
 /**
  * @group disconnected
  */
 public function testExecutesCommandOnCorrectConnection()
 {
     $command = ServerProfile::getDefault()->createCommand('get', array('node01:5431'));
     $connection1 = $this->getMockConnection('tcp://host1:7001');
     $connection1->expects($this->once())->method('executeCommand')->with($command);
     $connection2 = $this->getMockConnection('tcp://host1:7002');
     $connection2->expects($this->never())->method('executeCommand');
     $cluster = new PredisCluster();
     $cluster->add($connection1);
     $cluster->add($connection2);
     $cluster->executeCommand($command);
 }
local hashes = {}
for _, key in pairs(KEYS) do
    table.insert(hashes, key)
    table.insert(hashes, redis.call('hgetall', key))
end
return hashes
EOS;
    public function getScript()
    {
        return self::BODY;
    }
}
// ------------------------------------------------------------------------- //
$parameters = array('tcp://127.0.0.1:6379/?alias=master', 'tcp://127.0.0.1:6380/?alias=slave');
$options = array('profile' => function ($options) {
    $profile = ServerProfile::get('2.6');
    $profile->defineCommand('hmgetall', 'HashMultipleGetAll');
    return $profile;
}, 'replication' => function ($options) {
    $replication = new MasterSlaveReplication();
    $replication->setScriptReadOnly(HashMultipleGetAll::BODY);
    return $replication;
});
// ------------------------------------------------------------------------- //
$client = new Predis\Client($parameters, $options);
// Execute the following commands on the master server using redis-cli:
// $ ./redis-cli HMSET metavars foo bar hoge piyo
// $ ./redis-cli HMSET servers master host1 slave host2
$hashes = $client->hmgetall('metavars', 'servers');
$replication = $client->getConnection();
$stillOnSlave = $replication->getCurrent() === $replication->getConnectionById('slave');
Example #7
0
 /**
  * @group disconnected
  */
 public function testChainOfProcessors()
 {
     $processor = $this->getMock('Predis\\Commands\\Processors\\ICommandProcessor');
     $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');
 }
Example #8
0
 /**
  * @group disconnected
  */
 public function testClosingContextWithFalseSendsUnsubscriptions()
 {
     $profile = ServerProfile::get(REDIS_SERVER_VERSION);
     $classUnsubscribe = $profile->getCommandClass('unsubscribe');
     $classPunsubscribe = $profile->getCommandClass('punsubscribe');
     $connection = $this->getMock('Predis\\Network\\IConnectionSingle');
     $client = $this->getMock('Predis\\Client', array('disconnect'), array($connection));
     $options = array('subscribe' => 'channel:foo', 'psubscribe' => 'channels:*');
     $pubsub = new PubSubContext($client, $options);
     $connection->expects($this->exactly(2))->method('writeCommand')->with($this->logicalOr($this->isInstanceOf($classUnsubscribe), $this->isInstanceOf($classPunsubscribe)));
     $pubsub->closeContext(false);
 }
 /**
  * Returns a new instance of server profile.
  *
  * @param array $additional Additional connection parameters.
  * @return ServerProfile
  */
 protected function getProfile($version = null)
 {
     return ServerProfile::get($version ?: REDIS_SERVER_VERSION);
 }
local hashes = {}
for _, key in pairs(KEYS) do
    table.insert(hashes, key)
    table.insert(hashes, redis.call('hgetall', key))
end
return hashes
EOS;
    public function getScript()
    {
        return self::BODY;
    }
}
// ------------------------------------------------------------------------- //
$parameters = array('tcp://127.0.0.1:6379/?alias=master', 'tcp://127.0.0.1:6380/?alias=slave');
$options = array('profile' => function ($options) {
    $profile = ServerProfile::get('dev');
    $profile->defineCommand('hmgetall', 'HashMultipleGetAll');
    return $profile;
}, 'replication' => function ($options) {
    $replication = new MasterSlaveReplication();
    $replication->setScriptReadOnly(HashMultipleGetAll::BODY);
    return $replication;
});
// ------------------------------------------------------------------------- //
$client = new Predis\Client($parameters, $options);
// Execute the following commands on the master server using redis-cli:
// $ ./redis-cli HMSET metavars foo bar hoge piyo
// $ ./redis-cli HMSET servers master host1 slave host2
$hashes = $client->hmgetall('metavars', 'servers');
$replication = $client->getConnection();
$stillOnSlave = $replication->getCurrent() === $replication->getConnectionById('slave');
 /**
  * @group disconnected
  */
 public function testCanSetReadOnlyFlagForEvalScripts()
 {
     $profile = ServerProfile::get('dev');
     $cmdEval = $profile->createCommand('eval', array($script = "return redis.call('info');"));
     $cmdEvalSha = $profile->createCommand('evalsha', array($scriptSHA1 = sha1($script)));
     $master = $this->getMockConnection('tcp://host1?alias=master');
     $master->expects($this->never())->method('executeCommand');
     $slave1 = $this->getMockConnection('tcp://host2?alias=slave1');
     $slave1->expects($this->exactly(2))->method('executeCommand')->with($this->logicalOr($cmdEval, $cmdEvalSha));
     $replication = new MasterSlaveReplication();
     $replication->add($master);
     $replication->add($slave1);
     $replication->setScriptReadOnly($script);
     $replication->executeCommand($cmdEval);
     $replication->executeCommand($cmdEvalSha);
 }
Example #12
0
 /**
  * {@inheritdoc}
  */
 public function create($parameters, IServerProfile $profile = null)
 {
     if (!$parameters instanceof IConnectionParameters) {
         $parameters = new ConnectionParameters($parameters ?: array());
     }
     $scheme = $parameters->scheme;
     if (!isset($this->schemes[$scheme])) {
         throw new \InvalidArgumentException("Unknown connection scheme: {$scheme}");
     }
     $initializer = $this->schemes[$scheme];
     if (!is_callable($initializer)) {
         $connection = new $initializer($parameters);
         $this->prepareConnection($connection, $profile ?: ServerProfile::getDefault());
         return $connection;
     }
     $connection = call_user_func($initializer, $parameters, $profile);
     if (!$connection instanceof IConnectionSingle) {
         throw new \InvalidArgumentException('Objects returned by connection initializers must implement ' . 'the Predis\\Network\\IConnectionSingle interface');
     }
     return $connection;
 }
Example #13
0
 /**
  * @group disconnected
  */
 public function testValidationDoesNotSetPrefixProcessorWhenValueIsProfileInstance()
 {
     $options = $this->getMock('Predis\\Options\\ClientOptions', array('__isset', '__get'));
     $options->expects($this->never())->method('__isset');
     $options->expects($this->never())->method('__get');
     $option = new ClientProfile();
     $profile = $option->filter($options, ServerProfile::getDefault());
     $this->assertInstanceOf('Predis\\Profiles\\IServerProfile', $profile);
     $this->assertNull($profile->getProcessor());
 }
Example #14
0
 /**
  * @group disconnected
  */
 public function testCallingRedisCommandExecutesInstanceOfCommand()
 {
     $ping = ServerProfile::getDefault()->createCommand('ping', array());
     $connection = $this->getMock('Predis\\Network\\IConnection');
     $connection->expects($this->once())->method('executeCommand')->with($this->isInstanceOf('Predis\\Commands\\ConnectionPing'))->will($this->returnValue(true));
     $profile = $this->getMock('Predis\\Profiles\\IServerProfile');
     $profile->expects($this->once())->method('createCommand')->with('ping', array())->will($this->returnValue($ping));
     $client = $this->getMock('Predis\\Client', array('createCommand'), array($connection, $profile));
     $this->assertTrue($client->ping());
 }
Example #15
0
 /**
  * {@inheritdoc}
  */
 public function getDefault()
 {
     return ServerProfile::getDefault();
 }
Example #16
0
 /**
  * Return the server profile used during the tests.
  *
  * @return IServerProfile
  */
 protected function getProfile()
 {
     return ServerProfile::get(REDIS_SERVER_VERSION);
 }