Return Collections rather than arrays

This commit is contained in:
Oliver Davies 2019-09-27 01:51:32 +01:00
parent 3ec494d693
commit eac0cba9ae
3 changed files with 16 additions and 18 deletions

View file

@ -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);
}
}

View file

@ -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);
}

View file

@ -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(