单元测试 Symfony 回调
Unit testing Symfony callback
您好需要一些帮助来联合测试 Symfony 2.8 回调。
我认为我没有正确设置它,因为当我知道它应该失败时测试通过了
实体设置:
联系人实体中的验证回调:
/**
* 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();
}
}
联系人实体中的方法是 ValidCountryCode:
/**
* Get a list of invalid country codes
* @return array Collection of invalid country codes
*/
public function getInvalidCountryCodes()
{
return array('IS');
}
判断国家代码是否无效的方法:
/**
* 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;
}
validation.yml
AppBundle\Entity\Contact:
properties:
//...
country:
//..
- Callback:
callback: [ AppBundle\Entity\Contact, validate ]
groups: [ "AppBundle" ]
测试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));
}
此测试 returns $errors
计数为 0,但应为 1,因为国家/地区代码 'IS' 无效。
第一个问题是关于yml文件中约束的定义:您需要将callback放在constraint
部分而不是properties
,所以改变 validation.yml 文件如下:
validation.yml
AppBundle\Entity\Contact:
constraints:
- Callback:
callback: [ AppBundle\Entity\Contact, validate ]
groups: [ "AppBundle" ]
测试用例中的第二个:您需要从容器中获取验证器服务,而不是使用构建器创建一个新服务:此对象未使用对象结构等进行初始化.
Third 回调约束仅为 AppBundle
验证组定义,因此将验证组传递给验证器服务(作为服务的第三个参数)。
所以改变测试类如下:
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));
}
测试用例变为绿色。
希望对您有所帮助
您好需要一些帮助来联合测试 Symfony 2.8 回调。 我认为我没有正确设置它,因为当我知道它应该失败时测试通过了
实体设置:
联系人实体中的验证回调:
/**
* 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();
}
}
联系人实体中的方法是 ValidCountryCode:
/**
* Get a list of invalid country codes
* @return array Collection of invalid country codes
*/
public function getInvalidCountryCodes()
{
return array('IS');
}
判断国家代码是否无效的方法:
/**
* 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;
}
validation.yml
AppBundle\Entity\Contact:
properties:
//...
country:
//..
- Callback:
callback: [ AppBundle\Entity\Contact, validate ]
groups: [ "AppBundle" ]
测试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));
}
此测试 returns $errors
计数为 0,但应为 1,因为国家/地区代码 'IS' 无效。
第一个问题是关于yml文件中约束的定义:您需要将callback放在constraint
部分而不是properties
,所以改变 validation.yml 文件如下:
validation.yml
AppBundle\Entity\Contact:
constraints:
- Callback:
callback: [ AppBundle\Entity\Contact, validate ]
groups: [ "AppBundle" ]
测试用例中的第二个:您需要从容器中获取验证器服务,而不是使用构建器创建一个新服务:此对象未使用对象结构等进行初始化.
Third 回调约束仅为 AppBundle
验证组定义,因此将验证组传递给验证器服务(作为服务的第三个参数)。
所以改变测试类如下:
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));
}
测试用例变为绿色。
希望对您有所帮助