123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553 |
- <?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\Bridge\Doctrine\Tests\Validator\Constraints;
- use Doctrine\Common\Collections\ArrayCollection;
- use Doctrine\Common\Persistence\ManagerRegistry;
- use Doctrine\Common\Persistence\ObjectManager;
- use Doctrine\Common\Persistence\ObjectRepository;
- use Doctrine\ORM\Tools\SchemaTool;
- use Symfony\Bridge\Doctrine\Test\DoctrineTestHelper;
- use Symfony\Bridge\Doctrine\Tests\Fixtures\AssociationEntity;
- use Symfony\Bridge\Doctrine\Tests\Fixtures\DoubleNameEntity;
- use Symfony\Bridge\Doctrine\Tests\Fixtures\DoubleNullableNameEntity;
- use Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIntIdEntity;
- use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
- use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntityValidator;
- use Symfony\Component\Validator\Tests\Constraints\AbstractConstraintValidatorTest;
- use Symfony\Component\Validator\Validation;
- /**
- * @author Bernhard Schussek <bschussek@gmail.com>
- */
- class UniqueEntityValidatorTest extends AbstractConstraintValidatorTest
- {
- const EM_NAME = 'foo';
- /**
- * @var ObjectManager
- */
- protected $em;
- /**
- * @var ManagerRegistry
- */
- protected $registry;
- /**
- * @var ObjectRepository
- */
- protected $repository;
- protected function getApiVersion()
- {
- return Validation::API_VERSION_2_5;
- }
- protected function setUp()
- {
- $this->em = DoctrineTestHelper::createTestEntityManager();
- $this->registry = $this->createRegistryMock($this->em);
- $this->createSchema($this->em);
- parent::setUp();
- }
- protected function createRegistryMock(ObjectManager $em = null)
- {
- $registry = $this->getMockBuilder('Doctrine\Common\Persistence\ManagerRegistry')->getMock();
- $registry->expects($this->any())
- ->method('getManager')
- ->with($this->equalTo(self::EM_NAME))
- ->will($this->returnValue($em));
- return $registry;
- }
- protected function createRepositoryMock()
- {
- $repository = $this->getMockBuilder('Doctrine\Common\Persistence\ObjectRepository')
- ->setMethods(array('findByCustom', 'find', 'findAll', 'findOneBy', 'findBy', 'getClassName'))
- ->getMock()
- ;
- return $repository;
- }
- protected function createEntityManagerMock($repositoryMock)
- {
- $em = $this->getMockBuilder('Doctrine\Common\Persistence\ObjectManager')
- ->getMock()
- ;
- $em->expects($this->any())
- ->method('getRepository')
- ->will($this->returnValue($repositoryMock))
- ;
- $classMetadata = $this->getMockBuilder('Doctrine\Common\Persistence\Mapping\ClassMetadata')->getMock();
- $classMetadata
- ->expects($this->any())
- ->method('hasField')
- ->will($this->returnValue(true))
- ;
- $reflParser = $this->getMockBuilder('Doctrine\Common\Reflection\StaticReflectionParser')
- ->disableOriginalConstructor()
- ->getMock()
- ;
- $refl = $this->getMockBuilder('Doctrine\Common\Reflection\StaticReflectionProperty')
- ->setConstructorArgs(array($reflParser, 'property-name'))
- ->setMethods(array('getValue'))
- ->getMock()
- ;
- $refl
- ->expects($this->any())
- ->method('getValue')
- ->will($this->returnValue(true))
- ;
- $classMetadata->reflFields = array('name' => $refl);
- $em->expects($this->any())
- ->method('getClassMetadata')
- ->will($this->returnValue($classMetadata))
- ;
- return $em;
- }
- protected function createValidator()
- {
- return new UniqueEntityValidator($this->registry);
- }
- private function createSchema(ObjectManager $em)
- {
- $schemaTool = new SchemaTool($em);
- $schemaTool->createSchema(array(
- $em->getClassMetadata('Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIntIdEntity'),
- $em->getClassMetadata('Symfony\Bridge\Doctrine\Tests\Fixtures\DoubleNameEntity'),
- $em->getClassMetadata('Symfony\Bridge\Doctrine\Tests\Fixtures\DoubleNullableNameEntity'),
- $em->getClassMetadata('Symfony\Bridge\Doctrine\Tests\Fixtures\CompositeIntIdEntity'),
- $em->getClassMetadata('Symfony\Bridge\Doctrine\Tests\Fixtures\AssociationEntity'),
- ));
- }
- /**
- * This is a functional test as there is a large integration necessary to get the validator working.
- */
- public function testValidateUniqueness()
- {
- $constraint = new UniqueEntity(array(
- 'message' => 'myMessage',
- 'fields' => array('name'),
- 'em' => self::EM_NAME,
- ));
- $entity1 = new SingleIntIdEntity(1, 'Foo');
- $entity2 = new SingleIntIdEntity(2, 'Foo');
- $this->validator->validate($entity1, $constraint);
- $this->assertNoViolation();
- $this->em->persist($entity1);
- $this->em->flush();
- $this->validator->validate($entity1, $constraint);
- $this->assertNoViolation();
- $this->validator->validate($entity2, $constraint);
- $this->buildViolation('myMessage')
- ->atPath('property.path.name')
- ->setInvalidValue('Foo')
- ->setCode(UniqueEntity::NOT_UNIQUE_ERROR)
- ->assertRaised();
- }
- public function testValidateCustomErrorPath()
- {
- $constraint = new UniqueEntity(array(
- 'message' => 'myMessage',
- 'fields' => array('name'),
- 'em' => self::EM_NAME,
- 'errorPath' => 'bar',
- ));
- $entity1 = new SingleIntIdEntity(1, 'Foo');
- $entity2 = new SingleIntIdEntity(2, 'Foo');
- $this->em->persist($entity1);
- $this->em->flush();
- $this->validator->validate($entity2, $constraint);
- $this->buildViolation('myMessage')
- ->atPath('property.path.bar')
- ->setInvalidValue('Foo')
- ->setCode(UniqueEntity::NOT_UNIQUE_ERROR)
- ->assertRaised();
- }
- public function testValidateUniquenessWithNull()
- {
- $constraint = new UniqueEntity(array(
- 'message' => 'myMessage',
- 'fields' => array('name'),
- 'em' => self::EM_NAME,
- ));
- $entity1 = new SingleIntIdEntity(1, null);
- $entity2 = new SingleIntIdEntity(2, null);
- $this->em->persist($entity1);
- $this->em->persist($entity2);
- $this->em->flush();
- $this->validator->validate($entity1, $constraint);
- $this->assertNoViolation();
- }
- public function testValidateUniquenessWithIgnoreNullDisabled()
- {
- $constraint = new UniqueEntity(array(
- 'message' => 'myMessage',
- 'fields' => array('name', 'name2'),
- 'em' => self::EM_NAME,
- 'ignoreNull' => false,
- ));
- $entity1 = new DoubleNameEntity(1, 'Foo', null);
- $entity2 = new DoubleNameEntity(2, 'Foo', null);
- $this->validator->validate($entity1, $constraint);
- $this->assertNoViolation();
- $this->em->persist($entity1);
- $this->em->flush();
- $this->validator->validate($entity1, $constraint);
- $this->assertNoViolation();
- $this->validator->validate($entity2, $constraint);
- $this->buildViolation('myMessage')
- ->atPath('property.path.name')
- ->setInvalidValue('Foo')
- ->setCode(UniqueEntity::NOT_UNIQUE_ERROR)
- ->assertRaised();
- }
- /**
- * @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
- */
- public function testAllConfiguredFieldsAreCheckedOfBeingMappedByDoctrineWithIgnoreNullEnabled()
- {
- $constraint = new UniqueEntity(array(
- 'message' => 'myMessage',
- 'fields' => array('name', 'name2'),
- 'em' => self::EM_NAME,
- 'ignoreNull' => true,
- ));
- $entity1 = new SingleIntIdEntity(1, null);
- $this->validator->validate($entity1, $constraint);
- }
- public function testNoValidationIfFirstFieldIsNullAndNullValuesAreIgnored()
- {
- $constraint = new UniqueEntity(array(
- 'message' => 'myMessage',
- 'fields' => array('name', 'name2'),
- 'em' => self::EM_NAME,
- 'ignoreNull' => true,
- ));
- $entity1 = new DoubleNullableNameEntity(1, null, 'Foo');
- $entity2 = new DoubleNullableNameEntity(2, null, 'Foo');
- $this->validator->validate($entity1, $constraint);
- $this->assertNoViolation();
- $this->em->persist($entity1);
- $this->em->flush();
- $this->validator->validate($entity1, $constraint);
- $this->assertNoViolation();
- $this->validator->validate($entity2, $constraint);
- $this->assertNoViolation();
- }
- public function testValidateUniquenessWithValidCustomErrorPath()
- {
- $constraint = new UniqueEntity(array(
- 'message' => 'myMessage',
- 'fields' => array('name', 'name2'),
- 'em' => self::EM_NAME,
- 'errorPath' => 'name2',
- ));
- $entity1 = new DoubleNameEntity(1, 'Foo', 'Bar');
- $entity2 = new DoubleNameEntity(2, 'Foo', 'Bar');
- $this->validator->validate($entity1, $constraint);
- $this->assertNoViolation();
- $this->em->persist($entity1);
- $this->em->flush();
- $this->validator->validate($entity1, $constraint);
- $this->assertNoViolation();
- $this->validator->validate($entity2, $constraint);
- $this->buildViolation('myMessage')
- ->atPath('property.path.name2')
- ->setInvalidValue('Bar')
- ->setCode(UniqueEntity::NOT_UNIQUE_ERROR)
- ->assertRaised();
- }
- public function testValidateUniquenessUsingCustomRepositoryMethod()
- {
- $constraint = new UniqueEntity(array(
- 'message' => 'myMessage',
- 'fields' => array('name'),
- 'em' => self::EM_NAME,
- 'repositoryMethod' => 'findByCustom',
- ));
- $repository = $this->createRepositoryMock();
- $repository->expects($this->once())
- ->method('findByCustom')
- ->will($this->returnValue(array()))
- ;
- $this->em = $this->createEntityManagerMock($repository);
- $this->registry = $this->createRegistryMock($this->em);
- $this->validator = $this->createValidator();
- $this->validator->initialize($this->context);
- $entity1 = new SingleIntIdEntity(1, 'foo');
- $this->validator->validate($entity1, $constraint);
- $this->assertNoViolation();
- }
- public function testValidateUniquenessWithUnrewoundArray()
- {
- $constraint = new UniqueEntity(array(
- 'message' => 'myMessage',
- 'fields' => array('name'),
- 'em' => self::EM_NAME,
- 'repositoryMethod' => 'findByCustom',
- ));
- $entity = new SingleIntIdEntity(1, 'foo');
- $repository = $this->createRepositoryMock();
- $repository->expects($this->once())
- ->method('findByCustom')
- ->will(
- $this->returnCallback(function () use ($entity) {
- $returnValue = array(
- $entity,
- );
- next($returnValue);
- return $returnValue;
- })
- )
- ;
- $this->em = $this->createEntityManagerMock($repository);
- $this->registry = $this->createRegistryMock($this->em);
- $this->validator = $this->createValidator();
- $this->validator->initialize($this->context);
- $this->validator->validate($entity, $constraint);
- $this->assertNoViolation();
- }
- /**
- * @dataProvider resultTypesProvider
- */
- public function testValidateResultTypes($entity1, $result)
- {
- $constraint = new UniqueEntity(array(
- 'message' => 'myMessage',
- 'fields' => array('name'),
- 'em' => self::EM_NAME,
- 'repositoryMethod' => 'findByCustom',
- ));
- $repository = $this->createRepositoryMock();
- $repository->expects($this->once())
- ->method('findByCustom')
- ->will($this->returnValue($result))
- ;
- $this->em = $this->createEntityManagerMock($repository);
- $this->registry = $this->createRegistryMock($this->em);
- $this->validator = $this->createValidator();
- $this->validator->initialize($this->context);
- $this->validator->validate($entity1, $constraint);
- $this->assertNoViolation();
- }
- public function resultTypesProvider()
- {
- $entity = new SingleIntIdEntity(1, 'foo');
- return array(
- array($entity, array($entity)),
- array($entity, new \ArrayIterator(array($entity))),
- array($entity, new ArrayCollection(array($entity))),
- );
- }
- public function testAssociatedEntity()
- {
- $constraint = new UniqueEntity(array(
- 'message' => 'myMessage',
- 'fields' => array('single'),
- 'em' => self::EM_NAME,
- ));
- $entity1 = new SingleIntIdEntity(1, 'foo');
- $associated = new AssociationEntity();
- $associated->single = $entity1;
- $associated2 = new AssociationEntity();
- $associated2->single = $entity1;
- $this->em->persist($entity1);
- $this->em->persist($associated);
- $this->em->flush();
- $this->validator->validate($associated, $constraint);
- $this->assertNoViolation();
- $this->em->persist($associated2);
- $this->em->flush();
- $this->validator->validate($associated2, $constraint);
- $this->buildViolation('myMessage')
- ->atPath('property.path.single')
- ->setInvalidValue($entity1)
- ->setCode(UniqueEntity::NOT_UNIQUE_ERROR)
- ->assertRaised();
- }
- public function testAssociatedEntityWithNull()
- {
- $constraint = new UniqueEntity(array(
- 'message' => 'myMessage',
- 'fields' => array('single'),
- 'em' => self::EM_NAME,
- 'ignoreNull' => false,
- ));
- $associated = new AssociationEntity();
- $associated->single = null;
- $this->em->persist($associated);
- $this->em->flush();
- $this->validator->validate($associated, $constraint);
- $this->assertNoViolation();
- }
- /**
- * @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
- * @expectedExceptionMessage Object manager "foo" does not exist.
- */
- public function testDedicatedEntityManagerNullObject()
- {
- $constraint = new UniqueEntity(array(
- 'message' => 'myMessage',
- 'fields' => array('name'),
- 'em' => self::EM_NAME,
- ));
- $this->em = null;
- $this->registry = $this->createRegistryMock($this->em);
- $this->validator = $this->createValidator();
- $this->validator->initialize($this->context);
- $entity = new SingleIntIdEntity(1, null);
- $this->validator->validate($entity, $constraint);
- }
- /**
- * @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
- * @expectedExceptionMessage Unable to find the object manager associated with an entity of class "Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIntIdEntity"
- */
- public function testEntityManagerNullObject()
- {
- $constraint = new UniqueEntity(array(
- 'message' => 'myMessage',
- 'fields' => array('name'),
- // no "em" option set
- ));
- $this->em = null;
- $this->registry = $this->createRegistryMock($this->em);
- $this->validator = $this->createValidator();
- $this->validator->initialize($this->context);
- $entity = new SingleIntIdEntity(1, null);
- $this->validator->validate($entity, $constraint);
- }
- public function testValidateUniquenessOnNullResult()
- {
- $repository = $this->createRepositoryMock();
- $repository
- ->method('find')
- ->will($this->returnValue(null))
- ;
- $this->em = $this->createEntityManagerMock($repository);
- $this->registry = $this->createRegistryMock($this->em);
- $this->validator = $this->createValidator();
- $this->validator->initialize($this->context);
- $constraint = new UniqueEntity(array(
- 'message' => 'myMessage',
- 'fields' => array('name'),
- 'em' => self::EM_NAME,
- ));
- $entity = new SingleIntIdEntity(1, null);
- $this->em->persist($entity);
- $this->em->flush();
- $this->validator->validate($entity, $constraint);
- $this->assertNoViolation();
- }
- }
|