refactor: generalise logic in the fake repository

This commit is contained in:
Oliver Davies 2022-01-13 23:06:25 +00:00
parent ae38fbf2b9
commit f3eda70384
3 changed files with 8 additions and 21 deletions

View file

@ -1,11 +0,0 @@
<?php
declare(strict_types=1);
namespace App;
final class RsvpResponse
{
public const RESPONSE_NO = 'no';
public const RESPONSE_YES = 'yes';
}

View file

@ -13,7 +13,7 @@ trait EventRepositoryContractTest
{
$attendees = $this->repository->getConfirmedAttendees();
$this->assertCount(3, $attendees);
$this->assertFalse($attendees->pluck('is_attending')->contains(false));
}
/** @test */
@ -21,6 +21,6 @@ trait EventRepositoryContractTest
{
$attendees = $this->repository->getConfirmedAttendees();
$this->assertCount(3, $attendees);
$this->assertFalse($attendees->pluck('is_host')->contains(true));
}
}

View file

@ -5,24 +5,22 @@ declare(strict_types=1);
namespace App\Tests;
use App\EventRepository;
use App\RsvpResponse;
use Tightenco\Collect\Support\Collection;
final class FakeEventRepository implements EventRepository
{
private static array $rsvps = [
['name' => 'Oliver Davies.', 'response' => RsvpResponse::RESPONSE_YES, 'is_host' => true],
['name' => 'matthew s.', 'response' => RsvpResponse::RESPONSE_YES, 'is_host' => false],
['name' => 'Michael P.', 'response' => RsvpResponse::RESPONSE_YES, 'is_host' => false],
['name' => 'Kathryn "Kat" R.', 'response' => RsvpResponse::RESPONSE_YES, 'is_host' => false],
['name' => 'Did not attend', 'response' => RsvpResponse::RESPONSE_NO, 'is_host' => false],
['name' => 'Oliver Davies.', 'is_attending' => true, 'is_host' => true],
['name' => 'matthew s.', 'is_attending' => true, 'is_host' => false],
['name' => 'Michael P.', 'is_attending' => true, 'is_host' => false],
['name' => 'Kathryn "Kat" R.', 'is_attending' => true, 'is_host' => false],
['name' => 'Did not attend', 'is_attending' => false, 'is_host' => false],
];
public function getConfirmedAttendees(): Collection
{
return Collection::make(self::$rsvps)
->filter(fn (array $attendee): bool => $attendee['response']
== RsvpResponse::RESPONSE_YES)
->filter(fn (array $attendee): bool => $attendee['is_attending'])
->filter(fn (array $attendee): bool => !$attendee['is_host'])
;
}