Skip to content

Commit

Permalink
[4.x] Implement find or fail on EntryRepository (#9506)
Browse files Browse the repository at this point in the history
  • Loading branch information
benfurfie authored Feb 20, 2024
1 parent 20b8380 commit 1f31074
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/Exceptions/EntryNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Statamic\Exceptions;

class EntryNotFoundException extends \Exception
{
protected $entry;

public function __construct($entry)
{
parent::__construct("Entry [{$entry}] not found");

$this->entry = $entry;
}
}
1 change: 1 addition & 0 deletions src/Facades/Entry.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* @method static \Statamic\Entries\EntryCollection whereCollection(string $handle)
* @method static \Statamic\Entries\EntryCollection whereInCollection(array $handles)
* @method static null|\Statamic\Contracts\Entries\Entry find($id)
* @method static \Statamic\Contracts\Entries\Entry findOrFail($id)
* @method static null|\Statamic\Contracts\Entries\Entry findByUri(string $uri, string $site)
* @method static \Statamic\Contracts\Entries\Entry make()
* @method static \Statamic\Contracts\Entries\QueryBuilder query()
Expand Down
12 changes: 12 additions & 0 deletions src/Stache/Repositories/EntryRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Statamic\Contracts\Entries\EntryRepository as RepositoryContract;
use Statamic\Contracts\Entries\QueryBuilder;
use Statamic\Entries\EntryCollection;
use Statamic\Exceptions\EntryNotFoundException;
use Statamic\Stache\Query\EntryQueryBuilder;
use Statamic\Stache\Stache;
use Statamic\Support\Arr;
Expand Down Expand Up @@ -43,6 +44,17 @@ public function find($id): ?Entry
return $this->query()->where('id', $id)->first();
}

public function findOrFail($id): Entry
{
$entry = $this->find($id);

if (! $entry) {
throw new EntryNotFoundException($id);
}

return $entry;
}

public function findByUri(string $uri, ?string $site = null): ?Entry
{
$site = $site ?? $this->stache->sites()->first();
Expand Down
19 changes: 19 additions & 0 deletions tests/Stache/Repositories/EntryRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Statamic\Contracts\Entries\Entry;
use Statamic\Entries\EntryCollection;
use Statamic\Exceptions\EntryNotFoundException;
use Statamic\Facades\Collection;
use Statamic\Facades\Entry as EntryAPI;
use Statamic\Stache\Repositories\EntryRepository;
Expand Down Expand Up @@ -136,6 +137,24 @@ public function it_gets_entry_by_id()
$this->assertNull($this->repo->find('unknown'));
}

/** @test */
public function test_find_or_fail_gets_entry()
{
$entry = $this->repo->findOrFail('alphabetical-bravo');

$this->assertInstanceOf(Entry::class, $entry);
$this->assertEquals('Bravo', $entry->get('title'));
}

/** @test */
public function test_find_or_fail_throws_exception_when_entry_does_not_exist()
{
$this->expectException(EntryNotFoundException::class);
$this->expectExceptionMessage('Entry [does-not-exist] not found');

$this->repo->findOrFail('does-not-exist');
}

/**
* @test
*
Expand Down

0 comments on commit 1f31074

Please sign in to comment.