Author: Daniele Alessandri (suppakilla@gmail.com)
 /**
  * @group disconnected
  */
 public function testConstructorStartsConsumer()
 {
     $cmdMonitor = Profile\Factory::getDefault()->createCommand('monitor');
     $connection = $this->getMock('Predis\\Connection\\NodeConnectionInterface');
     $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);
     new MonitorConsumer($client);
 }
 /**
  * @group disconnected
  */
 public function testSetLuaScriptAsReadOperation()
 {
     $strategy = new ReplicationStrategy();
     $profile = Profile\Factory::getDevelopment();
     $writeScript = 'redis.call("set", "foo", "bar")';
     $readScript = 'return true';
     $strategy->setScriptReadOnly($readScript, true);
     $cmdEval = $profile->createCommand('EVAL', array($writeScript));
     $cmdEvalSHA = $profile->createCommand('EVALSHA', array(sha1($writeScript)));
     $this->assertFalse($strategy->isReadOperation($cmdEval));
     $this->assertFalse($strategy->isReadOperation($cmdEvalSHA));
     $cmdEval = $profile->createCommand('EVAL', array($readScript));
     $cmdEvalSHA = $profile->createCommand('EVALSHA', array(sha1($readScript)));
     $this->assertTrue($strategy->isReadOperation($cmdEval));
     $this->assertTrue($strategy->isReadOperation($cmdEvalSHA));
 }
 /**
  * @group disconnected
  */
 public function testIterationRewindable()
 {
     $client = $this->getMock('Predis\\Client', array('getProfile', 'zscan'));
     $client->expects($this->any())->method('getProfile')->will($this->returnValue(Profile\Factory::get('2.8')));
     $client->expects($this->exactly(2))->method('zscan')->with('key:zset', 0, array())->will($this->returnValue(array(0, array('member:1st' => 1.0, 'member:2nd' => 2.0))));
     $iterator = new SortedSetKey($client, 'key:zset');
     $iterator->rewind();
     $this->assertTrue($iterator->valid());
     $this->assertSame(1.0, $iterator->current());
     $this->assertSame('member:1st', $iterator->key());
     $iterator->rewind();
     $this->assertTrue($iterator->valid());
     $this->assertSame(1.0, $iterator->current());
     $this->assertSame('member:1st', $iterator->key());
     $iterator->next();
     $this->assertTrue($iterator->valid());
     $this->assertSame(2.0, $iterator->current());
     $this->assertSame('member:2nd', $iterator->key());
     $iterator->next();
     $this->assertFalse($iterator->valid());
 }
 /**
  * Loads a redis client using predis.
  *
  * @param array            $client    A client configuration
  * @param ContainerBuilder $container A ContainerBuilder instance
  */
 protected function loadPredisClient(array $client, ContainerBuilder $container)
 {
     if (null === $client['options']['cluster']) {
         unset($client['options']['cluster']);
     }
     // predis connection parameters have been renamed in v0.8
     $client['options']['async_connect'] = $client['options']['connection_async'];
     $client['options']['timeout'] = $client['options']['connection_timeout'];
     $client['options']['persistent'] = $client['options']['connection_persistent'];
     $client['options']['exceptions'] = $client['options']['throw_errors'];
     unset($client['options']['connection_async']);
     unset($client['options']['connection_timeout']);
     unset($client['options']['connection_persistent']);
     unset($client['options']['throw_errors']);
     $connectionAliases = array();
     $connectionCount = count($client['dsns']);
     foreach ($client['dsns'] as $i => $dsn) {
         /** @var \Snc\RedisBundle\DependencyInjection\Configuration\RedisDsn $dsn */
         if (!($connectionAlias = $dsn->getAlias())) {
             $connectionAlias = 1 === $connectionCount ? $client['alias'] : $client['alias'] . ($i + 1);
         }
         $connectionAliases[] = $connectionAlias;
         $connection = $client['options'];
         $connection['logging'] = $client['logging'];
         $connection['alias'] = $connectionAlias;
         if (null !== $dsn->getSocket()) {
             $connection['scheme'] = 'unix';
             $connection['path'] = $dsn->getSocket();
         } else {
             $connection['scheme'] = 'tcp';
             $connection['host'] = $dsn->getHost();
             $connection['port'] = $dsn->getPort();
         }
         if (null !== $dsn->getDatabase()) {
             $connection['database'] = $dsn->getDatabase();
         }
         $connection['password'] = $dsn->getPassword();
         $connection['weight'] = $dsn->getWeight();
         $this->loadPredisConnectionParameters($client['alias'], $connection, $container);
     }
     // TODO can be shared between clients?!
     $profileId = sprintf('snc_redis.client.%s_profile', $client['alias']);
     $profileDef = new Definition(get_class(\Predis\Profile\Factory::get($client['options']['profile'])));
     // TODO get_class alternative?
     $profileDef->setPublic(false);
     if (null !== $client['options']['prefix']) {
         $processorId = sprintf('snc_redis.client.%s_processor', $client['alias']);
         $processorDef = new Definition('Predis\\Command\\Processor\\KeyPrefixProcessor');
         $processorDef->setArguments(array($client['options']['prefix']));
         $container->setDefinition($processorId, $processorDef);
         $profileDef->addMethodCall('setProcessor', array(new Reference($processorId)));
     }
     $container->setDefinition($profileId, $profileDef);
     $client['options']['profile'] = new Reference($profileId);
     $optionId = sprintf('snc_redis.client.%s_options', $client['alias']);
     $optionDef = new Definition($container->getParameter('snc_redis.client_options.class'));
     $optionDef->setPublic(false);
     $optionDef->addArgument($client['options']);
     $container->setDefinition($optionId, $optionDef);
     $clientDef = new Definition($container->getParameter('snc_redis.client.class'));
     if (1 === $connectionCount) {
         $clientDef->addArgument(new Reference(sprintf('snc_redis.connection.%s_parameters', $connectionAliases[0])));
     } else {
         $connections = array();
         foreach ($connectionAliases as $alias) {
             $connections[] = new Reference(sprintf('snc_redis.connection.%s_parameters', $alias));
         }
         $clientDef->addArgument($connections);
     }
     $clientDef->addArgument(new Reference($optionId));
     $container->setDefinition(sprintf('snc_redis.%s', $client['alias']), $clientDef);
     $container->setAlias(sprintf('snc_redis.%s_client', $client['alias']), sprintf('snc_redis.%s', $client['alias']));
 }
    public function testClientsAliased()
    {
        $params = $this->getSomeParameters();

        $app = $this->register(array(
            'predis.clients' => array(
                '1st' => "{$params['scheme']}://{$params['host']}:{$params['port']}",
                '2nd' => $params,
                '3rd' => array('parameters' => $params),
                '4th' => array('parameters' => $params, 'options' => array('profile' => 'dev')),
            )
        ));

        $this->checkParameters($app['predis'], '1st', $params);
        $this->checkParameters($app['predis'], '2nd', $params);
        $this->checkParameters($app['predis'], '3rd', $params);
        $this->checkParameters($app['predis'], '4th', $params);

        list(, $options) = $this->getParametersAndOptions($app['predis']['1st']);
        $this->assertEquals(ProfileFactory::getDefault(), $options->profile);

        list(, $options) = $this->getParametersAndOptions($app['predis']['2nd']);
        $this->assertEquals(ProfileFactory::getDefault(), $options->profile);

        list(, $options) = $this->getParametersAndOptions($app['predis']['3rd']);
        $this->assertEquals(ProfileFactory::getDefault(), $options->profile);

        list(, $options) = $this->getParametersAndOptions($app['predis']['4th']);
        $this->assertEquals(ProfileFactory::getDevelopment(), $options->profile);
    }
 /**
  * Returns a new instance of server profile.
  *
  * @param string $version Redis profile.
  *
  * @return Profile\ProfileInterface
  */
 protected function getProfile($version = null)
 {
     return Profile\Factory::get($version ?: REDIS_SERVER_VERSION);
 }
 /**
  * @group disconnected
  */
 public function testDoesNotApplyPrefixOnProfileValue()
 {
     $option = new ProfileOption();
     $options = $this->getMock('Predis\\Configuration\\OptionsInterface');
     $value = Profile\Factory::getDefault();
     $options->expects($this->never())->method('__isset');
     $options->expects($this->never())->method('__get');
     $profile = $option->filter($options, $value);
     $this->assertInstanceOf('Predis\\Profile\\ProfileInterface', $profile);
     $this->assertInstanceOf(get_class(Profile\Factory::getDefault()), $profile);
     $this->assertNull($profile->getProcessor());
 }
 /**
  * @group disconnected
  */
 public function testIterationRewindable()
 {
     $client = $this->getMock('Predis\\Client', array('getProfile', 'scan'));
     $client->expects($this->any())->method('getProfile')->will($this->returnValue(Profile\Factory::get('2.8')));
     $client->expects($this->exactly(2))->method('scan')->with(0, array())->will($this->returnValue(array(0, array('key:1st', 'key:2nd'))));
     $iterator = new Keyspace($client);
     $iterator->rewind();
     $this->assertTrue($iterator->valid());
     $this->assertSame('key:1st', $iterator->current());
     $this->assertSame(0, $iterator->key());
     $iterator->rewind();
     $this->assertTrue($iterator->valid());
     $this->assertSame('key:1st', $iterator->current());
     $this->assertSame(0, $iterator->key());
     $iterator->next();
     $this->assertTrue($iterator->valid());
     $this->assertSame(1, $iterator->key());
     $this->assertSame('key:2nd', $iterator->current());
     $iterator->next();
     $this->assertFalse($iterator->valid());
 }
 /**
  * @group disconnected
  */
 public function testStoppingConsumerWithFalseSendsUnsubscriptions()
 {
     $profile = Profile\Factory::get(REDIS_SERVER_VERSION);
     $classUnsubscribe = $profile->getCommandClass('unsubscribe');
     $classPunsubscribe = $profile->getCommandClass('punsubscribe');
     $connection = $this->getMock('Predis\\Connection\\NodeConnectionInterface');
     $client = $this->getMock('Predis\\Client', array('disconnect'), array($connection));
     $options = array('subscribe' => 'channel:foo', 'psubscribe' => 'channels:*');
     $pubsub = new PubSubConsumer($client, $options);
     $connection->expects($this->exactly(2))->method('writeRequest')->with($this->logicalOr($this->isInstanceOf($classUnsubscribe), $this->isInstanceOf($classPunsubscribe)));
     $pubsub->stop(false);
 }
 /**
  * @group disconnected
  * @expectedException \InvalidArgumentException
  * @expectedExceptionMessage The class 'stdClass' is not a valid profile class.
  */
 public function testDefineInvalidProfile()
 {
     Factory::define('bogus', 'stdClass');
 }
 /**
  * @group disconnected
  */
 public function testCreatesNewCommandUsingSpecifiedProfile()
 {
     $ping = ProfileFactory::getDefault()->createCommand('ping', []);
     $profile = $this->getMock('Predis\\Profile\\ProfileInterface');
     $profile->expects($this->once())->method('createCommand')->with('ping', [])->will($this->returnValue($ping));
     $client = new Client(null, ['profile' => $profile]);
     $this->assertSame($ping, $client->createCommand('ping', []));
 }
 /**
  * @group disconnected
  */
 public function testExecuteCommandOnEachNode()
 {
     $ping = Profile\Factory::getDefault()->createCommand('ping', array());
     $connection1 = $this->getMock('Predis\\Connection\\NodeConnectionInterface');
     $connection1->expects($this->once())->method('executeCommand')->with($ping)->will($this->returnValue(true));
     $connection2 = $this->getMock('Predis\\Connection\\NodeConnectionInterface');
     $connection2->expects($this->once())->method('executeCommand')->with($ping)->will($this->returnValue(false));
     $cluster = new PredisCluster();
     $cluster->add($connection1);
     $cluster->add($connection2);
     $this->assertSame(array(true, false), $cluster->executeCommandOnNodes($ping));
 }
    public function testClientOptions()
    {
        $profile = 'dev';
        $prefix = 'silex:';

        $app = $this->register(array(
            'predis.options' => array(
                'profile' => $profile,
                'prefix' => $prefix,
            ),
        ));

        list(, $options) = $this->getParametersAndOptions($app['predis']);

        $profile = ProfileFactory::get($profile);
        $profile->setProcessor($options->prefix);

        $this->assertEquals($prefix, $options->prefix->getPrefix());
        $this->assertEquals($profile, $options->profile);
    }
 /**
  * @group disconnected
  * @expectedException \Predis\NotSupportedException
  * @expectedExceptionMessage Cannot use 'PING' with redis-cluster.
  */
 public function testThrowsExceptionOnNonSupportedCommand()
 {
     $ping = Profile\Factory::getDefault()->createCommand('ping');
     $cluster = new RedisCluster(new Connection\Factory());
     $cluster->add($this->getMockConnection('tcp://127.0.0.1:6379'));
     $cluster->getConnection($ping);
 }
 /**
  * @group disconnected
  */
 public function testSettingCustomCommandHandler()
 {
     $strategy = $this->getClusterStrategy();
     $profile = Profile\Factory::getDevelopment();
     $callable = $this->getMock('stdClass', array('__invoke'));
     $callable->expects($this->once())->method('__invoke')->with($this->isInstanceOf('Predis\\Command\\CommandInterface'))->will($this->returnValue('key'));
     $strategy->setCommandHandler('get', $callable);
     $command = $profile->createCommand('get', array('key'));
     $this->assertNotNull($strategy->getSlot($command));
 }
 /**
  * @group disconnected
  */
 public function testCallingRedisCommandExecutesInstanceOfCommand()
 {
     $ping = Profile\Factory::getDefault()->createCommand('ping', array());
     $connection = $this->getMock('Predis\\Connection\\ConnectionInterface');
     $connection->expects($this->once())->method('executeCommand')->with($this->isInstanceOf('Predis\\Command\\ConnectionPing'))->will($this->returnValue('PONG'));
     $profile = $this->getMock('Predis\\Profile\\ProfileInterface');
     $profile->expects($this->once())->method('createCommand')->with('ping', array())->will($this->returnValue($ping));
     $options = array('profile' => $profile);
     $client = $this->getMock('Predis\\Client', null, array($connection, $options));
     $this->assertEquals('PONG', $client->ping());
 }
 /**
  * @group disconnected
  */
 public function testExecuteCommandDoesNotSendCommandsWithoutExecute()
 {
     $profile = Profile\Factory::getDefault();
     $connection = $this->getMock('Predis\\Connection\\NodeConnectionInterface');
     $connection->expects($this->never())->method('writeRequest');
     $connection->expects($this->never())->method('readResponse');
     $pipeline = new Pipeline(new Client($connection));
     $pipeline->executeCommand($profile->createCommand('echo', array('one')));
     $pipeline->executeCommand($profile->createCommand('echo', array('two')));
     $pipeline->executeCommand($profile->createCommand('echo', array('three')));
 }
 /**
  * Returns a new instance of client options.
  *
  * @param array $override Override default options.
  *
  * @return Predis\Async\Configuration\OptionsInterface
  */
 protected function getOptions($override = null)
 {
     $options = new Options(array_merge(['profile' => ProfileFactory::get(REDIS_SERVER_VERSION), 'eventloop' => $this->getEventLoop()]), $override ?: []);
     return $options;
 }
