/** * {@inheritdoc} */ public function filter(OptionsInterface $options, $value) { if (is_string($value)) { $value = Factory::get($value); $this->setProcessors($options, $value); } elseif (!$value instanceof ProfileInterface) { throw new InvalidArgumentException('Invalid value for the profile option.'); } return $value; }
/** * @group disconnected */ public function testAppliesPrefixOnProfileCreatedFromStringValue() { $option = new ProfileOption(); $options = $this->getMock('Predis\\Configuration\\OptionsInterface'); $options->expects($this->once())->method('__isset')->with('prefix')->will($this->returnValue(true)); $options->expects($this->once())->method('__get')->with('prefix')->will($this->returnValue(new KeyPrefixProcessor('prefix:'))); $profile = $option->filter($options, '2.0'); $this->assertInstanceOf('Predis\\Profile\\ProfileInterface', $profile); $this->assertInstanceOf(get_class(Profile\Factory::get('2.0')), $profile); $this->assertInstanceOf('Predis\\Command\\Processor\\KeyPrefixProcessor', $profile->getProcessor()); $this->assertSame('prefix:', $profile->getProcessor()->getPrefix()); }
/** * @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 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'])); }
/** * 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); }
/** * 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; }
/** * @group disconnected */ public function testConstructorWithNullAndArrayArgument() { $factory = $this->getMock('Predis\\Connection\\FactoryInterface'); $arg2 = array('profile' => '2.0', 'prefix' => 'prefix:', 'connections' => $factory); $client = new Client(null, $arg2); $profile = $client->getProfile(); $this->assertSame($profile->getVersion(), Profile\Factory::get('2.0')->getVersion()); $this->assertInstanceOf('Predis\\Command\\Processor\\KeyPrefixProcessor', $profile->getProcessor()); $this->assertSame('prefix:', $profile->getProcessor()->getPrefix()); }
/** * @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 * @todo How should we test for the error callback? */ public function testConstructorWithNullAndArrayArgument() { $options = ['profile' => '2.0', 'prefix' => 'prefix:', 'eventloop' => $loop = $this->getEventLoop(), 'on_error' => $callback = function ($client, $error) { }]; $client = new Client(null, $options); $profile = $client->getProfile(); $this->assertSame($profile->getVersion(), ProfileFactory::get('2.0')->getVersion()); $this->assertInstanceOf('Predis\\Command\\Processor\\KeyPrefixProcessor', $profile->getProcessor()); $this->assertSame('prefix:', $profile->getProcessor()->getPrefix()); $this->assertSame($loop, $client->getEventLoop()); }
/** * @group disconnected */ public function testDefineProfile() { $profileClass = get_class($this->getMock('Predis\\Profile\\ProfileInterface')); Factory::define('mock', $profileClass); $this->assertInstanceOf($profileClass, Factory::get('mock')); }
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); }