From eb6a42bae7f7d0672613203d651e34ca3b968f4e Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Tue, 7 Nov 2017 18:08:23 +0000 Subject: [PATCH] Sort talks as part of the FormatTalksBundle --- source/_includes/talk/events.html.twig | 2 +- .../Twig/FormatTalksExtension.php | 22 +++++-------------- .../Twig/FormatTalksTest.php | 14 +++++++----- 3 files changed, 15 insertions(+), 23 deletions(-) diff --git a/source/_includes/talk/events.html.twig b/source/_includes/talk/events.html.twig index c3993f33..a13b6fce 100644 --- a/source/_includes/talk/events.html.twig +++ b/source/_includes/talk/events.html.twig @@ -2,7 +2,7 @@

Events

{% include "talks-table" with { - talks: { talks: [page], event_data: site.events }|all_talks|reverse, + talks: { talks: [page], event_data: site.events }|all_talks, talk_page: true } %} diff --git a/src/FormatTalksBundle/Twig/FormatTalksExtension.php b/src/FormatTalksBundle/Twig/FormatTalksExtension.php index 28147a09..effcab78 100644 --- a/src/FormatTalksBundle/Twig/FormatTalksExtension.php +++ b/src/FormatTalksBundle/Twig/FormatTalksExtension.php @@ -41,7 +41,7 @@ class FormatTalksExtension extends Twig_Extension */ public function getAll(array $data) { - return $this->sort($this->format($data)); + return $this->format($data)->sortBy('event.date'); } /** @@ -55,9 +55,9 @@ class FormatTalksExtension extends Twig_Extension */ public function getUpcoming(array $data) { - return $this->sort($this->format($data)->filter(function ($talk) { + return $this->format($data)->filter(function ($talk) { return $talk['event']['date'] >= $this->today; - })); + })->sortBy('event.date'); } /** @@ -71,9 +71,9 @@ class FormatTalksExtension extends Twig_Extension */ public function getPast(array $data) { - return $this->sort($this->format($data)->filter(function ($talk) { + return $this->format($data)->filter(function ($talk) { return $talk['event']['date'] < $this->today; - })); + })->sortByDesc('event.date'); } /** @@ -100,18 +100,6 @@ class FormatTalksExtension extends Twig_Extension }); } - /** - * Sort and return the talks. - * - * @param Collection $talks The talk data. - * - * @return array - */ - private function sort(Collection $talks) - { - return $talks->sortByDesc('event.date')->all(); - } - /** * {@inheritdoc} */ diff --git a/tests/FormatTalksBundle/Twig/FormatTalksTest.php b/tests/FormatTalksBundle/Twig/FormatTalksTest.php index 386f2bb3..11d6f438 100644 --- a/tests/FormatTalksBundle/Twig/FormatTalksTest.php +++ b/tests/FormatTalksBundle/Twig/FormatTalksTest.php @@ -4,6 +4,7 @@ namespace FormatTalksBundle\Tests\Twig; use DateTime; use FormatTalksBundle\Twig\FormatTalksExtension; +use Illuminate\Support\Collection; use PHPUnit\Framework\TestCase; class FormatTalksTest extends TestCase @@ -99,8 +100,9 @@ class FormatTalksTest extends TestCase $this->assertCount(3, $results); + // Earliest events should be returned first. $this->assertEquals( - [$eventA['date'], $eventC['date'], $eventB['date']], + [$eventB['date'], $eventC['date'], $eventA['date']], $this->extractDates($results) ); } @@ -128,8 +130,9 @@ class FormatTalksTest extends TestCase $this->assertCount(3, $results); + // Earliest events should be returned first. $this->assertEquals( - [$eventE['date'], $eventA['date'], $eventC['date']], + [$eventC['date'], $eventA['date'], $eventE['date']], $this->extractDates($results) ); } @@ -159,6 +162,7 @@ class FormatTalksTest extends TestCase $this->assertCount(2, $results); + // Latest events should be returned first. $this->assertEquals( [$eventB['date'], $eventF['date']], $this->extractDates($results) @@ -168,12 +172,12 @@ class FormatTalksTest extends TestCase /** * Extract the returned dates from the results. * - * @param array $results The results returned from the filter. + * @param Collection $results The results returned from the filter. * * @return array An array of dates. */ - private function extractDates(array $results) + private function extractDates(Collection $results) { - return collect($results)->pluck('event.date')->all(); + return $results->pluck('event.date')->all(); } }