Skip to content

Commit

Permalink
Merge pull request #2 from kamil-iskierka-westwing-pl/WMS-1703
Browse files Browse the repository at this point in the history
[WMS-1703] Create PHPArray adapter to cache-factory library
  • Loading branch information
crashev authored Feb 26, 2019
2 parents 2d4fa2e + 8306da5 commit 4958940
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 30 deletions.
28 changes: 28 additions & 0 deletions src/Factory/Adapter/PHPArray.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Cache\Factory\Adapter;

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

/**
* @inheritdoc
*/
protected function getConfiguredDriver(array $config)
{
return new \Cache\Adapter\PHPArray\ArrayCachePool();
}

/**
* @inheritdoc
*/
protected function getAdapterClassName($adapterClassName)
{
$adapterClassName = str_replace(__NAMESPACE__, '', $adapterClassName);
$adapterClassName = str_replace('\\', '', $adapterClassName);
$adapterClassName = sprintf(self::ADAPTER_NAMESPACE_TEMPLATE, $adapterClassName);

return $adapterClassName;
}
}
19 changes: 19 additions & 0 deletions src/Factory/Config/Adapter/PHPArray.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Cache\Factory\Config\Adapter;

use Symfony\Component\Config\Definition\Builder\TreeBuilder;

class PHPArray extends AbstractConfig
{
/**
* @inheritdoc
*/
public function getAdapterConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$node = $treeBuilder->root($this->getAdapterName());

return $node;
}
}
58 changes: 58 additions & 0 deletions tests/Adapter/BaseAdapterTests.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

use Cache\Factory\Config\Adapter\AdapterInterface as Config;
use Cache\Factory\Config\Loader;

abstract class BaseAdapterTests extends PHPUnit_Framework_TestCase
{
/**
* @var string Name of the adapter in the configuration
*/
protected $adapterName;

/**
* @var string Type of the cache pool adapter
*/
protected $adapterType;

/**
* @var string Path to the config file
*/
protected $configFile;

/**
* @var array
*/
protected $config;

/**
* Sets the config file
*/
protected function setUp()
{
if (!isset($this->adapterName)) {
throw new Exception(
sprintf('Provide $adapterName in %s', (new ReflectionClass($this))->getFileName())
);
} elseif (!isset($this->adapterType)) {
throw new Exception(
sprintf('Provide $adapterType in %s', (new ReflectionClass($this))->getFileName())
);
}

$this->configFile = __DIR__ . '/../cache.yml';

$configLoader = new Loader();
$config = $configLoader->load($this->configFile);

$configLoader->setAdapterName($this->adapterType);

$processedConfiguration = $configLoader->process($this->adapterName, $config);
$this->config = $processedConfiguration[Config::INDEX_ADAPTER][$this->adapterName];
}

/**
* Tests creation of the filesystem cache item pool
*/
abstract public function testMake();
}
30 changes: 1 addition & 29 deletions tests/Adapter/FilesystemAdapterTests.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
<?php

use Cache\Factory\Adapter\Filesystem;
use Cache\Factory\Config\Adapter\AdapterInterface as Config;
use Cache\Factory\Config\Loader;

class FilesystemAdapterTests extends PHPUnit_Framework_TestCase
class FilesystemAdapterTests extends BaseAdapterTests
{
/**
* @var string Name of the adapter in the configuration
Expand All @@ -16,32 +14,6 @@ class FilesystemAdapterTests extends PHPUnit_Framework_TestCase
*/
protected $adapterType = 'Filesystem';

/**
* @var string Path to the config file
*/
protected $configFile;

/**
* @var array
*/
protected $config;

/**
* Sets the config file
*/
protected function setUp()
{
$this->configFile = __DIR__ . '/../cache.yml';

$configLoader = new Loader();
$config = $configLoader->load($this->configFile);

$configLoader->setAdapterName($this->adapterType);

$processedConfiguration = $configLoader->process($this->adapterName, $config);
$this->config = $processedConfiguration[Config::INDEX_ADAPTER][$this->adapterName];
}

/**
* Tests creation of the filesystem cache item pool
*
Expand Down
28 changes: 28 additions & 0 deletions tests/Adapter/PHPArrayAdapterTests.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

use Cache\Factory\Adapter\PHPArray;

class PHPArrayAdapterTests extends BaseAdapterTests
{
/**
* @var string Name of the adapter in the configuration
*/
protected $adapterName = 'memory';

/**
* @var string Type of the cache pool adapter
*/
protected $adapterType = 'PHPArray';

/**
* Tests creation of the filesystem cache item pool
*/
public function testMake()
{
$filesystemAdapterFactory = new PHPArray();
$filesystemCacheItemPool = $filesystemAdapterFactory->make($this->config);

$this->assertInstanceOf('Psr\\Cache\\CacheItemPoolInterface', $filesystemCacheItemPool);
$this->assertInstanceOf('\\Cache\\Adapter\\PHPArray\\ArrayCachePool', $filesystemCacheItemPool);
}
}
2 changes: 1 addition & 1 deletion tests/FactoryTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public function testThatFactoryWorksWithValidConfig(array $config)
$taggableCachePool = $cachePoolFactory->makeTaggable($this->adapterName);

$this->assertInstanceOf('Psr\\Cache\\CacheItemPoolInterface', $cachePool);
$this->assertInstanceOf('Cache\\Taggable\\TaggablePoolInterface', $taggableCachePool);
$this->assertInstanceOf('Cache\\TagInterop\\TaggableCacheItemPoolInterface', $taggableCachePool);
}

/**
Expand Down
2 changes: 2 additions & 0 deletions tests/cache.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ Cache:
local:
type: Filesystem
path: '/tmp'
memory:
type: PHPArray

0 comments on commit 4958940

Please sign in to comment.