Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

- [Introduction](#introduction)
- [Environment](#environment)
- [Caching Configuration](#caching-config)
- [Creating Tests](#creating-tests)
- [Running Tests](#running-tests)
- [Running Tests in Parallel](#running-tests-in-parallel)
Expand Down Expand Up @@ -31,6 +32,62 @@ You are free to define other testing environment configuration values as necessa

In addition, you may create a `.env.testing` file in the root of your project. This file will be used instead of the `.env` file when running Pest and PHPUnit tests or executing Artisan commands with the `--env=testing` option.

<a name="caching-config"></a>
## Caching Config

When running tests, Laravel boots the application for each individual test method. Without a cached configuration file, each config file for your application must be loaded at the start of a test. To build the configuration once and re-use it for all tests in a single run, Laravel offers the `Illuminate\Foundation\Testing\WithCachedConfig` trait.

```php tab=Pest
<?php

use Illuminate\Foundation\Testing\WithCachedConfig;

pest()->use(WithCachedConfig::class);

test('modifies config', function () {
config(['services.postmark.key' => 'xyz']);
expect(config('services.postmark.key'))->toBe('xyz');
});

test('uses default config', function () {
expect(config('services.postmark.key'))->not->toBe('xyz');
});
```

```php tab=PHPUnit
<?php

namespace Tests\Feature;

use Illuminate\Foundation\Testing\WithCachedConfig;
use Tests\TestCase;

class ConfigTest extends TestCase
{
use WithCachedConfig;

/**
* A test that modifies the config.
*/
public function test_modifies_config(): void
{
config(['services.postmark.key' => 'xyz']);
$this->assertEquals('xyz', config('services.postmark.key'));
}

/**
* A test that makes no modification to the config.
*/
public function test_uses_default_config(): void
{
$this->assertNotEquals('xyz', config('services.postmark.key'));
}
}
```

> [!NOTE]
> Changes to the cache in one test case should not affect the configuration that is loaded in subsequent tests.

<a name="creating-tests"></a>
## Creating Tests

Expand Down