From f18555b3202dfbfafcce36aca1b3029b6325c048 Mon Sep 17 00:00:00 2001 From: David Maicher Date: Tue, 19 Jan 2016 21:16:42 +0100 Subject: [PATCH] add PSR-6 cache adapter for validator metadata --- .../Validator/Mapping/Cache/PsrCache.php | 75 ++++++++++++++++ .../Tests/Mapping/Cache/AbstractCacheTest.php | 90 +++++++++++++++++++ .../Tests/Mapping/Cache/DoctrineCacheTest.php | 70 ++------------- .../Tests/Mapping/Cache/PsrCacheTest.php | 26 ++++++ 4 files changed, 197 insertions(+), 64 deletions(-) create mode 100644 src/Symfony/Component/Validator/Mapping/Cache/PsrCache.php create mode 100644 src/Symfony/Component/Validator/Tests/Mapping/Cache/AbstractCacheTest.php create mode 100644 src/Symfony/Component/Validator/Tests/Mapping/Cache/PsrCacheTest.php diff --git a/src/Symfony/Component/Validator/Mapping/Cache/PsrCache.php b/src/Symfony/Component/Validator/Mapping/Cache/PsrCache.php new file mode 100644 index 0000000000000..440ad959f750d --- /dev/null +++ b/src/Symfony/Component/Validator/Mapping/Cache/PsrCache.php @@ -0,0 +1,75 @@ + + * + * 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 + */ +class PsrCache implements CacheInterface +{ + /** + * @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); + } +} diff --git a/src/Symfony/Component/Validator/Tests/Mapping/Cache/AbstractCacheTest.php b/src/Symfony/Component/Validator/Tests/Mapping/Cache/AbstractCacheTest.php new file mode 100644 index 0000000000000..3c39b66aea9a7 --- /dev/null +++ b/src/Symfony/Component/Validator/Tests/Mapping/Cache/AbstractCacheTest.php @@ -0,0 +1,90 @@ + + * + * 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' + ); + } +} diff --git a/src/Symfony/Component/Validator/Tests/Mapping/Cache/DoctrineCacheTest.php b/src/Symfony/Component/Validator/Tests/Mapping/Cache/DoctrineCacheTest.php index a2de306a2f46d..a7fe4326ed690 100644 --- a/src/Symfony/Component/Validator/Tests/Mapping/Cache/DoctrineCacheTest.php +++ b/src/Symfony/Component/Validator/Tests/Mapping/Cache/DoctrineCacheTest.php @@ -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()); } } diff --git a/src/Symfony/Component/Validator/Tests/Mapping/Cache/PsrCacheTest.php b/src/Symfony/Component/Validator/Tests/Mapping/Cache/PsrCacheTest.php new file mode 100644 index 0000000000000..08ad8d772da3e --- /dev/null +++ b/src/Symfony/Component/Validator/Tests/Mapping/Cache/PsrCacheTest.php @@ -0,0 +1,26 @@ + + * + * 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()); + } +} pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy