oliverdavies.uk/tests/Presentations/PresentationTwigExtensionTest.php

142 lines
4.5 KiB
PHP
Raw Normal View History

2024-05-03 13:18:05 +00:00
<?php
2024-09-04 19:05:14 +00:00
namespace Tests\Presentations;
2024-05-03 13:18:05 +00:00
2024-09-04 19:05:14 +00:00
use App\Presentations\PresentationTwigExtension;
2024-05-03 13:18:05 +00:00
use Dflydev\DotAccessConfiguration\Configuration;
use PHPUnit\Framework\TestCase;
use Sculpin\Contrib\ProxySourceCollection\ProxySourceItem;
2024-09-02 17:00:00 +00:00
class PresentationTwigExtensionTest extends TestCase
2024-05-03 13:18:05 +00:00
{
2024-09-02 17:00:00 +00:00
private PresentationTwigExtension $extension;
2024-05-03 13:18:05 +00:00
public function setUp(): void
{
2024-09-02 17:00:00 +00:00
$this->extension = new PresentationTwigExtension();
2024-05-03 13:18:05 +00:00
}
public function testNoPastEvents(): void
{
2024-09-02 17:00:00 +00:00
$presentation = $this->createPresentation(
2024-05-03 13:18:05 +00:00
events: [
2024-05-03 13:22:39 +00:00
['date' => (new \DateTime('+1 days'))->getTimestamp()],
2024-05-03 13:18:05 +00:00
],
);
2024-09-02 17:00:00 +00:00
$this->assertPresentationCount(expectedCount: 0, presentations: [$presentation]);
2024-05-03 13:18:05 +00:00
}
public function testSinglePastEvent(): void
{
2024-09-02 17:00:00 +00:00
$presentationA = $this->createPresentation(
2024-05-03 13:18:05 +00:00
events: [
2024-05-03 13:22:39 +00:00
['date' => (new \DateTime('+1 days'))->getTimestamp()],
2024-05-03 13:18:05 +00:00
],
);
2024-09-02 17:00:00 +00:00
$presentationB = $this->createPresentation(
2024-05-03 13:18:05 +00:00
events: [
['date' => (new \DateTime('-3 days'))->getTimestamp()],
],
);
2024-09-02 17:00:00 +00:00
$this->assertPresentationCount(expectedCount: 1, presentations: [$presentationA, $presentationB]);
2024-05-03 13:18:05 +00:00
}
2024-09-02 17:00:00 +00:00
public function testSinglePresentationWithMultiplePastEvents(): void
2024-05-03 13:45:58 +00:00
{
2024-09-02 17:00:00 +00:00
$presentation = $this->createPresentation(
2024-05-03 13:45:58 +00:00
events: [
['date' => (new \DateTime('-1 days'))->getTimestamp()],
['date' => (new \DateTime('-1 week'))->getTimestamp()],
['date' => (new \DateTime('-1 year'))->getTimestamp()],
],
);
2024-09-02 17:00:00 +00:00
$this->assertPresentationCount(expectedCount: 3, presentations: [$presentation]);
2024-05-03 13:45:58 +00:00
}
2024-09-02 17:00:00 +00:00
public function testSinglePresentationWithMultiplePastAndFutureEvents(): void
2024-05-03 13:45:58 +00:00
{
2024-09-02 17:00:00 +00:00
$presentation = $this->createPresentation(
2024-05-03 13:45:58 +00:00
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()],
],
);
2024-09-02 17:00:00 +00:00
$this->assertPresentationCount(expectedCount: 3, presentations: [$presentation]);
2024-05-03 13:45:58 +00:00
}
2024-05-03 13:18:05 +00:00
public function testMultiplePastEvents(): void
{
2024-09-02 17:00:00 +00:00
$presentationA = $this->createPresentation(
2024-05-03 13:18:05 +00:00
events: [
2024-05-03 13:22:39 +00:00
['date' => (new \DateTime('-1 days'))->getTimestamp()],
['date' => (new \DateTime('+1 days'))->getTimestamp()],
2024-05-03 13:18:05 +00:00
],
);
2024-09-02 17:00:00 +00:00
$presentationB = $this->createPresentation(
2024-05-03 13:18:05 +00:00
events: [
['date' => (new \DateTime('-3 days'))->getTimestamp()],
],
);
2024-09-02 17:00:00 +00:00
$this->assertPresentationCount(expectedCount: 2, presentations: [$presentationA, $presentationB]);
}
public function testTheCurrentDayIsNotCounted(): void
{
2024-09-02 17:00:00 +00:00
$presentationA = $this->createPresentation(
events: [
['date' => (new \DateTime('yesterday'))->getTimestamp()],
['date' => (new \DateTime('today'))->getTimestamp()],
],
);
2024-09-02 17:00:00 +00:00
$presentationB = $this->createPresentation(
events: [
['date' => (new \DateTime('today'))->getTimestamp()],
],
);
2024-09-02 17:00:00 +00:00
$presentationC = $this->createPresentation(
events: [
['date' => (new \DateTime('yesterday'))->getTimestamp()],
],
);
2024-09-02 17:00:00 +00:00
$this->assertPresentationCount(expectedCount: 2, presentations: [$presentationA, $presentationB, $presentationC]);
}
/**
2024-09-02 17:00:00 +00:00
* Assert the extension uses the correct number of presentations.
*/
2024-09-02 17:00:00 +00:00
private function assertPresentationCount(int $expectedCount, array $presentations): void
{
self::assertSame(
2024-09-02 17:00:00 +00:00
actual: $this->extension->getPresentationCount($presentations),
expected: $expectedCount,
);
2024-05-03 13:18:05 +00:00
}
/**
2024-09-02 17:00:00 +00:00
* Create a mock presentation with a list of events.
2024-05-03 13:18:05 +00:00
*/
2024-09-02 17:00:00 +00:00
private function createPresentation(array $events): ProxySourceItem
2024-05-03 13:18:05 +00:00
{
$configuration = $this->createMock(Configuration::class);
$configuration->method('get')->with($this->identicalTo('events'))->willReturn($events);
2024-09-02 17:00:00 +00:00
$presentation = $this->createMock(ProxySourceItem::class);
$presentation->method('data')->willReturn($configuration);
2024-05-03 13:18:05 +00:00
2024-09-02 17:00:00 +00:00
return $presentation;
2024-05-03 13:18:05 +00:00
}
}