Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@
}
},
"require": {
"php": ">=5.6",
"doctrine/cache": "2.2.0"
"php": ">=8.2",
"doctrine/cache": "^2.0",
"psr/cache": "^2.0|^3.0"
},
"require-dev": {
"mockery/mockery": "0.*",
"phpunit/phpunit": "^9.6.20"
"phpunit/phpunit": "^10.0",
"symfony/cache": "^5.4|^6.4|^7.0"
},
"extra": {
"branch-alias": {
Expand Down
32 changes: 9 additions & 23 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,29 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit
backupGlobals = "false"
backupStaticAttributes = "false"
colors = "true"
convertErrorsToExceptions = "true"
convertNoticesToExceptions = "true"
convertWarningsToExceptions = "true"
processIsolation = "false"
stopOnFailure = "false"
syntaxCheck = "false">

<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" colors="true"
bootstrap="./vendor/autoload.php"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd" cacheDirectory=".phpunit.cache">
<testsuites>
<testsuite name="Test Suite">
<testsuite name="unit">
<directory>./tests/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">./src</directory>
<exclude>
<directory>./tests</directory>
<directory>./vendor</directory>
</exclude>
</whitelist>
</filter>
<source>
<include>
<directory>./src</directory>
</include>
</source>
</phpunit>

Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace OpenClassrooms\DoctrineCacheExtension;

use Doctrine\Common\Cache\CacheProvider;
use Doctrine\Common\Cache\Psr6\DoctrineProvider;
use Psr\Cache\CacheItemPoolInterface;

/**
* @author Romain Kuzniak <romain.kuzniak@openclassrooms.com>
Expand All @@ -21,9 +23,9 @@ abstract class AbstractCacheProviderDecorator extends CacheProvider
*/
protected $defaultLifetime;

public function __construct(CacheProvider $cacheProvider, $defaultLifetime = self::DEFAULT_LIFE_TIME)
public function __construct(CacheItemPoolInterface $cache, $defaultLifetime = self::DEFAULT_LIFE_TIME)
{
$this->cacheProvider = $cacheProvider;
$this->cacheProvider = DoctrineProvider::wrap($cache);
$this->defaultLifetime = $defaultLifetime;
}

Expand All @@ -39,6 +41,11 @@ public function fetchWithNamespace($id, $namespaceId = null)
{
if (null !== $namespaceId) {
$namespace = $this->fetch($namespaceId);

if (false === $namespace) {
return false;
}

$id = $namespace.$id;
}

Expand All @@ -56,17 +63,12 @@ public function invalidate($namespaceId)
{
$namespace = $this->fetch($namespaceId);

if (false === $namespace) {
return false;
}
$newNamespace = random_int(0, 10000);

$newNamespace = rand(0, 10000);
// @codeCoverageIgnoreStart
while ($namespace === $newNamespace) {
$newNamespace = rand(0, 10000);
$newNamespace = random_int(0, 10000);
}

// @codeCoverageIgnoreEnd
return $this->save($namespaceId, $namespaceId.'_'.$newNamespace.'_', 0);
}

Expand Down Expand Up @@ -97,14 +99,6 @@ public function getDefaultLifetime()
return $this->defaultLifetime;
}

/**
* @return CacheProvider
*/
public function getCacheProvider()
{
return $this->cacheProvider;
}

/**
* Puts data into the cache.
*
Expand All @@ -130,59 +124,51 @@ public function saveWithNamespace($id, $data, $namespaceId = null, $lifeTime = n
return $this->save($id, $data, $lifeTime);
}

/**
* {@inheritdoc}
*/
public function __call($name, $arguments)
{
return $this->getCacheProvider()->$name(...$arguments);
}

/**
* {@inheritdoc}
*/
protected function doFetch($id)
{
return $this->getCacheProvider()->doFetch($id);
return $this->cacheProvider->doFetch($id);
}

/**
* {@inheritdoc}
*/
protected function doContains($id)
{
return $this->getCacheProvider()->doContains($id);
return $this->cacheProvider->doContains($id);
}

/**
* {@inheritdoc}
*/
protected function doSave($id, $data, $lifeTime = 0)
{
return $this->getCacheProvider()->doSave($id, $data, $lifeTime);
return $this->cacheProvider->doSave($id, $data, $lifeTime);
}

/**
* {@inheritdoc}
*/
protected function doDelete($id)
{
return $this->getCacheProvider()->doDelete($id);
return $this->cacheProvider->doDelete($id);
}

/**
* {@inheritdoc}
*/
protected function doFlush()
{
return $this->getCacheProvider()->doFlush();
return $this->cacheProvider->doFlush();
}

/**
* {@inheritdoc}
*/
protected function doGetStats()
{
return $this->getCacheProvider()->doGetStats();
return $this->cacheProvider->doGetStats();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Doctrine\Common\Cache\WinCacheCache;
use Doctrine\Common\Cache\XcacheCache;
use Doctrine\Common\Cache\ZendDataCache;
use Psr\Cache\CacheItemPoolInterface;

/**
* @author Romain Kuzniak <romain.kuzniak@openclassrooms.com>
Expand All @@ -30,56 +31,9 @@ class CacheProviderDecoratorFactory implements CacheProviderDecoratorFactoryInte
/**
* @return AbstractCacheProviderDecorator
*/
public static function create($type, ...$args)
public static function create(CacheItemPoolInterface $cache)
{
switch ($type) {
case 'apc':
$cacheProvider = new ApcCache();
break;
case 'apcu':
$cacheProvider = new ApcuCache();
break;
case 'array':
$cacheProvider = new ArrayCache();
break;
case 'couchbase':
$cacheProvider = new CouchbaseCache();
break;
case 'file_system':
$cacheProvider = new FilesystemCache(...$args);
break;
case 'memcache':
$cacheProvider = new MemcacheCache();
break;
case 'memcached':
$cacheProvider = new MemcachedCache();
break;
case 'mongodb':
$cacheProvider = new MongoDBCache(...$args);
break;
case 'php_file':
$cacheProvider = new PhpFileCache(...$args);
break;
case 'redis':
$cacheProvider = new RedisCache();
break;
case 'riak':
$cacheProvider = new RiakCache(...$args);
break;
case 'wincache':
$cacheProvider = new WinCacheCache();
break;
case 'xcache':
$cacheProvider = new XcacheCache();
break;
case 'zenddata':
$cacheProvider = new ZendDataCache();
break;
default:
throw new \InvalidArgumentException('Type "'.$type.'" is not supported.');
}

return new CacheProviderDecorator($cacheProvider, self::$defaultLifetime);
return new CacheProviderDecorator($cache, self::$defaultLifetime);
}

public function setDefaultLifetime($defaultLifetime)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace OpenClassrooms\DoctrineCacheExtension;

use Psr\Cache\CacheItemPoolInterface;

/**
* @author Romain Kuzniak <romain.kuzniak@openclassrooms.com>
*/
Expand All @@ -10,7 +12,7 @@ interface CacheProviderDecoratorFactoryInterface
/**
* @return AbstractCacheProviderDecorator
*/
public static function create($type, ...$args);
public static function create(CacheItemPoolInterface $pool);

public function setDefaultLifetime($defaultLifetime);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,83 +4,34 @@

use OpenClassrooms\DoctrineCacheExtension\CacheProviderDecoratorFactory;
use OpenClassrooms\DoctrineCacheExtension\CacheProviderDecoratorFactoryInterface;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Cache\Adapter\ArrayAdapter;

/**
* @author Romain Kuzniak <romain.kuzniak@openclassrooms.com>
*/
class CacheProviderDecoratorFactoryTest extends \PHPUnit_Framework_TestCase
class CacheProviderDecoratorFactoryTest extends TestCase
{
const DIRECTORY = __DIR__.'/../tmp';

/**
* @var CacheProviderDecoratorFactoryInterface
*/
private $factory;

/**
* @return array
*/
public static function typeProvider()
{
return [
['apc', 'Doctrine\Common\Cache\ApcCache', []],
['array', 'Doctrine\Common\Cache\ArrayCache', []],
['couchbase', 'Doctrine\Common\Cache\CouchbaseCache', []],
['file_system', 'Doctrine\Common\Cache\FilesystemCache', [self::DIRECTORY]],
['memcache', 'Doctrine\Common\Cache\MemcacheCache', []],
['memcached', 'Doctrine\Common\Cache\MemcachedCache', []],
['mongodb', 'Doctrine\Common\Cache\MongoDBCache', [\Mockery::mock('MongoCollection')]],
['php_file', 'Doctrine\Common\Cache\PhpFileCache', [self::DIRECTORY]],
['redis', 'Doctrine\Common\Cache\RedisCache', []],
['riak', 'Doctrine\Common\Cache\RiakCache', [\Mockery::mock('\Riak\Bucket')]],
['wincache', 'Doctrine\Common\Cache\WinCacheCache', []],
['xcache', 'Doctrine\Common\Cache\XCacheCache', []],
['zenddata', 'Doctrine\Common\Cache\ZendDataCache', []],
];
}

/**
* {@inheritdoc}
*/
public static function tearDownAfterClass()
{
rmdir(self::DIRECTORY);
}

/**
* @test
* @expectedException \InvalidArgumentException
*/
public function InvalidType_ThrowException()
{
$this->factory->create('invalid type');
}

/**
* @test
*/
public function WithDefaultLifetime_create()
{
$this->factory->setDefaultLifetime(100);
$cacheProviderDecorator = $this->factory->create('array');
$this->assertAttributeEquals(100, 'defaultLifetime', $cacheProviderDecorator);
$cacheProviderDecorator = $this->factory->create(new ArrayAdapter());
$this->assertEquals(100, $cacheProviderDecorator->getDefaultLifetime());
}

/**
* @test
* @dataProvider typeProvider
*/
public function Create($inputType, $expectedCacheProvider, $args)
{
$factory = new CacheProviderDecoratorFactory();
$actualCacheProvider = $factory->create($inputType, ...$args);
$this->assertAttributeInstanceOf($expectedCacheProvider, 'cacheProvider', $actualCacheProvider);
}

/**
* {@inheritdoc}
*/
protected function setUp()
protected function setUp(): void
{
$this->factory = new CacheProviderDecoratorFactory();
}
Expand Down
Loading