Skip to content

Commit

Permalink
Merge pull request #3 from kamil-iskierka-westwing-pl/WMS-1703
Browse files Browse the repository at this point in the history
[WMS-1703] Array adapter configuration
  • Loading branch information
crashev authored Mar 1, 2019
2 parents 4958940 + ec415f7 commit f259dae
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 5 deletions.
28 changes: 27 additions & 1 deletion src/Factory/Adapter/PHPArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,42 @@

namespace Cache\Factory\Adapter;

use Cache\Factory\Config\Adapter\PHPArray as Config;

class PHPArray extends AbstractAdapter
{
const ADAPTER_NAMESPACE_TEMPLATE = '\\Cache\\Adapter\\%s\\ArrayCachePool';

protected $cachePoolClassName;

/**
* @inheritdoc
*/
public function make(array $config)
{
$this->cachePoolClassName = $this->getAdapterClassName(self::class);
$cacheDriver = $this->getConfiguredDriver($config);

return $cacheDriver;
}

/**
* @inheritdoc
*/
protected function getConfiguredDriver(array $config)
{
return new \Cache\Adapter\PHPArray\ArrayCachePool();
$cacheLimit = $config[Config::INDEX_LIMIT];
$cacheInitialArray = $config[Config::INDEX_CACHE_ARRAY];

foreach ($cacheInitialArray as $key => $value) {
$cacheInitialArray[$key] = [
0 => $value,
1 => [],
2 => null
];
}

return new $this->cachePoolClassName($cacheLimit, $cacheInitialArray);
}

/**
Expand Down
16 changes: 16 additions & 0 deletions src/Factory/Config/Adapter/PHPArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@

class PHPArray extends AbstractConfig
{

const INDEX_LIMIT = 'limit';

const INDEX_CACHE_ARRAY = 'cache';

/**
* @inheritdoc
*/
Expand All @@ -14,6 +19,17 @@ public function getAdapterConfigTreeBuilder()
$treeBuilder = new TreeBuilder();
$node = $treeBuilder->root($this->getAdapterName());

$node
->children()
->scalarNode(self::INDEX_LIMIT)
->defaultValue(null)
->end()
->arrayNode(self::INDEX_CACHE_ARRAY)
->prototype('scalar')
->end()
->end()
->end();

return $node;
}
}
55 changes: 51 additions & 4 deletions tests/Adapter/PHPArrayAdapterTests.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use Cache\Factory\Adapter\PHPArray;
use Cache\Factory\Config\Adapter\PHPArray as Config;

class PHPArrayAdapterTests extends BaseAdapterTests
{
Expand All @@ -14,15 +15,61 @@ class PHPArrayAdapterTests extends BaseAdapterTests
*/
protected $adapterType = 'PHPArray';

/* @var Cache\Adapter\PHPArray\ArrayCachePool */
protected $arrayCacheItemPool;

public function setUp()
{
parent::setUp();

$arrayAdapterFactory = new PHPArray();
$this->arrayCacheItemPool = $arrayAdapterFactory->make($this->config);
}

/**
* Tests creation of the filesystem cache item pool
*/
public function testMake()
{
$filesystemAdapterFactory = new PHPArray();
$filesystemCacheItemPool = $filesystemAdapterFactory->make($this->config);
$this->assertInstanceOf('Psr\\Cache\\CacheItemPoolInterface', $this->arrayCacheItemPool);
$this->assertInstanceOf('\\Cache\\Adapter\\PHPArray\\ArrayCachePool', $this->arrayCacheItemPool);
}

$this->assertInstanceOf('Psr\\Cache\\CacheItemPoolInterface', $filesystemCacheItemPool);
$this->assertInstanceOf('\\Cache\\Adapter\\PHPArray\\ArrayCachePool', $filesystemCacheItemPool);
/**
* Test cache limit setting
*/
public function testConfigurationOfLimit()
{
// We are creating as many items as limit says
$limit = $this->config[Config::INDEX_LIMIT];
if ($limit === null) {
$this->markTestSkipped('Limit is not set in configuration');
}
for ($i = 1; $i <= $limit; $i++) {
$this->arrayCacheItemPool->set('variable' . $i, 'value');
}

// We are checking if first item exist - it should
$this->assertTrue($this->arrayCacheItemPool->hasItem('variable1'));

// We add one more item
$this->arrayCacheItemPool->set('variable' . $i, 'value');

// We are checking if first item exist - it should not
$this->assertFalse($this->arrayCacheItemPool->hasItem('variable1'));
}

/**
* Test if variables from provided array exists in cache
*/
public function testConfigurationOfInitialArray()
{
$initialCacheArray = $this->config[Config::INDEX_CACHE_ARRAY];
if (empty($initialCacheArray)) {
$this->markTestSkipped('Initial cache array is not set in configuration');
}
foreach ($initialCacheArray as $key => $value) {
$this->assertEquals($this->arrayCacheItemPool->getItem($key)->get(), $value);
}
}
}
2 changes: 2 additions & 0 deletions tests/cache.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ Cache:
path: '/tmp'
memory:
type: PHPArray
limit: 9
cache: { var1: 'value1', var2: 'value2' }

0 comments on commit f259dae

Please sign in to comment.