Move Talks bundle

This commit is contained in:
Oliver Davies 2019-04-14 08:09:24 +01:00
parent ac6f42095a
commit bfccea0312
9 changed files with 11 additions and 13 deletions

5
src/Talks/composer.json Normal file
View file

@ -0,0 +1,5 @@
{
"require": {
"tightenco/collect": "^5.4"
}
}

View file

@ -0,0 +1,20 @@
<?php
namespace App\Talks\DependencyInjection;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\Config\FileLocator;
class SculpinTalksExtension extends Extension
{
/**
* {@inheritdoc}
*/
public function load(array $configs, ContainerBuilder $container)
{
$loader = new YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
$loader->load('services.yml');
}
}

View file

@ -0,0 +1,5 @@
services:
twig.format_talks:
class: 'App\Talks\TwigExtension\TalksExtension'
tags:
- { name: twig.extension }

View file

@ -0,0 +1,9 @@
<?php
namespace App\Talks;
use Symfony\Component\HttpKernel\Bundle\Bundle;
class SculpinTalksBundle extends Bundle
{
}

View file

@ -0,0 +1,91 @@
<?php
namespace App\Talks\TwigExtension;
use Illuminate\Support\Collection;
use Sculpin\Contrib\ProxySourceCollection\ProxySourceCollection;
use Twig_Extension;
use Twig_SimpleFunction;
class TalksExtension extends Twig_Extension
{
/**
* @var string The current date.
*/
private $today;
public function __construct()
{
$this->today = (new \DateTime())
->modify('today')
->setTimezone(new \DateTimeZone('Europe/London'))
->getTimestamp();
}
/**
* {@inheritdoc}
*/
public function getFunctions()
{
return [
new Twig_SimpleFunction('getAllTalks', [$this, 'getAll']),
new Twig_SimpleFunction('getUpcomingTalks', [$this, 'getUpcoming']),
new Twig_SimpleFunction('getPastTalks', [$this, 'getPast']),
];
}
/**
* Get all upcoming and previous talks.
*
* @param ProxySourceCollection|array $talks All talk nodes.
*
* @return Collection A sorted collection of talks.
*/
public function getAll($talks): Collection
{
return collect($talks)->sortBy(function ($talk) {
return $this->getLastDate($talk);
});
}
/**
* Get all upcoming talks.
*
* @param ProxySourceCollection|array $talks All talk nodes.
*
* @return Collection A sorted collection of talks.
*/
public function getUpcoming($talks): Collection
{
return $this->getAll($talks)->filter(function ($talk) {
return $this->getLastDate($talk) >= $this->today;
})->values();
}
/**
* Get all past talks.
*
* @param ProxySourceCollection|array $talks All talk nodes.
*
* @return Collection A sorted collection of talks.
*/
public function getPast($talks): Collection
{
return $this->getAll($talks)->filter(function ($talk) {
return $this->getLastDate($talk) < $this->today;
})->values();
}
/**
* {@inheritdoc}
*/
public function getName()
{
return 'talks';
}
private function getLastDate($talk): string
{
return (string) collect($talk['events'])->pluck('date')->sort()->last();
}
}

View file

@ -0,0 +1,144 @@
<?php
namespace App\Tests\Talks\TwigExtension;
use DateTime;
use App\Talks\TwigExtension\TalksExtension;
use Illuminate\Support\Collection;
use PHPUnit\Framework\TestCase;
class TalksExtensionTest extends TestCase
{
/**
* @var TalksExtension
*/
private $extension;
/**
* {@inheritdoc}
*/
public function setUp()
{
$this->extension = new TalksExtension();
}
/** @test */
public function talks_given_multiple_times_are_only_returned_once()
{
$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(2, $this->extension->getAll([$talkA, $talkB]));
}
/** @test */
public function talks_are_ordered_by_the_most_recent_event_date()
{
$talkA = [
'title' => 'Talk A',
'events' => [
['event' => 'event_a', 'date' => (new DateTime('-5 days'))->getTimestamp()],
],
];
$talkB = [
'title' => 'Talk B',
'events' => [
['event' => 'event_a', 'date' => (new DateTime('-20 days'))->getTimestamp()],
],
];
$talkC = [
'title' => 'Talk C',
'events' => [
['event' => 'event_a', 'date' => (new DateTime('-3 days'))->getTimestamp()],
['event' => 'event_b', 'date' => (new DateTime('-10 days'))->getTimestamp()],
],
];
$unorderedTalks = [$talkC, $talkA, $talkB];
$orderedTalks = $this->extension->getAll($unorderedTalks);
$this->assertEquals([$talkC, $talkA, $talkB], $orderedTalks->toArray());
}
/** @test */
public function only_past_talks_can_be_retrieved()
{
$pastTalk = [
'title' => 'Past talk',
'events' => [
['date' => (new DateTime('-1 day'))->getTimestamp()],
]
];
$futureTalk = [
'title' => 'Future talk',
'events' => [
['date' => (new DateTime('+1 day'))->getTimestamp()],
],
];
$result = $this->extension->getPast([$pastTalk, $futureTalk]);
$this->assertCount(1, $result);
$this->assertSame($pastTalk, $result->first());
}
/** @test */
public function only_current_and_future_talks_can_be_retrieved()
{
$pastTalk = [
'title' => 'Past talk',
'events' => [
['date' => (new DateTime('-1 day'))->getTimestamp()],
]
];
$todayTalk = [
'title' => 'A talk that it happening today',
'events' => [
['date' => (new DateTime('now'))->getTimestamp()],
],
];
$futureTalk = [
'title' => 'Future talk',
'events' => [
['date' => (new DateTime('+1 day'))->getTimestamp()],
],
];
$result = $this->extension->getUpcoming([$pastTalk, $todayTalk, $futureTalk]);
$this->assertCount(2, $result);
$this->assertSame([$todayTalk, $futureTalk], $result->toArray());
}
/** @test */
public function if_a_talk_is_both_upcoming_and_past_then_it_is_only_shown_as_upcoming()
{
$talk = [
'title' => 'An upcoming talk that has been given before',
'events' => [
['date' => (new DateTime('-1 week'))->getTimestamp()],
['date' => (new DateTime('+1 week'))->getTimestamp()],
],
];
$this->assertCount(1, $this->extension->getUpcoming([$talk]));
$this->assertEmpty($this->extension->getPast([$talk]));
}
}