Last active
November 12, 2016 11:19
-
-
Save pinkeen/7a3b6688efc3c493fc16807ac89d3a3f to your computer and use it in GitHub Desktop.
Test for getting a violation error name from symfony validator's ConstraintViolation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use PHPUnit\Framework\TestCase; | |
use Symfony\Component\Validator\Constraint; | |
use Symfony\Component\Validator\ConstraintViolation; | |
use Symfony\Component\Validator\Validation; | |
use Symfony\Component\Validator\Constraints as Assert; | |
class ViolationErrorNameTest extends TestCase | |
{ | |
private function validate($data, $constraints) | |
{ | |
return Validation::createValidator()->validate($data, $constraints); | |
} | |
public function testProperConstraintIsAttachedToViolation() | |
{ | |
$violations = $this->validate( | |
[ | |
'name' => 'Testah', | |
], | |
[ | |
new Assert\Collection([ | |
'fields' => [ | |
'name' => new Assert\NotBlank(), | |
'email' => [] | |
], | |
'allowMissingFields' => false, | |
]) | |
] | |
); | |
$this->assertCount(1, $violations); | |
/** @var ConstraintViolation $violation */ | |
$violation = $violations->get(0); | |
$this->assertInstanceOf(ConstraintViolation::class, $violation); | |
$this->assertEquals('[email]', $violation->getPropertyPath()); | |
$this->assertEquals(Assert\Collection::MISSING_FIELD_ERROR, $violation->getCode()); | |
/* We should receive an instance of Assert\Collection, because this is the constraint | |
* That actuall failed. Instead Assert\NotBlank is returned which is wrong because | |
* it couldn't have failed at all. */ | |
$this->assertInstanceOf(Assert\Collection::class, $violation->getConstraint()); | |
} | |
public function testCanGetViolationErrorName() | |
{ | |
$violations = $this->validate( | |
[ | |
'name' => 'Testah', | |
], | |
[ | |
new Assert\Collection([ | |
'fields' => [ | |
'name' => [ | |
new Assert\NotBlank(), | |
new Assert\Length(['min' => 3]) | |
], | |
'email' => [ | |
new Assert\NotBlank(), | |
new Assert\Email(), | |
] | |
], | |
'allowMissingFields' => false, | |
]) | |
] | |
); | |
/** @var ConstraintViolation $violation */ | |
foreach ($violations as $violation) { | |
$this->assertInstanceOf(ConstraintViolation::class, $violation); | |
/** @var Constraint $constraint */ | |
$constraint = $violation->getConstraint(); | |
$this->assertInstanceOf(Constraint::class, $constraint); | |
/* This line fails with: | |
* Symfony\Component\Validator\Exception\InvalidArgumentException: The error code "2fa2158c-2a7f-484b-98aa-975522539ff8" does not exist for constraint of type "Symfony\Component\Validator\Constraints\Length". | |
* It should not fail and the error name should be set to "MISSING_FIELD_ERROR". */ | |
$errorName = $constraint->getErrorName($violation->getCode()); | |
$this->assertInternalType('string', $errorName); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment