Skip to content

Commit 8d25890

Browse files
authored
Merge pull request #827 from meyerbaptiste/add_test_validator_property_metadata_factory
Add tests for the ValidatorPropertyMetadataFactory class
2 parents 2442f24 + df332d8 commit 8d25890

File tree

2 files changed

+209
-0
lines changed

2 files changed

+209
-0
lines changed
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <dunglas@gmail.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace ApiPlatform\Core\Tests\Bridge\Symfony\Validator\Metadata\Property;
13+
14+
use ApiPlatform\Core\Bridge\Symfony\Validator\Metadata\Property\ValidatorPropertyMetadataFactory;
15+
use ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
16+
use ApiPlatform\Core\Metadata\Property\PropertyMetadata;
17+
use ApiPlatform\Core\Tests\Fixtures\DummyEntityValidator;
18+
use Doctrine\Common\Annotations\AnnotationReader;
19+
use Doctrine\Common\Annotations\DocParser;
20+
use Symfony\Component\Validator\Mapping\ClassMetadata;
21+
use Symfony\Component\Validator\Mapping\Factory\MetadataFactoryInterface;
22+
use Symfony\Component\Validator\Mapping\Loader\AnnotationLoader;
23+
24+
/**
25+
* @author Baptiste Meyer <baptiste.meyer@gmail.com>
26+
*/
27+
class ValidatorPropertyMetadataFactoryTest extends \PHPUnit_Framework_TestCase
28+
{
29+
private $validatorClassMetadata;
30+
31+
public function setUp()
32+
{
33+
$this->validatorClassMetadata = new ClassMetadata(DummyEntityValidator::class);
34+
(new AnnotationLoader(new AnnotationReader(new DocParser())))->loadClassMetadata($this->validatorClassMetadata);
35+
}
36+
37+
public function testCreateWithPropertyWithRequiredConstraints()
38+
{
39+
$propertyMetadata = new PropertyMetadata(null, 'A dummy', true, true, null, null, null, false);
40+
$expectedPropertyMetadata = $propertyMetadata->withRequired(true);
41+
42+
$decoratedPropertyMetadataFactory = $this->prophesize(PropertyMetadataFactoryInterface::class);
43+
$decoratedPropertyMetadataFactory->create(DummyEntityValidator::class, 'dummy', [])->willReturn($propertyMetadata)->shouldBeCalled();
44+
45+
$validatorMetadataFactory = $this->prophesize(MetadataFactoryInterface::class);
46+
$validatorMetadataFactory->getMetadataFor(DummyEntityValidator::class)->willReturn($this->validatorClassMetadata)->shouldBeCalled();
47+
48+
$validatorPropertyMetadataFactory = new ValidatorPropertyMetadataFactory($validatorMetadataFactory->reveal(), $decoratedPropertyMetadataFactory->reveal());
49+
$resultedPropertyMetadata = $validatorPropertyMetadataFactory->create(DummyEntityValidator::class, 'dummy');
50+
51+
$this->assertInstanceOf(PropertyMetadata::class, $resultedPropertyMetadata);
52+
$this->assertEquals($expectedPropertyMetadata, $resultedPropertyMetadata);
53+
}
54+
55+
public function testCreateWithPropertyWithNotRequiredConstraints()
56+
{
57+
$propertyMetadata = new PropertyMetadata(null, 'A dummy date', true, true, null, null, null, false);
58+
$expectedPropertyMetadata = $propertyMetadata->withRequired(false);
59+
60+
$decoratedPropertyMetadataFactory = $this->prophesize(PropertyMetadataFactoryInterface::class);
61+
$decoratedPropertyMetadataFactory->create(DummyEntityValidator::class, 'dummyDate', [])->willReturn($propertyMetadata)->shouldBeCalled();
62+
63+
$validatorMetadataFactory = $this->prophesize(MetadataFactoryInterface::class);
64+
$validatorMetadataFactory->getMetadataFor(DummyEntityValidator::class)->willReturn($this->validatorClassMetadata)->shouldBeCalled();
65+
66+
$validatorPropertyMetadataFactory = new ValidatorPropertyMetadataFactory($validatorMetadataFactory->reveal(), $decoratedPropertyMetadataFactory->reveal());
67+
$resultedPropertyMetadata = $validatorPropertyMetadataFactory->create(DummyEntityValidator::class, 'dummyDate');
68+
69+
$this->assertInstanceOf(PropertyMetadata::class, $resultedPropertyMetadata);
70+
$this->assertEquals($expectedPropertyMetadata, $resultedPropertyMetadata);
71+
}
72+
73+
public function testCreateWithPropertyWithoutConstraints()
74+
{
75+
$propertyMetadata = new PropertyMetadata(null, 'A dummy id', true, true, null, null, null, true);
76+
$expectedPropertyMetadata = $propertyMetadata->withRequired(false);
77+
78+
$decoratedPropertyMetadataFactory = $this->prophesize(PropertyMetadataFactoryInterface::class);
79+
$decoratedPropertyMetadataFactory->create(DummyEntityValidator::class, 'dummyId', [])->willReturn($propertyMetadata)->shouldBeCalled();
80+
81+
$validatorMetadataFactory = $this->prophesize(MetadataFactoryInterface::class);
82+
$validatorMetadataFactory->getMetadataFor(DummyEntityValidator::class)->willReturn($this->validatorClassMetadata)->shouldBeCalled();
83+
84+
$validatorPropertyMetadataFactory = new ValidatorPropertyMetadataFactory($validatorMetadataFactory->reveal(), $decoratedPropertyMetadataFactory->reveal());
85+
$resultedPropertyMetadata = $validatorPropertyMetadataFactory->create(DummyEntityValidator::class, 'dummyId');
86+
87+
$this->assertInstanceOf(PropertyMetadata::class, $resultedPropertyMetadata);
88+
$this->assertEquals($expectedPropertyMetadata, $resultedPropertyMetadata);
89+
}
90+
91+
public function testCreateWithPropertyWithRightValidationGroupsAndRequiredConstraints()
92+
{
93+
$propertyMetadata = new PropertyMetadata(null, 'A dummy group', true, true, null, null, null, false);
94+
$expectedPropertyMetadata = $propertyMetadata->withRequired(true);
95+
96+
$decoratedPropertyMetadataFactory = $this->prophesize(PropertyMetadataFactoryInterface::class);
97+
$decoratedPropertyMetadataFactory->create(DummyEntityValidator::class, 'dummyGroup', ['validation_groups' => ['dummy']])->willReturn($propertyMetadata)->shouldBeCalled();
98+
99+
$validatorMetadataFactory = $this->prophesize(MetadataFactoryInterface::class);
100+
$validatorMetadataFactory->getMetadataFor(DummyEntityValidator::class)->willReturn($this->validatorClassMetadata)->shouldBeCalled();
101+
102+
$validatorPropertyMetadataFactory = new ValidatorPropertyMetadataFactory($validatorMetadataFactory->reveal(), $decoratedPropertyMetadataFactory->reveal());
103+
$resultedPropertyMetadata = $validatorPropertyMetadataFactory->create(DummyEntityValidator::class, 'dummyGroup', ['validation_groups' => ['dummy']]);
104+
105+
$this->assertInstanceOf(PropertyMetadata::class, $resultedPropertyMetadata);
106+
$this->assertEquals($expectedPropertyMetadata, $resultedPropertyMetadata);
107+
}
108+
109+
public function testCreateWithPropertyWithBadValidationGroupsAndRequiredConstraints()
110+
{
111+
$propertyMetadata = new PropertyMetadata(null, 'A dummy group', true, true, null, null, null, false);
112+
$expectedPropertyMetadata = $propertyMetadata->withRequired(false);
113+
114+
$decoratedPropertyMetadataFactory = $this->prophesize(PropertyMetadataFactoryInterface::class);
115+
$decoratedPropertyMetadataFactory->create(DummyEntityValidator::class, 'dummyGroup', ['validation_groups' => ['ymmud']])->willReturn($propertyMetadata)->shouldBeCalled();
116+
117+
$validatorMetadataFactory = $this->prophesize(MetadataFactoryInterface::class);
118+
$validatorMetadataFactory->getMetadataFor(DummyEntityValidator::class)->willReturn($this->validatorClassMetadata)->shouldBeCalled();
119+
120+
$validatorPropertyMetadataFactory = new ValidatorPropertyMetadataFactory($validatorMetadataFactory->reveal(), $decoratedPropertyMetadataFactory->reveal());
121+
$resultedPropertyMetadata = $validatorPropertyMetadataFactory->create(DummyEntityValidator::class, 'dummyGroup', ['validation_groups' => ['ymmud']]);
122+
123+
$this->assertInstanceOf(PropertyMetadata::class, $resultedPropertyMetadata);
124+
$this->assertEquals($expectedPropertyMetadata, $resultedPropertyMetadata);
125+
}
126+
127+
public function testCreateWithPropertyWithNonStringValidationGroupsAndRequiredConstraints()
128+
{
129+
$propertyMetadata = new PropertyMetadata(null, 'A dummy group', true, true, null, null, null, false);
130+
$expectedPropertyMetadata = $propertyMetadata->withRequired(false);
131+
132+
$decoratedPropertyMetadataFactory = $this->prophesize(PropertyMetadataFactoryInterface::class);
133+
$decoratedPropertyMetadataFactory->create(DummyEntityValidator::class, 'dummyGroup', ['validation_groups' => [1312]])->willReturn($propertyMetadata)->shouldBeCalled();
134+
135+
$validatorMetadataFactory = $this->prophesize(MetadataFactoryInterface::class);
136+
$validatorMetadataFactory->getMetadataFor(DummyEntityValidator::class)->willReturn($this->validatorClassMetadata)->shouldBeCalled();
137+
138+
$validatorPropertyMetadataFactory = new ValidatorPropertyMetadataFactory($validatorMetadataFactory->reveal(), $decoratedPropertyMetadataFactory->reveal());
139+
$resultedPropertyMetadata = $validatorPropertyMetadataFactory->create(DummyEntityValidator::class, 'dummyGroup', ['validation_groups' => [1312]]);
140+
141+
$this->assertInstanceOf(PropertyMetadata::class, $resultedPropertyMetadata);
142+
$this->assertEquals($expectedPropertyMetadata, $resultedPropertyMetadata);
143+
}
144+
145+
public function testCreateWithRequiredByDecorated()
146+
{
147+
$propertyMetadata = new PropertyMetadata(null, 'A dummy date', true, true, null, null, true, false);
148+
$expectedPropertyMetadata = clone $propertyMetadata;
149+
150+
$decoratedPropertyMetadataFactory = $this->prophesize(PropertyMetadataFactoryInterface::class);
151+
$decoratedPropertyMetadataFactory->create(DummyEntityValidator::class, 'dummyDate', [])->willReturn($propertyMetadata)->shouldBeCalled();
152+
153+
$validatorMetadataFactory = $this->prophesize(MetadataFactoryInterface::class);
154+
155+
$validatorPropertyMetadataFactory = new ValidatorPropertyMetadataFactory($validatorMetadataFactory->reveal(), $decoratedPropertyMetadataFactory->reveal());
156+
$resultedPropertyMetadata = $validatorPropertyMetadataFactory->create(DummyEntityValidator::class, 'dummyDate');
157+
158+
$this->assertInstanceOf(PropertyMetadata::class, $resultedPropertyMetadata);
159+
$this->assertEquals($expectedPropertyMetadata, $resultedPropertyMetadata);
160+
}
161+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <dunglas@gmail.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace ApiPlatform\Core\Tests\Fixtures;
13+
14+
use Symfony\Component\Validator\Constraints as Assert;
15+
16+
/**
17+
* Dummy Entity Validator.
18+
*
19+
* @author Baptiste Meyer <baptiste.meyer@gmail.com>
20+
*/
21+
class DummyEntityValidator
22+
{
23+
/**
24+
* @var int A dummy ID
25+
*/
26+
public $dummyId;
27+
28+
/**
29+
* @var string A dummy
30+
*
31+
* @Assert\NotBlank
32+
*/
33+
public $dummy;
34+
35+
/**
36+
* @var \DateTimeInterface A dummy date
37+
*
38+
* @Assert\Date
39+
*/
40+
public $dummyDate;
41+
42+
/**
43+
* @var string A dummy group
44+
*
45+
* @Assert\NotNull(groups={"dummy"})
46+
*/
47+
public $dummyGroup;
48+
}

0 commit comments

Comments
 (0)