Move custom app code

Move custom app code (the `src` and `tests` directories) into `app`.
This commit is contained in:
Oliver Davies 2019-10-18 07:40:14 +01:00
parent b9830f8386
commit 10f0f7fd11
4 changed files with 2 additions and 2 deletions

View file

@ -0,0 +1,113 @@
<?php
namespace App\TwigExtension\Talk;
use Sculpin\Contrib\ProxySourceCollection\ProxySourceCollection;
use Tightenco\Collect\Support\Collection;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
use Twig\TwigFunction;
class TalksExtension extends AbstractExtension
{
/**
* @var int 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 TwigFunction('getTalks', [$this, 'getTalks']),
];
}
public function getFilters()
{
return [
new TwigFilter('pastEvents', [$this, 'filterPastEvents']),
new TwigFilter('pastTalks', [$this, 'filterPastTalks']),
new TwigFilter('upcomingEvents', [$this, 'filterUpcomingEvents']),
new TwigFilter('upcomingTalks', [$this, 'filterUpcomingTalks']),
];
}
/**
* {@inheritdoc}
*/
public function getName()
{
return 'app.talks';
}
/**
* Get all upcoming and previous talks.
*
* @param ProxySourceCollection|array $talks All talk nodes.
*
* @return Collection A sorted collection of talks.
*/
public function getTalks($talks): Collection
{
return (new Collection($talks))->sortBy(function ($talk) {
return $this->getLastDate($talk);
});
}
public function filterUpcomingTalks(Collection $talks): Collection
{
return $talks->filter(function ($talk): bool {
return $this->getLastDate($talk) >= $this->today;
})->values();
}
public function filterPastTalks(Collection $talks): Collection
{
return $talks->filter(function ($talk): bool {
return $this->getLastDate($talk) < $this->today;
})->values();
}
private function getLastDate($talk): string
{
return $this->eventsFromTalks(new Collection([$talk]))
->pluck('date')->max();
}
public function filterUpcomingEvents($talks): Collection
{
return $this->eventsFromTalks($talks)->filter(function ($event): bool {
return $event['date'] >= $this->today;
})->sortBy('date');
}
public function filterPastEvents($talks): Collection
{
return $this->eventsFromTalks($talks)->filter(function ($event): bool {
return $event['date'] < $this->today;
})->sortBy('date');
}
private function eventsFromTalks($talks): Collection
{
return (new Collection($talks))->flatMap(function ($talk): array {
return $talk['events'];
});
}
public function getAllEvents($talks): Collection
{
return $this->eventsFromTalks($talks);
}
}

View file

@ -0,0 +1,96 @@
<?php
namespace App\Tests\Event;
use App\TwigExtension\Talk\TalksExtension;
use DateTime;
use PHPUnit\Framework\TestCase;
use Tightenco\Collect\Support\Collection;
class RetrievingEventsTest extends TestCase
{
/**
* @var TalksExtension
*/
private $extension;
/**
* {@inheritdoc}
*/
public function setUp(): void
{
$this->extension = new TalksExtension();
}
/** @test */
public function get_past_events()
{
$talkA = [
'title' => 'Test Driven Drupal',
'events' => [
[
'event' => 'php_south_wales',
'date' => (new DateTime('+1 days'))->getTimestamp(),
],
[
'event' => 'drupalcamp_london',
'date' => (new DateTime('-1 days'))->getTimestamp(),
],
],
];
$talkB = [
'title' => 'Taking Flight with Tailwind CSS',
'events' => [
[
'event' => 'blue_conf_2019',
'date' => (new DateTime('-2 days'))->getTimestamp(),
],
],
];
$talks = $this->extension->getTalks([$talkA, $talkB]);
$events = $this->extension->filterPastEvents($talks);
$this->assertInstanceOf(Collection::class, $talks);
$this->assertInstanceOf(Collection::class, $events);
$this->assertCount(2, $events);
}
/** @test */
public function get_current_or_upcoming_events()
{
$talkA = [
'title' => 'Test Driven Drupal',
'events' => [
[
'event' => 'php_south_wales',
'date' => (new DateTime('+0 days'))->getTimestamp(),
],
[
'event' => 'drupalcamp_london',
'date' => (new DateTime('-1 days'))->getTimestamp(),
],
],
];
$talkB = [
'title' => 'Taking Flight with Tailwind CSS',
'events' => [
[
'event' => 'blue_conf_2019',
'date' => (new DateTime('+2 days'))->getTimestamp(),
],
],
];
$talks = $this->extension->getTalks([$talkA, $talkB]);
$events = $this->extension->filterUpcomingEvents($talks);
$this->assertInstanceOf(Collection::class, $talks);
$this->assertInstanceOf(Collection::class, $events);
$this->assertCount(2, $events);
}
}

View file

@ -0,0 +1,174 @@
<?php
namespace App\Tests\Talk;
use App\TwigExtension\Talk\TalksExtension;
use DateTime;
use PHPUnit\Framework\TestCase;
use Tightenco\Collect\Support\Collection;
class RetrievingTalksTest extends TestCase
{
/**
* @var TalksExtension
*/
private $extension;
/**
* {@inheritdoc}
*/
public function setUp(): void
{
$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->getTalks([$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->getTalks($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()],
],
];
$talks = $this->extension->getTalks([$pastTalk, $futureTalk]);
$filtered = $this->extension->filterPastTalks($talks);
$this->assertCount(1, $filtered);
$this->assertSame($pastTalk, $filtered->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()],
],
];
$talks = $this->extension->getTalks([$pastTalk, $todayTalk, $futureTalk]);
$filtered = $this->extension->filterUpcomingTalks($talks);
$this->assertSame(2, $filtered->count());
$this->assertSame([$todayTalk, $futureTalk], $filtered->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()],
],
];
$talks = $this->extension->getTalks([$talk]);
$this->assertCount(1, $this->extension->filterUpcomingTalks($talks));
$this->assertEmpty($this->extension->filterPastTalks($talks));
}
/** @test */
public function get_events_from_talks()
{
$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()],
],
];
$talks = new Collection([$talkA, $talkB]);
$events = $this->extension->getAllEvents($talks);
$this->assertCount(3, $events);
$this->assertSame(['event_a', 'event_b', 'event_a'], $events->pluck('event')->toArray());
$this->assertSame(3, $events->pluck('date')->unique()->count());
}
}