Get all events

This commit is contained in:
Oliver Davies 2019-05-20 08:28:28 +01:00
parent f6e0272469
commit 679da826b4
2 changed files with 57 additions and 3 deletions

View file

@ -30,13 +30,13 @@ class TalksExtension extends AbstractExtension
{
return [
new TwigFunction('getTalks', [$this, 'getTalks']),
new TwigFunction('getEvents', [$this, 'getEvents']),
];
}
public function getFilters()
{
return [
new TwigFilter('events', [$this, 'getEvents']),
new TwigFilter('past', [$this, 'filterPast']),
new TwigFilter('upcoming', [$this, 'filterUpcoming']),
];
@ -65,9 +65,9 @@ class TalksExtension extends AbstractExtension
}
public function getEvents(Collection $talks): Collection
public function getEvents($talks): Collection
{
return $talks->flatMap(function ($talk): array {
return collect($talks)->flatMap(function ($talk): array {
return $talk['events'];
});
}

View file

@ -0,0 +1,54 @@
<?php
namespace App\Tests\Talks;
use App\Talks\TwigExtension\TalksExtension;
use DateTime;
use PHPUnit\Framework\TestCase;
use Tightenco\Collect\Support\Collection;
class RetrievingEventsTest extends TestCase
{
/**
* @var TalksExtension
*/
private $extension;
/**
* {@inheritdoc}
*/
public function setUp()
{
$this->extension = new TalksExtension();
}
/** @test */
public function getting_all_events()
{
$talkA = [
'title' => 'Talk A',
'events' => [
[
'event' => 'event_a',
'date' => (new DateTime('-1 days'))->getTimestamp()
],
[
'event' => 'event_b',
'date' => (new DateTime('+1 days'))->getTimestamp()
],
],
];
$talkB = [
'title' => 'Talk B',
'events' => [
[
'event' => 'event_a',
'date' => (new DateTime('-3 days'))->getTimestamp()
],
],
];
$this->assertCount(3, $this->extension->getEvents([$talkA, $talkB]));
}
}