From 5ab4097ace26aad4c441466270504829ecd2b74f Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Thu, 13 Jan 2022 23:26:42 +0000 Subject: [PATCH] feat(meetup): add Meetup implementation --- config/services.yaml | 2 +- src/MeetupEventRepository.php | 35 +++++++++++++++++++++++++++ tests/EventRepositoryContractTest.php | 2 +- tests/FakeEventRepository.php | 23 +++++++++++------- tests/FakeEventRepositoryTest.php | 4 +++ tests/MeetupEventRepositoryTest.php | 23 ++++++++++++++++++ 6 files changed, 78 insertions(+), 11 deletions(-) create mode 100644 src/MeetupEventRepository.php create mode 100644 tests/MeetupEventRepositoryTest.php diff --git a/config/services.yaml b/config/services.yaml index 8b90440..3d38207 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -26,5 +26,5 @@ services: # add more service definitions when explicit configuration is needed # please note that last definitions always *replace* previous ones App\EventRepository: - class: App\Tests\FakeEventRepository + class: App\MeetupEventRepository public: true diff --git a/src/MeetupEventRepository.php b/src/MeetupEventRepository.php new file mode 100644 index 0000000..dc4687b --- /dev/null +++ b/src/MeetupEventRepository.php @@ -0,0 +1,35 @@ +client = $client; + } + + public function getConfirmedAttendees(): Collection + { + $response = $this->client->request('GET', 'https://api.meetup.com/php-south-wales/events/282265786/rsvps'); + + $rsvps = json_decode($response->getContent()); + + return Collection::make($rsvps) + ->filter(fn (\stdClass $rsvp): bool => $rsvp->response == 'yes') + ->filter(fn (\stdClass $attendee): bool => !$attendee->member->event_context->host) + ->map(function (\stdClass $attendee): \stdClass { + $attendee->is_attending = true; + $attendee->is_host = false; + + return $attendee; + }) + ; + } +} diff --git a/tests/EventRepositoryContractTest.php b/tests/EventRepositoryContractTest.php index 68533e4..7987b14 100644 --- a/tests/EventRepositoryContractTest.php +++ b/tests/EventRepositoryContractTest.php @@ -21,6 +21,6 @@ trait EventRepositoryContractTest { $attendees = $this->repository->getConfirmedAttendees(); - $this->assertFalse($attendees->pluck('is_host')->contains(true)); + $this->assertSame([false], $attendees->pluck('is_host')->unique()->toArray()); } } diff --git a/tests/FakeEventRepository.php b/tests/FakeEventRepository.php index b27e696..7ffefe9 100644 --- a/tests/FakeEventRepository.php +++ b/tests/FakeEventRepository.php @@ -9,19 +9,24 @@ use Tightenco\Collect\Support\Collection; final class FakeEventRepository implements EventRepository { - private static array $rsvps = [ - ['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], - ]; + private static array $rsvps = []; + + public function __construct() + { + self::$rsvps = [ + (object) ['name' => 'Oliver Davies.', 'is_attending' => true, 'is_host' => true], + (object) ['name' => 'matthew s.', 'is_attending' => true, 'is_host' => false], + (object) ['name' => 'Michael P.', 'is_attending' => true, 'is_host' => false], + (object) ['name' => 'Kathryn "Kat" R.', 'is_attending' => true, 'is_host' => false], + (object) ['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['is_attending']) - ->filter(fn (array $attendee): bool => !$attendee['is_host']) + ->filter(fn (\stdClass $attendee): bool => $attendee->is_attending) + ->filter(fn (\stdClass $attendee): bool => !$attendee->is_host) ; } } diff --git a/tests/FakeEventRepositoryTest.php b/tests/FakeEventRepositoryTest.php index 2c68065..a957bbf 100644 --- a/tests/FakeEventRepositoryTest.php +++ b/tests/FakeEventRepositoryTest.php @@ -15,6 +15,10 @@ final class FakeEventRepositoryTest extends KernelTestCase { self::bootKernel(); + $container = static::$container; + + $container->set(EventRepository::class, new FakeEventRepository()); + $this->repository = static::$container->get(EventRepository::class); } } diff --git a/tests/MeetupEventRepositoryTest.php b/tests/MeetupEventRepositoryTest.php new file mode 100644 index 0000000..c8dbf58 --- /dev/null +++ b/tests/MeetupEventRepositoryTest.php @@ -0,0 +1,23 @@ +repository = self::$container->get(EventRepository::class); + } +}