Skip to content

Commit

Permalink
Merge pull request #13 from chadicus/master
Browse files Browse the repository at this point in the history
Allow InMemoryCache to be constructed with ArrayObject
  • Loading branch information
chadicus authored Sep 24, 2018
2 parents dfc1032 + a7f303c commit 49e1a10
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 10 deletions.
17 changes: 14 additions & 3 deletions src/InMemoryCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace SubjectivePHP\Psr\SimpleCache;

use ArrayObject;
use DateInterval;
use DateTime;
use Psr\SimpleCache\CacheInterface;
Expand All @@ -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.
Expand Down Expand Up @@ -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;
}

Expand Down
29 changes: 22 additions & 7 deletions tests/InMemoryCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ::<private>
*/
final class InMemoryCacheTest extends \PHPUnit\Framework\TestCase
Expand All @@ -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);
}

/**
Expand All @@ -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'));
}

Expand All @@ -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'));
}

Expand Down Expand Up @@ -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()
);
}

/**
Expand Down Expand Up @@ -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());
}

/**
Expand Down

0 comments on commit 49e1a10

Please sign in to comment.