diff --git a/source/connector-mobilizon.php b/source/connector-mobilizon.php index e9b8074..7c853e1 100644 --- a/source/connector-mobilizon.php +++ b/source/connector-mobilizon.php @@ -18,7 +18,8 @@ require_once __DIR__ . '/includes/EventsCache.php'; require_once __DIR__ . '/includes/Settings.php'; require_once __DIR__ . '/includes/SiteSettings.php'; require_once __DIR__ . '/includes/LocalDateTime.php'; -require_once __DIR__ . '/includes/Formatter.php'; +require_once __DIR__ . '/includes/LocalDateTimeFormatter.php'; +require_once __DIR__ . '/includes/LineFormatter.php'; require_once __DIR__ . '/includes/GraphQlClient.php'; require_once __DIR__ . '/includes/EventsListBlock.php'; require_once __DIR__ . '/includes/EventsListShortcut.php'; diff --git a/source/includes/LineFormatter.php b/source/includes/LineFormatter.php index 09c4ef0..897ad25 100644 --- a/source/includes/LineFormatter.php +++ b/source/includes/LineFormatter.php @@ -5,17 +5,22 @@ final class LineFormatter { public static function format_date_time(\DateTimeZone $timeZone, string $dateFormat, string $timeFormat, string $start, ?string $end): string { $startDateTime = new LocalDateTime($start, $timeZone); - $dateText = LocalDateTimeFormatter::format($startDateTime, $dateFormat); - $dateText .= ' ' . LocalDateTimeFormatter::format($startDateTime, $timeFormat); + $startDate = LocalDateTimeFormatter::format($startDateTime, $dateFormat); + $startTime = LocalDateTimeFormatter::format($startDateTime, $timeFormat); + + $dateText = $startDate . ' ' . $startTime; if ($end) { $endDateTime = new LocalDateTime($end, $timeZone); - if (LocalDateTimeFormatter::format($startDateTime, $dateFormat) != LocalDateTimeFormatter::format($endDateTime, $dateFormat)) { + $endDate = LocalDateTimeFormatter::format($endDateTime, $dateFormat); + $endTime = LocalDateTimeFormatter::format($endDateTime, $timeFormat); + + if ($startDate != $endDate) { $dateText .= ' - '; - $dateText .= LocalDateTimeFormatter::format($endDateTime, $dateFormat) . ' '; + $dateText .= $endDate . ' '; } else { $dateText .= ' - '; } - $dateText .= LocalDateTimeFormatter::format($endDateTime, $timeFormat); + $dateText .= $endTime; } return $dateText; } diff --git a/tests/LineFormatterTest.php b/tests/LineFormatterTest.php index db73597..4294b66 100644 --- a/tests/LineFormatterTest.php +++ b/tests/LineFormatterTest.php @@ -4,34 +4,36 @@ declare(strict_types=1); use MobilizonConnector\LineFormatter; use PHPUnit\Framework\TestCase; +function date_i18n(string $format, int $timestamp) { + // Mock WordPress function. + if ($format == 'H:i') { + if ($timestamp == 1618482600) { + return '10:30'; + } else if ($timestamp == 1618500600 || $timestamp == 1618587000) { + return '15:30'; + } + } else if ($format == 'd/m/Y') { + if ($timestamp == 1618482600 || $timestamp == 1618500600) { + return '15/04/2021'; + } else if ($timestamp == 1618587000) { + return '16/04/2021'; + } + } + return ''; +} + final class LineFormatterTest extends TestCase { public function testCanDateFormatOneDate(): void { - $this->assertSame('15/04/2021 10:30 - 15:30', LineFormatter::format_date_time(new \DateTimeZone('UTC'), '2021-04-15T10:30:00Z', '2021-04-15T15:30:00Z', false)); - } - - public function testCanDateFormatOneDateWithOffset(): void { - $this->assertSame('15/04/2021 10:30 - 15:30 (UTC)', LineFormatter::format_date_time(new \DateTimeZone('UTC'), '2021-04-15T10:30:00Z', '2021-04-15T15:30:00Z', true)); - } - - public function testCanDateFormatOneDateWithTimeZoneOffset(): void { - $this->assertSame('15/04/2021 11:30 - 16:30', LineFormatter::format_date_time(new \DateTimeZone('+01:00'), '2021-04-15T10:30:00Z', '2021-04-15T15:30:00Z', false)); + $this->assertSame('15/04/2021 10:30 - 15:30', LineFormatter::format_date_time(new \DateTimeZone('UTC'), 'd/m/Y', 'H:i', '2021-04-15T10:30:00Z', '2021-04-15T15:30:00Z')); } public function testCanDateFormatTwoDates(): void { - $this->assertSame('15/04/2021 10:30 - 16/04/2021 15:30', LineFormatter::format_date_time(new \DateTimeZone('UTC'), '2021-04-15T10:30:00Z', '2021-04-16T15:30:00Z', false)); - } - - public function testCanDateFormatTwoDatesWithOffset(): void { - $this->assertSame('15/04/2021 10:30 - 16/04/2021 15:30 (UTC)', LineFormatter::format_date_time(new \DateTimeZone('UTC'), '2021-04-15T10:30:00Z', '2021-04-16T15:30:00Z', true)); + $this->assertSame('15/04/2021 10:30 - 16/04/2021 15:30', LineFormatter::format_date_time(new \DateTimeZone('UTC'), 'd/m/Y', 'H:i', '2021-04-15T10:30:00Z', '2021-04-16T15:30:00Z')); } public function testCanDateFormatWhenSecondDateIsNull(): void { - $this->assertSame('15/04/2021 10:30', LineFormatter::format_date_time(new \DateTimeZone('UTC'), '2021-04-15T10:30:00Z', null, false)); - } - - public function testCanDateFormatWhenSecondDateIsNullWithOffset(): void { - $this->assertSame('15/04/2021 10:30 (UTC)', LineFormatter::format_date_time(new \DateTimeZone('UTC'), '2021-04-15T10:30:00Z', null, true)); + $this->assertSame('15/04/2021 10:30', LineFormatter::format_date_time(new \DateTimeZone('UTC'), 'd/m/Y', 'H:i', '2021-04-15T10:30:00Z', null)); } public function testCanLocationFormatBothParameters(): void { diff --git a/tests/LocalDateTimeFormatterTest.php b/tests/LocalDateTimeFormatterTest.php deleted file mode 100644 index aaaf94c..0000000 --- a/tests/LocalDateTimeFormatterTest.php +++ /dev/null @@ -1,17 +0,0 @@ -assertSame('15 April 2021', LocalDateTimeFormatter::format(new LocalDateTime('2021-04-15T10:30:00Z', new DateTimeZone("utc")), 'j F Y')); - } - - public function testCanFormatTime(): void { - $this->assertSame('10:30', LocalDateTimeFormatter::format(new LocalDateTime('2021-04-15T10:30:00Z', new DateTimeZone("utc")), 'H:i')); - } -}