PHPackages                             mohammedmanssour/form-request-tester - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Testing &amp; Quality](/categories/testing)
4. /
5. mohammedmanssour/form-request-tester

ActiveLibrary[Testing &amp; Quality](/categories/testing)

mohammedmanssour/form-request-tester
====================================

a collection of test helpers that help with testing form requests

1.2.2(2y ago)93222.6k↓31.3%9[4 PRs](https://github.com/mohammedmanssour/form-request-tester/pulls)1MITPHP

Since May 30Pushed 2y ago3 watchersCompare

[ Source](https://github.com/mohammedmanssour/form-request-tester)[ Packagist](https://packagist.org/packages/mohammedmanssour/form-request-tester)[ RSS](/packages/mohammedmanssour-form-request-tester/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (5)Dependencies (1)Versions (14)Used By (1)

Laravel FormRequest Tester
==========================

[](#laravel-formrequest-tester)

A Simple collection of test helpers that help testing form request the easy way.

Why Bother
----------

[](#why-bother)

for full story on why this package was built please refer to [This Blog Post](https://mohammedmanssour.me/blog/testing-laravel-form-requests/)

Installation
------------

[](#installation)

1. Using composer

```
composer require --dev mohammedmanssour/form-request-tester

```

2. add `MohammedManssour\FormRequestTester\TestsFormRequests` trait to your test case.

Testing a form request
----------------------

[](#testing-a-form-request)

1. you need to intialize the form request using `formRequest` method, 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}')
```

2. use the available assertions to test for request

### Available Assertions

[](#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:

[](#example-usage)

Taking into consderation:

1. title &amp; content are required field,
2. **Content field is required** is a custom error message used for content field
3. `$this->route` method is used in authorize method
4. `Route::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
-------------------------------------------------------

[](#form-requests-related-methods-and-how-to-work-with-them)

### `$this->route('parameter')`:

[](#this-routeparameter)

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

1. Register the routing rule in your application routing files `web.php` or `api.php`

```
Route::put('posts/{post}', [PostsController::class, 'update']);
```

2. set the route using **FormRequestTester** `withRoute` method

```
$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-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:
-------------

[](#contributors)

1. [Mohammed Manssour](https://mohammedmanssour.me)
2. [Bryan Pedroza](https://www.bryanpedroza.com/)
3. [Kyler Moore](https://github.com/kylerdmoore)

###  Health Score

41

—

FairBetter than 89% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity48

Moderate usage in the ecosystem

Community20

Small or concentrated contributor base

Maturity63

Established project with proven stability

 Bus Factor1

Top contributor holds 85.7% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~328 days

Total

5

Last Release

865d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/aeb75f35979097845cce704062910a6a8c85bc0a7b49b081df7350a4f20023fc?d=identicon)[manssour.mohammed](/maintainers/manssour.mohammed)

---

Top Contributors

[![mohammedmanssour](https://avatars.githubusercontent.com/u/19733629?v=4)](https://github.com/mohammedmanssour "mohammedmanssour (36 commits)")[![bpedroza](https://avatars.githubusercontent.com/u/3336238?v=4)](https://github.com/bpedroza "bpedroza (2 commits)")[![kylerdmoore](https://avatars.githubusercontent.com/u/19496495?v=4)](https://github.com/kylerdmoore "kylerdmoore (2 commits)")[![jomisacu](https://avatars.githubusercontent.com/u/6318326?v=4)](https://github.com/jomisacu "jomisacu (1 commits)")[![shawnhooper](https://avatars.githubusercontent.com/u/2073284?v=4)](https://github.com/shawnhooper "shawnhooper (1 commits)")

### Embed Badge

![Health badge](/badges/mohammedmanssour-form-request-tester/health.svg)

```
[![Health](https://phpackages.com/badges/mohammedmanssour-form-request-tester/health.svg)](https://phpackages.com/packages/mohammedmanssour-form-request-tester)
```

###  Alternatives

[phpspec/prophecy

Highly opinionated mocking framework for PHP 5.3+

8.5k551.7M682](/packages/phpspec-prophecy)[brianium/paratest

Parallel testing for PHP

2.5k118.8M754](/packages/brianium-paratest)[beberlei/assert

Thin assertion library for input validation in business models.

2.4k96.9M570](/packages/beberlei-assert)[mikey179/vfsstream

Virtual file system to mock the real file system in unit tests.

1.4k108.0M2.7k](/packages/mikey179-vfsstream)[orchestra/testbench

Laravel Testing Helper for Packages Development

2.2k39.1M32.1k](/packages/orchestra-testbench)[phpspec/phpspec

Specification-oriented BDD framework for PHP 7.1+

1.9k36.7M3.1k](/packages/phpspec-phpspec)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
