单元测试验证器约束

时间:2015-03-07 21:28:25

标签: unit-testing symfony

我正在使用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应用程序中有关于单元测试的任何好样本,我都很感兴趣。

1 个答案:

答案 0 :(得分:0)

我们可以很容易地模拟&#34; setParameter&#34;和&#34; addViolation&#34;方法

类似的东西:

...

/**
 * @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;
}

...