这应该非常简单,但今天下午它让我发疯,对自定义 symfony 验证器进行单元测试的正确方法是什么?我能找到的所有文章都与我的做法完全相同
class Foo extends Constraint
{
public string $message = 'The string "{{ string }}" is not a valid foo.';
}
Run Code Online (Sandbox Code Playgroud)
class FooValidator extends ConstraintValidator
{
/**
* {@inheritdoc}
*/
public function validate($value, Constraint $constraint): void
{
if ($value !== 'foo') {
$this->context->buildViolation($constraint->message)
->setParameter('{{ string }}', $value)
->addViolation();
}
}
}
Run Code Online (Sandbox Code Playgroud)
class FooValidatorTest extends TestCase
{
/** @test */
public function validate(): void
{
$this->expectNotToPerformAssertions();
$constraint = new Foo();
$context = \Mockery::mock(ExecutionContextInterface::class);
$builder = \Mockery::mock(ConstraintViolationBuilderInterface::class);
$context->shouldReceive('buildViolation')
->with($constraint->message)
->andReturn($this->builder); …Run Code Online (Sandbox Code Playgroud)