mohammedmanssour/form-request-tester
Composer 安装命令:
composer require mohammedmanssour/form-request-tester
包简介
a collection of test helpers that help with testing form requests
README 文档
README
A Simple collection of test helpers that help testing form request the easy way.
Why Bother
for full story on why this package was built please refer to This Blog Post
Installation
- Using composer
composer require --dev mohammedmanssour/form-request-tester
- add
MohammedManssour\FormRequestTester\TestsFormRequeststrait to your test case.
Testing a form request
- you need to intialize the form request using
formRequestmethod, it takes the FormRequest class as first argument and an array of request data as a second argument
$this->formRequest(UpdatePost::class, [ 'title' => 'New Title', 'content' => 'Some Content here' ])
or you can use the intuitive methods to set form request method and data
$this->formRequest(UpdatePost::class) ->post([ 'title' => 'New Title', 'content' => 'Some Content here' ])
the previous code will intialize the request with post method and /fake-route if you want to change these options you can via the options array that can be set as a third argument
$this->formRequest(UpdatePost::class, [ 'title' => 'New Title', 'content' => 'Some Content here' ], [ 'method' => 'put', 'route' => 'posts/{post}' ])
$this->formRequest(UpdatePost::class) ->put([ 'title' => 'New Title', 'content' => 'Some Content here' ]) ->withRoute('posts/{post}')
- use the available assertions to test for request
Available Assertions
$this->assertValidationPassed() |
To make sure the validation have passed successfully with the help of the provided data. |
$this->assertValidationFailed() |
To make sure the validation have failed with the help of the provided data. |
$this->assertValidationErrors($keysArray) |
To assert that the keys mentioned in the $keysArray have occurred in the errors bag. |
$this->assertValidationErrorsMissing($keysArray) |
To assert that the keys mentioned in the $keysArray have not occurred in the errors bag. |
$this->assertValidationMessages($messagesArray) |
To assert that the messages exists in the error bag. Used when you define custom messages for your validation. |
$this->assertAuthorized() |
To assert that request have been authorized via the form request. |
$this->assertNotAuthorized() |
To assert that request have not been authorized via the form request. |
$this->assertValidationData($keysArray) |
To assert that the keys mentioned in the $keysArray have occurred in the validated data. |
$this->assertValidationDataMissing($keysArray) |
To assert that the keys mentioned in the $keysArray have not occurred in the validated data. |
Example Usage:
Taking into consderation:
- title & content are required field,
- Content field is required is a custom error message used for content field
$this->routemethod is used in authorize methodRoute::put('posts/{post}', 'PostsController@update')is the route used to update a post
$this->formRequest(UpdatePost::class,[ 'title' => 'New Title' ],[ 'method' => 'put' 'route' => 'posts/{post}' ])->assertAuthorized() ->assertValidationFailed() ->assertValidationErrors(['content']) ->assertValidationErrorsMissing(['title']) ->assertValidationMessages(['Content field is required'])
you can now use intuitive methods to build form request
$this->formRequest(UpdatePost::class) ->put([ 'title' => 'New Title' ]) ->withRoute('posts/1') ->assertAuthorized() ->assertValidationFailed() ->assertValidationErrors(['content']) ->assertValidationErrorsMissing(['title']) ->assertValidationMessages(['Content field is required'])
Form Requests related methods and how to work with them
$this->route('parameter'):
This method basically retreive the value of a routing rule parameter.
For example if you have a routing rule put posts/{post} and browsed to posts/1 then the value of $this->route('post') is 1.
for this to work with the package you need to
- Register the routing rule in your application routing files
web.phporapi.php
Route::put('posts/{post}', [PostsController::class, 'update']);
- set the route using FormRequestTester
withRoutemethod
$this->formRequest(UpdatePost::class) ->put($data) ->withRoute('posts/1');
this why when you use the $this->route('post') in your form request, the result will be 1.
The package also supports substitube binding. All you need to be is to ؤreate an explicit binding and will do the work for you.
// somewhere in your app 🤔, ideally, your service provider Route::model('post', Post::class);
when you do the reigster, the value of $this->route('post') will be a Post model rather than the id.
Alternatively
if you don't want to register a route just for the sake of resolving a route parameter then you can use FormRequestTester addRouteParameter($name, $value).
$this->formRequest(UpdatePost::class) ->put($data) ->addRouteParameter('post', 1) ->assertAuthorized();
according to the example above, with the new method addRouteParameter, $this->route('post') will be resolved to 1
$this->user():
This method will reteive the current authenticated user.
use laravel testing method actingAs to set a user as the current authenticated user
$user = User::factory()->create(); $this->actingAs($user);
Contributors:
mohammedmanssour/form-request-tester 适用场景与选型建议
mohammedmanssour/form-request-tester 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 240.2k 次下载、GitHub Stars 达 93, 最近一次更新时间为 2019 年 01 月 26 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 mohammedmanssour/form-request-tester 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 mohammedmanssour/form-request-tester 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 240.2k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 93
- 点击次数: 23
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-01-26