Group talk classes into a modules
directory
This commit is contained in:
parent
5aba021339
commit
2a4247684d
5 changed files with 11 additions and 15 deletions
141
modules/Talk/Tests/TwigExtension/TalkTwigExtensionTest.php
Normal file
141
modules/Talk/Tests/TwigExtension/TalkTwigExtensionTest.php
Normal file
|
@ -0,0 +1,141 @@
|
|||
<?php
|
||||
|
||||
namespace Modules\Talk\Tests\TwigExtension;
|
||||
|
||||
use Dflydev\DotAccessConfiguration\Configuration;
|
||||
use Modules\Talk\TwigExtension\TalkTwigExtension;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Sculpin\Contrib\ProxySourceCollection\ProxySourceItem;
|
||||
|
||||
class TalkTwigExtensionTest extends TestCase
|
||||
{
|
||||
private TalkTwigExtension $extension;
|
||||
|
||||
public function setUp(): void
|
||||
{
|
||||
$this->extension = new TalkTwigExtension();
|
||||
}
|
||||
|
||||
public function testNoPastEvents(): void
|
||||
{
|
||||
$talk = $this->createTalk(
|
||||
events: [
|
||||
['date' => (new \DateTime('+1 days'))->getTimestamp()],
|
||||
],
|
||||
);
|
||||
|
||||
$this->assertTalkCount(expectedCount: 0, talks: [$talk]);
|
||||
}
|
||||
|
||||
public function testSinglePastEvent(): void
|
||||
{
|
||||
$talkA = $this->createTalk(
|
||||
events: [
|
||||
['date' => (new \DateTime('+1 days'))->getTimestamp()],
|
||||
],
|
||||
);
|
||||
|
||||
$talkB = $this->createTalk(
|
||||
events: [
|
||||
['date' => (new \DateTime('-3 days'))->getTimestamp()],
|
||||
],
|
||||
);
|
||||
|
||||
$this->assertTalkCount(expectedCount: 1, talks: [$talkA, $talkB]);
|
||||
}
|
||||
|
||||
public function testSingleTalkWithMultiplePastEvents(): void
|
||||
{
|
||||
$talk = $this->createTalk(
|
||||
events: [
|
||||
['date' => (new \DateTime('-1 days'))->getTimestamp()],
|
||||
['date' => (new \DateTime('-1 week'))->getTimestamp()],
|
||||
['date' => (new \DateTime('-1 year'))->getTimestamp()],
|
||||
],
|
||||
);
|
||||
|
||||
$this->assertTalkCount(expectedCount: 3, talks: [$talk]);
|
||||
}
|
||||
|
||||
public function testSingleTalkWithMultiplePastAndFutureEvents(): void
|
||||
{
|
||||
$talk = $this->createTalk(
|
||||
events: [
|
||||
['date' => (new \DateTime('+1 day'))->getTimestamp()],
|
||||
['date' => (new \DateTime('-1 day'))->getTimestamp()],
|
||||
['date' => (new \DateTime('-1 week'))->getTimestamp()],
|
||||
['date' => (new \DateTime('+1 year'))->getTimestamp()],
|
||||
['date' => (new \DateTime('-1 year'))->getTimestamp()],
|
||||
],
|
||||
);
|
||||
|
||||
$this->assertTalkCount(expectedCount: 3, talks: [$talk]);
|
||||
}
|
||||
|
||||
public function testMultiplePastEvents(): void
|
||||
{
|
||||
$talkA = $this->createTalk(
|
||||
events: [
|
||||
['date' => (new \DateTime('-1 days'))->getTimestamp()],
|
||||
['date' => (new \DateTime('+1 days'))->getTimestamp()],
|
||||
],
|
||||
);
|
||||
|
||||
$talkB = $this->createTalk(
|
||||
events: [
|
||||
['date' => (new \DateTime('-3 days'))->getTimestamp()],
|
||||
],
|
||||
);
|
||||
|
||||
$this->assertTalkCount(expectedCount: 2, talks: [$talkA, $talkB]);
|
||||
}
|
||||
|
||||
public function testTheCurrentDayIsNotCounted(): void
|
||||
{
|
||||
$talkA = $this->createTalk(
|
||||
events: [
|
||||
['date' => (new \DateTime('yesterday'))->getTimestamp()],
|
||||
['date' => (new \DateTime('today'))->getTimestamp()],
|
||||
],
|
||||
);
|
||||
|
||||
$talkB = $this->createTalk(
|
||||
events: [
|
||||
['date' => (new \DateTime('today'))->getTimestamp()],
|
||||
],
|
||||
);
|
||||
|
||||
$talkC = $this->createTalk(
|
||||
events: [
|
||||
['date' => (new \DateTime('yesterday'))->getTimestamp()],
|
||||
],
|
||||
);
|
||||
|
||||
$this->assertTalkCount(expectedCount: 2, talks: [$talkA, $talkB, $talkC]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert the extension uses the correct number of talks.
|
||||
*/
|
||||
private function assertTalkCount(int $expectedCount, array $talks): void
|
||||
{
|
||||
self::assertSame(
|
||||
actual: $this->extension->getPastTalkCount($talks),
|
||||
expected: $expectedCount,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a mock talk with a list of events.
|
||||
*/
|
||||
private function createTalk(array $events): ProxySourceItem
|
||||
{
|
||||
$configuration = $this->createMock(Configuration::class);
|
||||
$configuration->method('get')->with($this->identicalTo('events'))->willReturn($events);
|
||||
|
||||
$talk = $this->createMock(ProxySourceItem::class);
|
||||
$talk->method('data')->willReturn($configuration);
|
||||
|
||||
return $talk;
|
||||
}
|
||||
}
|
44
modules/Talk/TwigExtension/TalkTwigExtension.php
Normal file
44
modules/Talk/TwigExtension/TalkTwigExtension.php
Normal file
|
@ -0,0 +1,44 @@
|
|||
<?php
|
||||
|
||||
namespace Modules\Talk\TwigExtension;
|
||||
|
||||
use Sculpin\Contrib\ProxySourceCollection\ProxySourceItem;
|
||||
use Twig\Extension\AbstractExtension;
|
||||
use Twig\TwigFunction;
|
||||
|
||||
class TalkTwigExtension extends AbstractExtension
|
||||
{
|
||||
public function getFunctions(): array
|
||||
{
|
||||
return [
|
||||
new TwigFunction('get_past_talk_count', [$this, 'getPastTalkCount']),
|
||||
new TwigFunction('get_years_of_experience', [$this, 'getYearsOfExperience']),
|
||||
];
|
||||
}
|
||||
|
||||
public function getName(): string
|
||||
{
|
||||
return 'app.opdavies_twig_extension';
|
||||
}
|
||||
|
||||
public function getPastTalkCount(array $talks): int
|
||||
{
|
||||
$today = (new \DateTime('today'))->getTimestamp();
|
||||
|
||||
return collect($talks)
|
||||
->flatMap(fn (ProxySourceItem $talk) => $talk->data()->get('events'))
|
||||
->filter(
|
||||
function (array $event) use ($today): bool {
|
||||
assert(array_key_exists(array: $event, key: 'date'));
|
||||
|
||||
return $event['date'] < $today;
|
||||
}
|
||||
)
|
||||
->count();
|
||||
}
|
||||
|
||||
public function getYearsOfExperience(): int
|
||||
{
|
||||
return (new \DateTimeImmutable())->format('Y') - 2007;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue