Download the PHP package soyhuce/laravel-testing without Composer
On this page you can find all versions of the php package soyhuce/laravel-testing. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download soyhuce/laravel-testing
More information about soyhuce/laravel-testing
Files in soyhuce/laravel-testing
Package laravel-testing
Short Description Helpers for Laravel tests
License MIT
Homepage https://github.com/soyhuce/laravel-testing
Informations about the package laravel-testing
Helpers for Laravel tests
Extra tools for your laravel tests
Installation
You can install the package via composer:
Usage
Laravel assertions
To use Laravel specific assertions, you will have to add \Soyhuce\Testing\Assertions\LaravelAssertions::class
trait to your test class.
assertModelIs
Matches if the model is equal to the given model.
assertCollectionEquals
Matches if the collections are equal.
2 Collections are considered equal if they contain the same elements, indexed by the same keys and in the same order.
If the Collections contain Models, assertCollectionEquals
will use Model comparison of assertIsModel
.
You can give an array in the $expected
parameter of assertCollectionEquals
:
assertCollectionEqualsCanonicalizing
Matches if the collections are canonically equals.
2 Collections are considered equal if they contain the same elements, indexed by the same keys.
If the Collections contain Models, assertCollectionEqualsCanonicalizing
will use Model comparison of assertIsModel
.
You can give an array in the $expected
parameter of assertCollectionEqualsCanonicalizing
:
TestResponse assertions
All these methods are available in Illuminate\Testing\TestResponse
:
Contract Testing
Requires hotmeteor/spectator package
TestResponse::assertValidContract(int $status)
: Verifies that the request and the response are valid according to the contract.
Data
TestResponse::assertData($expect)
: Alias forassertJsonPath('data', $expect)
TestResponse::assertDataPath(string $path, $expect)
: Alias forassertJsonPath('data.'.$path, $expect)
TestResponse::assertDataPaths(array $expectations)
: RunsassertDataPath
for each$path
=>$expect
pair in the array.TestResponse::assertDataPathCanonicalizing(string $path, array $expect)
: Alias forassertJsonPathCanonicalizing('data.'.$path, $expect)
TestResponse::assertDataPathsCanonicalizing(array<array> $expectations)
: RunsassertDataPathCanonicalizing
for each$path
=>$expect
pair in the array.TestResponse::assertDataMissing($item)
: Alias forassertJsonMissingPath('data', $item)
TestResponse::assertDataPathMissing(string $path, $item)
: Alias forassertJsonMissingPath('data.'.$path, $item)
Json
TestResponse::assertJsonPathMissing(string $path, $item)
: Verifies that the Json path does not contain$item
TestResponse::assertJsonMessage(string $message)
: Alias forassertJsonPath('message', $message)
TestResponse::assertSimplePaginated()
: Verifies that the response is a simple paginated response.TestResponse::assertPaginated()
: Verifies that the response is a paginated response.
View
TestResponse::assertViewHasNull(string $key)
: Verifies that the key is present in the view but is null.
FormRequest test in isolation
It's possible to test FormRequests in isolation thanks to the TestsFormRequests
trait.
$testFormRequest
have some methods to check authorization and validation of the request.
TestFormRequest::by(Authenticable $user, ?string $guard = null)
: set authenticated user in the requestTestFormRequest::withParams(array $params)
: set route parametersTestFormRequest::withParam(string $param, mixed $value)
: set a route parameterTestFormRequest::validate(array $data): TestValidationResult
: get Validation resultTestFormRequest::assertAuthorized()
: assert that the request is authorizedTestFormRequest::assertUnauthorized()
: assert that the request is unauthorizedTestValidationResult::assertPasses()
: assert that the validation passesTestValidationResult::assertFails(array $errors = [])
: assert that the validation failsTestValidationResult::assertValidated(array $expected)
: assert that the attributes and values that passed validation are the expected ones
For exemple :
JsonResource test in isolation
It's possible to test the JsonResources
in isolation thanks to the TestsJsonResources
trait.
TestsJsonResources::createResponse(JsonResource $resource, ?Request $request = null)
returns a Illuminate\Testing\TestResponse
.
Matcher
Let's take this test
We can simplify this test by using a Matcher
.
For Collections, we can use Matcher::collectionEquals()
or Matcher::collectionEqualsCanonicalizing()
.
For more complex cases, we can use Matcher::make
.
Partial match
In some cases, we wish to check only a few arguments or call argument methods:
We can use Matcher::match
to define our assertions on $data
:
In specific cases of object properties, we can use named parameters:
We can also check object type:
ActionMock
The trait MocksActions
provides a mockAction
method to simply mock an action. By convention, an action is a class with a execute
method.
Under the hood, it uses Mockery::mock
.
It allows to easily define your action's expectations. Instead of
you can write
You can also define the return value and capture it to use it in your test.
Helpers
It can be necessary to capture the return value of a callback, for exemple in returnUsing
of mocks.
In this case, we will use capture
function:
Once the mock executed, $version
is created and will contain the returned value of the callback.
Testing
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
- Colin DeCarlo for the FormRequest isolation testing
- Bastien Philippe
- All Contributors
License
The MIT License (MIT). Please see License File for more information.
All versions of laravel-testing with dependencies
illuminate/database Version ^11.0|^12.0
illuminate/support Version ^11.0|^12.0
illuminate/testing Version ^11.0|^12.0
phpunit/phpunit Version ^11.0
spatie/invade Version ^2.0