Wraps your Pest suite in a Laravel application instance, allowing global use of the framework in tests.

Overview

Pest Larastrap Plugin

This is currently a highly experimental project and is subject to large pre-release changes.

Pest PHP is an awesome PHP testing framework. It already has amazing support for Laravel via the Laravel plugin! Occasionally however, you'll come across some quirks when working with Pest and Laravel due to the fact that Laravel hasn't been booted during the Pest compile stage. Take a look at the following example, using Higher Order Tests:

it('points to the correct URL')
    ->expect(route('dashboard'))
    ->toBe(config('app.url').'/dashboard');

If you try to run this out of the box, it will fail; we're trying to make use of Laravel's route and config helpers, but they require the Laravel application to be booted.

Enter Larastrap. After installing this plugin, the above test will run without issue, and you can carry on testing without any headaches!

Installation

You can install Larastrap via composer:

composer require --dev lukeraymonddowning/pest-plugin-larastrap

It will be automatically registered, so you can start using it right away!

Prerequisites

The only prerequisite for using Larastrap is having a CreatesApplication trait in your application's tests folder. You almost certainly already have this, as it ships out of the box with Laravel. This trait should have a createApplication method inside it. If you don't know what we're talking about, it likely means everything is good to go and you can skip this section.

Common use cases

We've already highlighted that Larastrap is super useful in higher order tests.

it('can access the dashboard')
    ->get(route('dashboard'))
    ->assertOk();

Somewhere else it comes in handy is datasets. Traditionally, you wouldn't be able to make use of Laravel in your datasets. With Larastrap, however, you have the full power of Laravel at your fingertips. For example, perhaps you want to check that all service providers are present in your app.php config file:

dataset('laravel service providers', function () {
    yield from config('app.providers');
});

Now, we have access to a dynamically updated array of provider strings that we can make use of in any test.

Perhaps we want a dataset of the middleware registered on a certain route:

dataset('admin panel middleware', function () {
    return Route::gatherRouteMiddleware(Route::getRoutes()->getByName('admin.dashboard')));
});

You can see how powerful this concept is; it opens up the possibility of completely dynamic datasets that grow with your application automatically.

Caveats

There are a few caveats and limitations you should be aware of when using Larastrap:

You shouldn't use the database outside of the test

Because Larastrap is basically booting its own laravel instance prior to any of your tests running, the database will be reset before your test is run. As such, you should still perform database queries inside each test.

You might also like...
PHPUnit to Pest Converter

PestConverter PestConverter is a PHP library for converting PHPUnit tests to Pest tests. Before use Before using this converter, make sure your files

Given When Then (GWT) Plugin for Pest

Given When Then (GWT) Plugin for Pest A simple API allows you to structure your tests focused on the behaviour. Given-When-Then separation makes the t

Magic Test allows you to write browser tests by simply clicking around on the application being tested, all without the slowness of constantly restarting the testing environment.

Magic Test for Laravel Magic Test allows you to write browser tests by simply clicking around on the application being tested, all without the slownes

vfsStream is a stream wrapper for a virtual file system that may be helpful in unit tests to mock the real file system. It can be used with any unit test framework, like PHPUnit or SimpleTest.

vfsStream vfsStream is a stream wrapper for a virtual file system that may be helpful in unit tests to mock the real file system. It can be used with

Mock HTTP requests on the server side in your PHP unit tests

HTTP Mock for PHP Mock HTTP requests on the server side in your PHP unit tests. HTTP Mock for PHP mocks the server side of an HTTP request to allow in

TestDummy makes the process of preparing factories (dummy data) for your integration tests as easy as possible

TestDummy TestDummy makes the process of preparing factories (dummy data) for your integration tests as easy as possible. As easy as... Build a Post m

Some shorthand functions for skipping and focusing tests.

Pest Plugin: Shorthands This repository contains the Pest Plugin Shorthands. If you want to start testing your application with Pest, visit the main P

A PHP library for mocking date and time in tests

ClockMock Slope s.r.l. ClockMock provides a way for mocking the current timestamp used by PHP for \DateTime(Immutable) objects and date/time related f

Comments
  • Add /testapp to .gitattributes

    Add /testapp to .gitattributes

    Tested the package with the /testapp removed from vendor and it functioned as advertised. Thus this PR adds the /testapp folder to the .gitattributes file.

    Preventing it from being downloaded when the package is required, saving both bandwidth and space for the end-user.

    opened by thinkverse 2
Owner
Luke Downing
Luke Downing
Prevent none-test output in your Pest tests.

Pest Plugin Silence Often, when writing tests, we echo and dump test code to debug and check everything is working correctly. It can be easy to forget

Worksome 5 Feb 23, 2022
This plugin adds basic HTTP requests functionality to Pest tests, using minicli/curly

Curly Pest Plugin This plugin adds basic HTTP requests functionality to Pest tests, using minicli/curly. Installation composer require minicli/pest-pl

minicli 16 Mar 24, 2022
Pest is an elegant PHP Testing Framework with a focus on simplicity

Pest is an elegant PHP Testing Framework with a focus on simplicity. It was carefully crafted to bring the joy of testing to PHP. Explore the docs: pe

PEST 5.9k Dec 27, 2022
The Phoronix Test Suite is the most comprehensive testing and benchmarking platform

The Phoronix Test Suite is the most comprehensive testing and benchmarking platform available for Linux, Solaris, macOS, Windows, and BSD operating systems.

Phoronix Test Suite 1.9k Jan 7, 2023
⛽ Pest plugin to test Laravel applications powered by Octane.

⛽ Laravel Octane (Pest Plugin) Pest plugin to test Laravel applications powered by Octane. Install Via Composer composer require --dev cerbero/pest-pl

Andrea Marco Sartori 21 Apr 5, 2022
Extension to use built-in PHP server on Behat tests

Extension to use built-in PHP server on Behat tests Instalation composer require libresign/behat-builtin-extension Configuration Add the extension to

LibreSign 2 Feb 21, 2022
Add mocking capabilities to Pest or PHPUnit

This repository contains the Pest Plugin Mock. The Mocking API can be used in regular PHPUnit projects. For that, you just have to run the following c

PEST 16 Dec 3, 2022
A Pest plugin to control the flow of time

This Pest plugin offers a function testTime that allows you to freeze and manipulate the current time in your tests.

Spatie 34 Nov 16, 2022
Integrates Pest with PHP-VCR using plugins.

Pest plugin for PHP-VCR Integrates Pest with PHP-VCR using plugins. Installation You can install the package via composer: composer require phpjuice/p

PHPJuice 4 Sep 1, 2021
The Pest Parallel Plugin

This repository contains the Pest Plugin Parallel. If you want to start testing your application with Pest, visit the main Pest Repository. Explore th

PEST 28 Dec 5, 2022