Skip to content

add PSR-6 cache adapter for validator #17451

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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
75 changes: 75 additions & 0 deletions src/Symfony/Component/Validator/Mapping/Cache/PsrCache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Validator\Mapping\Cache;

use Psr\Cache\CacheItemPoolInterface;
use Symfony\Component\Validator\Mapping\ClassMetadata;

/**
* Adapts a PSR-6 cache to a CacheInterface.
*
* @author David Maicher <mail@dmaicher.de>
*/
class PsrCache implements CacheInterface
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wanted to name it PsrCacheAdapter but as the existing doctrine adapter is named DoctrineCache I followed the pattern.

{
/**
* @var CacheItemPoolInterface
*/
private $cache;

/**
* @param CacheItemPoolInterface $cache
*/
public function __construct(CacheItemPoolInterface $cache)
{
$this->cache = $cache;
}

/**
* @param string $class
*
* @return string
*/
private function getCacheKey($class)
{
//backslash is a reserved character and not allowed in PSR-6 cache keys
return str_replace('\\', '_', $class);
}

/**
* {@inheritdoc}
*/
public function has($class)
{
return $this->cache->hasItem($this->getCacheKey($class));
}

/**
* {@inheritdoc}
*/
public function read($class)
{
$item = $this->cache->getItem($this->getCacheKey($class));

return $item->isHit() ? $item->get() : false;
}

/**
* {@inheritdoc}
*/
public function write(ClassMetadata $metadata)
{
$item = $this->cache->getItem($this->getCacheKey($metadata->getClassName()));
$item->set($metadata);
$this->cache->save($item);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Validator\Tests\Mapping\Cache;

use Symfony\Component\Validator\Mapping\Cache\CacheInterface;
use Symfony\Component\Validator\Mapping\ClassMetadata;

abstract class AbstractCacheTest extends \PHPUnit_Framework_TestCase
{
/**
* @var CacheInterface
*/
private $cache;

public function setUp()
{
$this->cache = $this->getCache();
}

/**
* @param string $className
*
* @return \PHPUnit_Framework_MockObject_MockObject|ClassMetadata
*/
private function getMetaDataMock($className = 'Some\Nice\Class')
{
$meta = $this->getMockBuilder('Symfony\\Component\\Validator\\Mapping\\ClassMetadata')
->disableOriginalConstructor()
->setMethods(['getClassName'])
->getMock();

$meta->expects($this->atLeastOnce())
->method('getClassName')
->will($this->returnValue($className));

return $meta;
}

/**
* @return CacheInterface
*/
protected abstract function getCache();

public function testWrite()
{
$meta = $this->getMetaDataMock();

$this->cache->write($meta);

$this->assertInstanceOf(
'Symfony\\Component\\Validator\\Mapping\\ClassMetadata',
$this->cache->read($meta->getClassName()),
'write() stores metadata'
);
}

public function testHas()
{
$meta = $this->getMetaDataMock();

$this->assertFalse($this->cache->has($meta->getClassName()), 'has() returns false when there is no entry');

$this->cache->write($meta);
$this->assertTrue($this->cache->has($meta->getClassName()), 'has() returns true when the is an entry');
}

public function testRead()
{
$meta = $this->getMetaDataMock();

$this->assertFalse($this->cache->read($meta->getClassName()), 'read() returns false when there is no entry');

$this->cache->write($meta);

$this->assertInstanceOf(
'Symfony\\Component\\Validator\\Mapping\\ClassMetadata',
$this->cache->read($meta->getClassName()),
'read() returns metadata'
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,71 +14,13 @@
use Doctrine\Common\Cache\ArrayCache;
use Symfony\Component\Validator\Mapping\Cache\DoctrineCache;

class DoctrineCacheTest extends \PHPUnit_Framework_TestCase
class DoctrineCacheTest extends AbstractCacheTest
{
private $cache;

public function testWrite()
{
$meta = $this->getMockBuilder('Symfony\\Component\\Validator\\Mapping\\ClassMetadata')
->disableOriginalConstructor()
->setMethods(array('getClassName'))
->getMock();

$meta->expects($this->once())
->method('getClassName')
->will($this->returnValue('bar'));

$this->cache->write($meta);

$this->assertInstanceOf(
'Symfony\\Component\\Validator\\Mapping\\ClassMetadata',
$this->cache->read('bar'),
'write() stores metadata'
);
}

public function testHas()
{
$meta = $this->getMockBuilder('Symfony\\Component\\Validator\\Mapping\\ClassMetadata')
->disableOriginalConstructor()
->setMethods(array('getClassName'))
->getMock();

$meta->expects($this->once())
->method('getClassName')
->will($this->returnValue('bar'));

$this->assertFalse($this->cache->has('bar'), 'has() returns false when there is no entry');

$this->cache->write($meta);
$this->assertTrue($this->cache->has('bar'), 'has() returns true when the is an entry');
}

public function testRead()
{
$meta = $this->getMockBuilder('Symfony\\Component\\Validator\\Mapping\\ClassMetadata')
->disableOriginalConstructor()
->setMethods(array('getClassName'))
->getMock();

$meta->expects($this->once())
->method('getClassName')
->will($this->returnValue('bar'));

$this->assertFalse($this->cache->read('bar'), 'read() returns false when there is no entry');

$this->cache->write($meta);

$this->assertInstanceOf(
'Symfony\\Component\\Validator\\Mapping\\ClassMetadata',
$this->cache->read('bar'),
'read() returns metadata'
);
}

protected function setUp()
/**
* {@inheritdoc}
*/
protected function getCache()
{
$this->cache = new DoctrineCache(new ArrayCache());
return new DoctrineCache(new ArrayCache());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Validator\Tests\Mapping\Cache;

use Symfony\Component\Cache\Adapter\ArrayAdapter;
use Symfony\Component\Validator\Mapping\Cache\PsrCache;

class PsrCacheTest extends AbstractCacheTest
{
/**
* {@inheritdoc}
*/
protected function getCache()
{
return new PsrCache(new ArrayAdapter());
}
}