Created
April 28, 2022 15:19
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you replace line 41 with
it will also delete all automatically created language copies