amerald/laravel-validation-testkit 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

amerald/laravel-validation-testkit

最新稳定版本:v0.0.2

Composer 安装命令:

composer require amerald/laravel-validation-testkit

包简介

Test form requests in no time

README 文档

README

Test form requests with ease and in no time

Installation

composer require amerald/laravel-validation-testkit

Usage

<?php

use Illuminate\Foundation\Http\FormRequest;

class StoreUser extends FormRequest
{
    protected $rules = [
        'first_name' => 'required|string',
        'last_name' => 'required|string',
        'email' => 'required|email',
        'password' => 'required|string',
        'password_confirmation' => 'required|same:password',
        'addresses' => 'array',
        'addresses.*.country' => 'required_with:addresses|string',
    ];
}
<?php

use Amerald\LaravelValidationTestkit\Expectation;
use Amerald\LaravelValidationTestkit\Expectations;
use Amerald\LaravelValidationTestkit\TestsRequests;

class StoreUserTest extends TestCase
{
    use TestsRequests;
    
    /**
     * Request class to test.
     *
     * @return string
     */
    protected function request(): string
    {
        return StoreUser::class;
    }
    
   /**
    * Input to be validated.
    *
    * Should include all expected fields, including optional ones.
    *
    * @return array
    */
    protected function input(): array
    {
        return [
            'first_name' => 'John',
            'last_name' => 'Doe',
            'email' => 'johndoe@example.com',
            'password' => '1234',
            'password_confirmation' => '1234',
            'addresses' => [
                [
                   'country' => 'Ukraine',
                ],
            ],
        ];
    }
    
    /**
     * Define validation expectations.
     *
     * Examples:
     * $request->without('required_field')->shouldFail();
     * $request->without('optional_field')->shouldPass();
     * $request->with('some_field', 'wrongValue')->shouldFail();
     * $request->with('string_field', 'not:string')->shouldFail();
     * $request->without('array.*.field')->shouldFail();
     *
     * @param  Expectations  $request
     * @return Expectation[]
     */
    protected function expectations(Expectations $request): array
    {
        return [
            // Check if validation passes when all fields are present  
            $request->shouldPass(),
            
            // Required/optional
            $request->without('first_name')->shouldFail(),
            $request->without('last_name')->shouldFail(),
            $request->without('email')->shouldFail(),
            $request->without('password')->shouldFail(),
            $request->without('password_confirmation')->shouldFail(),
            $request->without('addresses')->shouldPass(),
            $request->without('addresses.*.country')->shouldFail(),
            
            // Types
            $request->with('first_name', 'not:string')->shouldFail(), // using not:string will set the field value to anything but a string 
            $request->with('last_name', 'not:string')->shouldFail(),
            $request->with('email', 'not:email')->shouldFail(),
            $request->with('password', 'not:string')->shouldFail(),
            $request->with('password_confirmation', 'not:string')->shouldFail(),
            $request->with('addresses', 'not:array')->shouldFail(),
            $request->with('addresses.*.country', 'not:string')->shouldFail(),

            // It is possible to use closure to set value.
            $request->with('password', function (Expectations $request) {
                return $request->originalInput('password_confirmation');
            })->shouldPass(),
                
            // Expectations are chainable
            $request->without('addresses')->without('addresses.*.country')->shouldPass(),
        ];
    }
}

统计信息

  • 总下载量: 31.86k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 0
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-12-05