This repository has been archived on 2025-01-19. You can view files and clone it, but cannot push or open issues or pull requests.
oliverdavies.uk-old-sculpin/src/Talks/tests/TwigExtension/TalksExtensionTest.php

145 lines
4 KiB
PHP
Raw Normal View History

2019-01-18 18:06:31 +00:00
<?php
2019-04-14 07:09:24 +00:00
namespace App\Tests\Talks\TwigExtension;
2019-01-18 18:06:31 +00:00
2019-04-14 07:09:24 +00:00
use App\Talks\TwigExtension\TalksExtension;
2019-04-14 07:23:57 +00:00
use DateTime;
2019-01-18 18:06:31 +00:00
use Illuminate\Support\Collection;
use PHPUnit\Framework\TestCase;
class TalksExtensionTest extends TestCase
{
/**
* @var TalksExtension
*/
private $extension;
/**
* {@inheritdoc}
*/
public function setUp()
{
$this->extension = new TalksExtension();
}
/** @test */
public function talks_given_multiple_times_are_only_returned_once()
{
$talkA = [
'title' => 'Talk A',
'events' => [
2019-02-13 06:55:18 +00:00
['event' => 'event_a', 'date' => (new DateTime('-1 days'))->getTimestamp()],
['event' => 'event_b', 'date' => (new DateTime('+1 days'))->getTimestamp()],
],
];
$talkB = [
'title' => 'Talk B',
'events' => [
2019-02-13 06:55:18 +00:00
['event' => 'event_a', 'date' => (new DateTime('-3 days'))->getTimestamp()],
],
];
$this->assertCount(2, $this->extension->getAll([$talkA, $talkB]));
2019-01-18 18:06:31 +00:00
}
/** @test */
public function talks_are_ordered_by_the_most_recent_event_date()
{
$talkA = [
'title' => 'Talk A',
'events' => [
2019-02-13 06:55:18 +00:00
['event' => 'event_a', 'date' => (new DateTime('-5 days'))->getTimestamp()],
],
];
$talkB = [
'title' => 'Talk B',
'events' => [
2019-02-13 06:55:18 +00:00
['event' => 'event_a', 'date' => (new DateTime('-20 days'))->getTimestamp()],
],
];
$talkC = [
'title' => 'Talk C',
'events' => [
2019-02-13 06:55:18 +00:00
['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->getAll($unorderedTalks);
$this->assertEquals([$talkC, $talkA, $talkB], $orderedTalks->toArray());
2019-01-18 18:06:31 +00:00
}
/** @test */
public function only_past_talks_can_be_retrieved()
2019-01-18 18:06:31 +00:00
{
$pastTalk = [
'title' => 'Past talk',
'events' => [
2019-02-13 06:55:18 +00:00
['date' => (new DateTime('-1 day'))->getTimestamp()],
]
];
$futureTalk = [
'title' => 'Future talk',
'events' => [
2019-02-13 06:55:18 +00:00
['date' => (new DateTime('+1 day'))->getTimestamp()],
],
];
$result = $this->extension->getPast([$pastTalk, $futureTalk]);
$this->assertCount(1, $result);
$this->assertSame($pastTalk, $result->first());
2019-01-18 18:06:31 +00:00
}
/** @test */
public function only_current_and_future_talks_can_be_retrieved()
2019-01-18 18:06:31 +00:00
{
$pastTalk = [
'title' => 'Past talk',
'events' => [
2019-02-13 06:55:18 +00:00
['date' => (new DateTime('-1 day'))->getTimestamp()],
]
];
$todayTalk = [
'title' => 'A talk that it happening today',
'events' => [
2019-02-13 06:55:18 +00:00
['date' => (new DateTime('now'))->getTimestamp()],
],
];
$futureTalk = [
'title' => 'Future talk',
'events' => [
2019-02-13 06:55:18 +00:00
['date' => (new DateTime('+1 day'))->getTimestamp()],
],
];
$result = $this->extension->getUpcoming([$pastTalk, $todayTalk, $futureTalk]);
$this->assertCount(2, $result);
$this->assertSame([$todayTalk, $futureTalk], $result->toArray());
2019-01-18 18:06:31 +00:00
}
2019-01-18 21:30:29 +00:00
/** @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' => [
2019-02-13 06:55:18 +00:00
['date' => (new DateTime('-1 week'))->getTimestamp()],
['date' => (new DateTime('+1 week'))->getTimestamp()],
],
];
$this->assertCount(1, $this->extension->getUpcoming([$talk]));
$this->assertEmpty($this->extension->getPast([$talk]));
2019-01-18 21:30:29 +00:00
}
2019-01-18 18:06:31 +00:00
}