Hi need some help with uniting testing a Symfony 2.8 callback. I don't think I have set it up correctly as the test is passing when I know it should be failing
The entity setup:
The validate callback in the Contact entity:
/**
* Validation callback for contact
* @param \AppBundle\Entity\Contact $object
* @param ExecutionContextInterface $context
*/
public static function validate(Contact $object, ExecutionContextInterface $context)
{
/**
* Check if the country code is valid
*/
if ($object->isValidCountryCode() === false) {
$context->buildViolation('Cannot register in that country')
->atPath('country')
->addViolation();
}
}
The method isValidCountryCode in Contact entity:
/**
* Get a list of invalid country codes
* @return array Collection of invalid country codes
*/
public function getInvalidCountryCodes()
{
return array('IS');
}
The method that checks if the country code is invalid:
/**
* Check if the country code is valid
* @return boolean
*/
public function isValidCountryCode()
{
$invalidCountryCodes = $this->getInvalidCountryCodes();
if (in_array($this->getCountry()->getCode(), $invalidCountryCodes)) {
return false;
}
return true;
}
The validation.yml
AppBundle\Entity\Contact:
properties:
//...
country:
//..
- Callback:
callback: [ AppBundle\Entity\Contact, validate ]
groups: [ "AppBundle" ]
The test class:
//..
use Symfony\Component\Validator\Validation;
class CountryTest extends WebTestCase
{
//...
public function testValidate()
{
$country = new Country();
$country->setCode('IS');
$contact = new Contact();
$contact->setCountry($country);
$validator = Validation::createValidatorBuilder()->getValidator();
$errors = $validator->validate($contact);
$this->assertEquals(1, count($errors));
}
This test returns $errors with a count of 0 but it should be 1 as the country code 'IS' is invalid.
First problem is about the definition of the constraint in the yml files: you need to put the callback under the constraint section instead of properties, so change the validation.yml files as follow:
validation.yml
AppBundle\Entity\Contact:
constraints:
- Callback:
callback: [ AppBundle\Entity\Contact, validate ]
groups: [ "AppBundle" ]
Second in the testCase: you need to take the validator service from the container instead of create a new one with the builder: this object is not initializated with the object structure ect.
Third The callback constraints is defined for the AppBundle validation group only, so pass the validation group to the validator service (As third argument of the service).
So change the testClass as follow:
public function testValidate()
{
$country = new Country();
$country->setCode('IS');
$contact = new Contact();
$contact->setCountry($country);
// $validator = Validation::createValidatorBuilder()->getValidator();
$validator = $this->createClient()->getContainer()->get('validator');
$errors = $validator->validate($contact, null, ['AppBundle']);
$this->assertEquals(1, count($errors));
}
And the testcase became green.
Hope this help
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With