Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
181 additions
and
62 deletions.
There are no files selected for viewing
80 changes: 80 additions & 0 deletions
80
src/Symfony/Component/Validator/Mapping/Cache/Psr6Cache.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<?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; | ||
|
||
/** | ||
* PSR-6 adapter. | ||
* | ||
* @author Kévin Dunglas <dunglas@gmail.com> | ||
*/ | ||
class Psr6Cache implements CacheInterface | ||
{ | ||
/** | ||
* @var CacheItemPoolInterface | ||
*/ | ||
private $cacheItemPool; | ||
|
||
public function __construct(CacheItemPoolInterface $cacheItemPool) | ||
{ | ||
$this->cacheItemPool = $cacheItemPool; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function has($class) | ||
{ | ||
$item = $this->cacheItemPool->getItem($this->escapeClassName($class)); | ||
|
||
return $item->isHit(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function read($class) | ||
{ | ||
$item = $this->cacheItemPool->getItem($this->escapeClassName($class)); | ||
|
||
if (!$item->isHit()) { | ||
return false; | ||
} | ||
|
||
return $item->get(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function write(ClassMetadata $metadata) | ||
{ | ||
$item = $this->cacheItemPool->getItem($this->escapeClassName($metadata->getClassName())); | ||
$item->set($metadata); | ||
|
||
$this->cacheItemPool->save($item); | ||
} | ||
|
||
/** | ||
* Replaces backslashes by underscores in a class name. | ||
* | ||
* @param string $class | ||
* | ||
* @return string | ||
*/ | ||
private function escapeClassName($class) | ||
{ | ||
return strtr($class, '\\', '_'); | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
src/Symfony/Component/Validator/Tests/Mapping/Cache/AbstractCacheTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?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; | ||
|
||
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('Foo\\Bar')); | ||
|
||
$this->cache->write($meta); | ||
|
||
$this->assertInstanceOf( | ||
'Symfony\\Component\\Validator\\Mapping\\ClassMetadata', | ||
$this->cache->read('Foo\\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('Foo\\Bar')); | ||
|
||
$this->assertFalse($this->cache->has('Foo\\Bar'), 'has() returns false when there is no entry'); | ||
|
||
$this->cache->write($meta); | ||
$this->assertTrue($this->cache->has('Foo\\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('Foo\\Bar')); | ||
|
||
$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('Foo\\Bar'), | ||
'read() returns metadata' | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
src/Symfony/Component/Validator/Tests/Mapping/Cache/Psr6CacheTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
namespace Symfony\Component\Validator\Tests\Mapping\Cache; | ||
|
||
use Symfony\Component\Cache\Adapter\ArrayAdapter; | ||
use Symfony\Component\Validator\Mapping\Cache\Psr6Cache; | ||
|
||
/** | ||
* @author Kévin Dunglas <dunglas@gmail.com> | ||
*/ | ||
class Psr6CacheTest extends AbstractCacheTest | ||
{ | ||
protected function setUp() | ||
{ | ||
$this->cache = new Psr6Cache(new ArrayAdapter()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters