spatie / pest-expectations
A collection of handy custom Pest customisations
Fund package maintenance!
spatie
Installs: 4 894
Dependents: 1
Suggesters: 0
Security: 0
Stars: 47
Watchers: 5
Forks: 2
Open Issues: 1
Requires
- php: ^8.1
- illuminate/database: ^10.7
Requires (Dev)
- illuminate/contracts: ^9.47|^10.0
- laravel/pint: ^1.2
- orchestra/testbench: ^8.3
- pestphp/pest: ^1.20|^2.0
- spatie/ray: ^1.28
README
This repo contains custom expectations to be used in a Pest test suite.
It also contains various helpers to make testing with Pest easier. Imagine, you only want to run a test on GitHub Actions. You can use the whenGitHubActions
helper to do so.
it('can only run well on github actions', function () { // your test })->whenGitHubActions();
Support us
We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.
We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.
Installation
You can install the package via composer:
composer require spatie/pest-expectations
Usage
Once installed, you can use the custom expectations and helpers provided by this package.
Expectations
toPassWith
This expectation can be used to test if an invokable validation rule works correctly.
In this example, the $value
will be given to YourValidationRule
. The expectation will pass if your rule passed for the given value.
expect(new YourValidationRule())->toPassWith($value);
You can expect the your validation not to pass for the given value, by using Pest's not()
.
expect(new YourValidationRule()->not()->toPassWith($value);
toFailWith
This expectation can be used to test if an invokable validation rule did not pass for a given value.
In this example, the $value
will be given to YourValidationRule
. The expectation will pass if your rule did not pass for the given value.
expect(new YourValidationRule())->toFailWith($value);
Optionally, you can also pass a message as the second argument. The expectation will pass is the validation rule return the given $message
.
expect(new YourValidationRule())->toFailWith($value, 'This value is not valid.');
toBeEnum
Expect that a value is the passed enum.
Given this test enum...
enum TestEnum: string { case first = 'first'; case second = 'second'; }
... all of these expectations will pass
expect($value)->toBeEnum(TestEnum::first); expect($value)->not()->toBeEnum(TestEnum::second); expect('first')->not()->toBeEnum(TestEnum::first);
toBeModel
Expect that a value is a model an equal to the passed model.
expect($model)->toBeModel($anotherModel);
The expectation will only pass if both models are Eloquent models of the same class, with the same key.
Helpers
This package offers various helpers that you can tack on any test. Here's an example of the whenGitHubActions
helper. When tacked on to a test, the test will be skipped unless you're running it on GitHub Actions.
it('can only run well on github actions', function () { // your test })->whenGitHubActions();
To use the helpers, you should call registerSpatiePestHelpers()
in your Pest.php
file.
These helpers are provided by this package:
whenConfig($configKey)
: only run the test when the given Laravel config key is setwhenEnvVar($envVarName)
: only run the test when the given environment variable is setwhenWindows
: the test will be skipped unless running on WindowswhenMac
: the test will be skipped unless running on macOSwhenLinux
: the test will be skipped unless running on LinuxwhenGitHubActions()
: the test will be skipped unless running on GitHub ActionsskipOnGitHubActions()
: the test will be skipped when running on GitHub ActionswhenPhpVersion($version)
: the test will be skipped unless running on the given PHP version, or higher. You can pass a version like8
or8.1
.
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.