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
3 changes: 2 additions & 1 deletion src/System/Cache/CacheManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace System\Cache;

use System\Cache\Exceptions\UnsupportedCacheDriverException;
use System\Cache\Storage\ArrayStorage;

class CacheManager implements CacheInterface
Expand Down Expand Up @@ -44,7 +45,7 @@ private function resolve(string $driver_name): CacheInterface
}

if (null === $driver) {
throw new \Exception("Can use driver {$driver_name}.");
throw new UnsupportedCacheDriverException("Can use driver {$driver_name}.");
}

return $this->driver[$driver_name] = $driver;
Expand Down
2 changes: 1 addition & 1 deletion src/System/Cache/Exceptions/CacheException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@

namespace System\Cache\Exceptions;

class CacheException extends \Exception
interface CacheException extends \Throwable
{
}
9 changes: 9 additions & 0 deletions src/System/Cache/Exceptions/InvalidCacheArgumentException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace System\Cache\Exceptions;

class InvalidCacheArgumentException extends \InvalidArgumentException implements CacheException
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace System\Cache\Exceptions;

class UnsupportedCacheDriverException extends \RuntimeException implements CacheException
{
}
6 changes: 4 additions & 2 deletions src/System/Cache/Storage/ApcuStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
namespace System\Cache\Storage;

use System\Cache\CacheInterface;
use System\Cache\Exceptions\InvalidCacheArgumentException;
use System\Cache\Exceptions\UnsupportedCacheDriverException;

class ApcuStorage implements CacheInterface
{
Expand All @@ -13,7 +15,7 @@ public function __construct(
private int $defaultTTL = 3_600,
) {
if (false === static::isSupported()) {
throw new \RuntimeException('APCu extension is not loaded or enabled.');
throw new UnsupportedCacheDriverException('APCu extension is not loaded or enabled.');
}
}

Expand Down Expand Up @@ -116,7 +118,7 @@ public function has(string $key): bool
public function increment(string $key, int $value): int
{
if ($this->has($key) && false === is_int($this->get($key))) {
throw new \InvalidArgumentException('Value increment must be integer.');
throw new InvalidCacheArgumentException('Value increment must be integer.');
}

$result = \apcu_inc($this->prefix . $key, $value, $success);
Expand Down
6 changes: 4 additions & 2 deletions src/System/Cache/Storage/MemcachedStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

use System\Cache\CacheInterface;
use System\Cache\Exceptions\CacheException;
use System\Cache\Exceptions\InvalidCacheArgumentException;
use System\Cache\Exceptions\UnsupportedCacheDriverException;

class MemcachedStorage implements CacheInterface
{
Expand All @@ -22,7 +24,7 @@ public function __construct(
$class = '\Memcached';

if (false === class_exists($class) || false === ($memcached instanceof $class)) {
throw new \InvalidArgumentException('The memcached must be an instance of \Memcached.');
throw new InvalidCacheArgumentException('The memcached must be an instance of \Memcached.');
}

$this->memcached = $memcached;
Expand Down Expand Up @@ -185,7 +187,7 @@ private function call(callable $operation): mixed
try {
return $operation();
} catch (\MemcachedException $e) {
throw new CacheException($e->getMessage(), (int) $e->getCode(), $e);
throw new UnsupportedCacheDriverException($e->getMessage(), (int) $e->getCode(), $e);
}
}

Expand Down
8 changes: 5 additions & 3 deletions src/System/Cache/Storage/PdoStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
namespace System\Cache\Storage;

use System\Cache\CacheInterface;
use System\Cache\Exceptions\InvalidCacheArgumentException;
use System\Cache\Exceptions\UnsupportedCacheDriverException;

class PdoStorage implements CacheInterface
{
Expand All @@ -19,11 +21,11 @@ public function __construct(
/** @var class-string $class */
$class = '\PDO';
if (false === extension_loaded('pdo') || false === class_exists($class)) {
throw new \RuntimeException('The PDO extension is required to use PdoStorage.');
throw new UnsupportedCacheDriverException('The PDO extension is required to use PdoStorage.');
}

if (false === ($this->pdo instanceof $class)) {
throw new \InvalidArgumentException('The pdo must be an instance of \PDO.');
throw new InvalidCacheArgumentException('The pdo must be an instance of \PDO.');
}
}

Expand Down Expand Up @@ -158,7 +160,7 @@ public function increment(string $key, int $value): int
$expiration = (int) $row['expiration'];

if (false === is_int($current)) {
throw new \InvalidArgumentException('Value increment must be integer.');
throw new InvalidCacheArgumentException('Value increment must be integer.');
}

$new = $current + $value;
Expand Down
17 changes: 17 additions & 0 deletions tests/Cache/CacheManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use PHPUnit\Framework\TestCase;
use System\Cache\CacheInterface;
use System\Cache\CacheManager;
use System\Cache\Exceptions\UnsupportedCacheDriverException;
use System\Cache\Storage\ArrayStorage;

class CacheManagerTest extends TestCase
Expand All @@ -30,4 +31,20 @@ public function testDriver(): void
$this->assertTrue($cache->driver('array2')->set('key1', 'value1'));
$this->assertEquals('value1', $cache->driver('array2')->get('key1'));
}

/**
* @test
*
* @testdox It throws UnsupportedCacheDriverException if driver cannot be resolved
*
* @covers \System\Cache\CacheManager::resolve
*/
public function itThrowsUnsupportedCacheDriverExceptionWhenDriverCannotBeResolved(): void
{
$cache = new CacheManager();
$cache->setDriver('invalid', fn () => null);

$this->expectException(UnsupportedCacheDriverException::class);
$cache->driver('invalid');
}
}
33 changes: 33 additions & 0 deletions tests/Cache/Storage/ApcuStorageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
namespace System\Text\Cache\Storage;

use PHPUnit\Framework\TestCase;
use System\Cache\Exceptions\InvalidCacheArgumentException;
use System\Cache\Exceptions\UnsupportedCacheDriverException;
use System\Cache\Storage\ApcuStorage;

/**
Expand Down Expand Up @@ -233,4 +235,35 @@ public function itCanRemember(): void
$value = $this->storage->remember('key1', 1, fn (): string => 'value2');
$this->assertEquals('value1', $value);
}

/**
* @test
*
* @testdox It throws UnsupportedCacheDriverException if APCu is not supported
*
* @covers \System\Cache\Storage\ApcuStorage::__construct
*/
public function itThrowsUnsupportedCacheDriverExceptionWhenApcuIsNotSupported(): void
{
if (ApcuStorage::isSupported()) {
$this->markTestSkipped('APCu extension is loaded or enabled for CLI.');
}

$this->expectException(UnsupportedCacheDriverException::class);
new ApcuStorage('test_');
}

/**
* @test
*
* @testdox It throws InvalidCacheArgumentException if increment value is not integer
*
* @covers \System\Cache\Storage\ApcuStorage::increment
*/
public function itThrowsInvalidCacheArgumentExceptionWhenIncrementValueIsNotInteger(): void
{
$this->storage->set('key', 'not an integer');
$this->expectException(InvalidCacheArgumentException::class);
$this->storage->increment('key', 1);
}
}
33 changes: 33 additions & 0 deletions tests/Cache/Storage/MemcachedStorageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
namespace System\Test\Cache\Storage;

use PHPUnit\Framework\TestCase;
use System\Cache\Exceptions\InvalidCacheArgumentException;
use System\Cache\Exceptions\UnsupportedCacheDriverException;
use System\Cache\Storage\MemcachedConnector;
use System\Cache\Storage\MemcachedStorage;

Expand Down Expand Up @@ -241,4 +243,35 @@ public function itNormalizesInvalidKeys(): void
$this->assertTrue($this->storage->set($longKey, 'value2'));
$this->assertEquals('value2', $this->storage->get($longKey));
}

