gpt4 book ai didi

unit-testing - 单元测试验证器约束

转载 作者:行者123 更新时间:2023-12-03 14:33:40 24 4
gpt4 key购买 nike

我正在使用 Symfony2 (2.6) PHPUnit。我想学习如何在 symfony 2.6 中对约束验证器进行单元测试

密码约束

<?php

namespace Test\MainBundle\Component\Validator\Constraints;

use Symfony\Component\Validator\Constraint;

class Password extends Constraint
{
public $message = "user.password_regex";
}

密码约束验证器
<?php

namespace Test\MainBundle\Component\Validator\Constraints;

use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;

class PasswordValidator extends ConstraintValidator
{
public function validate($value, Constraint $constraint)
{
if (!preg_match('/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z0-9]{6,}+$/', $value, $matches)) {
$this->context->buildViolation($constraint->message)
->setParameter('%string%', $value)
->addViolation();
}
}

我的测试尝试
<?php

namespace Test\MainBundle\Tests\Component\Validator\Constraints;

use Test\MainBundle\Component\Validator\Constraints\Password;
use Test\MainBundle\Component\Validator\Constraints\PasswordValidator;

class PasswordTest extends \PHPUnit_Framework_TestCase
{
private $constraint;

public function setUp()
{
$this->constraint = new Password();
}

public function testFailureValidate()
{
$context = $this
->getMockBuilder('Symfony\Component\Validator\ExecutionContext')
->disableOriginalConstructor()
->getMock();
$context
->expects($this->once())
->method('buildViolation')
->with($this->constraint->message, array());

$validator = new PasswordValidator();

$validator->initialize($context);
$validator->validate('test', $this->constraint);
}

public function testSuccessValidate()
{
$validator = new PasswordValidator();
$context = $this
->getMockBuilder('Symfony\Component\Validator\ExecutionContext')
->disableOriginalConstructor()
->getMock();
$validator->initialize($context);
$validator->validate('Testing007', $this->constraint);
}
}

请你能帮我解决这个问题吗?

先感谢您。

如果您有关于 symfony2 应用程序中的单元测试的任何好的示例,我非常感兴趣。

最佳答案

我们可以很容易地模拟“setParameter”和“addViolation”方法

类似的东西:

...

/**
* @test
* @dataProvider invalidTextProvider
*/
public function invalidItems($text)
{
$context = $this->getMockExecutionContext();
$context->expects($this->once())
->method('buildViolation')
->with($this->constraint->message)
->willReturn($this->getMockConstraintViolationBuilder())
;

$this->validator->initialize($context);
$this->validator->validate($text, $this->constraint);
}

/**
* @return mixed
*/
private function getMockExecutionContext()
{
$context = $this->getMockBuilder('Symfony\Component\Validator\ExecutionContext')
->disableOriginalConstructor()
->setMethods(array('buildViolation'))
->getMock()
;
return $context;
}

private function getMockConstraintViolationBuilder()
{
$constraintViolationBuilder = $this->getMockBuilder('Symfony\Component\Validator\Violation\ConstraintViolationBuilder')
->disableOriginalConstructor()
->getMock()
;
$constraintViolationBuilder
->method('setParameter')
->willReturn($constraintViolationBuilder);
$constraintViolationBuilder
->method('addViolation');
return $constraintViolationBuilder;
}

...

关于unit-testing - 单元测试验证器约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28920210/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com