bltr
@bltr
Любитель

Правильно ли написал тест?

Класс UserRequest:
<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class UserRequest extends FormRequest
{
    public function authorize()
    {
        return true;
    }

    public function rules()
    {
        return [
            'name' => [
                'required',
                'max:50',
                Rule::unique('users')->ignore($this->route('user'))
            ],
            'email' => [
                'required',
                'max:50',
                'email',
                Rule::unique('users')->ignore($this->route('user'))
            ],
            'password' => 'sometimes|required|min:8|confirmed',
            'short_about_me' => 'max:100',
            'about_me' => 'max:1024'
        ];
    }
}

тест:
<?php

namespace Tests\Unit;

use App\Http\Requests\UserRequest;
use Illuminate\Validation\DatabasePresenceVerifier;
use Tests\TestCase;

class UserRequestTest extends TestCase
{
    private $mail = 'admin@mail.ru';
    private $presentMail = 'presenе@mail.ru';
    private $name = 'admin';
    private $presentName = 'present';

    private $rules;

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

    /**
     * @dataProvider dataForRulesTest
     */
    public function testRules($expectedResult, $data)
    {
        $this->assertEquals($expectedResult, $this->valid($data));
    }

    /**
     * @dataProvider dataForUniqueTest
     */
    public function testUniqueRules($expectedResult, $data)
    {
        $this->mocks();
        $this->assertEquals($expectedResult, $this->valid($data));
    }

    public function dataForRulesTest()
    {
        $mail = $this->mail;
        $name = $this->name;
        return [
            'name_required' =>  [false, ['name' => '', 'email' => $mail]],
            'name_max:50' =>    [false, ['name' => str_pad('', 51, 'a'), 'email' => $mail]],
            'email_required' => [false, ['name' => $name, 'email' => '']],
            'email_max:50' =>   [false, ['name' => $name, 'email' => str_pad('', 40, 'a').$mail]],
            'email_valid' =>    [false, ['name' => $name, 'email' => 'admin mail.ru']],
            'password_required' =>     [false, ['name' => $name, 'email' => $mail, 'password' => '']],
            'password_min:8' =>        [false, ['name' => $name, 'email' => $mail, 'password' => '1234567']],
            'password_confirmed2' =>   [false, ['name' => $name, 'email' => $mail, 'password' => '12345678', 'password_confirmation' => '1234567']],
            'short_about_me_max:100' =>[false, ['name' => $name, 'email' => $mail, 'short_about_me' => str_pad('', 101, 'a')]],
            'about_me_max:1024' =>     [false, ['name' => $name, 'email' => $mail, 'about_me' => str_pad('', 1025, 'a')]],
            'all_right' => [true, ['name' => $name, 'email' => $mail, 'password' => '12345678', 'password_confirmation' => '12345678', 'short_about_me' => 'a', 'about_me' => 'a']]
        ];
    }

    public function dataForUniqueTest()
    {
        return [
            'name_unique' =>    [false, ['name' => $this->presentName, 'email' => $this->mail]],
            'email_unique' =>   [false, ['name' => $this->name, 'email' => $this->presentMail]],
        ];
    }

    private function valid($data): bool
    {
        return ! $this->app->get('validator')
            ->make($data, $this->rules)
            ->fails();
    }

    private function mocks(): void
    {
        $mock = \Mockery::mock(DatabasePresenceVerifier::class);
        $mock->shouldReceive('setConnection');
        $mock->shouldReceive('getMultiCount');
        $mock->shouldReceive('getCount')
            ->with('users', 'name', $this->name, null, 'id', [])->andReturn(0);
        $mock->shouldReceive('getCount')
            ->with('users', 'name', $this->presentName, null, 'id', [])->andReturn(1);
        $mock->shouldReceive('getCount')
            ->with('users', 'email', $this->mail, null, 'id', [])->andReturn(0);
        $mock->shouldReceive('getCount')
            ->with('users', 'email', $this->presentMail, null, 'id', [])->andReturn(1);

        $this->app->instance('validation.presence', $mock);
    }
}
  • Вопрос задан
  • 141 просмотр
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы