Skip to content

Conversation

@aerni
Copy link
Contributor

@aerni aerni commented Jan 22, 2026

This PR fixes the PreventsSavingStacheItemsToDisk trait with Pest PHP. The fakeStacheDirectory path is incorrectly calculated, causing test fixtures to be saved to the wrong location.

Root Cause

In AddonTestCase::setUp(), the current code uses reflection on $this to determine the test file path:

$reflection = new ReflectionClass($this);
$this->fakeStacheDirectory = Str::before(dirname($reflection->getFileName()), DIRECTORY_SEPARATOR.'tests').'/tests/__fixtures__/dev-null';

PHPUnit

In PHPUnit tests, $this refers to the actual test class (e.g., MyAddonTest), so $reflection->getFileName() correctly returns the test file path like /path/to/addon/tests/Feature/MyAddonTest.php.

Pest PHP

In Pest PHP tests, $this refers to a dynamically generated class created by Pest internally. As a result, $reflection->getFileName() returns a path inside vendor/pestphp/pest/src/Factories/, causing the fakeStacheDirectory to be set to an incorrect location like vendor/pestphp/pest/src/Factories/tests/__fixtures__/dev-null.

Solution

Use reflection on $this->addonServiceProvider instead of $this. This provides a reliable reference point regardless of whether Pest or PHPUnit is being used, and is consistent with the existing approach in getEnvironmentSetUp():

protected function setUp(): void
    // ...
    $reflector = new ReflectionClass($this->addonServiceProvider);
    $this->fakeStacheDirectory = dirname($reflector->getFileName()).'/../tests/__fixtures__/dev-null';
    // ...
}
protected function getEnvironmentSetUp($app)
{
    // ...
    $reflector = new ReflectionClass($this->addonServiceProvider);
    $directory = dirname($reflector->getFileName());
    // ...
    $app['config']->set('statamic.stache.stores.collections.directory', $directory.'/../tests/__fixtures__/content/collections');
    // ...
}

@jasonvarga jasonvarga merged commit 7bceb87 into statamic:master Jan 22, 2026
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants