如何解决对自定义symfony约束进行单元测试
这应该非常简单,但是今天下午让我发疯了,对自定义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 "Symfony\Component\Validator\Context\ExecutionContextInterface::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 "Symfony\Component\Validator\Context\ExecutionContextInterface::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 "Symfony\Component\Validator\Context\ExecutionContextInterface::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 "Symfony\Component\Validator\Context\ExecutionContextInterface::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 "Symfony\Component\Validator\Context\ExecutionContextInterface::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 "Symfony\Component\Validator\Context\ExecutionContextInterface::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 "Symfony\Component\Validator\Context\ExecutionContextInterface::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 "Symfony\Component\Validator\Context\ExecutionContextInterface::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".
由于这些方法上的@internal
注释。因此,如果您将SymfonyTestsListener
使用的弃用设置为0,则会导致测试失败。
有人知道您应该如何进行测试而不会弃用吗?我一直在转圈。我只尝试了ExecutionContextInterface
的部分模拟并直接模拟了ExecutionContext
,并且没有区别(后者也标记为@internal
)。
我肯定有一个非常简单的解决方案,但我一直在兜圈子,搜索时发现的所有内容基本上都是使用PHPUnit的createMock来做的(按这个速度,我可能会做...)
解决方法
您似乎可以将其用作示例https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Validator/Tests/Constraints/BlankValidatorTest.php,它扩展了ConstraintValidatorTestCase ...,它当然只是将PHPUnit模拟用于某些事情,而将具体类用于其他事情,但是我想这是这样做的方法
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。