Skip to content

Commit 4680b1b

Browse files
committed
Add tests
1 parent d5d9c03 commit 4680b1b

File tree

12 files changed

+334
-24
lines changed

12 files changed

+334
-24
lines changed

src/Symfony/Component/Validator/Mapping/Loader/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
validation constraints.
1515
]]></xsd:documentation>
1616
</xsd:annotation>
17-
17+
1818
<xsd:element name="constraint-mapping" type="constraint-mapping" />
19-
19+
2020
<xsd:complexType name="constraint-mapping">
2121
<xsd:annotation>
2222
<xsd:documentation><![CDATA[
@@ -28,7 +28,7 @@
2828
<xsd:element name="class" type="class" maxOccurs="unbounded" />
2929
</xsd:sequence>
3030
</xsd:complexType>
31-
31+
3232
<xsd:complexType name="namespace">
3333
<xsd:annotation>
3434
<xsd:documentation><![CDATA[
@@ -41,13 +41,13 @@
4141
</xsd:extension>
4242
</xsd:simpleContent>
4343
</xsd:complexType>
44-
44+
4545
<xsd:complexType name="class">
4646
<xsd:annotation>
4747
<xsd:documentation><![CDATA[
4848
Contains constraints for a single class.
49-
50-
Nested elements may be class constraints, property and/or getter
49+
50+
Nested elements may be class constraints, property and/or getter
5151
definitions.
5252
]]></xsd:documentation>
5353
</xsd:annotation>
@@ -72,15 +72,18 @@
7272
<xsd:element name="value" type="value" minOccurs="1" maxOccurs="unbounded" />
7373
</xsd:sequence>
7474
</xsd:complexType>
75-
75+
7676
<xsd:complexType name="group-sequence-provider">
7777
<xsd:annotation>
7878
<xsd:documentation><![CDATA[
7979
Defines the name of the group sequence provider for a class.
8080
]]></xsd:documentation>
8181
</xsd:annotation>
82+
<xsd:sequence>
83+
<xsd:element name="value" type="value" minOccurs="0" maxOccurs="unbounded" />
84+
</xsd:sequence>
8285
</xsd:complexType>
83-
86+
8487
<xsd:complexType name="property">
8588
<xsd:annotation>
8689
<xsd:documentation><![CDATA[
@@ -93,7 +96,7 @@
9396
</xsd:sequence>
9497
<xsd:attribute name="name" type="xsd:string" use="required" />
9598
</xsd:complexType>
96-
99+
97100
<xsd:complexType name="getter">
98101
<xsd:annotation>
99102
<xsd:documentation><![CDATA[
@@ -106,14 +109,14 @@
106109
</xsd:sequence>
107110
<xsd:attribute name="property" type="xsd:string" use="required" />
108111
</xsd:complexType>
109-
112+
110113
<xsd:complexType name="constraint" mixed="true">
111114
<xsd:annotation>
112115
<xsd:documentation><![CDATA[
113116
Contains a constraint definition. The name of the constraint should be
114117
given in the "name" option.
115-
116-
May contain a single value, multiple "constraint" elements,
118+
119+
May contain a single value, multiple "constraint" elements,
117120
multiple "value" elements or multiple "option" elements.
118121
]]></xsd:documentation>
119122
</xsd:annotation>
@@ -122,15 +125,15 @@
122125
<xsd:element name="option" type="option" minOccurs="1" maxOccurs="unbounded" />
123126
<xsd:element name="value" type="value" minOccurs="1" maxOccurs="unbounded" />
124127
</xsd:choice>
125-
<xsd:attribute name="name" type="xsd:string" use="required" />
128+
<xsd:attribute name="name" type="xsd:string" use="required" />
126129
</xsd:complexType>
127-
130+
128131
<xsd:complexType name="option" mixed="true">
129132
<xsd:annotation>
130133
<xsd:documentation><![CDATA[
131134
Contains a constraint option definition. The name of the option
132135
should be given in the "name" option.
133-
136+
134137
May contain a single value, multiple "value" elements or multiple
135138
"constraint" elements.
136139
]]></xsd:documentation>
@@ -139,14 +142,14 @@
139142
<xsd:element name="constraint" type="constraint" minOccurs="1" maxOccurs="unbounded" />
140143
<xsd:element name="value" type="value" minOccurs="1" maxOccurs="unbounded" />
141144
</xsd:choice>
142-
<xsd:attribute name="name" type="xsd:string" use="required" />
145+
<xsd:attribute name="name" type="xsd:string" use="required" />
143146
</xsd:complexType>
144-
147+
145148
<xsd:complexType name="value" mixed="true">
146149
<xsd:annotation>
147150
<xsd:documentation><![CDATA[
148151
A value of an element.
149-
152+
150153
May contain a single value, multiple "value" elements or multiple
151154
"constraint" elements.
152155
]]></xsd:documentation>
@@ -155,6 +158,6 @@
155158
<xsd:element name="constraint" type="constraint" minOccurs="1" maxOccurs="unbounded" />
156159
<xsd:element name="value" type="value" minOccurs="1" maxOccurs="unbounded" />
157160
</xsd:choice>
158-
<xsd:attribute name="key" type="xsd:string" use="optional" />
161+
<xsd:attribute name="key" type="xsd:string" use="optional" />
159162
</xsd:complexType>
160163
</xsd:schema>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.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 Symfony\Component\Validator\Tests\Constraints;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Validator\Constraints\GroupSequence;
16+
use Symfony\Component\Validator\Constraints\GroupSequenceProvider;
17+
use Symfony\Component\Validator\Tests\Dummy\DummyGroupSequenceProvider;
18+
19+
class GroupSequenceProviderTest extends TestCase
20+
{
21+
public function testCreate()
22+
{
23+
$sequence = new GroupSequenceProvider(['class' => DummyGroupSequenceProvider::class]);
24+
25+
$this->assertSame(DummyGroupSequenceProvider::class, $sequence->class);
26+
}
27+
28+
public function testCreateAttributeStyle()
29+
{
30+
$sequence = new GroupSequenceProvider(class: DummyGroupSequenceProvider::class);
31+
32+
$this->assertSame(DummyGroupSequenceProvider::class, $sequence->class);
33+
}
34+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.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+
use PHPUnit\Framework\TestCase;
13+
use Symfony\Component\DependencyInjection\Container;
14+
use Symfony\Component\Validator\ContainerGroupSequenceProviderFactory;
15+
use Symfony\Component\Validator\Exception\GroupDefinitionException;
16+
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
17+
use Symfony\Component\Validator\Tests\Dummy\DummyGroupSequenceProvider;
18+
19+
class ContainerGroupSequenceProviderFactoryTest extends TestCase
20+
{
21+
public function testGetInstanceCreatesProvider()
22+
{
23+
$factory = new ContainerGroupSequenceProviderFactory(new Container());
24+
$this->assertInstanceOf(DummyGroupSequenceProvider::class, $factory->getInstance(DummyGroupSequenceProvider::class));
25+
}
26+
27+
public function testGetInstanceReturnsExistingProvider()
28+
{
29+
$factory = new ContainerGroupSequenceProviderFactory(new Container());
30+
$v1 = $factory->getInstance(DummyGroupSequenceProvider::class);
31+
$v2 = $factory->getInstance(DummyGroupSequenceProvider::class);
32+
$this->assertSame($v1, $v2);
33+
}
34+
35+
public function testGetInstanceReturnsService()
36+
{
37+
$provider = new DummyGroupSequenceProvider();
38+
$container = new Container();
39+
$container->set(DummyGroupSequenceProvider::class, $provider);
40+
41+
$factory = new ContainerGroupSequenceProviderFactory($container);
42+
43+
$this->assertSame($provider, $factory->getInstance(DummyGroupSequenceProvider::class));
44+
}
45+
46+
public function testGetInstanceProviderNotFound()
47+
{
48+
$this->expectException(GroupDefinitionException::class);
49+
50+
$factory = new ContainerGroupSequenceProviderFactory(new Container());
51+
$factory->getInstance('Unknown\\Class');
52+
}
53+
54+
public function testGetInstanceProviderDoNotImplementInterface()
55+
{
56+
$this->expectException(UnexpectedTypeException::class);
57+
58+
$factory = new ContainerGroupSequenceProviderFactory(new Container());
59+
$factory->getInstance(stdClass::class);
60+
}
61+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.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 Symfony\Component\Validator\Tests\DependencyInjection;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
16+
use Symfony\Component\DependencyInjection\ContainerBuilder;
17+
use Symfony\Component\DependencyInjection\Definition;
18+
use Symfony\Component\DependencyInjection\Reference;
19+
use Symfony\Component\DependencyInjection\ServiceLocator;
20+
use Symfony\Component\Validator\DependencyInjection\AddGroupSequenceProvidersPass;
21+
22+
class AddGroupSequenceProvidersPassTest extends TestCase
23+
{
24+
public function testThatGroupSequenceProviderServicesAreProcessed()
25+
{
26+
$container = new ContainerBuilder();
27+
$groupSequenceProviderFactory = $container->register('validator.group_sequence_provider_factory')
28+
->addArgument([]);
29+
30+
$container->register('my_group_sequence_provider_service1', Provider1::class)
31+
->addTag('validator.group_sequence_provider', ['alias' => 'my_group_sequence_provider_alias1']);
32+
$container->register('my_group_sequence_provider_service2', Provider2::class)
33+
->addTag('validator.group_sequence_provider');
34+
35+
$addGroupSequenceProvidersPass = new AddGroupSequenceProvidersPass();
36+
$addGroupSequenceProvidersPass->process($container);
37+
38+
$locator = $container->getDefinition((string) $groupSequenceProviderFactory->getArgument(0));
39+
$this->assertTrue(!$locator->isPublic() || $locator->isPrivate());
40+
$expected = (new Definition(ServiceLocator::class, [[
41+
Provider1::class => new ServiceClosureArgument(new Reference('my_group_sequence_provider_service1')),
42+
'my_group_sequence_provider_alias1' => new ServiceClosureArgument(new Reference('my_group_sequence_provider_service1')),
43+
Provider2::class => new ServiceClosureArgument(new Reference('my_group_sequence_provider_service2')),
44+
]]))->addTag('container.service_locator')->setPublic(false);
45+
$this->assertEquals($expected, $locator->setPublic(false));
46+
}
47+
48+
public function testAbstractGroupSequenceProvider()
49+
{
50+
$this->expectException(\InvalidArgumentException::class);
51+
$this->expectExceptionMessage('The service "my_abstract_group_sequence_provider" tagged "validator.group_sequence_provider" must not be abstract.');
52+
$container = new ContainerBuilder();
53+
$container->register('validator.group_sequence_provider_factory')
54+
->addArgument([]);
55+
56+
$container->register('my_abstract_group_sequence_provider')
57+
->setAbstract(true)
58+
->addTag('validator.group_sequence_provider');
59+
60+
$addGroupSequenceProvidersPass = new AddGroupSequenceProvidersPass();
61+
$addGroupSequenceProvidersPass->process($container);
62+
}
63+
64+
public function testThatCompilerPassIsIgnoredIfThereIsNoGroupSequenceProviderFactoryDefinition()
65+
{
66+
$container = new ContainerBuilder();
67+
68+
$definitionsBefore = \count($container->getDefinitions());
69+
$aliasesBefore = \count($container->getAliases());
70+
71+
$addGroupSequenceProvidersPass = new AddGroupSequenceProvidersPass();
72+
$addGroupSequenceProvidersPass->process($container);
73+
74+
// the container is untouched (i.e. no new definitions or aliases)
75+
$this->assertCount($definitionsBefore, $container->getDefinitions());
76+
$this->assertCount($aliasesBefore, $container->getAliases());
77+
}
78+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.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 Symfony\Component\Validator\Tests\Dummy;
13+
14+
use Symfony\Component\Validator\Constraints\GroupSequence;
15+
use Symfony\Component\Validator\GroupSequenceProviderInterface;
16+
17+
class DummyGroupSequenceProvider implements GroupSequenceProviderInterface
18+
{
19+
public function getGroupSequence(): array|GroupSequence
20+
{
21+
return ['foo', 'bar'];
22+
}
23+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Microservices bundle.
5+
*
6+
* (c) Yonel Ceruto <yonelceruto@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 Symfony\Component\Validator\Tests\Fixtures\Attribute;
13+
14+
use Symfony\Component\Validator\Constraints as Assert;
15+
use Symfony\Component\Validator\Tests\Dummy\DummyGroupSequenceProvider;
16+
17+
#[Assert\GroupSequenceProvider(class: DummyGroupSequenceProvider::class)]
18+
class GroupSequenceProviderDto
19+
{
20+
public string $firstName = '';
21+
public string $lastName = '';
22+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.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+
use PHPUnit\Framework\TestCase;
13+
use Symfony\Component\Validator\GroupSequenceProviderFactory;
14+
use Symfony\Component\Validator\Tests\Dummy\DummyGroupSequenceProvider;
15+
16+
class GroupSequenceProviderFactoryTest extends TestCase
17+
{
18+
public function testGetInstance()
19+
{
20+
$factory = new GroupSequenceProviderFactory();
21+
$this->assertInstanceOf(DummyGroupSequenceProvider::class, $factory->getInstance(DummyGroupSequenceProvider::class));
22+
}
23+
24+
public function testPredefinedGetInstance()
25+
{
26+
$provider = new DummyGroupSequenceProvider();
27+
$factory = new GroupSequenceProviderFactory([DummyGroupSequenceProvider::class => $provider]);
28+
$this->assertSame($provider, $factory->getInstance(DummyGroupSequenceProvider::class));
29+
}
30+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy