A Laravel Package helper for Laravel Packages

v2.0.0 2024-03-13 06:38 UTC

This package is auto-updated.

Last update: 2024-05-13 07:00:29 UTC


Latest Version on Packagist Latest stable test run Codecov coverage Maintainability Sonarcloud Status Laravel Octane Compatibility

A Laravel Package for testing Laravel Packages.

public function test_has_service_registered(): void

Become a sponsor

Your support allows me to keep this package free, up-to-date and maintainable. Alternatively, you can spread the word!


  • PHP 8.1 or later.
  • Laravel 10.x or later.


Require this package into your project using Composer:

composer require --dev laragear/meta-testing

DO NOT install this package outside require-dev, unless you plan to use this package in production environments.


Testing the Service Provider

The InteractsWithServiceProvider allows to quickly test if the Service Provider of your package has registered all the needed bits of code into the Service Container.

use Orchestra\Testbench\TestCase
use Laragear\MetaTesting\InteractsWithServiceProvider;

class ServiceProviderTest extends TestCase
    use InteractsWithServiceProvider;
    public function test_is_registered_as_singleton(): void

The available assertions are in this table:

assertServices() assertBladeComponent() assertGlobalMiddleware()
assertSingletons() assertBladeDirectives() assertMiddlewareInGroup()
assertConfigMerged() assertValidationRules() assertGateHasPolicy()
assertPublishes() assertRouteByName() assertScheduledTask()
assertPublishesMigrations() assertRouteByUri() assertScheduledTaskRunsAt()
assertTranslations() assertRouteByAction() assertMacro()
assertViews() assertMiddlewareAlias()

Service Helpers

The InteractsWithServices trait includes helpers to retrieve services from the Service Container and do quick things like checks or preparation.

public function test_something_important(): void
    // Get a service from the Service Container, optionally run over a callback.
    $cache = $this->service('cache', fn ($cache) => $cache->set('foo', 'bar', 30));
    // Run a service once and forgets it, while running a callback over it.
    $compiler = $this->serviceOnce('blade.compiler', fn($compiler) => $compiler->check('cool'));
    // Executes a callback over a REAL service when already mocked.
    $this->unmock('files', function ($files): void {
        $files->copyDirectory('foo', 'bar');


This meta package includes a InteractsWithValidation trait, that assert if a rule passes or fails using minimal data. This is useful when creating validation rules and testing them without too much boilerplate.

public function test_validation_rule(): void
    // Assert the validation rule passes.
    $this->assertValidationPasses(['test' => 'foo'],['test' => 'my_rule']);
    // Assert the validation rule fails.
    $this->assertValidationFails(['test' => 'bar'],['test' => 'my_rule']);


You can test a middleware easily using the InteractsWithMiddleware trait and its middleware() method. It creates an on-demand route for the given path before sending a test Request to it, so there is no need to register a route.

use Illuminate\Http\Request;
use Vendor\Package\Http\Middleware\MyMiddleware;
use Laragear\MetaTesting\Http\Middleware\InteractsWithMiddleware;

public function test_middleware(): void
    $response = $this->middleware(MyMiddleware::class)->using(function (Request $request) {
        // ...
    })->post('test', ['foo' => 'bar']);

It proxies all MakesHttpRequest trait methods, like get() or withUnencryptedCookie(), so you can get creative with testing your middleware.

$this->middleware(MyMiddleware::class, 'test_argument')
    ->post('test/route', ['foo' => 'bar'])

Form Request

You can test a Form Request if it passes authorization a validation using different data using the InteractsWithFormRequests trait. The formRequest() requires the Form Request class, and an array with the data to include in the request, to test in isolation.

public function test_form_request()
    $this->formRequest(MyFormRequest::class, ['foo' => 'bar'])->assertOk();


To check if a policy or gate works appropriately, use the InteractsWithAuthorization trait to check whether a user can or cannot be authorized to a given action.

public function test_authorization()
    $admin = User::factory()->admin()->create();
    $this->assertUserCan($admin, 'viewDashboard');


The InteractsWithCast trait allows to quickly test if a cast sets values from an attribute appropriately, and can return a given value from an attribute value. It also supports checking on multiple attributes at a time.

public function test_cast()
        ->assertCastFrom(null, new Cast)
        ->assertCastTo('{"foo":"bar"}', new Cast(['foo' => 'bar']));

Laravel Octane compatibility

  • There are no singletons using a stale application instance.
  • There are no singletons using a stale config instance.
  • There are no singletons using a stale request instance.
  • There are no static properties being written.

There should be no problems using this package with Laravel Octane.


If you discover any security related issues, please email instead of using the issue tracker.


This specific package version is licensed under the terms of the MIT License, at time of publishing.

Laravel is a Trademark of Taylor Otwell. Copyright © 2011-2024 Laravel LLC.