duan198727 2017-02-14 07:28
浏览 71

PHPUNIT - 返回我实例化2次的类的同一实例

I need help please because I am not an expert at PHPUNIT.

I feel that when I instantiate my Validator twice in my test class, that PHPUNIT ignores the 2nd instance ...

Here is my code: ```php

<?php

namespace Tests\Validation;

use PHPUnit\Framework\TestCase;
use Validation\Validator;

class ValidatorTest extends TestCase
{
    public function testIsNotValid()
    {
        $_POST['input_test'] = '1';

        $validator = new Validator();

        $validator->rules([
            'input_test' => [
                'alpha '=> true,  // "$_POST['input_test']" must be alpha
            ],
        ]);

        // "isValid()" must return "false", and PHPUNIT returns "false"
        $this->assertFalse($validator->isValid());

        // "count($validator->getErrors())" Must return "1", and PHPUNIT returns "1"
        $this->assertEquals(1, count($validator->getErrors()));
    }

    public function testIsValid()
    {
        $_POST['input_test'] = 'aaa';

        $validator = new Validator();

        $validator->rules([
            'input_test' => [
                'alpha' => true,  // "$_POST['input_test']" must be alpha
            ],
        ]);

       // "isValid()" must return "true", but PHPUNIT returns "false"
        $this->assertTrue($validator->isValid());

        // "count($validator->getErrors())" must return "0", but PHPUNIT returns "1"
        $this->assertEquals(0, count($validator->getErrors()));
    }

}

``` So my test "testIsValid()" does not work properly. On the other hand, if I delete the test "testIsNotValid()", the test "testIsValid()" starts to work ...

Anyone have an idea?

Thank you very much.

  • 写回答

1条回答 默认 最新

  • dsf6281 2017-02-14 08:08
    关注

    Thank, here my validator class :

    <?php
    
    class Validator implements ValidatorInterface
    {
        /**
         * Name du input
         *
         * @var string
         */
        private $input;
    
        /**
         * Valeur des rules qu'on passe à un input
         *
         * @var mixed
         */
        private $value;
    
        /**
         * POST ou GET - Sera à POST par defaut
         *
         * @var mixed
         */
        private $requestMethod;
    
        /**
         * Contiendra les éventuels erreurs
         *
         * @var array
         */
        private $errors = [];
    
        /**
         * Validator constructor.
         *
         * @param null $requestMethod
         */
        public function __construct()
        {        
            $this->requestMethod = $_POST;
        }
    
        /**
         * Vérification des données soumises
         *
         * @param array $array
         */
        public function rules(array $array)
        {
            foreach ($array as $input => $rules) {
                $this->input = $input;
    
                if (is_array($rules)) {                
                    foreach ($rules as $rule => $value) {
                        if ($rule != 'label') {
                            if ($rule == 'required' || $rule == 'file' || isset($this->requestMethod[$this->input])) {
                                $this->value = $value;
    
                                $this->callRule($rule);
                            }
                        }
                    }
                }
            }
        }
    
        /**
         * Appeler la règle de validation
         *
         * @param string $rule
         */
        protected function callRule(string $rule)
        {
            $methodVerify = 'verify'.Str::convertSnakeCaseToCamelCase($rule);
    
            if (method_exists($this, $methodVerify)) {
                $this->$methodVerify();
            }
        }
    
        /**
         * Vérifier que valeur entrée dans le champ est bien alphabétique
         */
        private function verifyAlpha()
        {
            if ($this->value === true && !preg_match(self::REGEX_ALPHA, $this->requestMethod[$this->input])) {
                $this->errors[$this->input] = $this->pushError('alpha');
            }
        }
    
        /**
         * @return bool - True si formulaire soumis est valide, false si pas valide
         */
        public function isValid(): bool
        {
            return count($this->errors) === 0;
        }
    }
    

    Thank you

    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看