Add function for getting events for a talk

GH-160
This commit is contained in:
Oliver Davies 2019-12-27 21:24:14 +00:00
parent 105ae345a9
commit 5499f555c1
4 changed files with 86 additions and 5 deletions

View file

@ -33,6 +33,7 @@ class TalksExtension extends AbstractExtension
new TwigFunction('get_upcoming_talks', [$this, 'getUpcomingTalks']),
new TwigFunction('get_past_talks', [$this, 'getPastTalks']),
new TwigFunction('get_past_talk_count', [$this, 'getPastTalkCount']),
new TwigFunction('get_events_for_talk', [$this, 'getEventsForTalk']),
];
}
@ -89,6 +90,18 @@ class TalksExtension extends AbstractExtension
return $this->getPastEvents($talks)->count();
}
public function getEventsForTalk(array $talk, array $eventData): Collection
{
return (new Collection($talk['events']))
->map(function (array $event) use ($eventData): Collection {
return (new Collection($event))->merge($eventData[$event['event']]);
})
->filter(function (Collection $event): bool {
return !empty($event->get('date'));
})
->sortBy('date');
}
private function getLastDate($talk): string
{
return $this->eventsFromTalks(new Collection([$talk]))

View file

@ -83,4 +83,71 @@ class RetrievingEventsTest extends TestCase
$this->assertCount(1, $pastEvents);
$this->assertSame('php_south_wales', $pastEvents[0]['event']);
}
/** @test */
public function get_all_of_the_events_for_a_talk()
{
$talk = [
'title' => 'TDD - Test Driven Drupal',
'events' => [
[
'event' => 'drupal_developer_days_2018',
'date' => (new DateTime('-1 day'))->getTimestamp(),
],
[
'event' => 'drupalcamp_london_2019',
'date' => (new DateTime('+1 day'))->getTimestamp(),
],
],
];
$eventData = [
'drupal_developer_days_2018' => [
'name' => 'Drupal Developer Days, Lisbon 2018',
],
'drupalcamp_london_2019' => [
'name' => 'DrupalCamp London 2019',
],
];
$events = $this->extension->getEventsForTalk($talk, $eventData);
$this->assertCount(2, $events);
$this->assertSame(
['drupal_developer_days_2018', 'drupalcamp_london_2019'],
$events->pluck('event')->toArray()
);
}
/** @test */
public function events_with_no_date_are_not_returned_for_an_event()
{
$talk = [
'title' => 'TDD - Test Driven Drupal',
'events' => [
[
'event' => 'drupal_developer_days_2018',
'date' => (new DateTime('-2 days'))->getTimestamp(),
],
[
'event' => 'drupalcamp_london_2019',
'date' => '',
],
],
];
$eventData = [
'drupal_developer_days_2018' => [
'name' => 'Drupal Developer Days, Lisbon 2018',
],
'drupalcamp_london_2019' => [
'name' => 'DrupalCamp London 2019',
],
];
$events = $this->extension->getEventsForTalk($talk, $eventData);
$this->assertCount(1, $events);
$this->assertSame('drupal_developer_days_2018', $events->pluck('event')->first());
}
}