Update Composer, update everything

This commit is contained in:
Oliver Davies 2018-11-23 12:29:20 +00:00
parent ea3e94409f
commit dda5c284b6
19527 changed files with 1135420 additions and 351004 deletions

View file

@ -0,0 +1,79 @@
<?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\Mapping\Cache;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Validator\Mapping\Cache\CacheInterface;
use Symfony\Component\Validator\Mapping\ClassMetadata;
abstract class AbstractCacheTest extends TestCase
{
/**
* @var CacheInterface
*/
protected $cache;
public function testWrite()
{
$meta = $this->getMockBuilder(ClassMetadata::class)
->disableOriginalConstructor()
->setMethods(array('getClassName'))
->getMock();
$meta->expects($this->once())
->method('getClassName')
->will($this->returnValue('Foo\\Bar'));
$this->cache->write($meta);
$this->assertInstanceOf(
ClassMetadata::class,
$this->cache->read('Foo\\Bar'),
'write() stores metadata'
);
}
public function testHas()
{
$meta = $this->getMockBuilder(ClassMetadata::class)
->disableOriginalConstructor()
->setMethods(array('getClassName'))
->getMock();
$meta->expects($this->once())
->method('getClassName')
->will($this->returnValue('Foo\\Bar'));
$this->assertFalse($this->cache->has('Foo\\Bar'), 'has() returns false when there is no entry');
$this->cache->write($meta);
$this->assertTrue($this->cache->has('Foo\\Bar'), 'has() returns true when the is an entry');
}
public function testRead()
{
$meta = $this->getMockBuilder(ClassMetadata::class)
->disableOriginalConstructor()
->setMethods(array('getClassName'))
->getMock();
$meta->expects($this->once())
->method('getClassName')
->will($this->returnValue('Foo\\Bar'));
$this->assertFalse($this->cache->read('Foo\\Bar'), 'read() returns false when there is no entry');
$this->cache->write($meta);
$this->assertInstanceOf(ClassMetadata::class, $this->cache->read('Foo\\Bar'), 'read() returns metadata');
}
}

View file

@ -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\Mapping\Cache;
use Doctrine\Common\Cache\ArrayCache;
use Symfony\Component\Validator\Mapping\Cache\DoctrineCache;
class DoctrineCacheTest extends AbstractCacheTest
{
protected function setUp()
{
$this->cache = new DoctrineCache(new ArrayCache());
}
}

View file

@ -0,0 +1,26 @@
<?php
namespace Symfony\Component\Validator\Tests\Mapping\Cache;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
use Symfony\Component\Validator\Mapping\Cache\Psr6Cache;
use Symfony\Component\Validator\Mapping\ClassMetadata;
/**
* @author Kévin Dunglas <dunglas@gmail.com>
*/
class Psr6CacheTest extends AbstractCacheTest
{
protected function setUp()
{
$this->cache = new Psr6Cache(new ArrayAdapter());
}
public function testNameCollision()
{
$metadata = new ClassMetadata('Foo\\Bar');
$this->cache->write($metadata);
$this->assertFalse($this->cache->has('Foo_Bar'));
}
}

View file

@ -0,0 +1,323 @@
<?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\Mapping;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Constraints\Valid;
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Tests\Fixtures\ConstraintA;
use Symfony\Component\Validator\Tests\Fixtures\ConstraintB;
use Symfony\Component\Validator\Tests\Fixtures\PropertyConstraint;
class ClassMetadataTest extends TestCase
{
const CLASSNAME = 'Symfony\Component\Validator\Tests\Fixtures\Entity';
const PARENTCLASS = 'Symfony\Component\Validator\Tests\Fixtures\EntityParent';
const PROVIDERCLASS = 'Symfony\Component\Validator\Tests\Fixtures\GroupSequenceProviderEntity';
const PROVIDERCHILDCLASS = 'Symfony\Component\Validator\Tests\Fixtures\GroupSequenceProviderChildEntity';
protected $metadata;
protected function setUp()
{
$this->metadata = new ClassMetadata(self::CLASSNAME);
}
protected function tearDown()
{
$this->metadata = null;
}
public function testAddConstraintDoesNotAcceptValid()
{
$this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('Symfony\Component\Validator\Exception\ConstraintDefinitionException');
$this->metadata->addConstraint(new Valid());
}
public function testAddConstraintRequiresClassConstraints()
{
$this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('Symfony\Component\Validator\Exception\ConstraintDefinitionException');
$this->metadata->addConstraint(new PropertyConstraint());
}
public function testAddPropertyConstraints()
{
$this->metadata->addPropertyConstraint('firstName', new ConstraintA());
$this->metadata->addPropertyConstraint('lastName', new ConstraintB());
$this->assertEquals(array('firstName', 'lastName'), $this->metadata->getConstrainedProperties());
}
public function testAddMultiplePropertyConstraints()
{
$this->metadata->addPropertyConstraints('lastName', array(new ConstraintA(), new ConstraintB()));
$constraints = array(
new ConstraintA(array('groups' => array('Default', 'Entity'))),
new ConstraintB(array('groups' => array('Default', 'Entity'))),
);
$properties = $this->metadata->getPropertyMetadata('lastName');
$this->assertCount(1, $properties);
$this->assertEquals('lastName', $properties[0]->getName());
$this->assertEquals($constraints, $properties[0]->getConstraints());
}
public function testAddGetterConstraints()
{
$this->metadata->addGetterConstraint('lastName', new ConstraintA());
$this->metadata->addGetterConstraint('lastName', new ConstraintB());
$constraints = array(
new ConstraintA(array('groups' => array('Default', 'Entity'))),
new ConstraintB(array('groups' => array('Default', 'Entity'))),
);
$properties = $this->metadata->getPropertyMetadata('lastName');
$this->assertCount(1, $properties);
$this->assertEquals('getLastName', $properties[0]->getName());
$this->assertEquals($constraints, $properties[0]->getConstraints());
}
public function testAddMultipleGetterConstraints()
{
$this->metadata->addGetterConstraints('lastName', array(new ConstraintA(), new ConstraintB()));
$constraints = array(
new ConstraintA(array('groups' => array('Default', 'Entity'))),
new ConstraintB(array('groups' => array('Default', 'Entity'))),
);
$properties = $this->metadata->getPropertyMetadata('lastName');
$this->assertCount(1, $properties);
$this->assertEquals('getLastName', $properties[0]->getName());
$this->assertEquals($constraints, $properties[0]->getConstraints());
}
public function testMergeConstraintsMergesClassConstraints()
{
$parent = new ClassMetadata(self::PARENTCLASS);
$parent->addConstraint(new ConstraintA());
$this->metadata->mergeConstraints($parent);
$this->metadata->addConstraint(new ConstraintA());
$constraints = array(
new ConstraintA(array('groups' => array(
'Default',
'EntityParent',
'Entity',
))),
new ConstraintA(array('groups' => array(
'Default',
'Entity',
))),
);
$this->assertEquals($constraints, $this->metadata->getConstraints());
}
public function testMergeConstraintsMergesMemberConstraints()
{
$parent = new ClassMetadata(self::PARENTCLASS);
$parent->addPropertyConstraint('firstName', new ConstraintA());
$parent->addPropertyConstraint('firstName', new ConstraintB(array('groups' => 'foo')));
$this->metadata->mergeConstraints($parent);
$this->metadata->addPropertyConstraint('firstName', new ConstraintA());
$constraintA1 = new ConstraintA(array('groups' => array(
'Default',
'EntityParent',
'Entity',
)));
$constraintA2 = new ConstraintA(array('groups' => array(
'Default',
'Entity',
)));
$constraintB = new ConstraintB(array(
'groups' => array('foo'),
));
$constraints = array(
$constraintA1,
$constraintB,
$constraintA2,
);
$constraintsByGroup = array(
'Default' => array(
$constraintA1,
$constraintA2,
),
'EntityParent' => array(
$constraintA1,
),
'Entity' => array(
$constraintA1,
$constraintA2,
),
'foo' => array(
$constraintB,
),
);
$members = $this->metadata->getPropertyMetadata('firstName');
$this->assertCount(1, $members);
$this->assertEquals(self::PARENTCLASS, $members[0]->getClassName());
$this->assertEquals($constraints, $members[0]->getConstraints());
$this->assertEquals($constraintsByGroup, $members[0]->constraintsByGroup);
}
public function testMemberMetadatas()
{
$this->metadata->addPropertyConstraint('firstName', new ConstraintA());
$this->assertTrue($this->metadata->hasPropertyMetadata('firstName'));
$this->assertFalse($this->metadata->hasPropertyMetadata('non_existent_field'));
}
public function testMergeConstraintsKeepsPrivateMembersSeparate()
{
$parent = new ClassMetadata(self::PARENTCLASS);
$parent->addPropertyConstraint('internal', new ConstraintA());
$this->metadata->mergeConstraints($parent);
$this->metadata->addPropertyConstraint('internal', new ConstraintA());
$parentConstraints = array(
new ConstraintA(array('groups' => array(
'Default',
'EntityParent',
'Entity',
))),
);
$constraints = array(
new ConstraintA(array('groups' => array(
'Default',
'Entity',
))),
);
$members = $this->metadata->getPropertyMetadata('internal');
$this->assertCount(2, $members);
$this->assertEquals(self::PARENTCLASS, $members[0]->getClassName());
$this->assertEquals($parentConstraints, $members[0]->getConstraints());
$this->assertEquals(self::CLASSNAME, $members[1]->getClassName());
$this->assertEquals($constraints, $members[1]->getConstraints());
}
public function testGetReflectionClass()
{
$reflClass = new \ReflectionClass(self::CLASSNAME);
$this->assertEquals($reflClass, $this->metadata->getReflectionClass());
}
public function testSerialize()
{
$this->metadata->addConstraint(new ConstraintA(array('property1' => 'A')));
$this->metadata->addConstraint(new ConstraintB(array('groups' => 'TestGroup')));
$this->metadata->addPropertyConstraint('firstName', new ConstraintA());
$this->metadata->addGetterConstraint('lastName', new ConstraintB());
$metadata = unserialize(serialize($this->metadata));
$this->assertEquals($this->metadata, $metadata);
}
public function testGroupSequencesWorkIfContainingDefaultGroup()
{
$this->metadata->setGroupSequence(array('Foo', $this->metadata->getDefaultGroup()));
$this->assertInstanceOf('Symfony\Component\Validator\Constraints\GroupSequence', $this->metadata->getGroupSequence());
}
/**
* @expectedException \Symfony\Component\Validator\Exception\GroupDefinitionException
*/
public function testGroupSequencesFailIfNotContainingDefaultGroup()
{
$this->metadata->setGroupSequence(array('Foo', 'Bar'));
}
/**
* @expectedException \Symfony\Component\Validator\Exception\GroupDefinitionException
*/
public function testGroupSequencesFailIfContainingDefault()
{
$this->metadata->setGroupSequence(array('Foo', $this->metadata->getDefaultGroup(), Constraint::DEFAULT_GROUP));
}
/**
* @expectedException \Symfony\Component\Validator\Exception\GroupDefinitionException
*/
public function testGroupSequenceFailsIfGroupSequenceProviderIsSet()
{
$metadata = new ClassMetadata(self::PROVIDERCLASS);
$metadata->setGroupSequenceProvider(true);
$metadata->setGroupSequence(array('GroupSequenceProviderEntity', 'Foo'));
}
/**
* @expectedException \Symfony\Component\Validator\Exception\GroupDefinitionException
*/
public function testGroupSequenceProviderFailsIfGroupSequenceIsSet()
{
$metadata = new ClassMetadata(self::PROVIDERCLASS);
$metadata->setGroupSequence(array('GroupSequenceProviderEntity', 'Foo'));
$metadata->setGroupSequenceProvider(true);
}
/**
* @expectedException \Symfony\Component\Validator\Exception\GroupDefinitionException
*/
public function testGroupSequenceProviderFailsIfDomainClassIsInvalid()
{
$metadata = new ClassMetadata('stdClass');
$metadata->setGroupSequenceProvider(true);
}
public function testGroupSequenceProvider()
{
$metadata = new ClassMetadata(self::PROVIDERCLASS);
$metadata->setGroupSequenceProvider(true);
$this->assertTrue($metadata->isGroupSequenceProvider());
}
public function testMergeConstraintsMergesGroupSequenceProvider()
{
$parent = new ClassMetadata(self::PROVIDERCLASS);
$parent->setGroupSequenceProvider(true);
$metadata = new ClassMetadata(self::PROVIDERCHILDCLASS);
$metadata->mergeConstraints($parent);
$this->assertTrue($metadata->isGroupSequenceProvider());
}
/**
* https://github.com/symfony/symfony/issues/11604.
*/
public function testGetPropertyMetadataReturnsEmptyArrayWithoutConfiguredMetadata()
{
$this->assertCount(0, $this->metadata->getPropertyMetadata('foo'), '->getPropertyMetadata() returns an empty collection if no metadata is configured for the given property');
}
}

View file

@ -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\Mapping\Factory;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Validator\Mapping\Factory\BlackHoleMetadataFactory;
class BlackHoleMetadataFactoryTest extends TestCase
{
/**
* @expectedException \LogicException
*/
public function testGetMetadataForThrowsALogicException()
{
$metadataFactory = new BlackHoleMetadataFactory();
$metadataFactory->getMetadataFor('foo');
}
public function testHasMetadataForReturnsFalse()
{
$metadataFactory = new BlackHoleMetadataFactory();
$this->assertFalse($metadataFactory->hasMetadataFor('foo'));
}
}

View file

