Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Storage/Adapter/RedisOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class RedisOptions extends AdapterOptions
*
* @var string[]
*/
protected $__prioritizedProperties__ = ['resource_manager', 'resource_id'];
protected $__prioritizedProperties__ = ['resource_manager', 'resource_id', 'server'];
// @codingStandardsIgnoreEnd

/**
Expand Down
42 changes: 20 additions & 22 deletions src/Storage/Adapter/RedisResourceManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ public function getResource($id)
$resource['resource'] = $redis;
$this->connect($resource);

$this->normalizeLibOptions($resource['lib_options']);

foreach ($resource['lib_options'] as $k => $v) {
$redis->setOption($k, $v);
}
Expand Down Expand Up @@ -340,7 +342,6 @@ public function setResource($id, $resource)
$resource = array_merge($defaults, $resource);
// normalize and validate params
$this->normalizePersistentId($resource['persistent_id']);
$this->normalizeLibOptions($resource['lib_options']);

// #6495 note: order is important here, as `normalizeServer` applies destructive
// transformations on $resource['server']
Expand Down Expand Up @@ -394,9 +395,9 @@ public function setPersistentId($id, $persistentId)
}

$resource = & $this->resources[$id];
if ($resource instanceof RedisResource) {
if ($resource['resource'] instanceof RedisResource && $resource['initialized']) {
throw new Exception\RuntimeException(
"Can't change persistent id of resource {$id} after instanziation"
"Can't change persistent id of resource {$id} after initialization"
);
}

Expand All @@ -421,12 +422,6 @@ public function getPersistentId($id)

$resource = & $this->resources[$id];

if ($resource instanceof RedisResource) {
throw new Exception\RuntimeException(
"Can't get persistent id of an instantiated redis resource"
);
}

return $resource['persistent_id'];
}

Expand Down Expand Up @@ -455,19 +450,22 @@ public function setLibOptions($id, array $libOptions)
]);
}

$this->normalizeLibOptions($libOptions);
$resource = & $this->resources[$id];

$resource['lib_options'] = $libOptions;

if ($resource['resource'] instanceof RedisResource) {
$redis = & $resource['resource'];
if (method_exists($redis, 'setOptions')) {
$redis->setOptions($libOptions);
} else {
foreach ($libOptions as $key => $value) {
$redis->setOption($key, $value);
}
if (! $resource['resource'] instanceof RedisResource) {
return $this;
}

$this->normalizeLibOptions($libOptions);
$redis = & $resource['resource'];

if (method_exists($redis, 'setOptions')) {
$redis->setOptions($libOptions);
} else {
foreach ($libOptions as $key => $value) {
$redis->setOption($key, $value);
}
}

Expand All @@ -489,13 +487,13 @@ public function getLibOptions($id)

$resource = & $this->resources[$id];

if ($resource instanceof RedisResource) {
if ($resource['resource'] instanceof RedisResource) {
$libOptions = [];
$reflection = new ReflectionClass('Redis');
$constants = $reflection->getConstants();
foreach ($constants as $constName => $constValue) {
if (substr($constName, 0, 4) == 'OPT_') {
$libOptions[$constValue] = $resource->getOption($constValue);
$libOptions[$constValue] = $resource['resource']->getOption($constValue);
}
}
return $libOptions;
Expand Down Expand Up @@ -533,8 +531,8 @@ public function getLibOption($id, $key)
$this->normalizeLibOptionKey($key);
$resource = & $this->resources[$id];

if ($resource instanceof RedisResource) {
return $resource->getOption($key);
if ($resource['resource'] instanceof RedisResource) {
return $resource['resource']->getOption($key);
}

return isset($resource['lib_options'][$key]) ? $resource['lib_options'][$key] : null;
Expand Down
6 changes: 4 additions & 2 deletions test/Storage/Adapter/RedisResourceManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ public function testValidPersistentId()
$resource = [
'persistent_id' => 'my_connection_name',
'server' => [
'host' => 'localhost'
'host' => getenv('TESTS_ZEND_CACHE_REDIS_HOST') ?: 'localhost',
'port' => getenv('TESTS_ZEND_CACHE_REDIS_PORT') ?: 6379,
],
];
$expectedPersistentId = 'my_connection_name';
Expand All @@ -141,7 +142,8 @@ public function testNotValidPersistentIdOptionName()
$resource = [
'persistend_id' => 'my_connection_name',
'server' => [
'host' => 'localhost'
'host' => getenv('TESTS_ZEND_CACHE_REDIS_HOST') ?: 'localhost',
'port' => getenv('TESTS_ZEND_CACHE_REDIS_PORT') ?: 6379,
],
];
$expectedPersistentId = 'my_connection_name';
Expand Down
41 changes: 35 additions & 6 deletions test/Storage/Adapter/RedisTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,35 @@ public function getCommonAdapterNamesProvider()
];
}

public function testLibOptionsFirst()
{
$options = [
'resource_id' => __CLASS__ . '2',
'lib_options' => [
RedisResource::OPT_SERIALIZER => RedisResource::SERIALIZER_PHP,
],
];

if (getenv('TESTS_ZEND_CACHE_REDIS_HOST') && getenv('TESTS_ZEND_CACHE_REDIS_PORT')) {
$options['server'] = [getenv('TESTS_ZEND_CACHE_REDIS_HOST'), getenv('TESTS_ZEND_CACHE_REDIS_PORT')];
} elseif (getenv('TESTS_ZEND_CACHE_REDIS_HOST')) {
$options['server'] = [getenv('TESTS_ZEND_CACHE_REDIS_HOST')];
}

if (getenv('TESTS_ZEND_CACHE_REDIS_DATABASE')) {
$options['database'] = getenv('TESTS_ZEND_CACHE_REDIS_DATABASE');
}

if (getenv('TESTS_ZEND_CACHE_REDIS_PASSWORD')) {
$options['password'] = getenv('TESTS_ZEND_CACHE_REDIS_PASSWORD');
}

$redisOptions = new Cache\Storage\Adapter\RedisOptions($options);
$storage = new Cache\Storage\Adapter\Redis($redisOptions);

$this->assertInstanceOf('Zend\\Cache\\Storage\\Adapter\\Redis', $storage);
}

public function testRedisSerializer()
{
$this->_storage->addPlugin(new \Zend\Cache\Storage\Plugin\Serializer());
Expand Down Expand Up @@ -185,21 +214,21 @@ public function testGetSetPassword()

public function testGetSetLibOptionsOnExistingRedisResourceInstance()
{
$options = ['serializer', RedisResource::SERIALIZER_PHP];
$options = ['serializer' => RedisResource::SERIALIZER_PHP];
$this->_options->setLibOptions($options);

$value = ['value'];
$key = 'key';
//test if it's still possible to set/get item and if lib serializer works
$this->_storage->setItem($key, $value);

$this->assertEquals(
$value,
$this->_storage->getItem($key),
'Redis should return an array, lib options were not set correctly'
);


$options = ['serializer', RedisResource::SERIALIZER_NONE];
$options = ['serializer' => RedisResource::SERIALIZER_NONE];
$this->_options->setLibOptions($options);
$this->_storage->setItem($key, $value);
//should not serialize array correctly
Expand All @@ -212,7 +241,7 @@ public function testGetSetLibOptionsOnExistingRedisResourceInstance()

public function testGetSetLibOptionsWithCleanRedisResourceInstance()
{
$options = ['serializer', RedisResource::SERIALIZER_PHP];
$options = ['serializer' => RedisResource::SERIALIZER_PHP];
$this->_options->setLibOptions($options);

$redis = new Cache\Storage\Adapter\Redis($this->_options);
Expand All @@ -227,7 +256,7 @@ public function testGetSetLibOptionsWithCleanRedisResourceInstance()
);


$options = ['serializer', RedisResource::SERIALIZER_NONE];
$options = ['serializer' => RedisResource::SERIALIZER_NONE];
$this->_options->setLibOptions($options);
$redis->setItem($key, $value);
//should not serialize array correctly
Expand Down Expand Up @@ -285,7 +314,7 @@ public function testGetSetPersistentId()

public function testOptionsGetSetLibOptions()
{
$options = ['serializer', RedisResource::SERIALIZER_PHP];
$options = ['serializer' => RedisResource::SERIALIZER_PHP];
$this->_options->setLibOptions($options);
$this->assertEquals(
$options,
Expand Down