Exemple #19
0
 /**
  * {@inheritdoc}
  */
 public function getDefault(OptionsInterface $options)
 {
     $profile = Predis_Factory::getDefault();
     $this->setProcessors($options, $profile);
     return $profile;
 }
 /**
  * @group disconnected
  */
 public function testIterationRewindable()
 {
     $client = $this->getMock('Predis\\Client', array('getProfile', 'lrange'));
     $client->expects($this->any())->method('getProfile')->will($this->returnValue(Profile\Factory::getDefault()));
     $client->expects($this->exactly(2))->method('lrange')->with('key:list', 0, 9)->will($this->returnValue(array('item:1', 'item:2')));
     $iterator = new ListKey($client, 'key:list');
     $iterator->rewind();
     $this->assertTrue($iterator->valid());
     $this->assertSame('item:1', $iterator->current());
     $this->assertSame(0, $iterator->key());
     $iterator->rewind();
     $this->assertTrue($iterator->valid());
     $this->assertSame('item:1', $iterator->current());
     $this->assertSame(0, $iterator->key());
     $iterator->next();
     $this->assertTrue($iterator->valid());
     $this->assertSame(1, $iterator->key());
     $this->assertSame('item:2', $iterator->current());
     $iterator->next();
     $this->assertFalse($iterator->valid());
 }