Skip to content

Commit

Permalink
feat: Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yaraslau-kavaliou committed Jun 28, 2024
1 parent 2ddbcc2 commit 580d49d
Show file tree
Hide file tree
Showing 2 changed files with 200 additions and 47 deletions.
94 changes: 47 additions & 47 deletions common/persistence/class.PhpRedisDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,50 +144,6 @@ protected function callWithRetry($method, array $params, $attempt = 1)
return $result;
}

protected function getPrefix(array $params): ?string
{
$prefix = null;

if (!empty($this->params['prefix'])) {
$prefix = $this->params['prefix'];
}

return $prefix;
}

/**
* @param string|int $key
* @return string|int
*/
protected function prefixKey($key)
{
$prefix = $this->getPrefix($this->params);

if ($prefix === null) {
return $key;
}

return $prefix . ($this->params['prefixSeparator'] ?? self::DEFAULT_PREFIX_SEPARATOR) . $key;
}

protected function prefixKeys(array $keys, bool $keyValueMode = false): array
{
if ($this->getPrefix($this->params) !== null) {
$prefixedKeys = [];
foreach (array_values($keys) as $i => $element) {
if ($keyValueMode) {
$prefixedKeys[] = $i % 2 == 0 ? $this->prefixKey($element) : $element;
} else {
$prefixedKeys[] = $this->prefixKey($element);
}
}

return $prefixedKeys;
}

return $keys;
}

/**
* (non-PHPdoc)
* @see common_persistence_KvDriver::set()
Expand Down Expand Up @@ -257,7 +213,7 @@ public function hGetAll($key)
//Time complexity: O(N)
public function keys($pattern)
{
return $this->callWithRetry('keys', [$pattern]);
return $this->callWithRetry('keys', [$this->prefixKey($pattern)]);
}

//Time complexity: O(1)
Expand All @@ -283,9 +239,9 @@ public function scan(int &$iterator = null, string $pattern = null, int $count =

while ($attempt <= $retry) {
try {
return $this->connection->scan($iterator, $pattern, $count);
return $this->connection->scan($iterator, $this->prefixKey($pattern), $count);
} catch (Exception $exception) {
$this->reconnectOnException($exception, $method, $attempt, $retry);
$this->reconnectOnException($exception, 'scan', $attempt, $retry);
}

$attempt++;
Expand Down Expand Up @@ -330,6 +286,50 @@ public function getConnection()
return $this->connection;
}

protected function getPrefix(array $params): ?string
{
$prefix = null;

if (!empty($this->params['prefix'])) {
$prefix = $this->params['prefix'];
}

return $prefix;
}

/**
* @param string|int|null $key
* @return string|int|null
*/
private function prefixKey($key)
{
$prefix = $this->getPrefix($this->params);

if ($prefix === null) {
return $key;
}

return $prefix . ($this->params['prefixSeparator'] ?? self::DEFAULT_PREFIX_SEPARATOR) . $key;
}

private function prefixKeys(array $keys, bool $keyValueMode = false): array
{
if ($this->getPrefix($this->params) !== null) {
$prefixedKeys = [];
foreach (array_values($keys) as $i => $element) {
if ($keyValueMode) {
$prefixedKeys[] = $i % 2 == 0 ? $this->prefixKey($element) : $element;
} else {
$prefixedKeys[] = $this->prefixKey($element);
}
}

return $prefixedKeys;
}

return $keys;
}

/**
* @return void
* @throws RedisException
Expand Down
153 changes: 153 additions & 0 deletions test/unit/common/persistence/PhpRedisDriverTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
<?php

/**
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; under version 2
* of the License (non-upgradable).
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Copyright (c) 2024 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT);
*/

namespace oat\generis\test\unit\common\persistence;

use common_persistence_PhpRedisDriver;
use PHPUnit\Framework\TestCase;

class PhpRedisDriverTest extends TestCase
{
private common_persistence_PhpRedisDriver $driver;

private object $connection;

public function setUp(): void
{
$driver = new common_persistence_PhpRedisDriver();

$this->connection = new class
{
public array $calls = [];

public function __call($name, $arguments)
{
$this->calls[$name] = $arguments;
if ($name === 'scan') {
return [];
}

return 'data';
}
};

$this->setProperty($driver, 'connection', $this->connection);

$this->driver = $driver;
}

public function testKeyPrefixIsNotAddedIfPrefixIsNotSet()
{
$this->setProperty($this->driver, 'params', ['attempt' => 2]);

$this->driver->set('key', 'value');

$this->assertEquals('key', $this->getLastCallKey('set'));
}

public function testKeyPrefixIsAddedIfPrefixIsSet()
{
$this->setProperty($this->driver, 'params', ['attempt' => 2, 'prefix' => '25']);

$this->driver->set('key1', 'value');

$this->assertEquals('25::key1', $this->getLastCallKey('set'));
}

public function testKeyPrefixHasNotDefaultSeparatorIfSeparatorIsSet()
{
$this->setProperty($this->driver, 'params', [
'attempt' => 2,
'prefix' => '25',
'prefixSeparator' => '-'
]);

$this->driver->set('key2', 'value');

$this->assertEquals('25-key2', $this->getLastCallKey('set'));
}

public function testKeysHavePrefixWithoutKeyValueMode()
{
$this->setProperty($this->driver, 'params', ['attempt' => 2, 'prefix' => '26']);

$this->driver->mGet(['key1', 'key2']);

$this->assertEquals(['26::key1', '26::key2'], $this->getLastCallKey('mGet'));
}

public function testKeysHavePrefixWithKeyValueMode()
{
$this->setProperty($this->driver, 'params', ['attempt' => 2, 'prefix' => '25']);

$this->driver->mSet(['key1', 'value1', 'key2', 'value2']);

$this->assertEquals(['25::key1', 'value1', '25::key2', 'value2'], $this->getLastCallKey('mSet'));
}

public function testKeyPrefixIsAddedToAllMethods()
{
$this->setProperty($this->driver, 'params', ['attempt' => 2, 'prefix' => 25]);

$methods = [
'set' => ['key', 'value'],
'get' => ['key'],
'exists' => ['key'],
'del' => ['key'],
'hmSet' => ['key', 'field'],
'hExists' => ['key', 'field'],
'hSet' => ['key', 'field', 'value'],
'hGet' => ['key', 'field'],
'hDel' => ['key', 'field'],
'keys' => ['key'],
'incr' => ['key'],
'decr' => ['key'],
];

foreach ($methods as $method => $methodParams) {
$this->driver->$method(...$methodParams);
$this->assertEquals('25::key', $this->getLastCallKey($method));
}
}

public function testKeyPrefixIsAddedForScanMethod()
{
$this->setProperty($this->driver, 'params', ['attempt' => 2, 'prefix' => 'pref']);

$iterator = null;
$this->driver->scan($iterator, '*pattern*');

$this->assertEquals('pref::*pattern*', $this->driver->getConnection()->calls['scan'][1]);
}

private function getLastCallKey(string $method)
{
return $this->driver->getConnection()->calls[$method][0];
}

private function setProperty($driver, string $propertyName, $value): void
{
$reflection = new \ReflectionClass($driver);

$property = $reflection->getProperty($propertyName);
$property->setAccessible(true);
$property->setValue($driver, $value);
}
}

0 comments on commit 580d49d

Please sign in to comment.