diff --git a/src/Assets/AssetRepository.php b/src/Assets/AssetRepository.php index 8323955a5e4..cd2109a2bfb 100644 --- a/src/Assets/AssetRepository.php +++ b/src/Assets/AssetRepository.php @@ -5,6 +5,7 @@ use Statamic\Contracts\Assets\Asset; use Statamic\Contracts\Assets\AssetRepository as Contract; use Statamic\Contracts\Assets\QueryBuilder; +use Statamic\Exceptions\AssetNotFoundException; use Statamic\Facades\AssetContainer; use Statamic\Facades\Site; use Statamic\Facades\Stache; @@ -60,6 +61,17 @@ public function findByUrl(string $url) return $container->asset($path); } + public function findOrFail(string $id): Asset + { + $asset = $this->find($id); + + if (! $asset) { + throw new AssetNotFoundException($id); + } + + return $asset; + } + protected function resolveContainerFromUrl($url) { return AssetContainer::all()->sortByDesc(function ($container) { diff --git a/src/Auth/Eloquent/UserRepository.php b/src/Auth/Eloquent/UserRepository.php index 5a3368895df..d7b2ae0556e 100644 --- a/src/Auth/Eloquent/UserRepository.php +++ b/src/Auth/Eloquent/UserRepository.php @@ -6,6 +6,7 @@ use Statamic\Auth\UserCollection; use Statamic\Auth\UserRepository as BaseRepository; use Statamic\Contracts\Auth\User as UserContract; +use Statamic\Exceptions\UserNotFoundException; use Statamic\Facades\Blink; class UserRepository extends BaseRepository @@ -48,6 +49,17 @@ public function findByEmail(string $email): ?UserContract return $this->make()->model($model); } + public function findOrFail($id): UserContract + { + $user = $this->find($id); + + if (! $user) { + throw new UserNotFoundException($id); + } + + return $user; + } + public function model($method, ...$args) { $model = $this->config['model']; diff --git a/src/Contracts/Assets/AssetContainerRepository.php b/src/Contracts/Assets/AssetContainerRepository.php index 7f906992c08..f848c908c8f 100644 --- a/src/Contracts/Assets/AssetContainerRepository.php +++ b/src/Contracts/Assets/AssetContainerRepository.php @@ -12,5 +12,7 @@ public function find($id): ?AssetContainer; public function findByHandle(string $handle): ?AssetContainer; + public function findOrFail(string $handle): AssetContainer; + public function make(?string $handle = null): AssetContainer; } diff --git a/src/Contracts/Assets/AssetRepository.php b/src/Contracts/Assets/AssetRepository.php index feb5ce42165..75c89a8b280 100644 --- a/src/Contracts/Assets/AssetRepository.php +++ b/src/Contracts/Assets/AssetRepository.php @@ -18,6 +18,8 @@ public function findById(string $id); public function findByPath(string $path); + public function findOrFail(string $asset); + public function make(); public function query(); diff --git a/src/Contracts/Auth/UserRepository.php b/src/Contracts/Auth/UserRepository.php index b29fbb92fa7..b27ceca2f70 100644 --- a/src/Contracts/Auth/UserRepository.php +++ b/src/Contracts/Auth/UserRepository.php @@ -17,6 +17,8 @@ public function findByEmail(string $email): ?User; public function findByOAuthId(Provider $provider, string $id): ?User; + public function findOrFail($id): User; + public function current(): ?User; public function fromUser($user): ?User; diff --git a/src/Contracts/Entries/CollectionRepository.php b/src/Contracts/Entries/CollectionRepository.php index 34ea4209f7c..992afe08d5b 100644 --- a/src/Contracts/Entries/CollectionRepository.php +++ b/src/Contracts/Entries/CollectionRepository.php @@ -14,6 +14,8 @@ public function findByHandle($handle): ?Collection; public function findByMount($mount): ?Collection; + public function findOrFail($id): Collection; + public function make(?string $handle = null): Collection; public function handles(): IlluminateCollection; diff --git a/src/Contracts/Globals/GlobalRepository.php b/src/Contracts/Globals/GlobalRepository.php index 2b10b655355..ac396beac0c 100644 --- a/src/Contracts/Globals/GlobalRepository.php +++ b/src/Contracts/Globals/GlobalRepository.php @@ -12,5 +12,7 @@ public function find($id): ?GlobalSet; public function findByHandle($handle): ?GlobalSet; + public function findOrFail($id): GlobalSet; + public function save($global); } diff --git a/src/Contracts/Globals/GlobalVariablesRepository.php b/src/Contracts/Globals/GlobalVariablesRepository.php index 9115cf37233..f9a53c645da 100644 --- a/src/Contracts/Globals/GlobalVariablesRepository.php +++ b/src/Contracts/Globals/GlobalVariablesRepository.php @@ -10,6 +10,8 @@ public function all(): VariablesCollection; public function find($id): ?Variables; + public function findOrFail($id): Variables; + public function whereSet($handle): VariablesCollection; public function save($variable); diff --git a/src/Contracts/Structures/NavigationRepository.php b/src/Contracts/Structures/NavigationRepository.php index 0a09becf3dc..48421c11d3c 100644 --- a/src/Contracts/Structures/NavigationRepository.php +++ b/src/Contracts/Structures/NavigationRepository.php @@ -12,6 +12,8 @@ public function find($id): ?Nav; public function findByHandle($handle): ?Nav; + public function findOrFail($id): Nav; + public function save(Nav $nav); public function make(?string $handle = null): Nav; diff --git a/src/Contracts/Taxonomies/TaxonomyRepository.php b/src/Contracts/Taxonomies/TaxonomyRepository.php index 0e0b6690c15..042d64b991f 100644 --- a/src/Contracts/Taxonomies/TaxonomyRepository.php +++ b/src/Contracts/Taxonomies/TaxonomyRepository.php @@ -14,6 +14,8 @@ public function findByHandle($handle): ?Taxonomy; public function findByUri(string $uri): ?Taxonomy; + public function findOrFail($id): Taxonomy; + public function make(?string $handle = null): Taxonomy; public function handles(): Collection; diff --git a/src/Contracts/Taxonomies/TermRepository.php b/src/Contracts/Taxonomies/TermRepository.php index 98914cec302..df0c4919b06 100644 --- a/src/Contracts/Taxonomies/TermRepository.php +++ b/src/Contracts/Taxonomies/TermRepository.php @@ -14,6 +14,8 @@ public function find($id); public function findByUri(string $uri); + public function findOrFail($id); + public function make(?string $slug = null); public function query(); diff --git a/src/Exceptions/AssetNotFoundException.php b/src/Exceptions/AssetNotFoundException.php new file mode 100644 index 00000000000..3e27df6dbcf --- /dev/null +++ b/src/Exceptions/AssetNotFoundException.php @@ -0,0 +1,15 @@ +asset = $asset; + } +} diff --git a/src/Exceptions/FormNotFoundException.php b/src/Exceptions/FormNotFoundException.php new file mode 100644 index 00000000000..0150d78bdcb --- /dev/null +++ b/src/Exceptions/FormNotFoundException.php @@ -0,0 +1,15 @@ +form = $form; + } +} diff --git a/src/Exceptions/GlobalSetNotFoundException.php b/src/Exceptions/GlobalSetNotFoundException.php new file mode 100644 index 00000000000..b7588d780f9 --- /dev/null +++ b/src/Exceptions/GlobalSetNotFoundException.php @@ -0,0 +1,15 @@ +global = $global; + } +} diff --git a/src/Exceptions/GlobalVariablesNotFoundException.php b/src/Exceptions/GlobalVariablesNotFoundException.php new file mode 100644 index 00000000000..58099f0dc63 --- /dev/null +++ b/src/Exceptions/GlobalVariablesNotFoundException.php @@ -0,0 +1,15 @@ +variables = $variables; + } +} diff --git a/src/Exceptions/TermNotFoundException.php b/src/Exceptions/TermNotFoundException.php new file mode 100644 index 00000000000..ba204a1534b --- /dev/null +++ b/src/Exceptions/TermNotFoundException.php @@ -0,0 +1,17 @@ +term = $term; + } +} diff --git a/src/Exceptions/UserNotFoundException.php b/src/Exceptions/UserNotFoundException.php new file mode 100644 index 00000000000..73637e58f1f --- /dev/null +++ b/src/Exceptions/UserNotFoundException.php @@ -0,0 +1,15 @@ +user = $user; + } +} diff --git a/src/Facades/Asset.php b/src/Facades/Asset.php index 9ed3d198238..78568682da4 100644 --- a/src/Facades/Asset.php +++ b/src/Facades/Asset.php @@ -16,6 +16,7 @@ * @method static AssetContract|null findByUrl(string $url) * @method static AssetContract|null findById(string $id) * @method static AssetContract|null findByPath(string $path) + * @method static AssetContract findOrFail(string $asset) * @method static AssetContract make() * @method static QueryBuilder query() * @method static void save($asset) diff --git a/src/Facades/AssetContainer.php b/src/Facades/AssetContainer.php index bcc87900cb8..1d0644ada07 100644 --- a/src/Facades/AssetContainer.php +++ b/src/Facades/AssetContainer.php @@ -9,6 +9,7 @@ * @method static \Illuminate\Support\Collection all() * @method static null|\Statamic\Contracts\Assets\AssetContainer find($id) * @method static null|\Statamic\Contracts\Assets\AssetContainer findByHandle(string $handle) + * @method static \Statamic\Contracts\Assets\AssetContainer findOrFail($id) * @method static \Statamic\Contracts\Assets\AssetContainer make(string $handle = null) * * @see \Statamic\Assets\AssetRepository diff --git a/src/Facades/Blueprint.php b/src/Facades/Blueprint.php index ed1558e269b..4c381fe4896 100644 --- a/src/Facades/Blueprint.php +++ b/src/Facades/Blueprint.php @@ -9,6 +9,7 @@ * @method static self setDirectory(string $directory) * @method static self setFallbackDirectory(string $directory) * @method static null|\Statamic\Fields\Blueprint find($handle) + * @method static \Statamic\Fields\Blueprint findOrFail($handle) * @method static void save(Blueprint $blueprint) * @method static void delete(Blueprint $blueprint) * @method static \Statamic\Fields\Blueprint make($handle = null) diff --git a/src/Facades/Collection.php b/src/Facades/Collection.php index d939cdd9d54..6409e321e80 100644 --- a/src/Facades/Collection.php +++ b/src/Facades/Collection.php @@ -10,6 +10,7 @@ * @method static null|\Statamic\Entries\Collection find($id) * @method static null|\Statamic\Entries\Collection findByHandle($handle) * @method static null|\Statamic\Entries\Collection findByMount($mount) + * @method static \Statamic\Contracts\Entries\Collection findOrFail($id) * @method static \Statamic\Entries\Collection make(string $handle = null) * @method static \Illuminate\Support\Collection handles() * @method static bool handleExists(string $handle) diff --git a/src/Facades/Fieldset.php b/src/Facades/Fieldset.php index 3718ffbaac7..8793c8e827e 100644 --- a/src/Facades/Fieldset.php +++ b/src/Facades/Fieldset.php @@ -8,6 +8,7 @@ /** * @method static self setDirectory($directory) * @method static null|\Statamic\Fields\Fieldset find(string $handle) + * @method static \Statamic\Fields\Fieldset findOrFail(string $handle) * @method static bool exists(string $handle) * @method static \Statamic\Fields\Fieldset make($handle = null) * @method static \Illuminate\Support\Collection all() diff --git a/src/Facades/Form.php b/src/Facades/Form.php index b085f0f4971..9842ae975e2 100644 --- a/src/Facades/Form.php +++ b/src/Facades/Form.php @@ -7,6 +7,7 @@ /** * @method static \Statamic\Contracts\Forms\Form find($handle) + * @method static \Statamic\Contracts\Forms\Form findOrFail($handle) * @method static \Illuminate\Support\Collection all() * @method static \Statamic\Contracts\Forms\Form make($handle = null) * diff --git a/src/Facades/GlobalSet.php b/src/Facades/GlobalSet.php index 3a9b12aac15..adf8c868467 100644 --- a/src/Facades/GlobalSet.php +++ b/src/Facades/GlobalSet.php @@ -9,6 +9,7 @@ * @method static \Statamic\Globals\GlobalCollection all() * @method static null|\Statamic\Globals\GlobalCollection find($id) * @method static null|\Statamic\Globals\GlobalCollection findByHandle($handle) + * @method static \Statamic\Globals\GlobalCollection findOrFail($id) * @method static void save($global); * * @see \Statamic\Globals\GlobalCollection diff --git a/src/Facades/GlobalVariables.php b/src/Facades/GlobalVariables.php index c99719851d1..77591809ee7 100644 --- a/src/Facades/GlobalVariables.php +++ b/src/Facades/GlobalVariables.php @@ -8,6 +8,7 @@ /** * @method static \Statamic\Globals\VariablesCollection all() * @method static null|\Statamic\Globals\Variables find($id) + * @method static \Statamic\Globals\Variables findOrFail($id) * @method static \Statamic\Globals\VariablesCollection whereSet($set) * @method static void save($variable); * diff --git a/src/Facades/User.php b/src/Facades/User.php index 13dc93f1adb..6a0b214841e 100644 --- a/src/Facades/User.php +++ b/src/Facades/User.php @@ -12,6 +12,7 @@ * @method static null|\Statamic\Contracts\Auth\User find($id) * @method static null|\Statamic\Contracts\Auth\User findByEmail(string $email) * @method static null|\Statamic\Contracts\Auth\User findByOAuthId(Provider $provider, string $id) + * @method static \Statamic\Contracts\Auth\User findOrFail($id) * @method static null|\Statamic\Contracts\Auth\User current() * @method static null|\Statamic\Contracts\Auth\User fromUser($user) * @method static void save(User $user); diff --git a/src/Fields/BlueprintRepository.php b/src/Fields/BlueprintRepository.php index 4375b55c640..e189e15f87f 100644 --- a/src/Fields/BlueprintRepository.php +++ b/src/Fields/BlueprintRepository.php @@ -3,6 +3,7 @@ namespace Statamic\Fields; use Closure; +use Statamic\Exceptions\BlueprintNotFoundException; use Statamic\Facades\Blink; use Statamic\Facades\File; use Statamic\Facades\Path; @@ -51,6 +52,17 @@ public function find($blueprint): ?Blueprint }); } + public function findOrFail($id): Blueprint + { + $blueprint = $this->find($id); + + if (! $blueprint) { + throw new BlueprintNotFoundException($id); + } + + return $blueprint; + } + public function findStandardBlueprintPath($handle) { if (Str::startsWith($handle, 'vendor.')) { diff --git a/src/Fields/FieldsetRepository.php b/src/Fields/FieldsetRepository.php index fabcf2e98f2..b500cb04576 100644 --- a/src/Fields/FieldsetRepository.php +++ b/src/Fields/FieldsetRepository.php @@ -3,6 +3,7 @@ namespace Statamic\Fields; use Illuminate\Support\Collection; +use Statamic\Exceptions\FieldsetNotFoundException; use Statamic\Facades\File; use Statamic\Facades\Path; use Statamic\Facades\YAML; @@ -52,6 +53,17 @@ public function find(string $handle): ?Fieldset return $fieldset; } + public function findOrFail($id): Fieldset + { + $blueprint = $this->find($id); + + if (! $blueprint) { + throw new FieldsetNotFoundException($id); + } + + return $blueprint; + } + private function standardFieldsetPath(string $handle) { $handle = str_replace('/', '.', $handle); diff --git a/src/Forms/FormRepository.php b/src/Forms/FormRepository.php index 7b8ccd5e188..b18557fb3a1 100644 --- a/src/Forms/FormRepository.php +++ b/src/Forms/FormRepository.php @@ -6,6 +6,7 @@ use Statamic\Contracts\Forms\Form as FormContract; use Statamic\Contracts\Forms\FormRepository as Contract; use Statamic\Contracts\Forms\Submission as SubmissionContract; +use Statamic\Exceptions\FormNotFoundException; use Statamic\Facades\File; use Statamic\Facades\Folder; use Statamic\Forms\Exporters\ExporterRepository; @@ -31,6 +32,17 @@ public function find($handle) return $form->hydrate(); } + public function findOrFail($handle): FormContract + { + $form = $this->find($handle); + + if (! $form) { + throw new FormNotFoundException($handle); + } + + return $form; + } + /** * Get all forms. * diff --git a/src/Stache/Repositories/AssetContainerRepository.php b/src/Stache/Repositories/AssetContainerRepository.php index 7fcb89a3e8e..9648d704619 100644 --- a/src/Stache/Repositories/AssetContainerRepository.php +++ b/src/Stache/Repositories/AssetContainerRepository.php @@ -5,6 +5,7 @@ use Illuminate\Support\Collection; use Statamic\Contracts\Assets\AssetContainer; use Statamic\Contracts\Assets\AssetContainerRepository as RepositoryContract; +use Statamic\Exceptions\AssetContainerNotFoundException; use Statamic\Stache\Stache; class AssetContainerRepository implements RepositoryContract @@ -33,6 +34,17 @@ public function findByHandle(string $handle): ?AssetContainer return $this->store->getItem($handle); } + public function findOrFail($id): AssetContainer + { + $container = $this->find($id); + + if (! $container) { + throw new AssetContainerNotFoundException($id); + } + + return $container; + } + public function make(?string $handle = null): AssetContainer { return app(AssetContainer::class)->handle($handle); diff --git a/src/Stache/Repositories/CollectionRepository.php b/src/Stache/Repositories/CollectionRepository.php index b57c7412e30..0f04ec283bf 100644 --- a/src/Stache/Repositories/CollectionRepository.php +++ b/src/Stache/Repositories/CollectionRepository.php @@ -6,6 +6,7 @@ use Statamic\Contracts\Entries\Collection; use Statamic\Contracts\Entries\CollectionRepository as RepositoryContract; use Statamic\Data\StoresScopedComputedFieldCallbacks; +use Statamic\Exceptions\CollectionNotFoundException; use Statamic\Facades\Blink; use Statamic\Stache\Stache; @@ -51,6 +52,17 @@ public function findByMount($mount): ?Collection }); } + public function findOrFail($id): Collection + { + $collection = $this->find($id); + + if (! $collection) { + throw new CollectionNotFoundException($id); + } + + return $collection; + } + public function make(?string $handle = null): Collection { return app(Collection::class)->handle($handle); diff --git a/src/Stache/Repositories/GlobalRepository.php b/src/Stache/Repositories/GlobalRepository.php index 8a8061f4e6b..71f2bddda17 100644 --- a/src/Stache/Repositories/GlobalRepository.php +++ b/src/Stache/Repositories/GlobalRepository.php @@ -4,6 +4,7 @@ use Statamic\Contracts\Globals\GlobalRepository as RepositoryContract; use Statamic\Contracts\Globals\GlobalSet; +use Statamic\Exceptions\GlobalSetNotFoundException; use Statamic\Globals\GlobalCollection; use Statamic\Stache\Stache; @@ -42,6 +43,17 @@ public function findByHandle($handle): ?GlobalSet return $this->find($key); } + public function findOrFail($id): GlobalSet + { + $global = $this->find($id); + + if (! $global) { + throw new GlobalSetNotFoundException($id); + } + + return $global; + } + public function save($global) { $this->store->save($global); diff --git a/src/Stache/Repositories/GlobalVariablesRepository.php b/src/Stache/Repositories/GlobalVariablesRepository.php index 7e8608f132b..5d6559d0a79 100644 --- a/src/Stache/Repositories/GlobalVariablesRepository.php +++ b/src/Stache/Repositories/GlobalVariablesRepository.php @@ -4,6 +4,7 @@ use Statamic\Contracts\Globals\GlobalVariablesRepository as RepositoryContract; use Statamic\Contracts\Globals\Variables; +use Statamic\Exceptions\GlobalVariablesNotFoundException; use Statamic\Globals\VariablesCollection; use Statamic\Stache\Stache; use Statamic\Support\Str; @@ -31,6 +32,17 @@ public function find($id): ?Variables return $this->store->getItem($id); } + public function findOrFail($id): Variables + { + $variables = $this->find($id); + + if (! $variables) { + throw new GlobalVariablesNotFoundException($id); + } + + return $variables; + } + public function whereSet($handle): VariablesCollection { return $this diff --git a/src/Stache/Repositories/NavigationRepository.php b/src/Stache/Repositories/NavigationRepository.php index 1ded4311bc4..46ad9db1153 100644 --- a/src/Stache/Repositories/NavigationRepository.php +++ b/src/Stache/Repositories/NavigationRepository.php @@ -5,6 +5,7 @@ use Illuminate\Support\Collection; use Statamic\Contracts\Structures\Nav; use Statamic\Contracts\Structures\NavigationRepository as RepositoryContract; +use Statamic\Exceptions\NavigationNotFoundException; use Statamic\Stache\Stache; class NavigationRepository implements RepositoryContract @@ -35,6 +36,17 @@ public function findByHandle($handle): ?Nav return $this->store->getItem($handle); } + public function findOrFail($id): Nav + { + $nav = $this->find($id); + + if (! $nav) { + throw new NavigationNotFoundException($id); + } + + return $nav; + } + public function save(Nav $nav) { $this->store->save($nav); diff --git a/src/Stache/Repositories/TaxonomyRepository.php b/src/Stache/Repositories/TaxonomyRepository.php index 0e1bd76fd30..50bf5666964 100644 --- a/src/Stache/Repositories/TaxonomyRepository.php +++ b/src/Stache/Repositories/TaxonomyRepository.php @@ -5,6 +5,7 @@ use Illuminate\Support\Collection; use Statamic\Contracts\Taxonomies\Taxonomy; use Statamic\Contracts\Taxonomies\TaxonomyRepository as RepositoryContract; +use Statamic\Exceptions\TaxonomyNotFoundException; use Statamic\Facades; use Statamic\Stache\Stache; use Statamic\Support\Str; @@ -29,6 +30,17 @@ public function find($id): ?Taxonomy return $this->findByHandle($id); } + public function findOrFail($id): Taxonomy + { + $taxonomy = $this->find($id); + + if (! $taxonomy) { + throw new TaxonomyNotFoundException($id); + } + + return $taxonomy; + } + public function handles(): Collection { return $this->store->paths()->keys(); diff --git a/src/Stache/Repositories/TermRepository.php b/src/Stache/Repositories/TermRepository.php index 9ff7984c019..2c0ec7e2270 100644 --- a/src/Stache/Repositories/TermRepository.php +++ b/src/Stache/Repositories/TermRepository.php @@ -5,6 +5,7 @@ use Statamic\Contracts\Taxonomies\Term; use Statamic\Contracts\Taxonomies\TermRepository as RepositoryContract; use Statamic\Exceptions\TaxonomyNotFoundException; +use Statamic\Exceptions\TermNotFoundException; use Statamic\Facades\Collection; use Statamic\Facades\Taxonomy; use Statamic\Stache\Query\TermQueryBuilder; @@ -100,6 +101,17 @@ public function findByUri(string $uri, ?string $site = null): ?Term return $term->collection($collection); } + public function findOrFail($id): Term + { + $term = $this->find($id); + + if (! $term) { + throw new TermNotFoundException($id); + } + + return $term; + } + public function save($term) { $this->store diff --git a/src/Stache/Repositories/UserRepository.php b/src/Stache/Repositories/UserRepository.php index f253277e675..3a8d7515050 100644 --- a/src/Stache/Repositories/UserRepository.php +++ b/src/Stache/Repositories/UserRepository.php @@ -8,6 +8,7 @@ use Statamic\Auth\UserCollection; use Statamic\Auth\UserRepository as BaseRepository; use Statamic\Contracts\Auth\User; +use Statamic\Exceptions\UserNotFoundException; use Statamic\Stache\Query\UserQueryBuilder; use Statamic\Stache\Stache; @@ -41,6 +42,17 @@ public function findByEmail(string $email): ?User return $this->query()->where('email', $email)->first(); } + public function findOrFail($id): User + { + $user = $this->find($id); + + if (! $user) { + throw new UserNotFoundException($id); + } + + return $user; + } + public function query() { return new UserQueryBuilder($this->store); diff --git a/tests/Assets/AssetRepositoryTest.php b/tests/Assets/AssetRepositoryTest.php index 11ac982114b..b87f577c95c 100644 --- a/tests/Assets/AssetRepositoryTest.php +++ b/tests/Assets/AssetRepositoryTest.php @@ -6,6 +6,8 @@ use Illuminate\Support\Carbon; use Illuminate\Support\Facades\Storage; use Statamic\Assets\AssetRepository; +use Statamic\Contracts\Assets\Asset as AssetContract; +use Statamic\Exceptions\AssetNotFoundException; use Statamic\Facades\Asset; use Statamic\Facades\AssetContainer; use Tests\PreventSavingStacheItemsToDisk; @@ -79,4 +81,38 @@ public function it_resolves_the_correct_disk_from_similar_names() $this->assertInstanceOf(\Statamic\Contracts\Assets\Asset::class, $foundAssetLongUrl); $this->assertEquals('test_long_url_same_beginning/foo/image_in_long.jpg', $foundAssetLongUrl->url()); } + + /** @test */ + public function it_finds_assets_using_find_or_fail() + { + Storage::fake('disk_short', ['url' => 'test']); + + $assetRepository = new AssetRepository; + + $file = UploadedFile::fake()->image('image.jpg', 30, 60); // creates a 723 byte image + + Storage::disk('disk_short')->putFileAs('foo', $file, 'image_in_short.jpg'); + $realFilePath = Storage::disk('disk_short')->path('foo/image_in_short.jpg'); + touch($realFilePath, $timestamp = Carbon::now()->subMinutes(3)->timestamp); + + $containerShortUrl = tap(AssetContainer::make('container_short')->disk('disk_short'))->save(); + $assetShortUrl = $containerShortUrl->makeAsset('foo/image_in_short.jpg'); + $assetRepository->save($assetShortUrl); + + $asset = $assetRepository->findOrFail($assetShortUrl->id()); + + $this->assertInstanceOf(AssetContract::class, $asset); + $this->assertEquals($assetShortUrl->id(), $asset->id()); + } + + /** @test */ + public function test_find_or_fail_throws_exception_when_asset_does_not_exist() + { + $assetRepository = new AssetRepository; + + $this->expectException(AssetNotFoundException::class); + $this->expectExceptionMessage('Asset [does-not-exist] not found'); + + $assetRepository->findOrFail('does-not-exist'); + } } diff --git a/tests/Auth/UserRepositoryTests.php b/tests/Auth/UserRepositoryTests.php index 50439831cf0..3023d51ddfc 100644 --- a/tests/Auth/UserRepositoryTests.php +++ b/tests/Auth/UserRepositoryTests.php @@ -2,6 +2,7 @@ namespace Tests\Auth; +use Statamic\Exceptions\UserNotFoundException; use Statamic\Facades\User; trait UserRepositoryTests @@ -73,6 +74,22 @@ public function it_gets_user_by_email_with_overridden_classes() $this->assertInstanceOf($this->fakeUserClass(), User::findByEmail('foo@bar.com')); } + /** @test */ + public function find_or_fail_gets_user() + { + User::make()->id(123)->email('foo@bar.com')->data(['name' => 'foo', 'password' => 'foo'])->save(); + $this->assertInstanceOf($this->userClass(), User::findOrFail(123)); + } + + /** @test */ + public function find_or_fail_throws_exception_when_user_does_not_exist() + { + $this->expectException(UserNotFoundException::class); + $this->expectExceptionMessage('User [does-not-exist] not found'); + + User::findOrFail('does-not-exist'); + } + /** @test */ public function it_normalizes_statamic_user() { diff --git a/tests/Fields/BlueprintRepositoryTest.php b/tests/Fields/BlueprintRepositoryTest.php index 7e91f936473..3315b9a27f6 100644 --- a/tests/Fields/BlueprintRepositoryTest.php +++ b/tests/Fields/BlueprintRepositoryTest.php @@ -3,6 +3,7 @@ namespace Tests\Fields; use Illuminate\Support\Collection; +use Statamic\Exceptions\BlueprintNotFoundException; use Statamic\Facades; use Statamic\Facades\File; use Statamic\Fields\Blueprint; @@ -302,4 +303,34 @@ public function it_sets_the_namespace_when_passed_when_making() $this->assertSame($blueprint->namespace(), 'test'); $this->assertSame($blueprint->handle(), 'handle'); } + + /** @test */ + public function it_gets_a_blueprint_using_find_or_fail() + { + $contents = <<<'EOT' +title: Test +tabs: + main: + fields: + - one + - two +EOT; + + File::shouldReceive('exists')->with('/path/to/resources/blueprints/test.yaml')->once()->andReturnTrue(); + File::shouldReceive('get')->with('/path/to/resources/blueprints/test.yaml')->once()->andReturn($contents); + + $blueprint = $this->repo->findOrFail('test'); + + $this->assertInstanceOf(Blueprint::class, $blueprint); + $this->assertEquals('test', $blueprint->handle()); + } + + /** @test */ + public function find_or_fail_throws_exception_when_blueprint_does_not_exist() + { + $this->expectException(BlueprintNotFoundException::class); + $this->expectExceptionMessage('Blueprint [does-not-exist] not found'); + + $this->repo->findOrFail('does-not-exist'); + } } diff --git a/tests/Fields/FieldsetRepositoryTest.php b/tests/Fields/FieldsetRepositoryTest.php index 8cda3c9e7cd..0983e13e06d 100644 --- a/tests/Fields/FieldsetRepositoryTest.php +++ b/tests/Fields/FieldsetRepositoryTest.php @@ -3,6 +3,7 @@ namespace Tests\Fields; use Illuminate\Support\Collection; +use Statamic\Exceptions\FieldsetNotFoundException; use Statamic\Facades; use Statamic\Facades\File; use Statamic\Fields\Fieldset; @@ -39,6 +40,7 @@ public function it_gets_a_fieldset() type: text display: Second Field EOT; + File::shouldReceive('exists')->with('/path/to/resources/fieldsets/test.yaml')->once()->andReturnTrue(); File::shouldReceive('get')->with('/path/to/resources/fieldsets/test.yaml')->once()->andReturn($contents); @@ -292,4 +294,39 @@ public function it_gets_an_overridden_namespaced_fieldset() $this->assertNull($this->repo->find('vendor.foo.bar.baz.test')); $this->assertFalse($this->repo->exists('vendor.foo.bar.baz.test')); } + + /** @test */ + public function it_gets_a_blueprint_using_find_or_fail() + { + $contents = <<<'EOT' +title: Test Fieldset +fields: + - + handle: one + field: + type: text + display: First Field + - + handle: two + field: + type: text + display: Second Field +EOT; + File::shouldReceive('exists')->with('/path/to/resources/fieldsets/test.yaml')->once()->andReturnTrue(); + File::shouldReceive('get')->with('/path/to/resources/fieldsets/test.yaml')->once()->andReturn($contents); + + $fieldset = $this->repo->findOrFail('test'); + + $this->assertInstanceOf(Fieldset::class, $fieldset); + $this->assertEquals('test', $fieldset->handle()); + } + + /** @test */ + public function find_or_fail_throws_exception_when_blueprint_does_not_exist() + { + $this->expectException(FieldsetNotFoundException::class); + $this->expectExceptionMessage('Fieldset [does-not-exist] not found'); + + $this->repo->findOrFail('does-not-exist'); + } } diff --git a/tests/Forms/FormRepositoryTest.php b/tests/Forms/FormRepositoryTest.php new file mode 100644 index 00000000000..4e229b2d78c --- /dev/null +++ b/tests/Forms/FormRepositoryTest.php @@ -0,0 +1,44 @@ +sites(['en']); + $this->app->instance(Stache::class, $stache); + + $this->repo = new FormRepository($stache); + } + + /** @test */ + public function test_find_or_fail_gets_form() + { + $this->repo->make('test_form')->title('Test')->save(); + + $form = $this->repo->findOrFail('test_form'); + + $this->assertInstanceOf(Form::class, $form); + $this->assertEquals('Test', $form->title()); + } + + /** @test */ + public function test_find_or_fail_throws_exception_when_form_does_not_exist() + { + $this->expectException(FormNotFoundException::class); + $this->expectExceptionMessage('Form [does-not-exist] not found'); + + $this->repo->findOrFail('does-not-exist'); + } +} diff --git a/tests/Stache/Repositories/AssetContainerRepositoryTest.php b/tests/Stache/Repositories/AssetContainerRepositoryTest.php index 6836a3b1464..84ff593670c 100644 --- a/tests/Stache/Repositories/AssetContainerRepositoryTest.php +++ b/tests/Stache/Repositories/AssetContainerRepositoryTest.php @@ -4,6 +4,7 @@ use Illuminate\Support\Collection as IlluminateCollection; use Statamic\Contracts\Assets\AssetContainer; +use Statamic\Exceptions\AssetContainerNotFoundException; use Statamic\Facades; use Statamic\Stache\Repositories\AssetContainerRepository; use Statamic\Stache\Stache; @@ -75,4 +76,22 @@ public function it_saves_a_container_to_the_stache_and_to_a_file() $this->assertTrue(file_exists($this->directory.'/new.yaml')); @unlink($this->directory.'/new.yaml'); } + + /** @test */ + public function test_find_or_fail_gets_container() + { + $container = $this->repo->findOrFail('main'); + + $this->assertInstanceOf(AssetContainer::class, $container); + $this->assertEquals('Main Assets', $container->title()); + } + + /** @test */ + public function test_find_or_fail_throws_exception_when_container_does_not_exist() + { + $this->expectException(AssetContainerNotFoundException::class); + $this->expectExceptionMessage('Asset Container [does-not-exist] not found'); + + $this->repo->findOrFail('does-not-exist'); + } } diff --git a/tests/Stache/Repositories/CollectionRepositoryTest.php b/tests/Stache/Repositories/CollectionRepositoryTest.php index ec17ca91a67..0c1b2d5d328 100644 --- a/tests/Stache/Repositories/CollectionRepositoryTest.php +++ b/tests/Stache/Repositories/CollectionRepositoryTest.php @@ -4,6 +4,7 @@ use Illuminate\Support\Collection as IlluminateCollection; use Statamic\Entries\Collection; +use Statamic\Exceptions\CollectionNotFoundException; use Statamic\Facades\Collection as CollectionAPI; use Statamic\Stache\Repositories\CollectionRepository; use Statamic\Stache\Stache; @@ -116,4 +117,22 @@ public function it_gets_additional_preview_targets() $this->assertEquals($previewTargetsCollection2, $previewTargetsTest2->all()); $this->assertNotEquals($previewTargetsTest->all(), $previewTargetsTest2->all()); } + + /** @test */ + public function test_find_or_fail_gets_collection() + { + $collection = $this->repo->findOrFail('blog'); + + $this->assertInstanceOf(Collection::class, $collection); + $this->assertEquals('Blog', $collection->title()); + } + + /** @test */ + public function test_find_or_fail_throws_exception_when_collection_does_not_exist() + { + $this->expectException(CollectionNotFoundException::class); + $this->expectExceptionMessage('Collection [does-not-exist] not found'); + + $this->repo->findOrFail('does-not-exist'); + } } diff --git a/tests/Stache/Repositories/GlobalRepositoryTest.php b/tests/Stache/Repositories/GlobalRepositoryTest.php index 1fee0aca1b8..bcc2ecf45a0 100644 --- a/tests/Stache/Repositories/GlobalRepositoryTest.php +++ b/tests/Stache/Repositories/GlobalRepositoryTest.php @@ -3,6 +3,7 @@ namespace Tests\Stache\Repositories; use Statamic\Contracts\Globals\GlobalSet; +use Statamic\Exceptions\GlobalSetNotFoundException; use Statamic\Facades\GlobalSet as GlobalSetAPI; use Statamic\Globals\GlobalCollection; use Statamic\Stache\Repositories\GlobalRepository; @@ -102,4 +103,22 @@ public function it_saves_a_global_to_the_stache_and_to_a_file() $this->assertFileExists($this->directory.'/new.yaml'); @unlink($this->directory.'/new.yaml'); } + + /** @test */ + public function test_find_or_fail_gets_global() + { + $set = $this->repo->findOrFail('contact'); + + $this->assertInstanceOf(GlobalSet::class, $set); + $this->assertEquals('Contact Details', $set->title()); + } + + /** @test */ + public function test_find_or_fail_throws_exception_when_global_does_not_exist() + { + $this->expectException(GlobalSetNotFoundException::class); + $this->expectExceptionMessage('Global Set [does-not-exist] not found'); + + $this->repo->findOrFail('does-not-exist'); + } } diff --git a/tests/Stache/Repositories/GlobalVariablesRepositoryTest.php b/tests/Stache/Repositories/GlobalVariablesRepositoryTest.php index b8c5aeceed5..fb3a6241555 100644 --- a/tests/Stache/Repositories/GlobalVariablesRepositoryTest.php +++ b/tests/Stache/Repositories/GlobalVariablesRepositoryTest.php @@ -3,6 +3,7 @@ namespace Tests\Stache\Repositories; use Statamic\Contracts\Globals\Variables; +use Statamic\Exceptions\GlobalVariablesNotFoundException; use Statamic\Facades\GlobalSet; use Statamic\Facades\Site; use Statamic\Globals\VariablesCollection; @@ -310,4 +311,26 @@ public function it_deletes_a_global_from_the_stache_and_file_with_multi_site() $this->assertFileDoesNotExist($this->directory.'/en/new.yaml'); @unlink($this->directory.'/new.yaml'); } + + /** @test */ + public function test_find_or_fail_gets_global() + { + $this->setUpSingleSite(); + + $var = $this->repo->findOrFail('global::en'); + + $this->assertInstanceOf(Variables::class, $var); + $this->assertEquals('General', $var->title()); + } + + /** @test */ + public function test_find_or_fail_throws_exception_when_global_does_not_exist() + { + $this->setUpSingleSite(); + + $this->expectException(GlobalVariablesNotFoundException::class); + $this->expectExceptionMessage('Global Variables [does-not-exist] not found'); + + $this->repo->findOrFail('does-not-exist'); + } } diff --git a/tests/Stache/Repositories/NavigationRepositoryTest.php b/tests/Stache/Repositories/NavigationRepositoryTest.php index 95cca0f26ff..2ec659889e8 100644 --- a/tests/Stache/Repositories/NavigationRepositoryTest.php +++ b/tests/Stache/Repositories/NavigationRepositoryTest.php @@ -4,6 +4,7 @@ use Illuminate\Support\Collection; use Statamic\Contracts\Structures\Structure; +use Statamic\Exceptions\NavigationNotFoundException; use Statamic\Stache\Repositories\NavigationRepository; use Statamic\Stache\Stache; use Statamic\Stache\Stores\CollectionsStore; @@ -77,4 +78,22 @@ public function it_saves_a_nav_to_the_stache_and_to_a_file() $this->assertFileExists($this->directory.'/new.yaml'); @unlink($this->directory.'/new.yaml'); } + + /** @test */ + public function test_find_or_fail_gets_nav() + { + $nav = $this->repo->findOrFail('footer'); + + $this->assertInstanceOf(Structure::class, $nav); + $this->assertEquals('Footer', $nav->title()); + } + + /** @test */ + public function test_find_or_fail_throws_exception_when_nav_does_not_exist() + { + $this->expectException(NavigationNotFoundException::class); + $this->expectExceptionMessage('Navigation [does-not-exist] not found'); + + $this->repo->findOrFail('does-not-exist'); + } } diff --git a/tests/Stache/Repositories/TaxonomyRepositoryTest.php b/tests/Stache/Repositories/TaxonomyRepositoryTest.php index a2eaab04af0..c0b099c2385 100644 --- a/tests/Stache/Repositories/TaxonomyRepositoryTest.php +++ b/tests/Stache/Repositories/TaxonomyRepositoryTest.php @@ -3,6 +3,7 @@ namespace Tests\Stache\Repositories; use Illuminate\Support\Collection as IlluminateCollection; +use Statamic\Exceptions\TaxonomyNotFoundException; use Statamic\Facades\Collection; use Statamic\Facades\Taxonomy as TaxonomyAPI; use Statamic\Stache\Repositories\TaxonomyRepository; @@ -125,4 +126,22 @@ public function it_gets_additional_preview_targets() $this->assertEquals($previewTargetsTaxonomy2, $previewTargetsTest2->all()); $this->assertNotEquals($previewTargetsTest->all(), $previewTargetsTest2->all()); } + + /** @test */ + public function test_find_or_fail_gets_taxonomy() + { + $taxonomy = $this->repo->findOrFail('tags'); + + $this->assertInstanceOf(Taxonomy::class, $taxonomy); + $this->assertEquals('Tags', $taxonomy->title()); + } + + /** @test */ + public function test_find_or_fail_throws_exception_when_taxonomy_does_not_exist() + { + $this->expectException(TaxonomyNotFoundException::class); + $this->expectExceptionMessage('Taxonomy [does-not-exist] not found'); + + $this->repo->findOrFail('does-not-exist'); + } }