From eac0cba9ae52c65f6658cc6d15140cf974ff277d Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Fri, 27 Sep 2019 01:51:32 +0100 Subject: [PATCH] Return Collections rather than arrays --- .../src/TwigExtension/TalksExtension.php | 20 +++++++++---------- src/Talks/tests/RetrievingEventsTest.php | 4 ++-- src/Talks/tests/RetrievingTalksTest.php | 10 ++++------ 3 files changed, 16 insertions(+), 18 deletions(-) diff --git a/src/Talks/src/TwigExtension/TalksExtension.php b/src/Talks/src/TwigExtension/TalksExtension.php index b21ff1ec..3a199585 100644 --- a/src/Talks/src/TwigExtension/TalksExtension.php +++ b/src/Talks/src/TwigExtension/TalksExtension.php @@ -65,18 +65,18 @@ class TalksExtension extends AbstractExtension }); } - public function filterUpcomingTalks(Collection $talks): array + public function filterUpcomingTalks(Collection $talks): Collection { return $talks->filter(function ($talk): bool { return $this->getLastDate($talk) >= $this->today; - })->values()->toArray(); + })->values(); } - public function filterPastTalks(Collection $talks): array + public function filterPastTalks(Collection $talks): Collection { return $talks->filter(function ($talk): bool { return $this->getLastDate($talk) < $this->today; - })->values()->toArray(); + })->values(); } private function getLastDate($talk): string @@ -85,18 +85,18 @@ class TalksExtension extends AbstractExtension ->pluck('date')->max(); } - public function filterUpcomingEvents($talks): array + public function filterUpcomingEvents($talks): Collection { return $this->eventsFromTalks($talks)->filter(function ($event): bool { return $event['date'] >= $this->today; - })->sortBy('date')->toArray(); + })->sortBy('date'); } - public function filterPastEvents($talks): array + public function filterPastEvents($talks): Collection { return $this->eventsFromTalks($talks)->filter(function ($event): bool { return $event['date'] < $this->today; - })->sortBy('date')->toArray(); + })->sortBy('date'); } private function eventsFromTalks($talks): Collection @@ -106,8 +106,8 @@ class TalksExtension extends AbstractExtension }); } - public function getAllEvents($talks): array + public function getAllEvents($talks): Collection { - return $this->eventsFromTalks($talks)->toArray(); + return $this->eventsFromTalks($talks); } } diff --git a/src/Talks/tests/RetrievingEventsTest.php b/src/Talks/tests/RetrievingEventsTest.php index 3feb292b..abef9afd 100644 --- a/src/Talks/tests/RetrievingEventsTest.php +++ b/src/Talks/tests/RetrievingEventsTest.php @@ -53,7 +53,7 @@ class RetrievingEventsTest extends TestCase $events = $this->extension->filterPastEvents($talks); $this->assertInstanceOf(Collection::class, $talks); - $this->assertTrue(is_array($events)); + $this->assertInstanceOf(Collection::class, $events); $this->assertCount(2, $events); } @@ -89,7 +89,7 @@ class RetrievingEventsTest extends TestCase $events = $this->extension->filterUpcomingEvents($talks); $this->assertInstanceOf(Collection::class, $talks); - $this->assertTrue(is_array($events)); + $this->assertInstanceOf(Collection::class, $events); $this->assertCount(2, $events); } diff --git a/src/Talks/tests/RetrievingTalksTest.php b/src/Talks/tests/RetrievingTalksTest.php index 24f4c8a7..c70f36c0 100644 --- a/src/Talks/tests/RetrievingTalksTest.php +++ b/src/Talks/tests/RetrievingTalksTest.php @@ -95,7 +95,7 @@ class RetrievingTalksTest extends TestCase $filtered = $this->extension->filterPastTalks($talks); $this->assertCount(1, $filtered); - $this->assertSame($pastTalk, $filtered[0]); + $this->assertSame($pastTalk, $filtered->first()); } /** @test */ @@ -125,8 +125,8 @@ class RetrievingTalksTest extends TestCase $talks = $this->extension->getTalks([$pastTalk, $todayTalk, $futureTalk]); $filtered = $this->extension->filterUpcomingTalks($talks); - $this->assertCount(2, $filtered); - $this->assertSame([$todayTalk, $futureTalk], $filtered); + $this->assertSame(2, $filtered->count()); + $this->assertSame([$todayTalk, $futureTalk], $filtered->toArray()); } /** @test */ @@ -166,9 +166,7 @@ class RetrievingTalksTest extends TestCase $talks = collect([$talkA, $talkB]); - tap($this->extension->getAllEvents($talks), function (array $events) { - $events = collect($events); - + tap($this->extension->getAllEvents($talks), function (Collection $events) { $this->assertCount(3, $events); $this->assertSame(