spatie/pest-expectations

A collection of handy custom Pest customisations

Fund package maintenance!
spatie

1.10.0 2024-07-03 10:01 UTC

This package is auto-updated.

Last update: 2024-11-08 14:57:52 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

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.

toBeArrayOf

Expect that a value is an array of the specified value.

expect(User::all())->toBeArrayOf(User::class);

The specified value may be a class name or a class instance, or one of the following string values:

  • 'string'
  • 'int'
  • 'float'
  • 'bool'
  • 'scalar'
  • 'array'
  • 'object'
  • 'null'
expect([1, 2])->toBeArrayOf('int');
expect([true, false])->toBeArrayOf('bool');
expect(['foo', 1, false])->toBeArrayOf('scalar');

toBeScheduled

Expect that a value is a scheduled job, command or invokable class. The timezone parameter, if passed, will also check that the specified timezone was defined.

expect(MyJob::class)->toBeScheduled('0 * * * *', timezone: 'Europe/Paris');

Optionally, you may pass a callback that accepts an Illuminate\Console\Scheduling\Event or Illuminate\Console\Scheduling\CallbackEvent instance, so you can run any assertion needed:

expect(MyArtisanCommand::class)->toBeScheduled(function (Event $event) {
    expect($event->getExpression())->toBe('0 0 * * *');
    expect($event->getSummaryForDisplay())->toBe('Foo');
});

toHaveJsonApiPagination

Expect that a response has JSON:API pagination. Have a look at our package spatie/laravel-json-api-paginate to see how to add JSON:API pagination to your Laravel app.

$response = $this->get('/');

expect($response)->toHaveJsonApiPagination();

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.

Assertions

This package also provides a set of custom assertions that you can use in your tests.

In your TestCase class, you can use the CustomAssertions trait and call the registerCustomAssertions method in the setUp method.

class TestCase extends \Illuminate\Foundation\Testing\TestCase
{
    use CustomAssertions;

    protected function setUp(): void
    {
        parent::setUp();

        $this->registerCustomAssertions();
    }

assertHasJsonApiPagination

Assert that a response has JSON:API pagination. Have a look at our package spatie/laravel-json-api-paginate to see how to add JSON:API pagination to your Laravel app.

$this
    ->get('/')
    ->assertHasJsonApiPagination();

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.