laragear / meta-testing
A Laravel Package helper for Laravel Packages
Fund package maintenance!
Github Sponsorship
Paypal
Installs: 3 808
Dependents: 6
Suggesters: 0
Security: 0
Stars: 2
Watchers: 1
Forks: 1
Open Issues: 0
Requires
- php: ^8.1
Requires (Dev)
- orchestra/testbench: 8.*|9.*
README
A Laravel Package for testing Laravel Packages.
public function test_has_service_registered(): void { $this->assertHasServices('my-cool-service'); }
Become a sponsor
Your support allows me to keep this package free, up-to-date and maintainable. Alternatively, you can spread the word!
Requirements
- PHP 8.1 or later.
- Laravel 10.x or later.
Installation
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
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 { $this->assertHasSingletons(\Vendor\Package\MyService::class); } }
The available assertions are in this table:
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'); }); }
Validation
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']); }
Middleware
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']); $response->assertOk(); }
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') ->withUnencryptedCookie() ->be($this->myTestUser) ->post('test/route', ['foo' => 'bar']) ->assertSee('John');
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(); }
Authorization
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'); }
Casts
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() { $this->cast(MyTestCast::class) ->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.
Security
If you discover any security related issues, please email darkghosthunter@gmail.com instead of using the issue tracker.
License
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.