Laravel Test Assertions

Laravel Test Assertions stats

Downloads
2.9M
Stars
346
Open Issues
2
Forks
36

View on GitHub →

A set of helpful assertions when testing Laravel applications.

Laravel Test Assertions

A set of helpful assertions when testing Laravel applications.

Requirements

Your application must be running the Laravel 5.5 or higher and using Laravel's testing harness.

Installation

You may install these additional assertions with Composer by running:

composer require --dev jasonmccreary/laravel-test-assertions

Afterwards, add the trait to your base TestCase class:

<?php
namespace Tests;
 
use JMac\Testing\Traits\AdditionalAssertions;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
 
abstract class TestCase extends BaseTestCase
{
use CreatesApplication, AdditionalAssertions;
}

Documentation

This package adds several assertions, matchers, and helpers aimed at making testing within Laravel easier.

Assertions

assertActionUsesFormRequest(string $controller, string $method, string $form_request)

Verifies the action for a given controller performs validation using the given form request.

assertActionUsesMiddleware(string $controller, string $method, string|array $middleware)

Verifies the action for a given controller uses the given middleware or set of middleware.

assertRouteUsesFormRequest(string $routeName, string $formRequest)

Verifies that the corresponding action/controller, for a given route name performs the validation using the given form request.

assertRouteUsesMiddleware(string $routeName, array $middlewares, bool $exact)

Verifies the route for a given route name uses all the given middlewares or only the given set of middlewares.

assertValidationRules(array $expected, array $actual)

Verifies the expected subset of validation rules for fields are within a set of validation rules. Rules may be passed as a delimited string or array.

assertExactValidationRules(array $expected, array $actual)

Verifies the expected set of validation rules for fields exactly match a set of validation rules. Rules may be passed as a delimited string or array.

assertValidationRuleContains($rule, string $class)

Verifies the rule or rules contains an instance of the given Rule class.

assertViewHasNull($key)

Verifies the view data $key has an explicit value of null. Note: is a TestResponse assertion. It must be called on the response of a view.

assertNow(CarbonInterface $datetime, ?CarbonInterface $now = null)

Verifies $datetime is equal to $now, precise to the second. $now defaults to Carbon::now() which can be faked with the freezeNow() testing helper.

Matchers

LaravelMatchers::isModel(Model $model = null)

Matches an argument is the same as $model. When called without $model, will match any argument of type Illuminate\Database\Eloquent\Model.

LaravelMatchers::isCollection(Collection $collection = null)

Matches an argument equals $collection. When called without $collection, will match any argument of type Illuminate\Support\Collection.

LaravelMatchers::isEloquentCollection(Collection $collection = null)

Matches an argument equals $collection. When called without $collection, will match any argument of type \Illuminate\Database\Eloquent\Collection.

Helpers

createFormRequest(string $class, array $data = [])

Creates an instance of the given Form Request class with the given request data.

freezeNow(bool $subseconds = false)

Similar to Laravel's freezeTime() helper, but defaults with second precision and returns the frozen time. You may pass true to freeze with sub-second precision.

Support Policy

Starting with version 2, this package will only support the latest stable version of Laravel (currently Laravel 8). If you need to support older versions of Laravel, you may use version 1 or upgrade your application (try using Shift).

This package still follows semantic versioning. However, it does so with respect to its own code. Any breaking changes will increase its major version number. Otherwise, minor version number increases will contain new features. This includes changes for future versions of Laravel.

jasonmccreary photo

Creator of @laravel-shift, producer of Getting Git, and author of BaseCode.

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.


Jasonmccreary Laravel Test Assertions Related Articles

A Copy/Paste Detector CLI for PHP 8.5+ image

A Copy/Paste Detector CLI for PHP 8.5+

Read article
Ship AI with Laravel: Test Your AI System with Zero API Calls image

Ship AI with Laravel: Test Your AI System with Zero API Calls

Read article
Model::withoutRelation() in Laravel 12.54.0 image

Model::withoutRelation() in Laravel 12.54.0

Read article
Factory makeMany() Method in Laravel 12.52.0 image

Factory makeMany() Method in Laravel 12.52.0

Read article
Find Feature Tests Creating Database Records without Refreshing the Database in Laravel image

Find Feature Tests Creating Database Records without Refreshing the Database in Laravel

Read article
Testing Your Reporting System with Laravel Factories and Assertions image

Testing Your Reporting System with Laravel Factories and Assertions

Read article
LoadForge logo

LoadForge

Scalable load testing for web apps & APIs. Simulate real-world traffic and identify breaking points and performance limits with powerful, scalable load tests designed for Laravel.

LoadForge
PhpStorm logo

PhpStorm

The go-to PHP IDE with extensive out-of-the-box support for Laravel and its ecosystem.

PhpStorm
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Multi-tenant Laravel SaaS Starter Kit that comes with all features required to run a modern SaaS. Payments, Beautiful Checkout, Admin Panel, User dashboard, Auth, Ready Components, Stats, Blog, Docs and more.

SaaSykit: Laravel SaaS Starter Kit
Harpoon: Next generation time tracking and invoicing logo

Harpoon: Next generation time tracking and invoicing

The next generation time-tracking and billing software that helps your agency plan and forecast a profitable future.

Harpoon: Next generation time tracking and invoicing
The Certification of Competence for Laravel logo

The Certification of Competence for Laravel

A community-driven, proctored assessment across 4 levels designed to validate real-world Laravel knowledge, from Junior to mastery-level Artisan. Official Vue.js, Official Nuxt, Angular, React, JS certifications also available.

The Certification of Competence for Laravel
No Compromises logo

No Compromises

Joel and Aaron, the two seasoned devs from the No Compromises podcast, are now available to hire for your Laravel project. ⬧ Flat rate of $9500/mo. ⬧ No lengthy sales process. ⬧ No contracts. ⬧ 100% money back guarantee.

No Compromises