Given When Then (GWT) Plugin for Pest

Overview

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 test easier to understand at a glance.

Install

composer require milroyfraser/pest-plugin-gwt --dev

Usage

use App\Exceptions\BlockedUserException;
use App\Models\User;
use function Pest\Gwt\scenario;
use function Pest\Laravel\assertDatabaseHas;

scenario('activate user')
    ->given(fn() => User::factory()->create())
    ->when(fn(User $user) => $user->activate())
    ->then(fn(User $user) => assertDatabaseHas('users', [
        'id' => $user->id,
        'activated' => true,
    ]));

scenario('activate blocked user')
    ->given(fn() => User::factory()->blocked()->create())
    ->when(fn(User $user) => $user->activate())
    ->throws(BlockedUserException::class);

more examples

Given a state

Given method accepts a Closure. This is where we Arrange application state. The return values will become argument/s of the when closure.

When I do something

When method accepts a Closure. This is where we Act (perform) the operation. The return values will become argument/s of the then closure.

Then I expect an outcome

Then method accepts a Closure. This is where we Assert the outcome.

If you want to start testing your application with Pest, visit the main Pest Repository.

Pest was created by Nuno Maduro under the Sponsorware license. It got open-sourced and is now licensed under the MIT license.

You might also like...
A Laravel package to check if you can send e-mail through a given mailserver in name of a given e-mail address
A Laravel package to check if you can send e-mail through a given mailserver in name of a given e-mail address

A Laravel package to check if you can send e-mail through a given mailserver in name of a given e-mail address Mail spf checker A Laravel package to c

A PocketMine-MP plugin that replaces a block to another block when breaks, then back to the original block after a certain time

BlockReplacer A PocketMine-MP plugin that replaces a block to another block when breaks, then back to the original block after a certain time How to I

WPCloudDeploy is a WordPress plugin that allows you to easily deploy servers at major cloud-server providers and then install apps
WPCloudDeploy is a WordPress plugin that allows you to easily deploy servers at major cloud-server providers and then install apps

WPCloudDeploy is a WordPress plugin that allows you to easily deploy servers at major cloud-server providers and then install apps

AnsibleBoy aims to use the Asnible `facts` as data, which can then be visualized in a table format
AnsibleBoy aims to use the Asnible `facts` as data, which can then be visualized in a table format

AnsibleBoy - Ansible Frontend Hub About AnsibleBoy aims to use the Ansible facts as data, which can then be visualized as a table ToDo (note that this

This module integrates Silverstripe CMS with Google Translate API and then allows content editors to use automatic translation for every translatable field.
This module integrates Silverstripe CMS with Google Translate API and then allows content editors to use automatic translation for every translatable field.

Autotranslate This module integrates Silverstripe CMS with Google Translate API and then allows content editors to use automatic translation for every

A web app for the resolution of a mobile game in wich you have 4 images and a list of letters, then a few boxes to fill with the word connecting the four images.

4images_1mot_solutions A web app for the resolution of a mobile game in wich you have 4 images and a list of letters, then a few boxes to fill with th

⛽ 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

A plugin for working with popular money libraries in Pest

This package is a plugin for Pest PHP. It allows you to write tests against monetary values provided by either brick/money or moneyphp/money using the same declarative syntax you're used to with Pest's expectation syntax.

A Pest plugin to control the flow of time
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.

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

The Pest Laravel Expectations Plugin
The Pest Laravel Expectations Plugin

This Pest plugin adds Laravel specific expectations to the testing ecosystem it('can check model exists', function(){ $user = User::factory()-creat

A Pest plugin for WordPress
A Pest plugin for WordPress

WordPress Pest Plugin Supports integrating Pest with your WordPress code base through the Mantle Framework. Read about the Mantle Testing Framework he

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

Retrieve the GitHub Sponsors of a given user/organization.

Laravel GitHub Sponsors Retrieve the GitHub Sponsors of any user/organization and check if someone is sponsoring you. Installation composer require as

A CLI program that helps you check your endpoints by requesting the given servers and send a report message in any supported channel like Telegram

API Monitor A CLI program that help you check your endpoints by requesting the given servers and send a report message in any supported channel ( Tele

PSR-6 cache implementation adapting a given PSR-16 instance

PSR-6 cache implementation adapting PSR-16 This package provides a PSR-6 cache instance when you only have a PSR-16 cache at hand. As PSR-6 is more fe

A Magento implementation for validating JSON Structures against a given Schema

Zepgram JsonSchema A Magento implementation for validating JSON Structures against a given Schema with support for Schemas of Draft-3 or Draft-4. Base

A simple validator package to check if the given zipcode has a valid Dutch zipcode format
A simple validator package to check if the given zipcode has a valid Dutch zipcode format

Laravel Dutch Zipcode Validator A simple validator package to check if the given zipcode has a valid Dutch zipcode format Table of Contents Installati

A package which provides a monthly calendar with days and events depending on given months and events.

A package which provides a monthly calendar with days and events depending on given months and events. This is where your description should go. Try a

Comments
  • Check for an array type before wrapping in an array

    Check for an array type before wrapping in an array

    The array casting from previous didn't work well with eloquent since it supports being casted to an array and the behaviour was unexpected
    This should fix that.

    This resolves #1.

    opened by jradtilbrook 3
  • Eloquent model doesn't work as casting to array

    Eloquent model doesn't work as casting to array

    Hey @milroyfraser 👋

    First off, this is absolutely awesome, I love it! I've even already added it to vim-test/vimtest#644

    But I've found when returning an eloquent model from given or when it doesn't work. I think this is because https://github.com/milroyfraser/pest-plugin-gwt/blob/master/src/BehaviorDescriptor.php#L68 is casting the return type to an array which eloquent supports and converts properties to an array. This array seems to include some encoding on my system which subsequently fails with the error below.

    Screen Shot 2022-04-06 at 19 50 39

    Wrapping the return of the closures in an array fixes this and is pretty trivial. It would be good if it wasn't necessary though. What is the reasoning behind the array casting? Perhaps instead of casting to an array to support the expansion on following lines, these lines could instead check if its an array and pass it in expanded, or even use call_user_func or something?

    Unless I'm doing something wrong which is possible, this also means the examples in the readme don't work. I copied them verbatim into my test and I got the same error as above. I'm happy to PR a fix for the docs at least (to wrap in an array). But I'm wondering if changing the underlying logic would be a better approach. What do you think?

    And lastly, I don't know your plans for this but it would be awesome to support behat/gherkin style where you could do something like below (and put the common logic somewhere else)

    scenario('a user is activated')
        ->given('a new user') // calls the factory - defined by user of this package
        ->when('the user is activated') // calls the 'activated' method on the return from above
        ->then('we see the user is activated' /* or something like this */); // calls a user defined function which calls assertDatabaseHas
    

    This is surely a topic for another issue though, but just an idea 👍

    opened by jradtilbrook 1
Owner
Milroy Fraser
A full-stack developer.
Milroy Fraser
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
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
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
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
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
Wraps your Pest suite in a Laravel application instance, allowing global use of the framework in tests.

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

Luke Downing 3 Jan 6, 2022
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

null 10 Nov 21, 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
Enforce consistent styling for your Pest PHP tests

A set of PHP CS rules for formatting Pest PHP tests.

Worksome 2 Mar 15, 2022
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