Move src and tests directories

This commit is contained in:
Oliver Davies 2020-03-11 21:30:20 +00:00
parent 41ca11b514
commit 8551843b04
7 changed files with 4 additions and 4 deletions

View file

@ -1,63 +0,0 @@
<?php
declare(strict_types=1);
namespace App\Asset\TwigExtension;
use Sculpin\Contrib\ProxySourceCollection\ProxySourceCollection;
use Tightenco\Collect\Support\Collection;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
use Twig\TwigFunction;
class EncoreExtension extends AbstractExtension
{
/** @var string */
private $manifestDir;
/**
* {@inheritdoc}
*/
public function __construct(string $manifestDir)
{
$this->manifestDir = $manifestDir;
}
/**
* {@inheritdoc}
*/
public function getName()
{
return 'app.encore';
}
/**
* {@inheritdoc}
*/
public function getFunctions()
{
return [
new TwigFunction('asset', [$this, 'generateAssetPaths']),
];
}
public function generateAssetPaths(string $assetName): string
{
if (!$manifest = file_get_contents($this->manifestPath($assetName))) {
return $this->defaultPath($assetName);
}
return (new Collection(json_decode($manifest, true)))
->get($assetName, $assetName);
}
private function manifestPath(string $assetName): string
{
return preg_replace('/(?<=\/)[\w.]+$/', 'manifest.json', $this->defaultPath($assetName)) ?? $this->defaultPath($assetName);
}
private function defaultPath(string $assetName): string
{
return "{$this->manifestDir}/{$assetName}";
}
}

View file

@ -1,119 +0,0 @@
<?php
namespace App\Talk\TwigExtension;
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('get_all_talks', [$this, 'getAllTalks']),
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']),
];
}
/**
* {@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 getAllTalks($talks): Collection
{
return (new Collection($talks))->sortBy(function ($talk) {
return $this->getLastDate($talk);
});
}
public function getUpcomingTalks(array $talks): Collection
{
return $this->getAllTalks($talks)->filter(function ($talk): bool {
return $this->getLastDate($talk) >= $this->today;
})->values();
}
public function getPastTalks(array $talks): Collection
{
return $this->getAllTalks($talks)->filter(function ($talk): bool {
return $this->getLastDate($talk) < $this->today;
})->values();
}
public function getAllEvents($talks): Collection
{
return $this->eventsFromTalks($talks);
}
public function getPastEvents($talks): Collection
{
return $this->eventsFromTalks($talks)->filter(function ($event): bool {
return $event['date'] < $this->today;
})->sortBy('date');
}
public function getPastTalkCount($talks): int
{
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($eventData[$event['event']]))->merge($event);
})
->filter(function (Collection $event): bool {
return !empty($event->get('date'));
})
->sortBy('date');
}
private function getLastDate($talk): string
{
return $this->eventsFromTalks(new Collection([$talk]))
->pluck('date')->max();
}
private function eventsFromTalks($talks): Collection
{
return (new Collection($talks))->flatMap(function ($talk): array {
return $talk['events'];
})->filter(function ($event): bool {
return !empty($event['date']);
});
}
}

View file

@ -1,159 +0,0 @@
<?php
namespace App\Tests\Talk\TwigExtension;
use App\Talk\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(): 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->getAllTalks([$talkA, $talkB]);
$events = $this->extension->getPastEvents($talks);
$this->assertInstanceOf(Collection::class, $talks);
$this->assertInstanceOf(Collection::class, $events);
$this->assertCount(2, $events);
}
/** @test */
public function events_with_no_date_are_not_returned()
{
$talks = [
[
'title' => 'Deploying PHP applications with Ansible, Ansible Vault and Ansistrano',
'events' => [
[
'event' => 'php_south_wales',
'date' => (new DateTime('-1 days'))->getTimestamp(),
],
[
'event' => 'drupal_edinburgh',
'date' => '',
],
],
],
];
$this->assertSame(1, $this->extension->getPastTalkCount($talks));
$pastEvents = $this->extension->getPastEvents($talks);
$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());
}
/** @test */
public function specific_event_urls_override_global_urls()
{
$this->markTestSkipped();
}
}

View file

@ -1,168 +0,0 @@
<?php
namespace App\Tests\Talk\TwigExtension;
use App\Talk\TwigExtension\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->getAllTalks([$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->getAllTalks($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->getPastTalks([$pastTalk, $futureTalk]);
$this->assertCount(1, $talks);
$this->assertSame($pastTalk, $talks->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->getUpcomingTalks([$pastTalk, $todayTalk, $futureTalk]);
$this->assertSame(2, $talks->count());
$this->assertSame([$todayTalk, $futureTalk], $talks->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->getUpcomingTalks([$talk]));
$this->assertEmpty($this->extension->getPastTalks([$talk]));
}
/** @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());
}
}