Artisan command to run middleware-like hooks during phpunit tests
27
0
2
Package Data
Maintainer Username: thecrypticace
Maintainer Contact: jordan@cryptica.me (Jordan Pittman)
Package Create Date: 2017-06-25
Package Last Update: 2017-08-19
Language: PHP
License: MIT
Last Refreshed: 2024-11-23 03:04:50
Package Statistics
Total Downloads: 27
Monthly Downloads: 0
Daily Downloads: 0
Total Stars: 0
Total Watchers: 2
Total Forks: 0
Total Open Issues: 7

Suitey

Set up the world. Run code before and after PHPUnit.

Installation

  1. composer require thecrypticace/suitey
  2. php artisan vendor:publish --tag=suitey
  3. Update steps list to configure and run the steps you want before your tests.

Usage

Run your tests with the test artisan command:

php artisan test

This also accepts any parameter that PHPUnit does:

php artisan test --filter=my_test_method_name

Want to pass arguments to artisan before PHPUnit? Use a -- to separate the two lists:

php artisan test -vvv -- --filter=my_test_method_name

Adding steps

When you run php artisan test you'll be running one step: PHPUnit. You'll can see this because you will get output that looks like this:

[1/1] Run PHPUnit
… test details here …

Lets fix that.

Publishing the config

Run php artisan vendor:publish --tag=suitey to publish the config file. This file is where you can detail what steps run and how to load the test environment variables for tests.

Adding steps

In the config for Suitey you will see a steps array that looks like this:

"steps" => [
    // \TheCrypticAce\Suitey\MigrateDatabase::class,
    // \TheCrypticAce\Suitey\RefreshDatabase::class,
    // [
    //     "class" => \TheCrypticAce\Suitey\SeedDatabase::class,
    //     "options" => ["class" => "ExampleSeeder"],
    // ]
],

Uncomment the MigrateDatabase step and your database migrations will run before your tests.

"steps" => [
    \TheCrypticAce\Suitey\MigrateDatabase::class,

    // \TheCrypticAce\Suitey\RefreshDatabase::class,
    // [
    //     "class" => \TheCrypticAce\Suitey\SeedDatabase::class,
    //     "options" => ["class" => "ExampleSeeder"],
    // ]
],

Note: You may resolve the class through the container instead of using the facade if your wish.

Your migrations will now run before your test runs. Don't forget to remove the DatabaseMigrations trait from your tests.

This step is configurable if your have an atypical setup. You may optionally specify a connection name and/or a path to your migrations.

"steps" => [
    [
        "class" => \TheCrypticAce\Suitey\MigrateDatabase::class,
        "options" => ["database" => "connection_name", "path" => "path_to_migrations"],
    ],
],

And if you have more than one migration folder:

"steps" => [
    [
        "class" => \TheCrypticAce\Suitey\MigrateDatabase::class,
        "options" => ["database" => "foo", "path" => "database/migrations/foo"],
    ],
    [
        "class" => \TheCrypticAce\Suitey\MigrateDatabase::class,
        "options" => ["database" => "bar", "path" => "database/migrations/bar"],
    ],
    [
        "class" => \TheCrypticAce\Suitey\MigrateDatabase::class,
        "options" => ["database" => "baz", "path" => "database/migrations/baz"],
    ],
],

Available Steps

| Class | Config Option | Description | | ------|---------------|-------------| | MigrateDatabase | | Migrate a database via migrate and migrate:rollback before/after phpunit | | | database | optional The default connection to use during migration | | | path | optionalThe path to your migration files | | RefreshDatabase | | Refresh a database via migrate:refresh before starting phpunit | | | database | optionalThe default connection to use during migration | | | path | optionalThe path to your migration files | | SeedDatabase | | Run the given seeder before starting PHPUnit | | | name | required The name of the seeder you would like to run | | RunCode | | Run a closure! | | | name | required The name displayed to the user. This can be a closure that determines the name if needed. | | | code | required The code to run |

Want to see something meta?

Suitey can run its own tests: ./tests/Fixture/artisan test