Skip to content

Commit

Permalink
fix(cr): introduce CacheManagerException and update error handling in…
Browse files Browse the repository at this point in the history
… CacheManagerBase
  • Loading branch information
devmiguelangel committed Dec 18, 2024
1 parent 88e6e76 commit 5792fac
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 5 deletions.
4 changes: 2 additions & 2 deletions ProcessMaker/Cache/CacheManagerBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function __construct()
public function getKeysByPattern(string $pattern): array
{
if (!in_array($this->connection, self::AVAILABLE_CONNECTIONS)) {
throw new Exception('`getKeysByPattern` method only supports Redis connections.');
throw new CacheManagerException('`getKeysByPattern` method only supports Redis connections.');
}

try {
Expand All @@ -54,7 +54,7 @@ public function getKeysByPattern(string $pattern): array
// Filter keys by pattern
return array_filter($keys, fn ($key) => preg_match('/' . $pattern . '/', $key));
} catch (Exception $e) {
Log::info('CacheABC' . $e->getMessage());
Log::info('CacheManagerBase: ' . $e->getMessage());
}

return [];
Expand Down
9 changes: 9 additions & 0 deletions ProcessMaker/Cache/CacheManagerException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace ProcessMaker\Cache;

use Exception;

class CacheManagerException extends Exception
{
}
1 change: 1 addition & 0 deletions ProcessMaker/Cache/Settings/SettingCacheManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ public function clear(): bool
*/
public function clearBy(string $pattern): void
{
dump($this->connection);
if ($this->connection !== 'cache_settings') {
throw new SettingCacheException('The cache driver must be Redis.');
}
Expand Down
5 changes: 3 additions & 2 deletions tests/Feature/Cache/CacheManagerBaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;
use ProcessMaker\Cache\CacheManagerBase;
use ProcessMaker\Cache\CacheManagerException;
use Tests\TestCase;

class CacheManagerBaseTest extends TestCase
Expand Down Expand Up @@ -75,7 +76,7 @@ public function testGetKeysByPatternWithInvalidConnection()

$this->cacheManagerBase = $this->getMockForAbstractClass(CacheManagerBase::class);

$this->expectException(Exception::class);
$this->expectException(CacheManagerException::class);
$this->expectExceptionMessage('`getKeysByPattern` method only supports Redis connections.');

$this->cacheManagerBase->getKeysByPattern('pattern');
Expand All @@ -97,7 +98,7 @@ public function testGetKeysByPatternWithExceptionDuringKeyRetrieval()
->andThrow(new Exception('Redis error'));

Log::shouldReceive('info')
->with('CacheABC' . 'Redis error')
->with('CacheManagerBase: ' . 'Redis error')
->once();

$result = $this->cacheManagerBase->getKeysByPattern($pattern);
Expand Down
2 changes: 1 addition & 1 deletion tests/Feature/Cache/SettingCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ public function testTryClearByPatternWithNonRedisDriver()
{
config()->set('cache.default', 'array');

$this->expectException(\Exception::class);
$this->expectException(SettingCacheException::class);
$this->expectExceptionMessage('The cache driver must be Redis.');

\SettingCache::clearBy('pattern');
Expand Down

0 comments on commit 5792fac

Please sign in to comment.