Skip to content

Commit e09dccc

Browse files
mpajunennicolas-grekas
authored andcommitted
[FrameworkBundle] Add annotated validator cache test case
1 parent c60009e commit e09dccc

File tree

7 files changed

+79
-7
lines changed

7 files changed

+79
-7
lines changed

src/Symfony/Bundle/FrameworkBundle/CacheWarmer/AnnotationsCacheWarmer.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ class AnnotationsCacheWarmer implements CacheWarmerInterface
3636

3737
/**
3838
* @param Reader $annotationReader
39-
* @param string $phpArrayFile the PHP file where annotations are cached
40-
* @param CacheItemPoolInterface $fallbackPool the pool where runtime-discovered annotations are cached
39+
* @param string $phpArrayFile The PHP file where annotations are cached
40+
* @param CacheItemPoolInterface $fallbackPool The pool where runtime-discovered annotations are cached
4141
*/
4242
public function __construct(Reader $annotationReader, $phpArrayFile, CacheItemPoolInterface $fallbackPool)
4343
{

src/Symfony/Bundle/FrameworkBundle/CacheWarmer/SerializerCacheWarmer.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ class SerializerCacheWarmer implements CacheWarmerInterface
3737
private $fallbackPool;
3838

3939
/**
40-
* @param LoaderInterface[] $loaders the serializer metadata loaders
41-
* @param string $phpArrayFile the PHP file where metadata are cached
42-
* @param CacheItemPoolInterface $fallbackPool the pool where runtime-discovered metadata are cached
40+
* @param LoaderInterface[] $loaders The serializer metadata loaders
41+
* @param string $phpArrayFile The PHP file where metadata are cached
42+
* @param CacheItemPoolInterface $fallbackPool The pool where runtime-discovered metadata are cached
4343
*/
4444
public function __construct(array $loaders, $phpArrayFile, CacheItemPoolInterface $fallbackPool)
4545
{

src/Symfony/Bundle/FrameworkBundle/CacheWarmer/ValidatorCacheWarmer.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ class ValidatorCacheWarmer implements CacheWarmerInterface
3939

4040
/**
4141
* @param ValidatorBuilderInterface $validatorBuilder
42-
* @param string $phpArrayFile the PHP file where metadata are cached
43-
* @param CacheItemPoolInterface $fallbackPool the pool where runtime-discovered metadata are cached
42+
* @param string $phpArrayFile The PHP file where metadata are cached
43+
* @param CacheItemPoolInterface $fallbackPool The pool where runtime-discovered metadata are cached
4444
*/
4545
public function __construct(ValidatorBuilderInterface $validatorBuilder, $phpArrayFile, CacheItemPoolInterface $fallbackPool)
4646
{

src/Symfony/Bundle/FrameworkBundle/Tests/CacheWarmer/ValidatorCacheWarmerTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,39 @@ public function testWarmUp()
5151
$this->assertArrayHasKey('Symfony.Bundle.FrameworkBundle.Tests.Fixtures.Validation.Author', $values);
5252
}
5353

54+
public function testWarmUpWithAnnotations()
55+
{
56+
$validatorBuilder = new ValidatorBuilder();
57+
$validatorBuilder->addYamlMapping(__DIR__.'/../Fixtures/Validation/Resources/categories.yml');
58+
$validatorBuilder->enableAnnotationMapping();
59+
60+
$file = sys_get_temp_dir().'/cache-validator-with-annotations.php';
61+
@unlink($file);
62+
63+
$fallbackPool = new ArrayAdapter();
64+
65+
$warmer = new ValidatorCacheWarmer($validatorBuilder, $file, $fallbackPool);
66+
$warmer->warmUp(dirname($file));
67+
68+
$this->assertFileExists($file);
69+
70+
$values = require $file;
71+
72+
$this->assertInternalType('array', $values);
73+
$this->assertCount(1, $values);
74+
$this->assertArrayHasKey('Symfony.Bundle.FrameworkBundle.Tests.Fixtures.Validation.Category', $values);
75+
76+
// Simple check to make sure that at least one constraint is actually cached, in this case the "id" property Type.
77+
$this->assertContains('"int"', $values['Symfony.Bundle.FrameworkBundle.Tests.Fixtures.Validation.Category']);
78+
79+
$values = $fallbackPool->getValues();
80+
81+
$this->assertInternalType('array', $values);
82+
$this->assertCount(2, $values);
83+
$this->assertArrayHasKey('Symfony.Bundle.FrameworkBundle.Tests.Fixtures.Validation.Category', $values);
84+
$this->assertArrayHasKey('Symfony.Bundle.FrameworkBundle.Tests.Fixtures.Validation.SubCategory', $values);
85+
}
86+
5487
public function testWarmUpWithoutLoader()
5588
{
5689
$validatorBuilder = new ValidatorBuilder();
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Symfony\Bundle\FrameworkBundle\Tests\Fixtures\Validation;
4+
5+
use Symfony\Component\Validator\Constraints as Assert;
6+
7+
class Category
8+
{
9+
const NAME_PATTERN = '/\w+/';
10+
11+
public $id;
12+
13+
/**
14+
* @Assert\Type(Category::NAME_PATTERN)
15+
*/
16+
public $name;
17+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Symfony\Bundle\FrameworkBundle\Tests\Fixtures\Validation\Category:
2+
properties:
3+
id:
4+
- Type: int
5+
6+
Symfony\Bundle\FrameworkBundle\Tests\Fixtures\Validation\SubCategory:
7+
properties:
8+
id:
9+
- Type: int
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Symfony\Bundle\FrameworkBundle\Tests\Fixtures\Validation;
4+
5+
// Missing "use" for Assert\Type is on purpose
6+
7+
class SubCategory extends Category
8+
{
9+
/**
10+
* @Assert\Type(Category::class)
11+
*/
12+
public $main;
13+
}

0 commit comments

Comments
 (0)