Skip to content
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
1 change: 1 addition & 0 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ jobs:
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-version }}
extensions: redis

- name: Set composer cache directory
id: composer-cache
Expand Down
5 changes: 4 additions & 1 deletion src/Driver/Redis/Exception/RedisQueryException.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ class RedisQueryException extends RedisModelException
*/
public static function checkConnection(Redis $redis): void
{
// getLastError() returns null if there is no error (or false if there is no connection)
$error = $redis->getLastError();
if ($error !== false) {
if (is_string($error)) {
// the error is kept until it's cleared, so it would be reported again for the next query
$redis->clearLastError();
throw new static("Redis Query Error: " . $error);
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/Driver/Redis/Redis.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ public function save(ModelInterface $model): bool
$this->connect();
$key = $this->generateCacheKey($model::class, $model->getId());
try {
$this->connection->clearLastError();
$this->connection->set($key, json_encode($model), $model->getCacheTime());
RedisQueryException::checkConnection($this->connection);
} catch (RedisException $e) {
Expand All @@ -141,6 +142,7 @@ public function get(string $modelClass, mixed $id, ?ModelInterface $model = null

$this->connect();
try {
$this->connection->clearLastError();
$rawData = $this->connection->get($this->generateCacheKey($modelClass, $id));
RedisQueryException::checkConnection($this->connection);
} catch (RedisException $e) {
Expand Down Expand Up @@ -178,6 +180,7 @@ public function delete(ModelInterface $model): bool
$this->connect();
$key = $this->generateCacheKey($model::class, $model->getId());
try {
$this->connection->clearLastError();
$this->connection->del($key);
RedisQueryException::checkConnection($this->connection);
} catch (RedisException $e) {
Expand Down
63 changes: 63 additions & 0 deletions test/tests/RedisQueryExceptionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php /** @noinspection PhpComposerExtensionStubsInspection */

namespace Aternos\Model\Test\Tests;

use Aternos\Model\Driver\Redis\Exception\RedisQueryException;
use PHPUnit\Framework\Attributes\RequiresPhpExtension;
use PHPUnit\Framework\TestCase;
use Redis;

#[RequiresPhpExtension('redis')]
class RedisQueryExceptionTest extends TestCase
{
protected function createRedis(?string $lastError): Redis
{
return new class($lastError) extends Redis {
public bool $cleared = false;

public function __construct(protected ?string $lastError)
{
parent::__construct();
}

public function getLastError(): ?string
{
return $this->lastError;
}

public function clearLastError(): bool
{
$this->cleared = true;
$this->lastError = null;
return true;
}
};
}

public function testNoErrorDoesNotThrow(): void
{
$redis = $this->createRedis(null);
RedisQueryException::checkConnection($redis);
$this->assertFalse($redis->cleared);
}

public function testErrorThrows(): void
{
$this->expectException(RedisQueryException::class);
$this->expectExceptionMessage("Redis Query Error: ERR something went wrong");
RedisQueryException::checkConnection($this->createRedis("ERR something went wrong"));
}

public function testErrorIsClearedAfterThrowing(): void
{
$redis = $this->createRedis("ERR something went wrong");
try {
RedisQueryException::checkConnection($redis);
$this->fail("Expected RedisQueryException");
} catch (RedisQueryException) {
}

$this->assertTrue($redis->cleared);
RedisQueryException::checkConnection($redis);
}
}
Loading