Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yceruto committed Aug 11, 2023
1 parent d5d9c03 commit 4680b1b
Show file tree
Hide file tree
Showing 12 changed files with 334 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
validation constraints.
]]></xsd:documentation>
</xsd:annotation>

<xsd:element name="constraint-mapping" type="constraint-mapping" />

<xsd:complexType name="constraint-mapping">
<xsd:annotation>
<xsd:documentation><![CDATA[
Expand All @@ -28,7 +28,7 @@
<xsd:element name="class" type="class" maxOccurs="unbounded" />
</xsd:sequence>
</xsd:complexType>

<xsd:complexType name="namespace">
<xsd:annotation>
<xsd:documentation><![CDATA[
Expand All @@ -41,13 +41,13 @@
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>

<xsd:complexType name="class">
<xsd:annotation>
<xsd:documentation><![CDATA[
Contains constraints for a single class.
Nested elements may be class constraints, property and/or getter
Nested elements may be class constraints, property and/or getter
definitions.
]]></xsd:documentation>
</xsd:annotation>
Expand All @@ -72,15 +72,18 @@
<xsd:element name="value" type="value" minOccurs="1" maxOccurs="unbounded" />
</xsd:sequence>
</xsd:complexType>

<xsd:complexType name="group-sequence-provider">
<xsd:annotation>
<xsd:documentation><![CDATA[
Defines the name of the group sequence provider for a class.
]]></xsd:documentation>
</xsd:annotation>
<xsd:sequence>
<xsd:element name="value" type="value" minOccurs="0" maxOccurs="unbounded" />
</xsd:sequence>
</xsd:complexType>

<xsd:complexType name="property">
<xsd:annotation>
<xsd:documentation><![CDATA[
Expand All @@ -93,7 +96,7 @@
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>

<xsd:complexType name="getter">
<xsd:annotation>
<xsd:documentation><![CDATA[
Expand All @@ -106,14 +109,14 @@
</xsd:sequence>
<xsd:attribute name="property" type="xsd:string" use="required" />
</xsd:complexType>

<xsd:complexType name="constraint" mixed="true">
<xsd:annotation>
<xsd:documentation><![CDATA[
Contains a constraint definition. The name of the constraint should be
given in the "name" option.
May contain a single value, multiple "constraint" elements,
May contain a single value, multiple "constraint" elements,
multiple "value" elements or multiple "option" elements.
]]></xsd:documentation>
</xsd:annotation>
Expand All @@ -122,15 +125,15 @@
<xsd:element name="option" type="option" minOccurs="1" maxOccurs="unbounded" />
<xsd:element name="value" type="value" minOccurs="1" maxOccurs="unbounded" />
</xsd:choice>
<xsd:attribute name="name" type="xsd:string" use="required" />
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>

<xsd:complexType name="option" mixed="true">
<xsd:annotation>
<xsd:documentation><![CDATA[
Contains a constraint option definition. The name of the option
should be given in the "name" option.
May contain a single value, multiple "value" elements or multiple
"constraint" elements.
]]></xsd:documentation>
Expand All @@ -139,14 +142,14 @@
<xsd:element name="constraint" type="constraint" minOccurs="1" maxOccurs="unbounded" />
<xsd:element name="value" type="value" minOccurs="1" maxOccurs="unbounded" />
</xsd:choice>
<xsd:attribute name="name" type="xsd:string" use="required" />
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>

<xsd:complexType name="value" mixed="true">
<xsd:annotation>
<xsd:documentation><![CDATA[
A value of an element.
May contain a single value, multiple "value" elements or multiple
"constraint" elements.
]]></xsd:documentation>
Expand All @@ -155,6 +158,6 @@
<xsd:element name="constraint" type="constraint" minOccurs="1" maxOccurs="unbounded" />
<xsd:element name="value" type="value" minOccurs="1" maxOccurs="unbounded" />
</xsd:choice>
<xsd:attribute name="key" type="xsd:string" use="optional" />
<xsd:attribute name="key" type="xsd:string" use="optional" />
</xsd:complexType>
</xsd:schema>
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?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\Constraints;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Validator\Constraints\GroupSequence;
use Symfony\Component\Validator\Constraints\GroupSequenceProvider;
use Symfony\Component\Validator\Tests\Dummy\DummyGroupSequenceProvider;

class GroupSequenceProviderTest extends TestCase
{
public function testCreate()
{
$sequence = new GroupSequenceProvider(['class' => DummyGroupSequenceProvider::class]);

$this->assertSame(DummyGroupSequenceProvider::class, $sequence->class);
}

public function testCreateAttributeStyle()
{
$sequence = new GroupSequenceProvider(class: DummyGroupSequenceProvider::class);

$this->assertSame(DummyGroupSequenceProvider::class, $sequence->class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?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.
*/

use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\Validator\ContainerGroupSequenceProviderFactory;
use Symfony\Component\Validator\Exception\GroupDefinitionException;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
use Symfony\Component\Validator\Tests\Dummy\DummyGroupSequenceProvider;

class ContainerGroupSequenceProviderFactoryTest extends TestCase
{
public function testGetInstanceCreatesProvider()
{
$factory = new ContainerGroupSequenceProviderFactory(new Container());
$this->assertInstanceOf(DummyGroupSequenceProvider::class, $factory->getInstance(DummyGroupSequenceProvider::class));
}

public function testGetInstanceReturnsExistingProvider()
{
$factory = new ContainerGroupSequenceProviderFactory(new Container());
$v1 = $factory->getInstance(DummyGroupSequenceProvider::class);
$v2 = $factory->getInstance(DummyGroupSequenceProvider::class);
$this->assertSame($v1, $v2);
}

public function testGetInstanceReturnsService()
{
$provider = new DummyGroupSequenceProvider();
$container = new Container();
$container->set(DummyGroupSequenceProvider::class, $provider);

$factory = new ContainerGroupSequenceProviderFactory($container);

$this->assertSame($provider, $factory->getInstance(DummyGroupSequenceProvider::class));
}

public function testGetInstanceProviderNotFound()
{
$this->expectException(GroupDefinitionException::class);

$factory = new ContainerGroupSequenceProviderFactory(new Container());
$factory->getInstance('Unknown\\Class');
}

public function testGetInstanceProviderDoNotImplementInterface()
{
$this->expectException(UnexpectedTypeException::class);

$factory = new ContainerGroupSequenceProviderFactory(new Container());
$factory->getInstance(stdClass::class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?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\DependencyInjection;

use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\ServiceLocator;
use Symfony\Component\Validator\DependencyInjection\AddGroupSequenceProvidersPass;

class AddGroupSequenceProvidersPassTest extends TestCase
{
public function testThatGroupSequenceProviderServicesAreProcessed()
{
$container = new ContainerBuilder();
$groupSequenceProviderFactory = $container->register('validator.group_sequence_provider_factory')
->addArgument([]);

$container->register('my_group_sequence_provider_service1', Provider1::class)
->addTag('validator.group_sequence_provider', ['alias' => 'my_group_sequence_provider_alias1']);
$container->register('my_group_sequence_provider_service2', Provider2::class)
->addTag('validator.group_sequence_provider');

$addGroupSequenceProvidersPass = new AddGroupSequenceProvidersPass();
$addGroupSequenceProvidersPass->process($container);

$locator = $container->getDefinition((string) $groupSequenceProviderFactory->getArgument(0));
$this->assertTrue(!$locator->isPublic() || $locator->isPrivate());
$expected = (new Definition(ServiceLocator::class, [[
Provider1::class => new ServiceClosureArgument(new Reference('my_group_sequence_provider_service1')),
'my_group_sequence_provider_alias1' => new ServiceClosureArgument(new Reference('my_group_sequence_provider_service1')),
Provider2::class => new ServiceClosureArgument(new Reference('my_group_sequence_provider_service2')),
]]))->addTag('container.service_locator')->setPublic(false);
$this->assertEquals($expected, $locator->setPublic(false));
}

public function testAbstractGroupSequenceProvider()
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('The service "my_abstract_group_sequence_provider" tagged "validator.group_sequence_provider" must not be abstract.');
$container = new ContainerBuilder();
$container->register('validator.group_sequence_provider_factory')
->addArgument([]);

$container->register('my_abstract_group_sequence_provider')
->setAbstract(true)
->addTag('validator.group_sequence_provider');

$addGroupSequenceProvidersPass = new AddGroupSequenceProvidersPass();
$addGroupSequenceProvidersPass->process($container);
}

public function testThatCompilerPassIsIgnoredIfThereIsNoGroupSequenceProviderFactoryDefinition()
{
$container = new ContainerBuilder();

$definitionsBefore = \count($container->getDefinitions());
$aliasesBefore = \count($container->getAliases());

$addGroupSequenceProvidersPass = new AddGroupSequenceProvidersPass();
$addGroupSequenceProvidersPass->process($container);

// the container is untouched (i.e. no new definitions or aliases)
$this->assertCount($definitionsBefore, $container->getDefinitions());
$this->assertCount($aliasesBefore, $container->getAliases());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?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\Dummy;

use Symfony\Component\Validator\Constraints\GroupSequence;
use Symfony\Component\Validator\GroupSequenceProviderInterface;

class DummyGroupSequenceProvider implements GroupSequenceProviderInterface
{
public function getGroupSequence(): array|GroupSequence
{
return ['foo', 'bar'];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/*
* This file is part of the Microservices bundle.
*
* (c) Yonel Ceruto <yonelceruto@gmail.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\Fixtures\Attribute;

use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Tests\Dummy\DummyGroupSequenceProvider;

#[Assert\GroupSequenceProvider(class: DummyGroupSequenceProvider::class)]
class GroupSequenceProviderDto
{
public string $firstName = '';
public string $lastName = '';
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?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.
*/

use PHPUnit\Framework\TestCase;
use Symfony\Component\Validator\GroupSequenceProviderFactory;
use Symfony\Component\Validator\Tests\Dummy\DummyGroupSequenceProvider;

class GroupSequenceProviderFactoryTest extends TestCase
{
public function testGetInstance()
{
$factory = new GroupSequenceProviderFactory();
$this->assertInstanceOf(DummyGroupSequenceProvider::class, $factory->getInstance(DummyGroupSequenceProvider::class));
}

public function testPredefinedGetInstance()
{
$provider = new DummyGroupSequenceProvider();
$factory = new GroupSequenceProviderFactory([DummyGroupSequenceProvider::class => $provider]);
$this->assertSame($provider, $factory->getInstance(DummyGroupSequenceProvider::class));
}
}

0 comments on commit 4680b1b

Please sign in to comment.