@ -0,0 +1,218 @@
<?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\Mapping\Factory;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Validator\Constraints\Callback;
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Mapping\Factory\LazyLoadingMetadataFactory;
use Symfony\Component\Validator\Mapping\Loader\LoaderInterface;
use Symfony\Component\Validator\Tests\Fixtures\ConstraintA;
class LazyLoadingMetadataFactoryTest extends TestCase
{
const CLASS_NAME = 'Symfony\Component\Validator\Tests\Fixtures\Entity';
const PARENT_CLASS = 'Symfony\Component\Validator\Tests\Fixtures\EntityParent';
const INTERFACE_A_CLASS = 'Symfony\Component\Validator\Tests\Fixtures\EntityInterfaceA';
const INTERFACE_B_CLASS = 'Symfony\Component\Validator\Tests\Fixtures\EntityInterfaceB';
const PARENT_INTERFACE_CLASS = 'Symfony\Component\Validator\Tests\Fixtures\EntityParentInterface';
public function testLoadClassMetadataWithInterface()
{
$factory = new LazyLoadingMetadataFactory(new TestLoader());
$metadata = $factory->getMetadataFor(self::PARENT_CLASS);
$constraints = array(
new ConstraintA(array('groups' => array('Default', 'EntityParent'))),
new ConstraintA(array('groups' => array('Default', 'EntityInterfaceA', 'EntityParent'))),
);
$this->assertEquals($constraints, $metadata->getConstraints());
}
public function testMergeParentConstraints()
{
$factory = new LazyLoadingMetadataFactory(new TestLoader());
$metadata = $factory->getMetadataFor(self::CLASS_NAME);
$constraints = array(
new ConstraintA(array('groups' => array(
'Default',
'Entity',
))),
new ConstraintA(array('groups' => array(
'Default',
'EntityParent',
'Entity',
))),
new ConstraintA(array('groups' => array(
'Default',
'EntityInterfaceA',
'EntityParent',
'Entity',
))),
new ConstraintA(array('groups' => array(
'Default',
'EntityInterfaceB',
'Entity',
))),
new ConstraintA(array('groups' => array(
'Default',
'EntityParentInterface',
'EntityInterfaceB',
'Entity',
))),
);
$this->assertEquals($constraints, $metadata->getConstraints());
}
public function testWriteMetadataToCache()
{
$cache = $this->getMockBuilder('Symfony\Component\Validator\Mapping\Cache\CacheInterface')->getMock();
$factory = new LazyLoadingMetadataFactory(new TestLoader(), $cache);
$parentClassConstraints = array(
new ConstraintA(array('groups' => array('Default', 'EntityParent'))),
new ConstraintA(array('groups' => array('Default', 'EntityInterfaceA', 'EntityParent'))),
);
$interfaceAConstraints = array(
new ConstraintA(array('groups' => array('Default', 'EntityInterfaceA'))),
);
$cache->expects($this->never())
->method('has');
$cache->expects($this->exactly(2))
->method('read')
->withConsecutive(
array($this->equalTo(self::PARENT_CLASS)),
array($this->equalTo(self::INTERFACE_A_CLASS))
)
->will($this->returnValue(false));
$cache->expects($this->exactly(2))
->method('write')
->withConsecutive(
$this->callback(function ($metadata) use ($interfaceAConstraints) {
return $interfaceAConstraints == $metadata->getConstraints();
}),
$this->callback(function ($metadata) use ($parentClassConstraints) {
return $parentClassConstraints == $metadata->getConstraints();
})
);
$metadata = $factory->getMetadataFor(self::PARENT_CLASS);
$this->assertEquals(self::PARENT_CLASS, $metadata->getClassName());
$this->assertEquals($parentClassConstraints, $metadata->getConstraints());
}
public function testReadMetadataFromCache()
{
$loader = $this->getMockBuilder('Symfony\Component\Validator\Mapping\Loader\LoaderInterface')->getMock();
$cache = $this->getMockBuilder('Symfony\Component\Validator\Mapping\Cache\CacheInterface')->getMock();
$factory = new LazyLoadingMetadataFactory($loader, $cache);
$metadata = new ClassMetadata(self::PARENT_CLASS);
$metadata->addConstraint(new ConstraintA());
$parentClass = self::PARENT_CLASS;
$interfaceClass = self::INTERFACE_A_CLASS;
$loader->expects($this->never())
->method('loadClassMetadata');
$cache->expects($this->never())
->method('has');
$cache->expects($this->exactly(2))
->method('read')
->withConsecutive(
array(self::PARENT_CLASS),
array(self::INTERFACE_A_CLASS)
)
->willReturnCallback(function ($name) use ($metadata, $parentClass, $interfaceClass) {
if ($parentClass == $name) {
return $metadata;
}
return new ClassMetadata($interfaceClass);
});
$this->assertEquals($metadata, $factory->getMetadataFor(self::PARENT_CLASS));
}
/**
* @expectedException \Symfony\Component\Validator\Exception\NoSuchMetadataException
*/
public function testNonClassNameStringValues()
{
$testedValue = 'error@example.com';
$loader = $this->getMockBuilder('Symfony\Component\Validator\Mapping\Loader\LoaderInterface')->getMock();
$cache = $this->getMockBuilder('Symfony\Component\Validator\Mapping\Cache\CacheInterface')->getMock();
$factory = new LazyLoadingMetadataFactory($loader, $cache);
$cache
->expects($this->never())
->method('read');
$factory->getMetadataFor($testedValue);
}
public function testMetadataCacheWithRuntimeConstraint()
{
$cache = $this->getMockBuilder('Symfony\Component\Validator\Mapping\Cache\CacheInterface')->getMock();
$factory = new LazyLoadingMetadataFactory(new TestLoader(), $cache);
$cache
->expects($this->any())
->method('write')
->will($this->returnCallback(function ($metadata) { serialize($metadata); }))
;
$cache->expects($this->any())
->method('read')
->will($this->returnValue(false));
$metadata = $factory->getMetadataFor(self::PARENT_CLASS);
$metadata->addConstraint(new Callback(function () {}));
$this->assertCount(3, $metadata->getConstraints());
$metadata = $factory->getMetadataFor(self::CLASS_NAME);
$this->assertCount(6, $metadata->getConstraints());
}
public function testGroupsFromParent()
{
$reader = new \Symfony\Component\Validator\Mapping\Loader\StaticMethodLoader();
$factory = new LazyLoadingMetadataFactory($reader);
$metadata = $factory->getMetadataFor('Symfony\Component\Validator\Tests\Fixtures\EntityStaticCarTurbo');
$groups = array();
foreach ($metadata->getPropertyMetadata('wheels') as $propertyMetadata) {
$constraints = $propertyMetadata->getConstraints();
$groups = array_replace($groups, $constraints[0]->groups);
}
$this->assertCount(4, $groups);
$this->assertContains('Default', $groups);
$this->assertContains('EntityStaticCarTurbo', $groups);
$this->assertContains('EntityStaticCar', $groups);
$this->assertContains('EntityStaticVehicle', $groups);
}
}
class TestLoader implements LoaderInterface
{
public function loadClassMetadata(ClassMetadata $metadata)
{
$metadata->addConstraint(new ConstraintA());
}
}

