Skip to content

Instantly share code, notes, and snippets.

@yoeriboven
Created April 28, 2022 15:19
Show Gist options
  • Select an option

  • Save yoeriboven/08f751dc7beba7452491c148cea806d5 to your computer and use it in GitHub Desktop.

Select an option

Save yoeriboven/08f751dc7beba7452491c148cea806d5 to your computer and use it in GitHub Desktop.
Write your Laravel Statamic tests with this trait. More info: https://yoeri.me/blog/testing-your-statamic-implementation
<?php
namespace Tests;
use Exception;
use Statamic\Facades\User;
use Statamic\Stache\Stache;
use Statamic\Entries\Entry;
trait WithStatamicEntryFaking
{
protected array $filesToRemove = [];
protected function cleanupStatamicData(): void
{
foreach ($this->filesToRemove as $file) {
unlink($file);
}
}
protected function makeStatamicUser(): \Statamic\Auth\File\User
{
$user = User::make()
->id((new Stache())->generateId())
->email('random@email.com')
->save();
$this->filesToRemove[] = $user->initialPath();
return $user;
}
protected function makeEntry(callable $callback): Entry
{
$entry = $callback();
if (is_null($entry)) {
throw new Exception('Entry not found.');
}
$this->filesToRemove[] = $entry->initialPath();
return $entry;
}
}
@ben182
Copy link

ben182 commented Sep 10, 2022

If you replace line 41 with

$this->filesToRemove[] = $entry->initialPath();
$entry
    ->descendants()
    ->values()
    ->each(function(Entry $entry) {
        $this->filesToRemove[] = $entry->initialPath();
    });

it will also delete all automatically created language copies

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment