From 39586f1494761e317e305751f35d401624a52ce2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Dunglas?= Date: Tue, 19 Jan 2016 14:37:06 +0100 Subject: [PATCH 1/4] [Validator] Add a PSR-6 adapter --- .../Validator/Mapping/Cache/Psr6Cache.php | 78 ++++++++++++++++++ .../Tests/Mapping/Cache/AbstractCacheTest.php | 81 +++++++++++++++++++ .../Tests/Mapping/Cache/DoctrineCacheTest.php | 63 +-------------- .../Tests/Mapping/Cache/Psr6CacheTest.php | 17 ++++ src/Symfony/Component/Validator/composer.json | 2 + 5 files changed, 179 insertions(+), 62 deletions(-) create mode 100644 src/Symfony/Component/Validator/Mapping/Cache/Psr6Cache.php create mode 100644 src/Symfony/Component/Validator/Tests/Mapping/Cache/AbstractCacheTest.php create mode 100644 src/Symfony/Component/Validator/Tests/Mapping/Cache/Psr6CacheTest.php diff --git a/src/Symfony/Component/Validator/Mapping/Cache/Psr6Cache.php b/src/Symfony/Component/Validator/Mapping/Cache/Psr6Cache.php new file mode 100644 index 0000000000000..d9f9767144c1c --- /dev/null +++ b/src/Symfony/Component/Validator/Mapping/Cache/Psr6Cache.php @@ -0,0 +1,78 @@ + + * + * 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; + +/** + * PSR-6 adapter. + * + * @author Kévin Dunglas + */ +class Psr6Cache implements CacheInterface +{ + /** + * @var CacheItemPoolInterface + */ + private $cacheItemPool; + + public function __construct(CacheItemPoolInterface $cacheItemPool) + { + $this->cacheItemPool = $cacheItemPool; + } + + /** + * {@inheritdoc} + */ + public function has($class) + { + return $this->cacheItemPool->hasItem($this->escapeClassName($class)); + } + + /** + * {@inheritdoc} + */ + public function read($class) + { + $key = $this->escapeClassName($class); + + if (!$this->cacheItemPool->hasItem($key)) { + return false; + } + + return $this->cacheItemPool->getItem($key)->get(); + } + + /** + * {@inheritdoc} + */ + public function write(ClassMetadata $metadata) + { + $item = $this->cacheItemPool->getItem($this->escapeClassName($metadata->getClassName())); + $item->set($metadata); + + $this->cacheItemPool->save($item); + } + + /** + * Replaces anteslashes by underscores in a class name. + * + * @param string $class + * + * @return string + */ + private function escapeClassName($class) + { + return strtr($class, '\\', '_'); + } +} 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..09a987b699077 --- /dev/null +++ b/src/Symfony/Component/Validator/Tests/Mapping/Cache/AbstractCacheTest.php @@ -0,0 +1,81 @@ + + * + * 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; + +abstract class AbstractCacheTest extends \PHPUnit_Framework_TestCase +{ + /** + * @var CacheInterface + */ + protected $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' + ); + } +} diff --git a/src/Symfony/Component/Validator/Tests/Mapping/Cache/DoctrineCacheTest.php b/src/Symfony/Component/Validator/Tests/Mapping/Cache/DoctrineCacheTest.php index a2de306a2f46d..6296030fd7dff 100644 --- a/src/Symfony/Component/Validator/Tests/Mapping/Cache/DoctrineCacheTest.php +++ b/src/Symfony/Component/Validator/Tests/Mapping/Cache/DoctrineCacheTest.php @@ -14,69 +14,8 @@ 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() { $this->cache = new DoctrineCache(new ArrayCache()); diff --git a/src/Symfony/Component/Validator/Tests/Mapping/Cache/Psr6CacheTest.php b/src/Symfony/Component/Validator/Tests/Mapping/Cache/Psr6CacheTest.php new file mode 100644 index 0000000000000..9bdb898c7050b --- /dev/null +++ b/src/Symfony/Component/Validator/Tests/Mapping/Cache/Psr6CacheTest.php @@ -0,0 +1,17 @@ + + */ +class Psr6CacheTest extends AbstractCacheTest +{ + protected function setUp() + { + $this->cache = new Psr6Cache(new ArrayAdapter()); + } +} diff --git a/src/Symfony/Component/Validator/composer.json b/src/Symfony/Component/Validator/composer.json index d0c4a1155151a..881f9ae117820 100644 --- a/src/Symfony/Component/Validator/composer.json +++ b/src/Symfony/Component/Validator/composer.json @@ -26,11 +26,13 @@ "symfony/config": "~2.8|~3.0", "symfony/property-access": "~2.8|~3.0", "symfony/expression-language": "~2.8|~3.0", + "symfony/cache": "~3.1", "doctrine/annotations": "~1.0", "doctrine/cache": "~1.0", "egulias/email-validator": "~1.2,>=1.2.1" }, "suggest": { + "psr/cache-implementation": "For using the metadata cache.", "doctrine/annotations": "For using the annotation mapping. You will also need doctrine/cache.", "doctrine/cache": "For using the default cached annotation reader and metadata cache.", "symfony/http-foundation": "", From 856ae14a9d39537b2f46d2e5423fd6856d942295 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Dunglas?= Date: Tue, 19 Jan 2016 14:56:57 +0100 Subject: [PATCH 2/4] Fix read() --- src/Symfony/Component/Validator/Mapping/Cache/Psr6Cache.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Symfony/Component/Validator/Mapping/Cache/Psr6Cache.php b/src/Symfony/Component/Validator/Mapping/Cache/Psr6Cache.php index d9f9767144c1c..3949676cbaa68 100644 --- a/src/Symfony/Component/Validator/Mapping/Cache/Psr6Cache.php +++ b/src/Symfony/Component/Validator/Mapping/Cache/Psr6Cache.php @@ -44,13 +44,13 @@ public function has($class) */ public function read($class) { - $key = $this->escapeClassName($class); + $item = $this->cacheItemPool->getItem($this->escapeClassName($class)); - if (!$this->cacheItemPool->hasItem($key)) { + if (!$item->isHit()) { return false; } - return $this->cacheItemPool->getItem($key)->get(); + return $item->get(); } /** From 01b746bd4f7c2e0d739033758cea6818bda67cb6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Dunglas?= Date: Tue, 19 Jan 2016 15:08:20 +0100 Subject: [PATCH 3/4] Do not use has() --- src/Symfony/Component/Validator/Mapping/Cache/Psr6Cache.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/Validator/Mapping/Cache/Psr6Cache.php b/src/Symfony/Component/Validator/Mapping/Cache/Psr6Cache.php index 3949676cbaa68..fe773bd73e28f 100644 --- a/src/Symfony/Component/Validator/Mapping/Cache/Psr6Cache.php +++ b/src/Symfony/Component/Validator/Mapping/Cache/Psr6Cache.php @@ -36,7 +36,9 @@ public function __construct(CacheItemPoolInterface $cacheItemPool) */ public function has($class) { - return $this->cacheItemPool->hasItem($this->escapeClassName($class)); + $item = $this->cacheItemPool->getItem($this->escapeClassName($class)); + + return $item->isHit(); } /** From 1f79d6768e625078d0b1807fcb5a08fbe7f53323 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Dunglas?= Date: Tue, 26 Jan 2016 08:06:45 +0100 Subject: [PATCH 4/4] Update tests --- .../Validator/Mapping/Cache/Psr6Cache.php | 2 +- .../Tests/Mapping/Cache/AbstractCacheTest.php | 16 ++++++++-------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/Symfony/Component/Validator/Mapping/Cache/Psr6Cache.php b/src/Symfony/Component/Validator/Mapping/Cache/Psr6Cache.php index fe773bd73e28f..5c671220d2c11 100644 --- a/src/Symfony/Component/Validator/Mapping/Cache/Psr6Cache.php +++ b/src/Symfony/Component/Validator/Mapping/Cache/Psr6Cache.php @@ -67,7 +67,7 @@ public function write(ClassMetadata $metadata) } /** - * Replaces anteslashes by underscores in a class name. + * Replaces backslashes by underscores in a class name. * * @param string $class * diff --git a/src/Symfony/Component/Validator/Tests/Mapping/Cache/AbstractCacheTest.php b/src/Symfony/Component/Validator/Tests/Mapping/Cache/AbstractCacheTest.php index 09a987b699077..0b181a2f95a38 100644 --- a/src/Symfony/Component/Validator/Tests/Mapping/Cache/AbstractCacheTest.php +++ b/src/Symfony/Component/Validator/Tests/Mapping/Cache/AbstractCacheTest.php @@ -29,13 +29,13 @@ public function testWrite() $meta->expects($this->once()) ->method('getClassName') - ->will($this->returnValue('bar')); + ->will($this->returnValue('Foo\\Bar')); $this->cache->write($meta); $this->assertInstanceOf( 'Symfony\\Component\\Validator\\Mapping\\ClassMetadata', - $this->cache->read('bar'), + $this->cache->read('Foo\\Bar'), 'write() stores metadata' ); } @@ -49,12 +49,12 @@ public function testHas() $meta->expects($this->once()) ->method('getClassName') - ->will($this->returnValue('bar')); + ->will($this->returnValue('Foo\\Bar')); - $this->assertFalse($this->cache->has('bar'), 'has() returns false when there is no entry'); + $this->assertFalse($this->cache->has('Foo\\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'); + $this->assertTrue($this->cache->has('Foo\\Bar'), 'has() returns true when the is an entry'); } public function testRead() @@ -66,15 +66,15 @@ public function testRead() $meta->expects($this->once()) ->method('getClassName') - ->will($this->returnValue('bar')); + ->will($this->returnValue('Foo\\Bar')); - $this->assertFalse($this->cache->read('bar'), 'read() returns false when there is no entry'); + $this->assertFalse($this->cache->read('Foo\\Bar'), 'read() returns false when there is no entry'); $this->cache->write($meta); $this->assertInstanceOf( 'Symfony\\Component\\Validator\\Mapping\\ClassMetadata', - $this->cache->read('bar'), + $this->cache->read('Foo\\Bar'), 'read() returns metadata' ); } 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