View file

@ -0,0 +1,72 @@
<?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\Mapping;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Validator\Mapping\GetterMetadata;
use Symfony\Component\Validator\Tests\Fixtures\Entity;
class GetterMetadataTest extends TestCase
{
const CLASSNAME = 'Symfony\Component\Validator\Tests\Fixtures\Entity';
public function testInvalidPropertyName()
{
$this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('Symfony\Component\Validator\Exception\ValidatorException');
new GetterMetadata(self::CLASSNAME, 'foobar');
}
public function testGetPropertyValueFromPublicGetter()
{
// private getters don't work yet because ReflectionMethod::setAccessible()
// does not exist yet in a stable PHP release
$entity = new Entity('foobar');
$metadata = new GetterMetadata(self::CLASSNAME, 'internal');
$this->assertEquals('foobar from getter', $metadata->getPropertyValue($entity));
}
public function testGetPropertyValueFromOverriddenPublicGetter()
{
$entity = new Entity();
$metadata = new GetterMetadata(self::CLASSNAME, 'data');
$this->assertEquals('Overridden data', $metadata->getPropertyValue($entity));
}
public function testGetPropertyValueFromIsser()
{
$entity = new Entity();
$metadata = new GetterMetadata(self::CLASSNAME, 'valid', 'isValid');
$this->assertEquals('valid', $metadata->getPropertyValue($entity));
}
public function testGetPropertyValueFromHasser()
{
$entity = new Entity();
$metadata = new GetterMetadata(self::CLASSNAME, 'permissions');
$this->assertEquals('permissions', $metadata->getPropertyValue($entity));
}
/**
* @expectedException \Symfony\Component\Validator\Exception\ValidatorException
* @expectedExceptionMessage The hasLastName() method does not exist in class Symfony\Component\Validator\Tests\Fixtures\Entity.
*/
public function testUndefinedMethodNameThrowsException()
{
new GetterMetadata(self::CLASSNAME, 'lastName', 'hasLastName');
}
}

View file

@ -0,0 +1,19 @@
<?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\Mapping\Loader;
use Symfony\Component\Validator\Mapping\ClassMetadata;
abstract class AbstractStaticMethodLoader
{
abstract public static function loadMetadata(ClassMetadata $metadata);
}

View file

@ -0,0 +1,169 @@
<?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\Mapping\Loader;
use Doctrine\Common\Annotations\AnnotationReader;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Validator\Constraints\All;
use Symfony\Component\Validator\Constraints\Callback;
use Symfony\Component\Validator\Constraints\Choice;
use Symfony\Component\Validator\Constraints\Collection;
use Symfony\Component\Validator\Constraints\IsTrue;
use Symfony\Component\Validator\Constraints\NotNull;
use Symfony\Component\Validator\Constraints\Range;
use Symfony\Component\Validator\Constraints\Valid;
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Mapping\Loader\AnnotationLoader;
use Symfony\Component\Validator\Tests\Fixtures\ConstraintA;
class AnnotationLoaderTest extends TestCase
{
public function testLoadClassMetadataReturnsTrueIfSuccessful()
{
$reader = new AnnotationReader();
$loader = new AnnotationLoader($reader);
$metadata = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\Entity');
$this->assertTrue($loader->loadClassMetadata($metadata));
}
public function testLoadClassMetadataReturnsFalseIfNotSuccessful()
{
$loader = new AnnotationLoader(new AnnotationReader());
$metadata = new ClassMetadata('\stdClass');
$this->assertFalse($loader->loadClassMetadata($metadata));
}
public function testLoadClassMetadata()
{
$loader = new AnnotationLoader(new AnnotationReader());
$metadata = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\Entity');
$loader->loadClassMetadata($metadata);
$expected = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\Entity');
$expected->setGroupSequence(array('Foo', 'Entity'));
$expected->addConstraint(new ConstraintA());
$expected->addConstraint(new Callback(array('Symfony\Component\Validator\Tests\Fixtures\CallbackClass', 'callback')));
$expected->addConstraint(new Callback(array('callback' => 'validateMe', 'payload' => 'foo')));
$expected->addConstraint(new Callback('validateMeStatic'));
$expected->addPropertyConstraint('firstName', new NotNull());
$expected->addPropertyConstraint('firstName', new Range(array('min' => 3)));
$expected->addPropertyConstraint('firstName', new All(array(new NotNull(), new Range(array('min' => 3)))));
$expected->addPropertyConstraint('firstName', new All(array('constraints' => array(new NotNull(), new Range(array('min' => 3))))));
$expected->addPropertyConstraint('firstName', new Collection(array('fields' => array(
'foo' => array(new NotNull(), new Range(array('min' => 3))),
'bar' => new Range(array('min' => 5)),
))));
$expected->addPropertyConstraint('firstName', new Choice(array(
'message' => 'Must be one of %choices%',
'choices' => array('A', 'B'),
)));
$expected->addPropertyConstraint('childA', new Valid());
$expected->addPropertyConstraint('childB', new Valid());
$expected->addGetterConstraint('lastName', new NotNull());
$expected->addGetterMethodConstraint('valid', 'isValid', new IsTrue());
$expected->addGetterConstraint('permissions', new IsTrue());
// load reflection class so that the comparison passes
$expected->getReflectionClass();
$this->assertEquals($expected, $metadata);
}
/**
* Test MetaData merge with parent annotation.
*/
public function testLoadParentClassMetadata()
{
$loader = new AnnotationLoader(new AnnotationReader());
// Load Parent MetaData
$parent_metadata = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\EntityParent');
$loader->loadClassMetadata($parent_metadata);
$expected_parent = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\EntityParent');
$expected_parent->addPropertyConstraint('other', new NotNull());
$expected_parent->getReflectionClass();
$this->assertEquals($expected_parent, $parent_metadata);
}
/**
* Test MetaData merge with parent annotation.
*/
public function testLoadClassMetadataAndMerge()
{
$loader = new AnnotationLoader(new AnnotationReader());
// Load Parent MetaData
$parent_metadata = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\EntityParent');
$loader->loadClassMetadata($parent_metadata);
$metadata = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\Entity');
// Merge parent metaData.
$metadata->mergeConstraints($parent_metadata);
$loader->loadClassMetadata($metadata);
$expected_parent = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\EntityParent');
$expected_parent->addPropertyConstraint('other', new NotNull());
$expected_parent->getReflectionClass();
$expected = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\Entity');
$expected->mergeConstraints($expected_parent);
$expected->setGroupSequence(array('Foo', 'Entity'));
$expected->addConstraint(new ConstraintA());
$expected->addConstraint(new Callback(array('Symfony\Component\Validator\Tests\Fixtures\CallbackClass', 'callback')));
$expected->addConstraint(new Callback(array('callback' => 'validateMe', 'payload' => 'foo')));
$expected->addConstraint(new Callback('validateMeStatic'));
$expected->addPropertyConstraint('firstName', new NotNull());
$expected->addPropertyConstraint('firstName', new Range(array('min' => 3)));
$expected->addPropertyConstraint('firstName', new All(array(new NotNull(), new Range(array('min' => 3)))));
$expected->addPropertyConstraint('firstName', new All(array('constraints' => array(new NotNull(), new Range(array('min' => 3))))));
$expected->addPropertyConstraint('firstName', new Collection(array('fields' => array(
'foo' => array(new NotNull(), new Range(array('min' => 3))),
'bar' => new Range(array('min' => 5)),
))));
$expected->addPropertyConstraint('firstName', new Choice(array(
'message' => 'Must be one of %choices%',
'choices' => array('A', 'B'),
)));
$expected->addPropertyConstraint('childA', new Valid());
$expected->addPropertyConstraint('childB', new Valid());
$expected->addGetterConstraint('lastName', new NotNull());
$expected->addGetterMethodConstraint('valid', 'isValid', new IsTrue());
$expected->addGetterConstraint('permissions', new IsTrue());
// load reflection class so that the comparison passes
$expected->getReflectionClass();
$this->assertEquals($expected, $metadata);
}
public function testLoadGroupSequenceProviderAnnotation()
{
$loader = new AnnotationLoader(new AnnotationReader());
$metadata = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\GroupSequenceProviderEntity');
$loader->loadClassMetadata($metadata);
$expected = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\GroupSequenceProviderEntity');
$expected->setGroupSequenceProvider(true);
$expected->getReflectionClass();
$this->assertEquals($expected, $metadata);
}
}

