Skip to content

Commit

Permalink
[FrameworkBundle] Add annotated validator cache test case
Browse files Browse the repository at this point in the history
  • Loading branch information
mpajunen authored and nicolas-grekas committed Jan 12, 2017
1 parent c60009e commit e09dccc
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 7 deletions.
Expand Up @@ -36,8 +36,8 @@ class AnnotationsCacheWarmer implements CacheWarmerInterface

/**
* @param Reader $annotationReader
* @param string $phpArrayFile the PHP file where annotations are cached
* @param CacheItemPoolInterface $fallbackPool the pool where runtime-discovered annotations are cached
* @param string $phpArrayFile The PHP file where annotations are cached
* @param CacheItemPoolInterface $fallbackPool The pool where runtime-discovered annotations are cached
*/
public function __construct(Reader $annotationReader, $phpArrayFile, CacheItemPoolInterface $fallbackPool)
{
Expand Down
Expand Up @@ -37,9 +37,9 @@ class SerializerCacheWarmer implements CacheWarmerInterface
private $fallbackPool;

/**
* @param LoaderInterface[] $loaders the serializer metadata loaders
* @param string $phpArrayFile the PHP file where metadata are cached
* @param CacheItemPoolInterface $fallbackPool the pool where runtime-discovered metadata are cached
* @param LoaderInterface[] $loaders The serializer metadata loaders
* @param string $phpArrayFile The PHP file where metadata are cached
* @param CacheItemPoolInterface $fallbackPool The pool where runtime-discovered metadata are cached
*/
public function __construct(array $loaders, $phpArrayFile, CacheItemPoolInterface $fallbackPool)
{
Expand Down
Expand Up @@ -39,8 +39,8 @@ class ValidatorCacheWarmer implements CacheWarmerInterface

/**
* @param ValidatorBuilderInterface $validatorBuilder
* @param string $phpArrayFile the PHP file where metadata are cached
* @param CacheItemPoolInterface $fallbackPool the pool where runtime-discovered metadata are cached
* @param string $phpArrayFile The PHP file where metadata are cached
* @param CacheItemPoolInterface $fallbackPool The pool where runtime-discovered metadata are cached
*/
public function __construct(ValidatorBuilderInterface $validatorBuilder, $phpArrayFile, CacheItemPoolInterface $fallbackPool)
{
Expand Down
Expand Up @@ -51,6 +51,39 @@ public function testWarmUp()
$this->assertArrayHasKey('Symfony.Bundle.FrameworkBundle.Tests.Fixtures.Validation.Author', $values);
}

public function testWarmUpWithAnnotations()
{
$validatorBuilder = new ValidatorBuilder();
$validatorBuilder->addYamlMapping(__DIR__.'/../Fixtures/Validation/Resources/categories.yml');
$validatorBuilder->enableAnnotationMapping();

$file = sys_get_temp_dir().'/cache-validator-with-annotations.php';
@unlink($file);

$fallbackPool = new ArrayAdapter();

$warmer = new ValidatorCacheWarmer($validatorBuilder, $file, $fallbackPool);
$warmer->warmUp(dirname($file));

$this->assertFileExists($file);

$values = require $file;

$this->assertInternalType('array', $values);
$this->assertCount(1, $values);
$this->assertArrayHasKey('Symfony.Bundle.FrameworkBundle.Tests.Fixtures.Validation.Category', $values);

// Simple check to make sure that at least one constraint is actually cached, in this case the "id" property Type.
$this->assertContains('"int"', $values['Symfony.Bundle.FrameworkBundle.Tests.Fixtures.Validation.Category']);

$values = $fallbackPool->getValues();

$this->assertInternalType('array', $values);
$this->assertCount(2, $values);
$this->assertArrayHasKey('Symfony.Bundle.FrameworkBundle.Tests.Fixtures.Validation.Category', $values);
$this->assertArrayHasKey('Symfony.Bundle.FrameworkBundle.Tests.Fixtures.Validation.SubCategory', $values);
}

public function testWarmUpWithoutLoader()
{
$validatorBuilder = new ValidatorBuilder();
Expand Down
@@ -0,0 +1,17 @@
<?php

namespace Symfony\Bundle\FrameworkBundle\Tests\Fixtures\Validation;

use Symfony\Component\Validator\Constraints as Assert;

class Category
{
const NAME_PATTERN = '/\w+/';

public $id;

/**
* @Assert\Type(Category::NAME_PATTERN)
*/
public $name;
}
@@ -0,0 +1,9 @@
Symfony\Bundle\FrameworkBundle\Tests\Fixtures\Validation\Category:
properties:
id:
- Type: int

Symfony\Bundle\FrameworkBundle\Tests\Fixtures\Validation\SubCategory:
properties:
id:
- Type: int
@@ -0,0 +1,13 @@
<?php

namespace Symfony\Bundle\FrameworkBundle\Tests\Fixtures\Validation;

// Missing "use" for Assert\Type is on purpose

class SubCategory extends Category
{
/**
* @Assert\Type(Category::class)
*/
public $main;
}

0 comments on commit e09dccc

Please sign in to comment.