Laravel和PHPunit-表单请求验证-带有“需要”规则验证的奇怪问题

时间:2019-01-27 10:55:34

标签: laravel unit-testing phpunit laravel-validation

在Laravel 5.7项目中,我试图使用phpunit-framework验证LoginRequest类,并且在单元测试中遇到一个奇怪的问题。

LoginRequest看起来像这样:

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class LoginRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'user.identity' => 'required|between:3,254',
            'user.password' => 'required|between:8,36'
        ];
    }
}

我已经为我的登录请求创建了单元测试,以通过两种方法检查所有验证规则:

<?php

namespace Tests\Unit\Requests;

use App\Http\Requests\LoginRequest;
use Illuminate\Support\Str;
use Tests\TestCase;

class LoginRequestTest extends TestCase
{
    use PrepareValidator;

    private $loginUserUrl = '/api/users/login';


    public function setUp()
    {
        parent::setUp();
        $this->validationRules = (new LoginRequest())->rules();
    }

    public function testIdentityRule()
    {
        $this->assertTrue(
            $this->validateField('user.identity', 'userlogin')
        );
        $this->assertTrue(
            $this->validateField('user.identity', 'myemail@gmail.com')
        );
        $this->assertFalse(
            $this->validateField('user.identity', '')
        );
        $this->assertFalse(
            $this->validateField('user.identity', 'ut')
        );
        $this->assertFalse(
            $this->validateField('user.identity', str_random(255))
        );
    }

    public function testPasswordRule()
    {
        $this->assertTrue(
            $this->validateField('user.password', 'mypasswordqwerty111')
        );
        $this->assertFalse(
            $this->validateField('user.password', 'mobmob')
        );
        $this->assertFalse(
            $this->validateField('user.password', Str::random(37))
        );
    }
}

要检查所有验证规则,我已经为自己的目的创建了特殊特征like in this advise

<?php


namespace Tests\Unit\Requests;


use Illuminate\Support\Facades\Validator;

trait PrepareValidator
{
    private $validationRules;

    private function getFieldValidator($field, $value)
    {
        return Validator::make(
            [$field => $value],
            [$field => $this->validationRules[$field]]);
    }

    private function validateField($field, $value): bool
    {
        return $this->getFieldValidator($field, $value)->passes();
    }
}

问题:

LoginRequestTest中的每个方法,我试图在其中检查 require 规则的正确值-返回 false

$this->assertTrue(
            $this->validateField('user.identity', 'thisisthecorrectlogin')
        );

... // =>否

并且在testPasswordRule()方法中存在相同的问题:

$this->assertTrue(
            $this->validateField('user.password', 'thisIsMyPassword')
        );

// =>假

所有其他方法都可以正常工作,并且接受了被接受的错误,并且还存在正确的错误消息。

调试之后,我从 Validator pass方法收到了正确的消息,因为所有方法都接受正确的登录正确的密码发出他们的验证时收到此消息:

<?php

namespace Illuminate\Validation;

...

class Validator implements ValidatorContract
{

...
/**
 * Determine if the data passes the validation rules.
 *
 * @return bool
 */
public function passes()
    {
        $this->messages = new MessageBag;

        $this->distinctValues = [];

        foreach ($this->rules as $attribute => $rules) {
            $attribute = str_replace('\.', '->', $attribute);

            foreach ($rules as $rule) {
                $this->validateAttribute($attribute, $rule);

                if ($this->shouldStopValidating($attribute)) {
                    break;
                }
            }
        }

        foreach ($this->after as $after) {
            call_user_func($after);
        }
        return $this->messages->isEmpty();
    }

...

}

$ this->消息输出-//=>{"user.identity":["user.identity字段为必填项。“]} 正确的登录名必须为空并返回true。

$ this->消息输出-//=>{"user.password":["user.password字段为必填。“]} 还要输入正确的密码空并返回true。

您能提供一些建议吗-我做错了什么? ps。我已经阅读了数十个示例,没有任何解决方法。

0 个答案:

没有答案