View file

@ -0,0 +1,49 @@
<?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\Mapping\Loader;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Mapping\Loader\LoaderInterface;
class FilesLoaderTest extends TestCase
{
public function testCallsGetFileLoaderInstanceForeachPath()
{
$loader = $this->getFilesLoader($this->getFileLoader());
$this->assertEquals(4, $loader->getTimesCalled());
}
public function testCallsActualFileLoaderForMetadata()
{
$fileLoader = $this->getFileLoader();
$fileLoader->expects($this->exactly(4))
->method('loadClassMetadata');
$loader = $this->getFilesLoader($fileLoader);
$loader->loadClassMetadata(new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\Entity'));
}
public function getFilesLoader(LoaderInterface $loader)
{
return $this->getMockForAbstractClass('Symfony\Component\Validator\Tests\Fixtures\FilesLoader', array(array(
__DIR__.'/constraint-mapping.xml',
__DIR__.'/constraint-mapping.yaml',
__DIR__.'/constraint-mapping.test',
__DIR__.'/constraint-mapping.txt',
), $loader));
}
public function getFileLoader()
{
return $this->getMockBuilder('Symfony\Component\Validator\Mapping\Loader\LoaderInterface')->getMock();
}
}

View file

@ -0,0 +1,85 @@
<?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\Mapping\Loader;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Mapping\Loader\LoaderChain;
class LoaderChainTest extends TestCase
{
public function testAllLoadersAreCalled()
{
$metadata = new ClassMetadata('\stdClass');
$loader1 = $this->getMockBuilder('Symfony\Component\Validator\Mapping\Loader\LoaderInterface')->getMock();
$loader1->expects($this->once())
->method('loadClassMetadata')
->with($this->equalTo($metadata));
$loader2 = $this->getMockBuilder('Symfony\Component\Validator\Mapping\Loader\LoaderInterface')->getMock();
$loader2->expects($this->once())
->method('loadClassMetadata')
->with($this->equalTo($metadata));
$chain = new LoaderChain(array(
$loader1,
$loader2,
));
$chain->loadClassMetadata($metadata);
}
public function testReturnsTrueIfAnyLoaderReturnedTrue()
{
$metadata = new ClassMetadata('\stdClass');
$loader1 = $this->getMockBuilder('Symfony\Component\Validator\Mapping\Loader\LoaderInterface')->getMock();
$loader1->expects($this->any())
->method('loadClassMetadata')
->will($this->returnValue(true));
$loader2 = $this->getMockBuilder('Symfony\Component\Validator\Mapping\Loader\LoaderInterface')->getMock();
$loader2->expects($this->any())
->method('loadClassMetadata')
->will($this->returnValue(false));
$chain = new LoaderChain(array(
$loader1,
$loader2,
));
$this->assertTrue($chain->loadClassMetadata($metadata));
}
public function testReturnsFalseIfNoLoaderReturnedTrue()
{
$metadata = new ClassMetadata('\stdClass');
$loader1 = $this->getMockBuilder('Symfony\Component\Validator\Mapping\Loader\LoaderInterface')->getMock();
$loader1->expects($this->any())
->method('loadClassMetadata')
->will($this->returnValue(false));
$loader2 = $this->getMockBuilder('Symfony\Component\Validator\Mapping\Loader\LoaderInterface')->getMock();
$loader2->expects($this->any())
->method('loadClassMetadata')
->will($this->returnValue(false));
$chain = new LoaderChain(array(
$loader1,
$loader2,
));
$this->assertFalse($chain->loadClassMetadata($metadata));
}
}

View file

@ -0,0 +1,140 @@
<?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\Mapping\Loader;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Mapping\Loader\StaticMethodLoader;
use Symfony\Component\Validator\Tests\Fixtures\ConstraintA;
class StaticMethodLoaderTest extends TestCase
{
private $errorLevel;
protected function setUp()
{
$this->errorLevel = error_reporting();
}
protected function tearDown()
{
error_reporting($this->errorLevel);
}
public function testLoadClassMetadataReturnsTrueIfSuccessful()
{
$loader = new StaticMethodLoader('loadMetadata');
$metadata = new ClassMetadata(__NAMESPACE__.'\StaticLoaderEntity');
$this->assertTrue($loader->loadClassMetadata($metadata));
}
public function testLoadClassMetadataReturnsFalseIfNotSuccessful()
{
$loader = new StaticMethodLoader('loadMetadata');
$metadata = new ClassMetadata('\stdClass');
$this->assertFalse($loader->loadClassMetadata($metadata));
}
public function testLoadClassMetadata()
{
$loader = new StaticMethodLoader('loadMetadata');
$metadata = new ClassMetadata(__NAMESPACE__.'\StaticLoaderEntity');
$loader->loadClassMetadata($metadata);
$this->assertEquals(StaticLoaderEntity::$invokedWith, $metadata);
}
public function testLoadClassMetadataDoesNotRepeatLoadWithParentClasses()
{
$loader = new StaticMethodLoader('loadMetadata');
$metadata = new ClassMetadata(__NAMESPACE__.'\StaticLoaderDocument');
$loader->loadClassMetadata($metadata);
$this->assertCount(0, $metadata->getConstraints());
$loader = new StaticMethodLoader('loadMetadata');
$metadata = new ClassMetadata(__NAMESPACE__.'\BaseStaticLoaderDocument');
$loader->loadClassMetadata($metadata);
$this->assertCount(1, $metadata->getConstraints());
}
public function testLoadClassMetadataIgnoresInterfaces()
{
$loader = new StaticMethodLoader('loadMetadata');
$metadata = new ClassMetadata(__NAMESPACE__.'\StaticLoaderInterface');
$loader->loadClassMetadata($metadata);
$this->assertCount(0, $metadata->getConstraints());
}
public function testLoadClassMetadataInAbstractClasses()
{
$loader = new StaticMethodLoader('loadMetadata');
$metadata = new ClassMetadata(__NAMESPACE__.'\AbstractStaticLoader');
$loader->loadClassMetadata($metadata);
$this->assertCount(1, $metadata->getConstraints());
}
public function testLoadClassMetadataIgnoresAbstractMethods()
{
// Disable error reporting, as AbstractStaticMethodLoader produces a
// strict standards error
error_reporting(0);
$metadata = new ClassMetadata(__NAMESPACE__.'\AbstractStaticMethodLoader');
$loader = new StaticMethodLoader('loadMetadata');
$loader->loadClassMetadata($metadata);
$this->assertCount(0, $metadata->getConstraints());
}
}
interface StaticLoaderInterface
{
public static function loadMetadata(ClassMetadata $metadata);
}
abstract class AbstractStaticLoader
{
public static function loadMetadata(ClassMetadata $metadata)
{
$metadata->addConstraint(new ConstraintA());
}
}
class StaticLoaderEntity
{
public static $invokedWith = null;
public static function loadMetadata(ClassMetadata $metadata)
{
self::$invokedWith = $metadata;
}
}
class StaticLoaderDocument extends BaseStaticLoaderDocument
{
}
class BaseStaticLoaderDocument
{
public static function loadMetadata(ClassMetadata $metadata)
{
$metadata->addConstraint(new ConstraintA());
}
}

View file

@ -0,0 +1,136 @@
<?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\Mapping\Loader;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Validator\Constraints\All;
use Symfony\Component\Validator\Constraints\Callback;
use Symfony\Component\Validator\Constraints\Choice;
use Symfony\Component\Validator\Constraints\Collection;
use Symfony\Component\Validator\Constraints\IsTrue;
use Symfony\Component\Validator\Constraints\NotNull;
use Symfony\Component\Validator\Constraints\Range;
use Symfony\Component\Validator\Constraints\Regex;
use Symfony\Component\Validator\Constraints\Traverse;
use Symfony\Component\Validator\Exception\MappingException;
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Mapping\Loader\XmlFileLoader;
use Symfony\Component\Validator\Tests\Fixtures\ConstraintA;
use Symfony\Component\Validator\Tests\Fixtures\ConstraintB;
class XmlFileLoaderTest extends TestCase
{
public function testLoadClassMetadataReturnsTrueIfSuccessful()
{
$loader = new XmlFileLoader(__DIR__.'/constraint-mapping.xml');
$metadata = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\Entity');
$this->assertTrue($loader->loadClassMetadata($metadata));
}
public function testLoadClassMetadataReturnsFalseIfNotSuccessful()
{
$loader = new XmlFileLoader(__DIR__.'/constraint-mapping.xml');
$metadata = new ClassMetadata('\stdClass');
$this->assertFalse($loader->loadClassMetadata($metadata));
}
public function testLoadClassMetadata()
{
$loader = new XmlFileLoader(__DIR__.'/constraint-mapping.xml');
$metadata = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\Entity');
$loader->loadClassMetadata($metadata);
$expected = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\Entity');
$expected->setGroupSequence(array('Foo', 'Entity'));
$expected->addConstraint(new ConstraintA());
$expected->addConstraint(new ConstraintB());
$expected->addConstraint(new Callback('validateMe'));
$expected->addConstraint(new Callback('validateMeStatic'));
$expected->addConstraint(new Callback(array('Symfony\Component\Validator\Tests\Fixtures\CallbackClass', 'callback')));
$expected->addConstraint(new Traverse(false));
$expected->addPropertyConstraint('firstName', new NotNull());
$expected->addPropertyConstraint('firstName', new Range(array('min' => 3)));
$expected->addPropertyConstraint('firstName', new Choice(array('A', 'B')));
$expected->addPropertyConstraint('firstName', new All(array(new NotNull(), new Range(array('min' => 3)))));
$expected->addPropertyConstraint('firstName', new All(array('constraints' => array(new NotNull(), new Range(array('min' => 3))))));
$expected->addPropertyConstraint('firstName', new Collection(array('fields' => array(
'foo' => array(new NotNull(), new Range(array('min' => 3))),
'bar' => array(new Range(array('min' => 5))),
))));
$expected->addPropertyConstraint('firstName', new Choice(array(
'message' => 'Must be one of %choices%',
'choices' => array('A', 'B'),
)));
$expected->addGetterConstraint('lastName', new NotNull());
$expected->addGetterConstraint('valid', new IsTrue());
$expected->addGetterConstraint('permissions', new IsTrue());
$this->assertEquals($expected, $metadata);
}
public function testLoadClassMetadataWithNonStrings()
{
$loader = new XmlFileLoader(__DIR__.'/constraint-mapping-non-strings.xml');
$metadata = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\Entity');
$loader->loadClassMetadata($metadata);
$expected = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\Entity');
$expected->addPropertyConstraint('firstName', new Regex(array('pattern' => '/^1/', 'match' => false)));
$properties = $metadata->getPropertyMetadata('firstName');
$constraints = $properties[0]->getConstraints();
$this->assertFalse($constraints[0]->match);
}
public function testLoadGroupSequenceProvider()
{
$loader = new XmlFileLoader(__DIR__.'/constraint-mapping.xml');
$metadata = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\GroupSequenceProviderEntity');
$loader->loadClassMetadata($metadata);
$expected = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\GroupSequenceProviderEntity');
$expected->setGroupSequenceProvider(true);
$this->assertEquals($expected, $metadata);
}
public function testThrowExceptionIfDocTypeIsSet()
{
$loader = new XmlFileLoader(__DIR__.'/withdoctype.xml');
$metadata = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\Entity');
$this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('\Symfony\Component\Validator\Exception\MappingException');
$loader->loadClassMetadata($metadata);
}
/**
* @see https://github.com/symfony/symfony/pull/12158
*/
public function testDoNotModifyStateIfExceptionIsThrown()
{
$loader = new XmlFileLoader(__DIR__.'/withdoctype.xml');
$metadata = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\Entity');
try {
$loader->loadClassMetadata($metadata);
} catch (MappingException $e) {
$this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('\Symfony\Component\Validator\Exception\MappingException');
$loader->loadClassMetadata($metadata);
}
}
}

View file

@ -0,0 +1,152 @@
<?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\Mapping\Loader;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Validator\Constraints\All;
use Symfony\Component\Validator\Constraints\Callback;
use Symfony\Component\Validator\Constraints\Choice;
use Symfony\Component\Validator\Constraints\Collection;
use Symfony\Component\Validator\Constraints\IsTrue;
use Symfony\Component\Validator\Constraints\NotNull;
use Symfony\Component\Validator\Constraints\Range;
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Mapping\Loader\YamlFileLoader;
use Symfony\Component\Validator\Tests\Fixtures\ConstraintA;
use Symfony\Component\Validator\Tests\Fixtures\ConstraintB;
class YamlFileLoaderTest extends TestCase
{
public function testLoadClassMetadataReturnsFalseIfEmpty()
{
$loader = new YamlFileLoader(__DIR__.'/empty-mapping.yml');
$metadata = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\Entity');
$this->assertFalse($loader->loadClassMetadata($metadata));
$r = new \ReflectionProperty($loader, 'classes');
$r->setAccessible(true);
$this->assertSame(array(), $r->getValue($loader));
}
/**
* @dataProvider provideInvalidYamlFiles
* @expectedException \InvalidArgumentException
*/
public function testInvalidYamlFiles($path)
{
$loader = new YamlFileLoader(__DIR__.'/'.$path);
$metadata = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\Entity');
$loader->loadClassMetadata($metadata);
}
public function provideInvalidYamlFiles()
{
return array(
array('nonvalid-mapping.yml'),
array('bad-format.yml'),
);
}
/**
* @see https://github.com/symfony/symfony/pull/12158
*/
public function testDoNotModifyStateIfExceptionIsThrown()
{
$loader = new YamlFileLoader(__DIR__.'/nonvalid-mapping.yml');
$metadata = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\Entity');
try {
$loader->loadClassMetadata($metadata);
} catch (\InvalidArgumentException $e) {
// Call again. Again an exception should be thrown
$this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('\InvalidArgumentException');
$loader->loadClassMetadata($metadata);
}
}
public function testLoadClassMetadataReturnsTrueIfSuccessful()
{
$loader = new YamlFileLoader(__DIR__.'/constraint-mapping.yml');
$metadata = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\Entity');
$this->assertTrue($loader->loadClassMetadata($metadata));
}
public function testLoadClassMetadataReturnsFalseIfNotSuccessful()
{
$loader = new YamlFileLoader(__DIR__.'/constraint-mapping.yml');
$metadata = new ClassMetadata('\stdClass');
$this->assertFalse($loader->loadClassMetadata($metadata));
}
public function testLoadClassMetadata()
{
$loader = new YamlFileLoader(__DIR__.'/constraint-mapping.yml');
$metadata = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\Entity');
$loader->loadClassMetadata($metadata);
$expected = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\Entity');
$expected->setGroupSequence(array('Foo', 'Entity'));
$expected->addConstraint(new ConstraintA());
$expected->addConstraint(new ConstraintB());
$expected->addConstraint(new Callback('validateMe'));
$expected->addConstraint(new Callback('validateMeStatic'));
$expected->addConstraint(new Callback(array('Symfony\Component\Validator\Tests\Fixtures\CallbackClass', 'callback')));
$expected->addPropertyConstraint('firstName', new NotNull());
$expected->addPropertyConstraint('firstName', new Range(array('min' => 3)));
$expected->addPropertyConstraint('firstName', new Choice(array('A', 'B')));
$expected->addPropertyConstraint('firstName', new All(array(new NotNull(), new Range(array('min' => 3)))));
$expected->addPropertyConstraint('firstName', new All(array('constraints' => array(new NotNull(), new Range(array('min' => 3))))));
$expected->addPropertyConstraint('firstName', new Collection(array('fields' => array(
'foo' => array(new NotNull(), new Range(array('min' => 3))),
'bar' => array(new Range(array('min' => 5))),
))));
$expected->addPropertyConstraint('firstName', new Choice(array(
'message' => 'Must be one of %choices%',
'choices' => array('A', 'B'),
)));
$expected->addGetterConstraint('lastName', new NotNull());
$expected->addGetterConstraint('valid', new IsTrue());
$expected->addGetterConstraint('permissions', new IsTrue());
$this->assertEquals($expected, $metadata);
}
public function testLoadClassMetadataWithConstants()
{
$loader = new YamlFileLoader(__DIR__.'/mapping-with-constants.yml');
$metadata = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\Entity');
$loader->loadClassMetadata($metadata);
$expected = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\Entity');
$expected->addPropertyConstraint('firstName', new Range(array('max' => PHP_INT_MAX)));
$this->assertEquals($expected, $metadata);
}
public function testLoadGroupSequenceProvider()
{
$loader = new YamlFileLoader(__DIR__.'/constraint-mapping.yml');
$metadata = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\GroupSequenceProviderEntity');
$loader->loadClassMetadata($metadata);
$expected = new ClassMetadata('Symfony\Component\Validator\Tests\Fixtures\GroupSequenceProviderEntity');
$expected->setGroupSequenceProvider(true);
$this->assertEquals($expected, $metadata);
}
}

View file

@ -0,0 +1,9 @@
namespaces:
custom: Symfony\Component\Validator\Tests\Fixtures\
Symfony\Component\Validator\Tests\Fixtures\Entity:
constraints:
# Custom constraint
- Symfony\Component\Validator\Tests\Fixtures\ConstraintA: ~
# Custom constraint with namespaces prefix
- "custom:ConstraintB": ~

View file

@ -0,0 +1,19 @@
<?xml version="1.0" ?>
<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">
<namespace prefix="custom">Symfony\Component\Validator\Tests\Fixtures\</namespace>
<class name="Symfony\Component\Validator\Tests\Fixtures\Entity">
<property name="firstName">
<!-- Constraint with a Boolean -->
<constraint name="Regex">
<option name="pattern">/^1/</option>
<option name="match">false</option>
</constraint>
</property>
</class>
</constraint-mapping>

View file

@ -0,0 +1,124 @@
<?xml version="1.0" ?>
<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">
<namespace prefix="custom">Symfony\Component\Validator\Tests\Fixtures\</namespace>
<class name="Symfony\Component\Validator\Tests\Fixtures\Entity">
<group-sequence>
<value>Foo</value>
<value>Entity</value>
</group-sequence>
<!-- CLASS CONSTRAINTS -->
<!-- Custom constraint -->
<constraint name="Symfony\Component\Validator\Tests\Fixtures\ConstraintA" />
<!-- Custom constraint with namespace abbreviation-->
<constraint name="custom:ConstraintB" />
<!-- Callbacks -->
<constraint name="Callback">validateMe</constraint>
<constraint name="Callback">validateMeStatic</constraint>
<constraint name="Callback">
<value>Symfony\Component\Validator\Tests\Fixtures\CallbackClass</value>
<value>callback</value>
</constraint>
<!-- Traverse with boolean default option -->
<constraint name="Traverse">
false
</constraint>
<!-- PROPERTY CONSTRAINTS -->
<property name="firstName">
<!-- Constraint without value -->
<constraint name="NotNull" />
<!-- Constraint with single value -->
<constraint name="Range">
<option name="min">3</option>
</constraint>
<!-- Constraint with multiple values -->
<constraint name="Choice">
<value>A</value>
<value>B</value>
</constraint>
<!-- Constraint with child constraints -->
<constraint name="All">
<constraint name="NotNull" />
<constraint name="Range">
<option name="min">3</option>
</constraint>
</constraint>
<!-- Option with child constraints -->
<constraint name="All">
<option name="constraints">
<constraint name="NotNull" />
<constraint name="Range">
<option name="min">3</option>
</constraint>
</option>
</constraint>
<!-- Value with child constraints -->
<constraint name="Collection">
<option name="fields">
<value key="foo">
<constraint name="NotNull" />
<constraint name="Range">
<option name="min">3</option>
</constraint>
</value>
<value key="bar">
<constraint name="Range">
<option name="min">5</option>
</constraint>
</value>
</option>
</constraint>
<!-- Constraint with options -->
<constraint name="Choice">
<!-- Option with single value -->
<option name="message"> Must be one of %choices% </option>
<!-- Option with multiple values -->
<option name="choices">
<value>A</value>
<value>B</value>
</option>
</constraint>
</property>
<!-- GETTER CONSTRAINTS -->
<getter property="lastName">
<constraint name="NotNull" />
</getter>
<getter property="valid">
<constraint name="IsTrue" />
</getter>
<getter property="permissions">
<constraint name="IsTrue" />
</getter>
</class>
<class name="Symfony\Component\Validator\Tests\Fixtures\GroupSequenceProviderEntity">
<!-- GROUP SEQUENCE PROVIDER -->
<group-sequence-provider />
</class>
</constraint-mapping>

View file

@ -0,0 +1,62 @@
namespaces:
custom: Symfony\Component\Validator\Tests\Fixtures\
Symfony\Component\Validator\Tests\Fixtures\Entity:
group_sequence:
- Foo
- Entity
constraints:
# Custom constraint
- Symfony\Component\Validator\Tests\Fixtures\ConstraintA: ~
# Custom constraint with namespaces prefix
- "custom:ConstraintB": ~
# Callbacks
- Callback: validateMe
- Callback: validateMeStatic
- Callback: [Symfony\Component\Validator\Tests\Fixtures\CallbackClass, callback]
properties:
firstName:
# Constraint without value
- NotNull: ~
# Constraint with single value
- Range:
min: 3
# Constraint with multiple values
- Choice: [A, B]
# Constraint with child constraints
- All:
- NotNull: ~
- Range:
min: 3
# Option with child constraints
- All:
constraints:
- NotNull: ~
- Range:
min: 3
# Value with child constraints
- Collection:
fields:
foo:
- NotNull: ~
- Range:
min: 3
bar:
- Range:
min: 5
# Constraint with options
- Choice: { choices: [A, B], message: Must be one of %choices% }
dummy:
getters:
lastName:
- NotNull: ~
valid:
- "IsTrue": ~
permissions:
- "IsTrue": ~
Symfony\Component\Validator\Tests\Fixtures\GroupSequenceProviderEntity:
group_sequence_provider: true

View file

@ -0,0 +1,8 @@
namespaces:
custom: Symfony\Component\Validator\Tests\Fixtures\
Symfony\Component\Validator\Tests\Fixtures\Entity:
properties:
firstName:
- Range:
max: !php/const PHP_INT_MAX

View file

@ -0,0 +1 @@
foo

View file

@ -0,0 +1,7 @@
<?xml version="1.0"?>
<!DOCTYPE foo>
<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">
<class name="Symfony\Component\Validator\Tests\Fixtures\Entity" />
</constraint-mapping>

View file

@ -0,0 +1,84 @@
<?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\Mapping;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Validator\Constraints\Valid;
use Symfony\Component\Validator\Mapping\MemberMetadata;
use Symfony\Component\Validator\Tests\Fixtures\ClassConstraint;
use Symfony\Component\Validator\Tests\Fixtures\ConstraintA;
use Symfony\Component\Validator\Tests\Fixtures\ConstraintB;
class MemberMetadataTest extends TestCase
{
protected $metadata;
protected function setUp()
{
$this->metadata = new TestMemberMetadata(
'Symfony\Component\Validator\Tests\Fixtures\Entity',
'getLastName',
'lastName'
);
}
protected function tearDown()
{
$this->metadata = null;
}
public function testAddConstraintRequiresClassConstraints()
{
$this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('Symfony\Component\Validator\Exception\ConstraintDefinitionException');
$this->metadata->addConstraint(new ClassConstraint());
}
public function testSerialize()
{
$this->metadata->addConstraint(new ConstraintA(array('property1' => 'A')));
$this->metadata->addConstraint(new ConstraintB(array('groups' => 'TestGroup')));
$metadata = unserialize(serialize($this->metadata));
$this->assertEquals($this->metadata, $metadata);
}
public function testSerializeCollectionCascaded()
{
$this->metadata->addConstraint(new Valid(array('traverse' => true)));
$metadata = unserialize(serialize($this->metadata));
$this->assertEquals($this->metadata, $metadata);
}
public function testSerializeCollectionNotCascaded()
{
$this->metadata->addConstraint(new Valid(array('traverse' => false)));
$metadata = unserialize(serialize($this->metadata));
$this->assertEquals($this->metadata, $metadata);
}
}
class TestMemberMetadata extends MemberMetadata
{
public function getPropertyValue($object)
{
}
protected function newReflectionMember($object)
{
}
}

View file

@ -0,0 +1,56 @@
<?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\Mapping;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Validator\Mapping\PropertyMetadata;
use Symfony\Component\Validator\Tests\Fixtures\Entity;
class PropertyMetadataTest extends TestCase
{
const CLASSNAME = 'Symfony\Component\Validator\Tests\Fixtures\Entity';
const PARENTCLASS = 'Symfony\Component\Validator\Tests\Fixtures\EntityParent';
public function testInvalidPropertyName()
{
$this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('Symfony\Component\Validator\Exception\ValidatorException');
new PropertyMetadata(self::CLASSNAME, 'foobar');
}
public function testGetPropertyValueFromPrivateProperty()
{
$entity = new Entity('foobar');
$metadata = new PropertyMetadata(self::CLASSNAME, 'internal');
$this->assertEquals('foobar', $metadata->getPropertyValue($entity));
}
public function testGetPropertyValueFromOverriddenPrivateProperty()
{
$entity = new Entity('foobar');
$metadata = new PropertyMetadata(self::PARENTCLASS, 'data');
$this->assertTrue($metadata->isPublic($entity));
$this->assertEquals('Overridden data', $metadata->getPropertyValue($entity));
}
public function testGetPropertyValueFromRemovedProperty()
{
$entity = new Entity('foobar');
$metadata = new PropertyMetadata(self::CLASSNAME, 'internal');
$metadata->name = 'test';
$this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}('Symfony\Component\Validator\Exception\ValidatorException');
$metadata->getPropertyValue($entity);
}
}