Skip to content

[Validator] Add unique entity violation cause #23845

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Symfony/Bridge/Doctrine/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ CHANGELOG
-----

* added support for doctrine/dbal v2.6 types
* added cause of UniqueEntity constraint violation

3.1.0
-----
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ public function testValidateUniqueness()
->atPath('property.path.name')
->setParameter('{{ value }}', '"Foo"')
->setInvalidValue($entity2)
->setCause(array($entity1))
->setCode(UniqueEntity::NOT_UNIQUE_ERROR)
->assertRaised();
}
Expand All @@ -215,6 +216,7 @@ public function testValidateCustomErrorPath()
->atPath('property.path.bar')
->setParameter('{{ value }}', '"Foo"')
->setInvalidValue($entity2)
->setCause(array($entity1))
->setCode(UniqueEntity::NOT_UNIQUE_ERROR)
->assertRaised();
}
Expand Down Expand Up @@ -268,6 +270,7 @@ public function testValidateUniquenessWithIgnoreNullDisabled()
->atPath('property.path.name')
->setParameter('{{ value }}', '"Foo"')
->setInvalidValue('Foo')
->setCause(array($entity1))
->setCode(UniqueEntity::NOT_UNIQUE_ERROR)
->assertRaised();
}
Expand Down Expand Up @@ -346,6 +349,7 @@ public function testValidateUniquenessWithValidCustomErrorPath()
->atPath('property.path.name2')
->setParameter('{{ value }}', '"Bar"')
->setInvalidValue('Bar')
->setCause(array($entity1))
->setCode(UniqueEntity::NOT_UNIQUE_ERROR)
->assertRaised();
}
Expand Down Expand Up @@ -481,6 +485,7 @@ public function testAssociatedEntity()
->setParameter('{{ value }}', 'object("Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIntIdEntity") identified by (id => 1)')
->setInvalidValue($entity1)
->setCode(UniqueEntity::NOT_UNIQUE_ERROR)
->setCause(array($associated, $associated2))
->assertRaised();
}

Expand Down Expand Up @@ -517,6 +522,7 @@ public function testValidateUniquenessNotToStringEntityWithAssociatedEntity()
->atPath('property.path.single')
->setParameter('{{ value }}', $expectedValue)
->setInvalidValue($entity1)
->setCause(array($associated, $associated2))
->setCode(UniqueEntity::NOT_UNIQUE_ERROR)
->assertRaised();
}
Expand Down Expand Up @@ -575,6 +581,7 @@ public function testValidateUniquenessWithArrayValue()
->atPath('property.path.phoneNumbers')
->setParameter('{{ value }}', 'array')
->setInvalidValue(array(123))
->setCause(array($entity1))
->setCode(UniqueEntity::NOT_UNIQUE_ERROR)
->assertRaised();
}
Expand Down Expand Up @@ -652,6 +659,7 @@ public function testValidateInheritanceUniqueness()
->atPath('property.path.name')
->setInvalidValue('Foo')
->setCode('23bd9dbf-6b9b-41cd-a99e-4844bcf3077f')
->setCause(array($entity1))
->setParameters(array('{{ value }}' => '"Foo"'))
->assertRaised();
}
Expand Down Expand Up @@ -703,6 +711,7 @@ public function testValidateUniquenessWithCompositeObjectNoToStringIdEntity()
->atPath('property.path.objectOne')
->setParameter('{{ value }}', $expectedValue)
->setInvalidValue($objectOne)
->setCause(array($entity))
->setCode(UniqueEntity::NOT_UNIQUE_ERROR)
->assertRaised();
}
Expand Down Expand Up @@ -730,6 +739,43 @@ public function testValidateUniquenessWithCustomDoctrineTypeValue()
->atPath('property.path.name')
->setParameter('{{ value }}', $expectedValue)
->setInvalidValue($existingEntity->name)
->setCause(array($existingEntity))
->setCode(UniqueEntity::NOT_UNIQUE_ERROR)
->assertRaised();
}

/**
* This is a functional test as there is a large integration necessary to get the validator working.
*/
public function testValidateUniquenessCause()
{
$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')
->setParameter('{{ value }}', '"Foo"')
->setInvalidValue($entity2)
->setCause(array($entity1))
->setCode(UniqueEntity::NOT_UNIQUE_ERROR)
->assertRaised();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ public function validate($entity, Constraint $constraint)
->setParameter('{{ value }}', $this->formatWithIdentifiers($em, $class, $invalidValue))
->setInvalidValue($invalidValue)
->setCode(UniqueEntity::NOT_UNIQUE_ERROR)
->setCause($result)
->addViolation();
}

Expand Down