spatie/pest-expectations

A collection of handy custom Pest customisations

Fund package maintenance!
spatie

1.6.1 2024-03-11 23:40 UTC

README

Latest Version on Packagist Tests Total Downloads

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

68747470733a2f2f6769746875622d6164732e73332e65752d63656e7472616c2d312e616d617a6f6e6177732e636f6d2f706573742d6578706563746174696f6e732e6a70673f743d31

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.');

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 set
  • whenEnvVar($envVarName): only run the test when the given environment variable is set
  • whenWindows: the test will be skipped unless running on Windows
  • whenMac: the test will be skipped unless running on macOS
  • whenLinux: the test will be skipped unless running on Linux
  • whenGitHubActions(): the test will be skipped unless running on GitHub Actions
  • skipOnGitHubActions(): the test will be skipped when running on GitHub Actions
  • whenPhpVersion($version): the test will be skipped unless running on the given PHP version, or higher. You can pass a version like 8 or 8.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.