diff --git a/composer.json b/composer.json index 0580651..5a2cb92 100644 --- a/composer.json +++ b/composer.json @@ -12,7 +12,8 @@ "php": ">=7.4", "ext-ldap": ">=7.4", "phpmailer/phpmailer": "^6.5.0", - "symfony/cache": "^v5.4.42" + "symfony/cache": "^v5.4.42", + "predis/predis": "^v2.2.2" }, "require-dev": { "phpunit/phpunit": ">=8", diff --git a/src/Ltb/Cache.php b/src/Ltb/Cache/Cache.php similarity index 83% rename from src/Ltb/Cache.php rename to src/Ltb/Cache/Cache.php index 5e39477..30df9d4 100644 --- a/src/Ltb/Cache.php +++ b/src/Ltb/Cache/Cache.php @@ -1,30 +1,10 @@ -cache = new FilesystemAdapter( - $namespace, - $defaultLifetime, - $directory - ); - - // Clean cache from expired entries - $this->cache->prune(); - - } - # Generate a cache entry containing a token, # expiring after $cache_form_expiration seconds function generate_form_token($cache_form_expiration) diff --git a/src/Ltb/Cache/FileCache.php b/src/Ltb/Cache/FileCache.php new file mode 100644 index 0000000..2791a25 --- /dev/null +++ b/src/Ltb/Cache/FileCache.php @@ -0,0 +1,27 @@ +cache = new FilesystemAdapter( + $namespace, + $defaultLifetime, + $directory + ); + + // Clean cache from expired entries + $this->cache->prune(); + + } + +} + +?> diff --git a/src/Ltb/Cache/RedisCache.php b/src/Ltb/Cache/RedisCache.php new file mode 100644 index 0000000..edbd81c --- /dev/null +++ b/src/Ltb/Cache/RedisCache.php @@ -0,0 +1,26 @@ +cache = new RedisAdapter( + $redis_connection, + $namespace, + $defaultLifetime, + ); + + } + +} + +?> diff --git a/tests/Ltb/CacheTest.php b/tests/Ltb/CacheTest.php index b357d2d..a1973bf 100644 --- a/tests/Ltb/CacheTest.php +++ b/tests/Ltb/CacheTest.php @@ -5,21 +5,10 @@ final class CacheTest extends \Mockery\Adapter\Phpunit\MockeryTestCase { - public function test_construct(): void - { - $cacheInstance = new \Ltb\Cache( - "testCache", - 0, - null - ); - $this->assertTrue($cacheInstance->cache instanceof Symfony\Component\Cache\Adapter\FilesystemAdapter, "Error while initializing cache object"); - } - - public function test_generate_form_token(): void { - $cacheInstance = new \Ltb\Cache( + $cacheInstance = new \Ltb\Cache\FileCache( "testCache", 0, null @@ -65,7 +54,7 @@ public function test_verify_form_token_ok(): void $generated_token = "e712b08e55f8977e2b9ecad35d5180ed24345e76607413411e90df66b9538fa1"; - $cacheInstance = new \Ltb\Cache( + $cacheInstance = new \Ltb\Cache\FileCache( "testCache", 0, null @@ -102,7 +91,7 @@ public function test_verify_form_token_ko(): void $generated_token = "e712b08e55f8977e2b9ecad35d5180ed24345e76607413411e90df66b9538fa1"; - $cacheInstance = new \Ltb\Cache( + $cacheInstance = new \Ltb\Cache\FileCache( "testCache", 0, null @@ -137,7 +126,7 @@ public function test_get_token_ok(): void $tokenid = "e712b08e55f8977e2b9ecad35d5180ed24345e76607413411e90df66b9538fa1"; $token_content = "test"; - $cacheInstance = new \Ltb\Cache( + $cacheInstance = new \Ltb\Cache\FileCache( "testCache", 0, null @@ -172,7 +161,7 @@ public function test_get_token_ko(): void $tokenid = "e712b08e55f8977e2b9ecad35d5180ed24345e76607413411e90df66b9538fa1"; $token_content = "test"; - $cacheInstance = new \Ltb\Cache( + $cacheInstance = new \Ltb\Cache\FileCache( "testCache", 0, null @@ -211,7 +200,7 @@ public function test_save_token_new(): void ]; $cache_token_expiration = 3600; - $cacheInstance = new \Ltb\Cache( + $cacheInstance = new \Ltb\Cache\FileCache( "testCache", 0, null @@ -253,7 +242,7 @@ public function test_save_token_existing(): void ]; $cache_token_expiration = 3600; - $cacheInstance = new \Ltb\Cache( + $cacheInstance = new \Ltb\Cache\FileCache( "testCache", 0, null @@ -294,7 +283,7 @@ public function test_save_token_existing_noexpiration(): void 'par2' => 'val2' ]; - $cacheInstance = new \Ltb\Cache( + $cacheInstance = new \Ltb\Cache\FileCache( "testCache", 0, null diff --git a/tests/Ltb/FileCacheTest.php b/tests/Ltb/FileCacheTest.php new file mode 100644 index 0000000..123c223 --- /dev/null +++ b/tests/Ltb/FileCacheTest.php @@ -0,0 +1,18 @@ +assertTrue($cacheInstance->cache instanceof Symfony\Component\Cache\Adapter\FilesystemAdapter, "Error while initializing cache object"); + } + +} diff --git a/tests/Ltb/RedisCacheTest.php b/tests/Ltb/RedisCacheTest.php new file mode 100644 index 0000000..f82af58 --- /dev/null +++ b/tests/Ltb/RedisCacheTest.php @@ -0,0 +1,38 @@ +shouldreceive('createConnection') + ->with( $redis_url ) + ->andReturn( $redis_connection ); + + $redisAdapterMock->shouldReceive('__construct') + ->once() + ->with( + $redis_connection, + $namespace, + $defaultLifetime + ); + + $cacheInstance = new \Ltb\Cache\RedisCache( + $redis_url, + $namespace, + $defaultLifetime + ); + $this->assertTrue($cacheInstance->cache instanceof Symfony\Component\Cache\Adapter\RedisAdapter, "Error while initializing Redis cache object"); + } + + +}