Файл: symfony-2.7/src/Symfony/Bridge/Doctrine/Tests/Validator/Constraints/UniqueEntityValidatorTest.php
Строк: 569
<?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 SymfonyBridgeDoctrineTestsValidatorConstraints;
use DoctrineCommonPersistenceManagerRegistry;
use DoctrineCommonPersistenceObjectManager;
use DoctrineCommonPersistenceObjectRepository;
use SymfonyBridgeDoctrineTestDoctrineTestHelper;
use SymfonyBridgeDoctrineTestsFixturesCompositeIntIdEntity;
use SymfonyBridgeDoctrineTestsFixturesSingleIntIdEntity;
use SymfonyBridgeDoctrineTestsFixturesDoubleNameEntity;
use SymfonyBridgeDoctrineTestsFixturesAssociationEntity;
use SymfonyBridgeDoctrineValidatorConstraintsUniqueEntity;
use SymfonyBridgeDoctrineValidatorConstraintsUniqueEntityValidator;
use SymfonyComponentValidatorTestsConstraintsAbstractConstraintValidatorTest;
use SymfonyComponentValidatorValidation;
use DoctrineORMToolsSchemaTool;
/**
* @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->getMock('DoctrineCommonPersistenceManagerRegistry');
$registry->expects($this->any())
->method('getManager')
->with($this->equalTo(self::EM_NAME))
->will($this->returnValue($em));
return $registry;
}
protected function createRepositoryMock()
{
$repository = $this->getMockBuilder('DoctrineCommonPersistenceObjectRepository')
->setMethods(array('findByCustom', 'find', 'findAll', 'findOneBy', 'findBy', 'getClassName'))
->getMock()
;
return $repository;
}
protected function createEntityManagerMock($repositoryMock)
{
$em = $this->getMockBuilder('DoctrineCommonPersistenceObjectManager')
->getMock()
;
$em->expects($this->any())
->method('getRepository')
->will($this->returnValue($repositoryMock))
;
$classMetadata = $this->getMock('DoctrineCommonPersistenceMappingClassMetadata');
$classMetadata
->expects($this->any())
->method('hasField')
->will($this->returnValue(true))
;
$reflParser = $this->getMockBuilder('DoctrineCommonReflectionStaticReflectionParser')
->disableOriginalConstructor()
->getMock()
;
$refl = $this->getMockBuilder('DoctrineCommonReflectionStaticReflectionProperty')
->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('SymfonyBridgeDoctrineTestsFixturesSingleIntIdEntity'),
$em->getClassMetadata('SymfonyBridgeDoctrineTestsFixturesDoubleNameEntity'),
$em->getClassMetadata('SymfonyBridgeDoctrineTestsFixturesCompositeIntIdEntity'),
$em->getClassMetadata('SymfonyBridgeDoctrineTestsFixturesAssociationEntity'),
));
}
/**
* 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')
->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')
->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 testValidateUniquenessWithIgnoreNull()
{
$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')
->assertRaised();
}
public function testValidateUniquenessAfterConsideringMultipleQueryResults()
{
$constraint = new UniqueEntity(array(
'message' => 'myMessage',
'fields' => array('name'),
'em' => self::EM_NAME,
));
$entity1 = new SingleIntIdEntity(1, 'Foo');
$entity2 = new SingleIntIdEntity(2, 'Foo');
$this->em->persist($entity1);
$this->em->persist($entity2);
$this->em->flush();
$this->validator->validate($entity1, $constraint);
$this->buildViolation('myMessage')
->atPath('property.path.name')
->setInvalidValue('Foo')
->assertRaised();
$this->context->getViolations()->remove(0);
$this->validator->validate($entity2, $constraint);
$this->buildViolation('myMessage')
->atPath('property.path.name')
->setInvalidValue('Foo')
->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();
}
/**
* @group GH-1635
*/
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(1)
->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 SymfonyComponentValidatorExceptionConstraintDefinitionException
* @expectedExceptionMessage Associated entities are not allowed to have more than one identifier field
* @group GH-1635
*/
public function testAssociatedCompositeEntity()
{
$constraint = new UniqueEntity(array(
'message' => 'myMessage',
'fields' => array('composite'),
'em' => self::EM_NAME,
));
$composite = new CompositeIntIdEntity(1, 1, "test");
$associated = new AssociationEntity();
$associated->composite = $composite;
$this->em->persist($composite);
$this->em->persist($associated);
$this->em->flush();
$this->validator->validate($associated, $constraint);
}
/**
* @expectedException SymfonyComponentValidatorExceptionConstraintDefinitionException
* @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 SymfonyComponentValidatorExceptionConstraintDefinitionException
* @expectedExceptionMessage Unable to find the object manager associated with an entity of class "SymfonyBridgeDoctrineTestsFixturesSingleIntIdEntity"
*/
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);
}
}