From a7f303c573c3634d26fd07bd10bd94a3124e4f89 Mon Sep 17 00:00:00 2001 From: chadicus Date: Sun, 23 Sep 2018 09:21:13 -0400 Subject: [PATCH] Allow InMemoryCache to be constructed with ArrayObject --- src/InMemoryCache.php | 17 ++++++++++++++--- tests/InMemoryCacheTest.php | 29 ++++++++++++++++++++++------- 2 files changed, 36 insertions(+), 10 deletions(-) diff --git a/src/InMemoryCache.php b/src/InMemoryCache.php index 635033e..ef227d6 100644 --- a/src/InMemoryCache.php +++ b/src/InMemoryCache.php @@ -2,6 +2,7 @@ namespace SubjectivePHP\Psr\SimpleCache; +use ArrayObject; use DateInterval; use DateTime; use Psr\SimpleCache\CacheInterface; @@ -15,9 +16,19 @@ final class InMemoryCache implements CacheInterface use TTLValidatorTrait; /** - * @var array + * @var ArrayObject */ - private $cache = []; + private $cache; + + /** + * Construct a new instance of InMemoryCache. + * + * @param ArrayObject $cache Initial cache container. + */ + public function __construct(ArrayObject $cache = null) + { + $this->cache = $cache ?? new ArrayObject(); + } /** * Fetches a value from the cache. @@ -155,7 +166,7 @@ public function deleteMultiple($keys)//@codingStandardsIgnoreLine Interface does */ public function clear()//@codingStandardsIgnoreLine Interface does not define type-hints or return { - $this->cache = []; + $this->cache->exchangeArray([]); return true; } diff --git a/tests/InMemoryCacheTest.php b/tests/InMemoryCacheTest.php index 2cddc45..ae542d8 100644 --- a/tests/InMemoryCacheTest.php +++ b/tests/InMemoryCacheTest.php @@ -2,11 +2,13 @@ namespace SubjectivePHPTest\Psr\SimpleCache; +use ArrayObject; use DateTime; use SubjectivePHP\Psr\SimpleCache\InMemoryCache; /** * @coversDefaultClass \SubjectivePHP\Psr\SimpleCache\InMemoryCache + * @covers ::__construct * @covers :: */ final class InMemoryCacheTest extends \PHPUnit\Framework\TestCase @@ -16,12 +18,18 @@ final class InMemoryCacheTest extends \PHPUnit\Framework\TestCase */ private $cache; + /** + * @var ArrayObject + */ + private $container; + /** * @return void */ public function setUp() { - $this->cache = new InMemoryCache(); + $this->container = new ArrayObject(); + $this->cache = new InMemoryCache($this->container); } /** @@ -33,7 +41,7 @@ public function setUp() public function get() { $dateTime = new DateTime(); - $this->cache->set('foo', $dateTime); + $this->container['foo'] = ['data' => $dateTime, 'expires' => PHP_INT_MAX]; $this->assertEquals($dateTime, $this->cache->get('foo')); } @@ -57,7 +65,7 @@ public function getKeyNotFound() */ public function getExpired() { - $this->cache->set('foo', new DateTime(), -10); + $this->container['foo'] = ['data' => new DateTime(), 'expires' => -10]; $this->assertNull($this->cache->get('foo')); } @@ -90,7 +98,15 @@ public function setWithIntegerTTL() { $dateTime = new \DateTime(); $this->assertTrue($this->cache->set('foo', $dateTime, 3600)); - $this->assertEquals($dateTime, $this->cache->get('foo')); + $this->assertSame( + [ + 'foo' => [ + 'data' => $dateTime, + 'expires' => time() + 3600, + ], + ], + $this->container->getArrayCopy() + ); } /** @@ -167,9 +183,8 @@ public function clear() $this->cache->set('baz', 'baz'); $this->cache->clear(); - $this->assertNull($this->cache->get('foo')); - $this->assertNull($this->cache->get('bar')); - $this->assertNull($this->cache->get('baz')); + + $this->assertSame([], $this->container->getArrayCopy()); } /**