/**
* @test
*
* @testdox It throws InvalidCacheArgumentException if memcached is not an instance of \Memcached
*
* @covers \System\Cache\Storage\MemcachedStorage::__construct
*/
public function itThrowsInvalidCacheArgumentExceptionWhenMemcachedIsNotInstanceOfMemcached(): void
{
$this->expectException(InvalidCacheArgumentException::class);
new MemcachedStorage(new \stdClass());
}

/**
* @test
*
* @testdox It throws UnsupportedCacheDriverException if MemcachedException is thrown
*
* @covers \System\Cache\Storage\MemcachedStorage::get
*/
public function itThrowsUnsupportedCacheDriverExceptionWhenMemcachedExceptionIsThrown(): void
{
$memcachedMock = $this->createMock(\Memcached::class);
$memcachedMock->method('get')->willThrowException(new \MemcachedException('Test exception'));

$storage = new MemcachedStorage($memcachedMock);

$this->expectException(UnsupportedCacheDriverException::class);
$storage->get('key');
}
}
15 changes: 15 additions & 0 deletions tests/Cache/Storage/PdoStorageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace System\Test\Cache\Storage;

use PHPUnit\Framework\TestCase;
use System\Cache\Exceptions\InvalidCacheArgumentException;
use System\Cache\Storage\PdoStorage;

/**
Expand Down Expand Up @@ -167,4 +168,18 @@ public function itCanHandleMultipleCacheOperations()
$this->assertNull($this->storage->get('a'));
$this->assertNull($this->storage->get('b'));
}

/**
* @test
*
* @testdox It throws InvalidCacheArgumentException if increment value is not integer
*
* @covers \System\Cache\Storage\PdoStorage::increment
*/
public function itThrowsInvalidCacheArgumentExceptionWhenIncrementValueIsNotInteger(): void
{
$this->storage->set('key', 'not an integer');
$this->expectException(InvalidCacheArgumentException::class);
$this->storage->increment('key', 1);
}
}
Loading