From d992c767e83e83ebc37ae31e5fb4d5ab60eb86af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A8ir=20Noordermeer?= Date: Sun, 25 Mar 2018 12:48:54 +0200 Subject: [PATCH] Improve code, fix filesystem test failing after first run --- composer.json | 4 +-- src/Adapter/CacheAdapterInterface.php | 2 +- src/Adapter/FilesystemAdapter.php | 29 +++++++++++---------- src/Adapter/MemoryAdapter.php | 13 ++++----- src/Adapter/NamespaceAdapter.php | 15 ++++++----- src/Adapter/NullAdapter.php | 4 +-- src/Adapter/PredisAdapter.php | 2 +- src/Serializer/JsonSerializer.php | 4 +-- test/AbstractTestCase.php | 7 +++-- test/Unit/Adapter/FilesystemAdapterTest.php | 22 +++++++++++++++- test/Unit/Adapter/MemoryAdapterTest.php | 5 +++- test/Unit/Serializer/JsonSerializerTest.php | 4 +-- 12 files changed, 68 insertions(+), 43 deletions(-) diff --git a/composer.json b/composer.json index d331dff..785a058 100644 --- a/composer.json +++ b/composer.json @@ -1,13 +1,13 @@ { "name": "iqparts/cache", "type": "library", + "description": "Simple cache library with Redis and Filesystem adapters.", "license": "MIT", "require": { "predis/predis": "^1.1" }, "require-dev": { - "phpunit/phpunit": "^6.3", - "phpstan/phpstan": "^0.9.1" + "phpunit/phpunit": "^6.3" }, "autoload": { "psr-4": { diff --git a/src/Adapter/CacheAdapterInterface.php b/src/Adapter/CacheAdapterInterface.php index 079d923..8de2431 100644 --- a/src/Adapter/CacheAdapterInterface.php +++ b/src/Adapter/CacheAdapterInterface.php @@ -17,7 +17,7 @@ interface CacheAdapterInterface * @param int|null $ttl * @return void */ - public function set(string $key, $value, int $ttl = null); + public function set(string $key, $value, int $ttl = null): void; /** * @param string $key diff --git a/src/Adapter/FilesystemAdapter.php b/src/Adapter/FilesystemAdapter.php index 7371050..507bea5 100644 --- a/src/Adapter/FilesystemAdapter.php +++ b/src/Adapter/FilesystemAdapter.php @@ -49,21 +49,21 @@ class FilesystemAdapter implements CacheAdapterInterface * @param SerializerInterface $serializer * @param int|null $chmod * @param int|null $ttl - * @param string|null $directorySeperator + * @param string|null $directorySeparator */ public function __construct( string $directory, SerializerInterface $serializer, int $chmod = null, int $ttl = null, - string $directorySeperator = null + string $directorySeparator = null ) { $this->indexLocation = $directory . '/' . md5($directory) . 'index'; $this->ttlLocation = $directory . '/' . md5($directory) . 'ttl'; $this->directory = $directory; $this->serializer = $serializer; - $this->directorySeparator = $directorySeperator; + $this->directorySeparator = $directorySeparator; $this->chmod = $chmod; $this->ttl = $ttl; @@ -106,7 +106,7 @@ class FilesystemAdapter implements CacheAdapterInterface * @param int|null $ttl * @return void */ - public function set(string $key, $value, int $ttl = null) + public function set(string $key, $value, int $ttl = null): void { $file = $this->getFilename($key); file_put_contents($file, $this->serializer->serialize($value)); @@ -136,8 +136,7 @@ class FilesystemAdapter implements CacheAdapterInterface if ($this->exists($file)) { unlink($file); } - unset($this->index[$key]); - unset($this->timeToLive[$key]); + unset($this->index[$key], $this->timeToLive[$key]); $this->saveIndex(); } } @@ -175,7 +174,7 @@ class FilesystemAdapter implements CacheAdapterInterface /** * @param $pattern */ - private function deleteGlob($pattern) + private function deleteGlob($pattern): void { foreach ($this->index as $key => $value) { if (fnmatch($pattern, $key)) { @@ -188,7 +187,7 @@ class FilesystemAdapter implements CacheAdapterInterface * @param $file * @return bool */ - private function exists(string $file) + private function exists(string $file): bool { return file_exists($file); } @@ -197,9 +196,9 @@ class FilesystemAdapter implements CacheAdapterInterface * @param $key * @return string */ - private function getFilename(string $key) + private function getFilename(string $key): string { - list($directory, $file) = $this->getDirectoryAndFile($key); + [$directory, $file] = $this->getDirectoryAndFile($key); return $directory . '/' . md5($file); } @@ -207,7 +206,7 @@ class FilesystemAdapter implements CacheAdapterInterface * @param string $key * @return array */ - private function getDirectoryAndFile(string $key) + private function getDirectoryAndFile(string $key): array { $directory = $this->directory; if ($this->directorySeparator !== null) { @@ -224,17 +223,19 @@ class FilesystemAdapter implements CacheAdapterInterface /** * @param $directory */ - private function createSubDirectoryIfNotExists($directory) + private function createSubDirectoryIfNotExists($directory): void { if (file_exists($directory) === false) { - mkdir($directory); + if (!mkdir($directory) && !is_dir($directory)) { + throw new \RuntimeException(sprintf('Directory "%s" was not created', $directory)); + } if ($this->chmod !== null) { chmod($directory, $this->chmod); } } } - private function saveIndex() + private function saveIndex(): void { file_put_contents($this->indexLocation, json_encode($this->index)); file_put_contents($this->ttlLocation, json_encode($this->timeToLive)); diff --git a/src/Adapter/MemoryAdapter.php b/src/Adapter/MemoryAdapter.php index 490a2a8..6452a53 100644 --- a/src/Adapter/MemoryAdapter.php +++ b/src/Adapter/MemoryAdapter.php @@ -32,8 +32,7 @@ final class MemoryAdapter implements CacheAdapterInterface return $this->data[$key]; } - unset($this->timeToLive[$key]); - unset($this->data[$key]); + unset($this->timeToLive[$key], $this->data[$key]); return null; } @@ -43,7 +42,7 @@ final class MemoryAdapter implements CacheAdapterInterface * @param int|null $ttl * @return void */ - public function set(string $key, $value, int $ttl = null) + public function set(string $key, $value, int $ttl = null): void { $this->data[$key] = $value; if ($ttl !== null) { @@ -53,13 +52,11 @@ final class MemoryAdapter implements CacheAdapterInterface /** * @param string $key - * @return mixed + * @return void */ - public function delete(string $key) + public function delete(string $key): void { - unset($this->data[$key]); - unset($this->timeToLive[$key]); - return 1; + unset($this->data[$key], $this->timeToLive[$key]); } /** diff --git a/src/Adapter/NamespaceAdapter.php b/src/Adapter/NamespaceAdapter.php index 2f00af7..2c2d3a4 100644 --- a/src/Adapter/NamespaceAdapter.php +++ b/src/Adapter/NamespaceAdapter.php @@ -48,7 +48,7 @@ final class NamespaceAdapter implements CacheAdapterInterface * @param string $key * @return void */ - public function delete(string $key) + public function delete(string $key): void { $this->cacheAdapter->delete($this->namespace . $key); } @@ -57,19 +57,20 @@ final class NamespaceAdapter implements CacheAdapterInterface * @param string $key * @return array */ - public function keys($key = '*') + public function keys($key = '*'): array { if (substr($key, -1) !== '*') { - $key = $key . '*'; + $key .= '*'; } + /** @var string[] $keys */ $keys = $this->cacheAdapter->keys($this->namespace . $key); $allowed = []; - $len = strlen($this->namespace); + $len = \strlen($this->namespace); - foreach ($keys as $key) { - if (substr($key, 0, $len) === $this->namespace) { - $allowed[] = substr($key, $len); + foreach ($keys as $cacheKey) { + if (0 === strpos($cacheKey, $this->namespace)) { + $allowed[] = substr($cacheKey, $len); } } diff --git a/src/Adapter/NullAdapter.php b/src/Adapter/NullAdapter.php index fdc7503..5aaa653 100644 --- a/src/Adapter/NullAdapter.php +++ b/src/Adapter/NullAdapter.php @@ -20,7 +20,7 @@ final class NullAdapter implements CacheAdapterInterface * @param int|null $ttl * @return void */ - public function set(string $key, $value, int $ttl = null) + public function set(string $key, $value, int $ttl = null): void { } @@ -28,7 +28,7 @@ final class NullAdapter implements CacheAdapterInterface * @param string $key * @return void */ - public function delete(string $key) + public function delete(string $key): void { } diff --git a/src/Adapter/PredisAdapter.php b/src/Adapter/PredisAdapter.php index a1667cb..dd92a80 100644 --- a/src/Adapter/PredisAdapter.php +++ b/src/Adapter/PredisAdapter.php @@ -48,7 +48,7 @@ final class PredisAdapter implements CacheAdapterInterface * @param $value * @param int|null $ttl TTL in seconds */ - public function set(string $key, $value, int $ttl = null) + public function set(string $key, $value, int $ttl = null): void { if ($ttl !== null && $ttl !== 0) { $this->client->setex($key, $ttl ?? $this->defaultTtl, $this->serializer->serialize($value)); diff --git a/src/Serializer/JsonSerializer.php b/src/Serializer/JsonSerializer.php index d9cc997..a5eabbb 100644 --- a/src/Serializer/JsonSerializer.php +++ b/src/Serializer/JsonSerializer.php @@ -4,9 +4,9 @@ namespace IQParts\Cache\Serializer; final class JsonSerializer implements SerializerInterface { - public function serialize($input) + public function serialize($input): string { - if (is_array($input)) { + if (\is_array($input)) { return json_encode($input); } diff --git a/test/AbstractTestCase.php b/test/AbstractTestCase.php index daae177..03dab81 100644 --- a/test/AbstractTestCase.php +++ b/test/AbstractTestCase.php @@ -6,11 +6,14 @@ use PHPUnit\Framework\TestCase; abstract class AbstractTestCase extends TestCase { - public function getTmpDirectory() + /** + * @return string + */ + public function getTmpDirectory(): string { $location = __DIR__ . '/../tmp'; if (!file_exists($location)) { - if (!mkdir($location)) { + if (!mkdir($location) && !is_dir($location)) { throw new \RuntimeException('Could not create directory: ' . $location); } chmod($location, 0777); diff --git a/test/Unit/Adapter/FilesystemAdapterTest.php b/test/Unit/Adapter/FilesystemAdapterTest.php index f5cef8e..b4295e5 100644 --- a/test/Unit/Adapter/FilesystemAdapterTest.php +++ b/test/Unit/Adapter/FilesystemAdapterTest.php @@ -6,6 +6,8 @@ namespace IQParts\CacheTest\Unit\Adapter; use IQParts\Cache\Adapter\FilesystemAdapter; use IQParts\Cache\Serializer\JsonSerializer; use IQParts\CacheTest\AbstractTestCase; +use RecursiveDirectoryIterator; +use RecursiveIteratorIterator; final class FilesystemAdapterTest extends AbstractTestCase { @@ -27,7 +29,7 @@ final class FilesystemAdapterTest extends AbstractTestCase $adapter->set('ttl', 'b', 200); $this->assertTrue($adapter->ttl('ttl') > 0); - $this->assertTrue($adapter->ttl('b') === $adapter::NO_TTL); + $this->assertSame($adapter->ttl('b'), $adapter::NO_TTL); $adapter->set('a-keys', 'a'); $adapter->set('b-keys', 'a'); @@ -118,4 +120,22 @@ final class FilesystemAdapterTest extends AbstractTestCase $this->assertEquals('a', $adapter->get('a/b')); $adapter->delete('a/b'); } + + protected function tearDown() + { + parent::tearDown(); + $files = new RecursiveIteratorIterator( + new RecursiveDirectoryIterator($this->getTmpDirectory(), RecursiveDirectoryIterator::SKIP_DOTS), + RecursiveIteratorIterator::CHILD_FIRST + ); + + foreach ($files as $file) { + if ($file->isDir()) { + rmdir($file->getRealPath()); + } else { + unlink($file->getRealPath()); + } + } + } + } \ No newline at end of file diff --git a/test/Unit/Adapter/MemoryAdapterTest.php b/test/Unit/Adapter/MemoryAdapterTest.php index ff1420e..62f0768 100644 --- a/test/Unit/Adapter/MemoryAdapterTest.php +++ b/test/Unit/Adapter/MemoryAdapterTest.php @@ -8,6 +8,9 @@ use IQParts\CacheTest\AbstractTestCase; final class MemoryAdapterTest extends AbstractTestCase { + /** + * + */ public function testAdapter() { $adapter = new MemoryAdapter(); @@ -25,7 +28,7 @@ final class MemoryAdapterTest extends AbstractTestCase $adapter->set('a', 'b', 200); $this->assertTrue($adapter->ttl('a') > 0); - $this->assertTrue($adapter->ttl('c') === MemoryAdapter::NO_TTL); + $this->assertSame($adapter->ttl('c'), MemoryAdapter::NO_TTL); } public function testTtl() diff --git a/test/Unit/Serializer/JsonSerializerTest.php b/test/Unit/Serializer/JsonSerializerTest.php index 3b3c32c..01e3bde 100644 --- a/test/Unit/Serializer/JsonSerializerTest.php +++ b/test/Unit/Serializer/JsonSerializerTest.php @@ -10,10 +10,10 @@ final class JsonSerializerTest extends AbstractTestCase public function testSerialize() { $serializer = new JsonSerializer(); - $string = "myString"; + $string = 'myString'; $array = [0 => '1', 1 => '2', 2 => '3']; - $this->assertTrue(is_string($serializer->serialize($array))); + $this->assertInternalType('string', $serializer->serialize($array)); $this->assertEquals($string, $serializer->serialize($string)); $this->assertEquals($array, $serializer->deserialize($serializer->serialize($array)));