|
9 | 9 | use Statamic\Facades\Blueprint;
|
10 | 10 | use Statamic\Facades\Collection;
|
11 | 11 | use Statamic\Facades\Entry;
|
| 12 | +use Statamic\Query\Exceptions\MultipleRecordsFoundException; |
| 13 | +use Statamic\Query\Exceptions\RecordsNotFoundException; |
12 | 14 | use Statamic\Query\Scopes\Scope;
|
13 | 15 | use Tests\PreventSavingStacheItemsToDisk;
|
14 | 16 | use Tests\TestCase;
|
@@ -922,6 +924,112 @@ public function values_can_be_plucked()
|
922 | 924 | 'thing-2',
|
923 | 925 | ], Entry::query()->where('type', 'b')->pluck('slug')->all());
|
924 | 926 | }
|
| 927 | + |
| 928 | + #[Test] |
| 929 | + public function entry_can_be_found_using_first_or_fail() |
| 930 | + { |
| 931 | + Collection::make('posts')->save(); |
| 932 | + $entry = EntryFactory::collection('posts')->id('hoff')->slug('david-hasselhoff')->data(['title' => 'David Hasselhoff'])->create(); |
| 933 | + |
| 934 | + $firstOrFail = Entry::query() |
| 935 | + ->where('collection', 'posts') |
| 936 | + ->where('id', 'hoff') |
| 937 | + ->firstOrFail(); |
| 938 | + |
| 939 | + $this->assertSame($entry, $firstOrFail); |
| 940 | + } |
| 941 | + |
| 942 | + #[Test] |
| 943 | + public function exception_is_thrown_when_entry_does_not_exist_using_first_or_fail() |
| 944 | + { |
| 945 | + $this->expectException(RecordsNotFoundException::class); |
| 946 | + |
| 947 | + Entry::query() |
| 948 | + ->where('collection', 'posts') |
| 949 | + ->where('id', 'ze-hoff') |
| 950 | + ->firstOrFail(); |
| 951 | + } |
| 952 | + |
| 953 | + #[Test] |
| 954 | + public function entry_can_be_found_using_first_or() |
| 955 | + { |
| 956 | + Collection::make('posts')->save(); |
| 957 | + $entry = EntryFactory::collection('posts')->id('hoff')->slug('david-hasselhoff')->data(['title' => 'David Hasselhoff'])->create(); |
| 958 | + |
| 959 | + $firstOrFail = Entry::query() |
| 960 | + ->where('collection', 'posts') |
| 961 | + ->where('id', 'hoff') |
| 962 | + ->firstOr(function () { |
| 963 | + return 'fallback'; |
| 964 | + }); |
| 965 | + |
| 966 | + $this->assertSame($entry, $firstOrFail); |
| 967 | + } |
| 968 | + |
| 969 | + #[Test] |
| 970 | + public function callback_is_called_when_entry_does_not_exist_using_first_or() |
| 971 | + { |
| 972 | + $firstOrFail = Entry::query() |
| 973 | + ->where('collection', 'posts') |
| 974 | + ->where('id', 'hoff') |
| 975 | + ->firstOr(function () { |
| 976 | + return 'fallback'; |
| 977 | + }); |
| 978 | + |
| 979 | + $this->assertSame('fallback', $firstOrFail); |
| 980 | + } |
| 981 | + |
| 982 | + #[Test] |
| 983 | + public function sole_entry_is_returned() |
| 984 | + { |
| 985 | + Collection::make('posts')->save(); |
| 986 | + $entry = EntryFactory::collection('posts')->id('hoff')->slug('david-hasselhoff')->data(['title' => 'David Hasselhoff'])->create(); |
| 987 | + |
| 988 | + $sole = Entry::query() |
| 989 | + ->where('collection', 'posts') |
| 990 | + ->where('id', 'hoff') |
| 991 | + ->sole(); |
| 992 | + |
| 993 | + $this->assertSame($entry, $sole); |
| 994 | + } |
| 995 | + |
| 996 | + #[Test] |
| 997 | + public function exception_is_thrown_by_sole_when_multiple_entries_are_returned_from_query() |
| 998 | + { |
| 999 | + Collection::make('posts')->save(); |
| 1000 | + EntryFactory::collection('posts')->id('hoff')->slug('david-hasselhoff')->data(['title' => 'David Hasselhoff'])->create(); |
| 1001 | + EntryFactory::collection('posts')->id('smoff')->slug('joe-hasselsmoff')->data(['title' => 'Joe Hasselsmoff'])->create(); |
| 1002 | + |
| 1003 | + $this->expectException(MultipleRecordsFoundException::class); |
| 1004 | + |
| 1005 | + Entry::query() |
| 1006 | + ->where('collection', 'posts') |
| 1007 | + ->sole(); |
| 1008 | + } |
| 1009 | + |
| 1010 | + #[Test] |
| 1011 | + public function exception_is_thrown_by_sole_when_no_entries_are_returned_from_query() |
| 1012 | + { |
| 1013 | + $this->expectException(RecordsNotFoundException::class); |
| 1014 | + |
| 1015 | + Entry::query() |
| 1016 | + ->where('collection', 'posts') |
| 1017 | + ->sole(); |
| 1018 | + } |
| 1019 | + |
| 1020 | + #[Test] |
| 1021 | + public function exists_returns_true_when_results_are_found() |
| 1022 | + { |
| 1023 | + $this->createDummyCollectionAndEntries(); |
| 1024 | + |
| 1025 | + $this->assertTrue(Entry::query()->exists()); |
| 1026 | + } |
| 1027 | + |
| 1028 | + #[Test] |
| 1029 | + public function exists_returns_false_when_no_results_are_found() |
| 1030 | + { |
| 1031 | + $this->assertFalse(Entry::query()->exists()); |
| 1032 | + } |
925 | 1033 | }
|
926 | 1034 |
|
927 | 1035 | class CustomScope extends Scope
|
|
0 commit comments