anthonyedmonds / laravel-testing-traits
Make testing easier with this collection of testing traits!
Installs: 3 650
Dependents: 3
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/anthonyedmonds/laravel-testing-traits
Requires
- php: ^8
- laravel/framework: ^12
- phpunit/phpunit: ^12
README
Make testing easier with this collection of testing traits!
Installation
- Add the library using Composer:
composer require anthonyedmonds/laravel-testing-traits --dev
- The service provider will be automatically registered. If required, you can manually register the service provider by adding it to your bootstrap/providers.php:
return [ ... AnthonyEdmonds\GovukLaravel\Providers\GovukServiceProvider::class, ... ];
- If you are not using the standard
App\Models\Usermodel, publish the config file using Artisan:
Then update the config to point to your model:php artisan vendor:publish --provider="AnthonyEdmonds\LaravelTestingTraits\TestingTraitsServiceProvider"return [ 'exclude_views' => [ '::', 'errors.', 'vendor.', ], 'user_model' => \App\Models\User::class, ];
Usage
- Add the desired traits to your
Tests\TestCase.phpclass - Use the test methods in your tests:
$this->assertFormRequestPasses(...); $this->assertBelongsTo(...);
Available traits
| Trait | Purpose | Methods |
|---|---|---|
| AssertsActivities | Test whether a Spatie/ActivityLog has been recorded | assertActivity |
| AssetsFlashMessages | Test whether a Laracasts/Flash message has been set | assertFlashed |
| AssertsFormRequests | Test whether a FormRequest validates as expected | assertFormRequestPasses, assertFormRequestFails |
| AssertsOrder | Test whether a Collection is in order | assertAscending, assertDescending |
| AssertsPolicies | Test whether a Policy works as expected | assertPolicyAllows, assertPolicyDenies |
| AssertsRelationships | Test whether a Model relationship loads as expected | assertBelongsTo, assertBelongsToMany, assertHasMany, assertHasOne |
| AssertsResults | Test whether a Collection contains the expected values | assertResultsMatch, assertResultsContain, assertResultsDontContain, assertResultsCount |
| AssertsValidationRules | Test whether a custom Rule works as expected | assertRulesPasses, assertRuleFails |
| AssertsViews | Test whether a View renders as expected | assertViewRenders, assertMailRenders, assertNotificationRenders |
| FakesRoutes | Fool Laravel into thinking you are on a specific route | fakeRoute |
| GetsRawCsvs | Process a raw CSV into an array for parsing | processRawCsv |
| GetsStreamedResponses | Intercept a StreamedResponse for testing files | getStreamedResponse |
| SetsViewVariables | Set View variables for testing rendered blades, such as with Mojito | setRequestOld, setViewAttributes, setViewErrors, setViewSlot |
| SignsInUsers | Sign a User in with relevant Spatie/LaravelPermission Roles and Permissions | signIn, signInAs, signInWithRole, signInWithPermission |
PHPUnit Extension
An extension is provided for asserting that all blade files in your project have been rendered successfully.
When used in conjunction with assertViewRenders, an check will be performed at the end of all unit tests to ensure that all blade files within resources/views have been rendered at least once.
<phpunit> <extensions> <bootstrap class="AnthonyEdmonds\LaravelTestingTraits\PhpUnit\AssertAllViewsRenderedExtension"/> </extensions> </phpunit>
The results are output into the terminal and recorded in .phpunit.cache/view-render-results.json.
You can configure which views are excluded from the count by adding wildcards to the exclude_views key of the testing-traits config file.