Split src/
and tests/
directories
This commit is contained in:
parent
89f6a6dada
commit
51cd9ffdca
6 changed files with 17 additions and 11 deletions
|
@ -1,29 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Modules\Experience\TwigExtension;
|
||||
|
||||
use Sculpin\Contrib\ProxySourceCollection\ProxySourceItem;
|
||||
use Twig\Extension\AbstractExtension;
|
||||
use Twig\TwigFunction;
|
||||
|
||||
class ExperienceTwigExtension extends AbstractExtension
|
||||
{
|
||||
private static $startYear = 2007;
|
||||
|
||||
public function getFunctions(): array
|
||||
{
|
||||
return [
|
||||
new TwigFunction('get_years_of_experience', [$this, 'getYearsOfExperience']),
|
||||
];
|
||||
}
|
||||
|
||||
public function getName(): string
|
||||
{
|
||||
return 'modules.experience';
|
||||
}
|
||||
|
||||
public function getYearsOfExperience(): int
|
||||
{
|
||||
return (new \DateTimeImmutable())->format('Y') - self::$startYear;
|
||||
}
|
||||
}
|
|
@ -1,141 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Modules\Presentations\Tests\TwigExtension;
|
||||
|
||||
use Dflydev\DotAccessConfiguration\Configuration;
|
||||
use Modules\Presentations\TwigExtension\PresentationTwigExtension;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Sculpin\Contrib\ProxySourceCollection\ProxySourceItem;
|
||||
|
||||
class PresentationTwigExtensionTest extends TestCase
|
||||
{
|
||||
private PresentationTwigExtension $extension;
|
||||
|
||||
public function setUp(): void
|
||||
{
|
||||
$this->extension = new PresentationTwigExtension();
|
||||
}
|
||||
|
||||
public function testNoPastEvents(): void
|
||||
{
|
||||
$presentation = $this->createPresentation(
|
||||
events: [
|
||||
['date' => (new \DateTime('+1 days'))->getTimestamp()],
|
||||
],
|
||||
);
|
||||
|
||||
$this->assertPresentationCount(expectedCount: 0, presentations: [$presentation]);
|
||||
}
|
||||
|
||||
public function testSinglePastEvent(): void
|
||||
{
|
||||
$presentationA = $this->createPresentation(
|
||||
events: [
|
||||
['date' => (new \DateTime('+1 days'))->getTimestamp()],
|
||||
],
|
||||
);
|
||||
|
||||
$presentationB = $this->createPresentation(
|
||||
events: [
|
||||
['date' => (new \DateTime('-3 days'))->getTimestamp()],
|
||||
],
|
||||
);
|
||||
|
||||
$this->assertPresentationCount(expectedCount: 1, presentations: [$presentationA, $presentationB]);
|
||||
}
|
||||
|
||||
public function testSinglePresentationWithMultiplePastEvents(): void
|
||||
{
|
||||
$presentation = $this->createPresentation(
|
||||
events: [
|
||||
['date' => (new \DateTime('-1 days'))->getTimestamp()],
|
||||
['date' => (new \DateTime('-1 week'))->getTimestamp()],
|
||||
['date' => (new \DateTime('-1 year'))->getTimestamp()],
|
||||
],
|
||||
);
|
||||
|
||||
$this->assertPresentationCount(expectedCount: 3, presentations: [$presentation]);
|
||||
}
|
||||
|
||||
public function testSinglePresentationWithMultiplePastAndFutureEvents(): void
|
||||
{
|
||||
$presentation = $this->createPresentation(
|
||||
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->assertPresentationCount(expectedCount: 3, presentations: [$presentation]);
|
||||
}
|
||||
|
||||
public function testMultiplePastEvents(): void
|
||||
{
|
||||
$presentationA = $this->createPresentation(
|
||||
events: [
|
||||
['date' => (new \DateTime('-1 days'))->getTimestamp()],
|
||||
['date' => (new \DateTime('+1 days'))->getTimestamp()],
|
||||
],
|
||||
);
|
||||
|
||||
$presentationB = $this->createPresentation(
|
||||
events: [
|
||||
['date' => (new \DateTime('-3 days'))->getTimestamp()],
|
||||
],
|
||||
);
|
||||
|
||||
$this->assertPresentationCount(expectedCount: 2, presentations: [$presentationA, $presentationB]);
|
||||
}
|
||||
|
||||
public function testTheCurrentDayIsNotCounted(): void
|
||||
{
|
||||
$presentationA = $this->createPresentation(
|
||||
events: [
|
||||
['date' => (new \DateTime('yesterday'))->getTimestamp()],
|
||||
['date' => (new \DateTime('today'))->getTimestamp()],
|
||||
],
|
||||
);
|
||||
|
||||
$presentationB = $this->createPresentation(
|
||||
events: [
|
||||
['date' => (new \DateTime('today'))->getTimestamp()],
|
||||
],
|
||||
);
|
||||
|
||||
$presentationC = $this->createPresentation(
|
||||
events: [
|
||||
['date' => (new \DateTime('yesterday'))->getTimestamp()],
|
||||
],
|
||||
);
|
||||
|
||||
$this->assertPresentationCount(expectedCount: 2, presentations: [$presentationA, $presentationB, $presentationC]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert the extension uses the correct number of presentations.
|
||||
*/
|
||||
private function assertPresentationCount(int $expectedCount, array $presentations): void
|
||||
{
|
||||
self::assertSame(
|
||||
actual: $this->extension->getPresentationCount($presentations),
|
||||
expected: $expectedCount,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a mock presentation with a list of events.
|
||||
*/
|
||||
private function createPresentation(array $events): ProxySourceItem
|
||||
{
|
||||
$configuration = $this->createMock(Configuration::class);
|
||||
$configuration->method('get')->with($this->identicalTo('events'))->willReturn($events);
|
||||
|
||||
$presentation = $this->createMock(ProxySourceItem::class);
|
||||
$presentation->method('data')->willReturn($configuration);
|
||||
|
||||
return $presentation;
|
||||
}
|
||||
}
|
|
@ -1,38 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Modules\Presentations\TwigExtension;
|
||||
|
||||
use Sculpin\Contrib\ProxySourceCollection\ProxySourceItem;
|
||||
use Twig\Extension\AbstractExtension;
|
||||
use Twig\TwigFunction;
|
||||
|
||||
class PresentationTwigExtension extends AbstractExtension
|
||||
{
|
||||
public function getFunctions(): array
|
||||
{
|
||||
return [
|
||||
new TwigFunction('get_presentation_count', [$this, 'getPresentationCount']),
|
||||
];
|
||||
}
|
||||
|
||||
public function getName(): string
|
||||
{
|
||||
return 'modules.presentations';
|
||||
}
|
||||
|
||||
public function getPresentationCount(array $presentations): int
|
||||
{
|
||||
$today = (new \DateTime('today'))->getTimestamp();
|
||||
|
||||
return collect($presentations)
|
||||
->flatMap(fn (ProxySourceItem $presentation) => $presentation->data()->get('events'))
|
||||
->filter(
|
||||
function (array $event) use ($today): bool {
|
||||
assert(array_key_exists(array: $event, key: 'date'));
|
||||
|
||||
return $event['date'] < $today;
|
||||
}
|
||||
)
|
||||
->count();
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue