Skip to content

Commit

Permalink
[Validator] Add a PSR-6 adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
dunglas authored and fabpot committed Jan 26, 2016
1 parent cc84be9 commit 1f0ba00
Show file tree
Hide file tree
Showing 5 changed files with 181 additions and 62 deletions.
80 changes: 80 additions & 0 deletions src/Symfony/Component/Validator/Mapping/Cache/Psr6Cache.php
@@ -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, '\\', '_');
}
}
@@ -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'
);
}
}
Expand Up @@ -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());
Expand Down
@@ -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());
}
}
2 changes: 2 additions & 0 deletions src/Symfony/Component/Validator/composer.json
Expand Up @@ -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": "",
Expand Down

0 comments on commit 1f0ba00

Please sign in to comment.