From 92f3865ee746892f93b08d195836b54df01a3017 Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Wed, 25 Aug 2021 01:11:57 +0100 Subject: [PATCH] Count the number of events from talks --- source/_pages/talks.html.twig | 4 +-- src/TwigExtension/TalkExtension.php | 10 +++++-- tests/TalkExtensionTest.php | 42 ++++++++++++++++++++++++++++- 3 files changed, 51 insertions(+), 5 deletions(-) diff --git a/source/_pages/talks.html.twig b/source/_pages/talks.html.twig index b86ef50ec..b549aedcd 100644 --- a/source/_pages/talks.html.twig +++ b/source/_pages/talks.html.twig @@ -2,10 +2,10 @@ title: Talks and workshops use: - talks -intro_text: | - Starting with my first talk in September 2012, I have given 82 presentations and workshops at various conferences and meetups, in-person and remotely, on topics including PHP, Drupal, automated testing, Git, CSS, and systems administration. --- +

Starting with my first talk in September 2012, I have given {{ get_past_talk_count(data.talks) }} presentations and workshops at various conferences and meetups, in-person and remotely, on topics including PHP, Drupal, automated testing, Git, CSS, and systems administration.

+
{% for talk in data.talks|sort((a,b) => a.sortable_date < b.sortable_date) %} diff --git a/src/TwigExtension/TalkExtension.php b/src/TwigExtension/TalkExtension.php index 6511ec6f5..aef6094eb 100644 --- a/src/TwigExtension/TalkExtension.php +++ b/src/TwigExtension/TalkExtension.php @@ -17,10 +17,16 @@ final class TalkExtension extends AbstractExtension ]; } - public function getPastTalkCount(array $talks = []): int + public function getPastTalkCount(iterable $talks = []): int + { + return $this->getEventsFromTalks($talks)->count(); + } + + private function getEventsFromTalks(iterable $talks): Collection { $talkCollection = new Collection($talks); - return $talkCollection->count(); + return $talkCollection + ->flatMap(fn($talk): array => (array) $talk['events']); } } diff --git a/tests/TalkExtensionTest.php b/tests/TalkExtensionTest.php index d6d33a562..cee33ec19 100644 --- a/tests/TalkExtensionTest.php +++ b/tests/TalkExtensionTest.php @@ -7,9 +7,49 @@ use PHPUnit\Framework\TestCase; final class TalkExtensionTest extends TestCase { + private TalkExtension $subject; + + public function setUp(): void + { + $this->subject = new TalkExtension(); + } + /** @test */ public function it_returns_zero_if_there_are_no_talks(): void { - $this->assertSame(0, (new TalkExtension())->getPastTalkCount([])); + $this->assertSame(0, $this->subject->getPastTalkCount([])); + } + + /** @test */ + public function it_counts_a_single_event_from_a_single_talk(): void + { + $talks = [ + [ + 'title' => 'Building static sites with Sculpin', + 'events' => [ + '', + ] + ], + ]; + + $this->assertSame(1, $this->subject->getPastTalkCount($talks)); + } + + /** @test */ + public function it_counts_multiple_events_from_a_single_talk(): void + { + $this->markTestIncomplete(); + } + + /** @test */ + public function it_counts_multiple_events_from_multiple_talks(): void + { + $this->markTestIncomplete(); + } + + /** @test */ + public function it_excludes_future_talks(): void + { + $this->markTestIncomplete(); } }