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/tests/FormatTalksBundle/Twig/FormatTalksTest.php

193 lines
5.6 KiB
PHP
Raw Normal View History

2017-11-01 17:24:23 +00:00
<?php
2017-11-14 20:45:38 +00:00
namespace Tests\FormatTalksBundle\Twig;
2017-11-01 17:24:23 +00:00
use DateTime;
use FormatTalksBundle\Twig\FormatTalksExtension;
use Illuminate\Support\Collection;
2017-11-07 13:00:40 +00:00
use PHPUnit\Framework\TestCase;
2017-11-01 17:24:23 +00:00
2017-11-07 13:00:40 +00:00
class FormatTalksTest extends TestCase
2017-11-01 17:24:23 +00:00
{
/**
* @var FormatTalksExtension
*/
private $extension;
/**
* {@inheritdoc}
*/
public function setUp()
{
$this->extension = new FormatTalksExtension();
}
2017-11-07 18:08:47 +00:00
/**
* @covers FormatTalksExtension::format()
*/
2017-11-07 08:18:16 +00:00
public function testFormat()
{
2017-11-14 18:20:27 +00:00
$event_data = [
2017-11-14 18:26:01 +00:00
'event-a' => [
'name' => 'Event A',
'location' => 'Somewhere',
'website' => 'http://event-a.com',
],
'event-b' => [
'name' => 'Event B',
'location' => 'Somewhere else',
'website' => 'http://event-b.com',
],
];
2017-11-14 13:43:46 +00:00
2017-11-14 18:20:27 +00:00
$talks = [
2017-11-14 18:26:01 +00:00
[
'title' => 'Talk A',
'events' => [
['event' => 'event-a', 'date' => '2018-01-01', 'time' => '09:00'],
['event' => 'event-b', 'date' => '2018-01-30', 'time' => '12:00'],
2017-11-07 08:18:16 +00:00
],
2017-11-14 18:26:01 +00:00
],
[
'title' => 'Talk B',
'events' => [
['event' => 'event-b', 'date' => '2018-01-31', 'time' => '17:00'],
2017-11-07 20:50:03 +00:00
],
2017-11-14 18:26:01 +00:00
],
2017-11-07 08:18:16 +00:00
];
2017-11-14 13:43:46 +00:00
$results = $this->extension->format($talks, $event_data)->all();
2017-11-07 08:18:16 +00:00
2017-11-07 20:50:03 +00:00
$this->assertCount(3, $results);
2017-11-07 08:18:16 +00:00
tap($results[0], function ($result) {
$this->assertArrayHasKey('event', $result);
$this->assertArrayHasKey('talk', $result);
$this->assertEquals([
'date' => '2018-01-01',
2017-11-07 20:44:54 +00:00
'event' => 'event-a',
2017-11-07 08:18:16 +00:00
'location' => 'Somewhere',
2017-11-07 20:44:54 +00:00
'name' => 'Event A',
2017-11-07 08:18:16 +00:00
'time' => '09:00',
2017-11-07 20:44:54 +00:00
'website' => 'http://event-a.com',
2017-11-07 08:18:16 +00:00
], $result['event']);
2017-11-07 20:50:03 +00:00
$this->assertEquals('Talk A', $result['talk']['title']);
2017-11-07 08:18:16 +00:00
});
tap($results[1], function ($result) {
$this->assertArrayHasKey('event', $result);
$this->assertArrayHasKey('talk', $result);
$this->assertEquals([
'date' => '2018-01-30',
2017-11-07 20:44:54 +00:00
'event' => 'event-b',
2017-11-07 08:18:16 +00:00
'location' => 'Somewhere else',
2017-11-07 20:44:54 +00:00
'name' => 'Event B',
2017-11-07 08:18:16 +00:00
'time' => '12:00',
2017-11-07 20:44:54 +00:00
'website' => 'http://event-b.com',
2017-11-07 08:18:16 +00:00
], $result['event']);
2017-11-07 20:50:03 +00:00
$this->assertEquals('Talk A', $result['talk']['title']);
});
tap($results[2], function ($result) {
$this->assertEquals('Talk B', $result['talk']['title']);
2017-11-07 08:18:16 +00:00
});
}
2017-11-01 17:24:23 +00:00
/**
2017-11-07 18:08:47 +00:00
* @covers FormatTalksExtension::getAll()
2017-11-01 17:24:23 +00:00
*/
public function testGetAll()
{
$eventA = ['date' => (new DateTime('+1 week'))->format('Y-m-d')];
$eventB = ['date' => (new DateTime('-2 weeks'))->format('Y-m-d')];
$eventC = ['date' => (new DateTime('today'))->format('Y-m-d')];
2017-11-14 13:43:46 +00:00
$talks = [
['events' => [$eventA, $eventB]],
['events' => [$eventC]],
2017-11-01 17:24:23 +00:00
];
2017-11-14 13:43:46 +00:00
$results = $this->extension->getAll($talks);
2017-11-01 17:24:23 +00:00
2017-11-01 19:57:41 +00:00
$this->assertCount(3, $results);
// Earliest events should be returned first.
2017-11-01 19:57:41 +00:00
$this->assertEquals(
[$eventB['date'], $eventC['date'], $eventA['date']],
2017-11-01 19:57:41 +00:00
$this->extractDates($results)
);
2017-11-01 17:24:23 +00:00
}
/**
2017-11-07 18:08:47 +00:00
* @covers FormatTalksExtension::getUpcoming()
2017-11-01 17:24:23 +00:00
*/
2017-11-07 18:08:47 +00:00
public function testGetUpcoming()
2017-11-01 17:24:23 +00:00
{
$eventA = ['date' => (new DateTime('+1 week'))->format('Y-m-d')];
$eventB = ['date' => (new DateTime('-2 weeks'))->format('Y-m-d')];
$eventC = ['date' => (new DateTime('today'))->format('Y-m-d')];
$eventD = ['date' => (new DateTime('+1 day'))->format('Y-m-d')];
$eventE = ['date' => (new DateTime('+2 weeks'))->format('Y-m-d')];
2017-11-14 13:43:46 +00:00
$talks = [
['events' => [$eventA, $eventC]],
['events' => [$eventB, $eventE]],
2017-11-01 17:24:23 +00:00
];
2017-11-14 13:43:46 +00:00
$results = $this->extension->getUpcoming($talks);
2017-11-01 17:24:23 +00:00
$this->assertCount(3, $results);
2017-11-01 19:57:41 +00:00
// Earliest events should be returned first.
2017-11-01 19:57:41 +00:00
$this->assertEquals(
[$eventC['date'], $eventA['date'], $eventE['date']],
2017-11-01 19:57:41 +00:00
$this->extractDates($results)
);
2017-11-01 17:24:23 +00:00
}
/**
2017-11-07 18:08:47 +00:00
* @covers FormatTalksExtension::getPast()
2017-11-01 17:24:23 +00:00
*/
2017-11-07 18:08:47 +00:00
public function testGetPast()
2017-11-01 17:24:23 +00:00
{
$eventA = ['date' => (new DateTime('+1 week'))->format('Y-m-d')];
$eventB = ['date' => (new DateTime('-2 weeks'))->format('Y-m-d')];
$eventC = ['date' => (new DateTime('today'))->format('Y-m-d')];
$eventD = ['date' => (new DateTime('+1 day'))->format('Y-m-d')];
2017-11-01 19:57:41 +00:00
$eventE = ['date' => (new DateTime('-2 days'))->format('Y-m-d')];
$eventF = ['date' => (new DateTime('-2 months'))->format('Y-m-d')];
2017-11-01 17:24:23 +00:00
2017-11-14 13:43:46 +00:00
$talks = [
['events' => [$eventD]],
['events' => [$eventA, $eventB, $eventC]],
['events' => [$eventF]],
2017-11-01 17:24:23 +00:00
];
2017-11-14 13:43:46 +00:00
$results = $this->extension->getPast($talks);
2017-11-01 17:24:23 +00:00
2017-11-01 19:57:41 +00:00
$this->assertCount(2, $results);
// Latest events should be returned first.
2017-11-01 19:57:41 +00:00
$this->assertEquals(
[$eventB['date'], $eventF['date']],
$this->extractDates($results)
);
}
/**
* Extract the returned dates from the results.
*
* @param Collection $results The results returned from the filter.
2017-11-01 19:57:41 +00:00
*
* @return array An array of dates.
*/
private function extractDates(Collection $results)
2017-11-01 19:57:41 +00:00
{
return $results->pluck('event.date')->all();
2017-11-01 17:24:23 +00:00
}
}