单元测试自定义符号约束



这应该非常简单,但今天下午它让我抓狂,对自定义symfony验证器进行单元测试的正确方法是什么?我能找到的所有文章都和我做的方式完全一样

class Foo extends Constraint
{
public string $message = 'The string "{{ string }}" is not a valid foo.';
}
class FooValidator extends ConstraintValidator
{
/**
* {@inheritdoc}
*/
public function validate($value, Constraint $constraint): void
{
if ($value !== 'foo') {
$this->context->buildViolation($constraint->message)
->setParameter('{{ string }}', $value)
->addViolation();
}
}
}
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);
$builder->shouldReceive('setParameter')
->with('{{ string }}', 'foo-bar')
->andReturn($builder);
$builder->shouldReceive('addViolation');
$validator = new FooValidator();
$validator->initialize($context);
$validator->validate('foo-bar', $constraint);
}
}

这应该是有效的,事实上确实如此,但它会导致9个降级警告


1x: The "SymfonyComponentValidatorContextExecutionContextInterface::setGroup()" method is considered internal Used by the validator engine. Should not be called by user *           code. It may change without further notice. You should not extend it from "Mockery_0_Symfony_Component_Validator_Context_ExecutionContextInterface".
1x: The "SymfonyComponentValidatorContextExecutionContextInterface::setConstraint()" method is considered internal Used by the validator engine. Should not be called by user *           code. It may change without further notice. You should not extend it from "Mockery_0_Symfony_Component_Validator_Context_ExecutionContextInterface".
1x: The "SymfonyComponentValidatorContextExecutionContextInterface::markGroupAsValidated()" method is considered internal Used by the validator engine. Should not be called by user *           code. It may change without further notice. You should not extend it from "Mockery_0_Symfony_Component_Validator_Context_ExecutionContextInterface".
1x: The "SymfonyComponentValidatorContextExecutionContextInterface::isGroupValidated()" method is considered internal Used by the validator engine. Should not be called by user *           code. It may change without further notice. You should not extend it from "Mockery_0_Symfony_Component_Validator_Context_ExecutionContextInterface".
1x: The "SymfonyComponentValidatorContextExecutionContextInterface::markConstraintAsValidated()" method is considered internal Used by the validator engine. Should not be called by user *           code. It may change without further notice. You should not extend it from "Mockery_0_Symfony_Component_Validator_Context_ExecutionContextInterface".
1x: The "SymfonyComponentValidatorContextExecutionContextInterface::isConstraintValidated()" method is considered internal Used by the validator engine. Should not be called by user *           code. It may change without further notice. You should not extend it from "Mockery_0_Symfony_Component_Validator_Context_ExecutionContextInterface".
1x: The "SymfonyComponentValidatorContextExecutionContextInterface::markObjectAsInitialized()" method is considered internal Used by the validator engine. Should not be called by user *           code. It may change without further notice. You should not extend it from "Mockery_0_Symfony_Component_Validator_Context_ExecutionContextInterface".
1x: The "SymfonyComponentValidatorContextExecutionContextInterface::isObjectInitialized()" method is considered internal Used by the validator engine. Should not be called by user *           code. It may change without further notice. You should not extend it from "Mockery_0_Symfony_Component_Validator_Context_ExecutionContextInterface".

由于对那些方法的CCD_ 1注释。因此,如果使用的SymfonyTestsListener的弃用设置为0,则会导致测试失败。

有人知道你应该如何在不被贬低的情况下测试这一点吗?我一直在兜圈子。我只尝试了ExecutionContextInterface的部分模拟和直接模拟ExecutionContext,没有区别(后者也标记为@internal(。

我相信有一个非常简单的解决方案,但我一直在兜圈子,我在搜索时发现的一切基本上都是用PHPUnit的createMock做同样的事情(按照这个速度,我可能只做…(

似乎可以将其用作示例https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Validator/Tests/Constraints/BlankValidatorTest.php它扩展了ConstraintValidatorTestCase。。。诚然,它只是对某些事情使用PHPUnit mock,对其他事情使用具体类,但我想这就是

最新更新