函数测试验证器:与其他属性进行比较

发布于 2025-02-11 21:25:08 字数 579 浏览 2 评论 0原文

基于示例 sillthan.htthan.html为我自己的验证器@ASSERT \量编写功能调查。 $金额应根据$货币进行验证。

我的问题是,如何在函数测试中设置$货币的值?我已经查看了包装符号/验证器的测试,但找不到线索。我已经为验证器编写了函数测试。但是我没有任何要求。

有人可以给我提示还是向我展示一个例子。

例子:

class Entity
{
     /**
     * @var \string
     * @Assert\Currency()
     */
    protected $currency;

     /**
     * @var \float
     * @Assert\Amount(propertyPath="currency")
     */
    protected $amount;
}

based on the example LessThan.html#propertypath I would like to write a FunctionalTest for my own validator @Assert\Amount. $amount is to be validated depending on $currency.

My question is, how can I set the value for $currency in the FunctionalTest for the Amount Validator? I have looked at the tests of the package symfony/validator but can't find a clue. I have already written FunctionalTests for validators. But I am not getting anywhere with this requirement.

Could someone give me a hint or show me an example.

Example:

class Entity
{
     /**
     * @var \string
     * @Assert\Currency()
     */
    protected $currency;

     /**
     * @var \float
     * @Assert\Amount(propertyPath="currency")
     */
    protected $amount;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

命比纸薄 2025-02-18 21:25:08

我找到了解决方案。我验证了完整的实体,并查看了验证器中的对象,并找到了完整的实体。

当我考虑如何测试时,我进行了一些研究,并发现了其他相同的测试。我不知道这是否是最好的解决方案,但是我现在可以编写测试。

CondectValidatTestCase

<?php
    
namespace App\Tests\Validator\Constraint;

use App\Entity\Entity;
use App\Validator\MinimumAmount;
use App\Validator\MinimumAmountValidator;
use Symfony\Component\Validator\Context\ExecutionContext;
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
use Symfony\Component\Validator\ConstraintValidatorInterface;

class MinimumAmountValidatorTest extends ConstraintValidatorTestCase
{
   /**
   * @var ExecutionContextInterface
   */
   protected $context;

    /**
    * @var ConstraintValidatorInterface
    */
    protected $validator;

    /**
    * @var Constraint
    */
    protected $constraint;

    protected function setUp(): void
    {
        $this->constraint = new Entity();
        $this->context = $this->createContext();
        $this->validator = $this->createValidator();
        $this->validator->initialize($this->context);
    }

    public function createValidator(): MinimumAmountValidator
    {
        return new MinimumAmountValidator();
    }

    public function createContext(): ExecutionContext
    {
        $myEntity = new Entity();
        $myEntity->setCurrency('EUR');

        $translator = $this->createMock(TranslatorInterface::class);
        $translator->expects($this->any())->method('trans')->willReturnArgument(0);
        $validator = $this->createMock(ValidatorInterface::class);

        $executionContext = new ExecutionContext($validator, $myEntity, $translator);
        $context->setNode('InvalidValue', null, null, 'property.path');
        $context->setConstraint($this->constraint);
        return $executionContext;
    }


    public function testValidation()
    {
        $this->validator->validate(40.00, new  MinimumAmount());
        $this->assertNoViolation();
    }
}
 

约束

<?php

namespace App\Validator;

use Symfony\Component\Validator\Constraint;

/**
 * @Annotation
 */
class MinimumAmount extends Constraint
{
    /**
     * @var string
     */
    public $message = '';

    /**
     * @return string
     */
    public function validatedBy(): string
    {
        return MinimumAmountValidator::class;
    }
}

验证器

<?php

namespace App\Validator;

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

class MinimumAmountValidator extends ConstraintValidator
{
    /**
     * @param float $value
     * @param Constraint $constraint
     *
     * @return void
     */
    public function validate($value, Constraint $constraint)
    {
        /**
         * @var Entity
         */
        $contextRoot = $this->context->getRoot();
        $currency = $contextRoot->getCurrency(); // EUR
        $amount = $value; // 40.00

        // Validation implementation

    }
}

I have found a solution. I validated the complete entity and looked at the object in the validator and found the complete entity.

When I thought about how to test it, I did some research and found other tests that did the same. I don't know if this is the best solution, but I can now write my tests.

ConstraintValidatorTestCase

<?php
    
namespace App\Tests\Validator\Constraint;

use App\Entity\Entity;
use App\Validator\MinimumAmount;
use App\Validator\MinimumAmountValidator;
use Symfony\Component\Validator\Context\ExecutionContext;
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
use Symfony\Component\Validator\ConstraintValidatorInterface;

class MinimumAmountValidatorTest extends ConstraintValidatorTestCase
{
   /**
   * @var ExecutionContextInterface
   */
   protected $context;

    /**
    * @var ConstraintValidatorInterface
    */
    protected $validator;

    /**
    * @var Constraint
    */
    protected $constraint;

    protected function setUp(): void
    {
        $this->constraint = new Entity();
        $this->context = $this->createContext();
        $this->validator = $this->createValidator();
        $this->validator->initialize($this->context);
    }

    public function createValidator(): MinimumAmountValidator
    {
        return new MinimumAmountValidator();
    }

    public function createContext(): ExecutionContext
    {
        $myEntity = new Entity();
        $myEntity->setCurrency('EUR');

        $translator = $this->createMock(TranslatorInterface::class);
        $translator->expects($this->any())->method('trans')->willReturnArgument(0);
        $validator = $this->createMock(ValidatorInterface::class);

        $executionContext = new ExecutionContext($validator, $myEntity, $translator);
        $context->setNode('InvalidValue', null, null, 'property.path');
        $context->setConstraint($this->constraint);
        return $executionContext;
    }


    public function testValidation()
    {
        $this->validator->validate(40.00, new  MinimumAmount());
        $this->assertNoViolation();
    }
}
 

Constraint

<?php

namespace App\Validator;

use Symfony\Component\Validator\Constraint;

/**
 * @Annotation
 */
class MinimumAmount extends Constraint
{
    /**
     * @var string
     */
    public $message = '';

    /**
     * @return string
     */
    public function validatedBy(): string
    {
        return MinimumAmountValidator::class;
    }
}

Validator

<?php

namespace App\Validator;

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

class MinimumAmountValidator extends ConstraintValidator
{
    /**
     * @param float $value
     * @param Constraint $constraint
     *
     * @return void
     */
    public function validate($value, Constraint $constraint)
    {
        /**
         * @var Entity
         */
        $contextRoot = $this->context->getRoot();
        $currency = $contextRoot->getCurrency(); // EUR
        $amount = $value; // 40.00

        // Validation implementation

    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文