Add OpdaviesTwigExtensionTest

This commit is contained in:
Oliver Davies 2024-05-03 14:18:05 +01:00
parent 6888675252
commit 6ee3e91363
5 changed files with 1732 additions and 3 deletions

4
.gitignore vendored
View file

@ -7,3 +7,7 @@
/node_modules/
/source/build/
# PHPUnit.
/.phpunit.cache/
/.phpunit.result.cache

View file

@ -12,5 +12,12 @@
"autoload": {
"psr-4": {
"App\\": "src" }
}
},
"autoload-dev": {
"psr-4": {
"App\\Tests\\": "tests" }
},
"require-dev": {
"phpunit/phpunit": "^11.1"
}
}

1636
composer.lock generated

File diff suppressed because it is too large Load diff

8
phpunit.xml.dist Normal file
View file

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="./vendor/autoload.php" colors="true" stopOnFailure="true" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/11.1/phpunit.xsd" cacheDirectory=".phpunit.cache">
<testsuites>
<testsuite name="unit tests">
<directory suffix="Test.php">tests</directory>
</testsuite>
</testsuites>
</phpunit>

View file

@ -0,0 +1,78 @@
<?php
namespace App\Tests\Opdavies\TwigExtension;
use App\Opdavies\TwigExtension\OpdaviesTwigExtension;
use Dflydev\DotAccessConfiguration\Configuration;
use PHPUnit\Framework\TestCase;
use Sculpin\Contrib\ProxySourceCollection\ProxySourceItem;
class OpdaviesTwigExtensionTest extends TestCase
{
private OpdaviesTwigExtension $extension;
public function setUp(): void
{
$this->extension = new OpdaviesTwigExtension();
}
public function testNoPastEvents(): void
{
$talk = $this->createTalk(
events: [
['date' => (new \DateTime('+1 days'))->getTimestamp()],
],
);
self::assertSame(0, $this->extension->getPastTalkCount([$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()],
],
);
self::assertSame(1, $this->extension->getPastTalkCount([$talkA, $talkB]));
}
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()],
],
);
self::assertSame(2, $this->extension->getPastTalkCount([$talkA, $talkB]));
}
/**
* 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;
}
}