Ensure that talks are sorted by the nearest event date

This commit is contained in:
Oliver Davies 2019-01-18 20:38:58 +00:00
parent 18e6d2e32f
commit 303162e65d
2 changed files with 29 additions and 2 deletions

View file

@ -46,7 +46,9 @@ class TalksExtension extends Twig_Extension
*/
public function getAll($talks, array $eventData = []): Collection
{
return collect($talks);
return collect($talks)->sortBy(function ($talk) {
return collect($talk['events'])->pluck('date')->sort()->last();
});
}
/**

View file

@ -46,7 +46,32 @@ class TalksExtensionTest extends TestCase
/** @test */
public function talks_are_ordered_by_the_most_recent_event_date()
{
$this->markTestIncomplete();
$talkA = [
'title' => 'Talk A',
'events' => [
['event' => 'event_a', 'date' => (new DateTime('-5 days'))->format(TalksExtension::DATE_FORMAT)],
],
];
$talkB = [
'title' => 'Talk B',
'events' => [
['event' => 'event_a', 'date' => (new DateTime('-20 days'))->format(TalksExtension::DATE_FORMAT)],
],
];
$talkC = [
'title' => 'Talk C',
'events' => [
['event' => 'event_a', 'date' => (new DateTime('-3 days'))->format(TalksExtension::DATE_FORMAT)],
['event' => 'event_b', 'date' => (new DateTime('-10 days'))->format(TalksExtension::DATE_FORMAT)],
],
];
$unorderedTalks = [$talkC, $talkA, $talkB];
$orderedTalks = $this->extension->getAll($unorderedTalks);
$this->assertEquals([$talkC, $talkA, $talkB], $orderedTalks->toArray());
}
/** @test */