|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Tests\Integration\MartinGeorgiev\Doctrine\ORM\Query\AST\Functions; |
| 6 | + |
| 7 | +use MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\DateTrunc; |
| 8 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 9 | +use PHPUnit\Framework\Attributes\Test; |
| 10 | + |
| 11 | +class DateTruncTest extends DateTestCase |
| 12 | +{ |
| 13 | + protected function getStringFunctions(): array |
| 14 | + { |
| 15 | + return [ |
| 16 | + 'DATE_TRUNC' => DateTrunc::class, |
| 17 | + ]; |
| 18 | + } |
| 19 | + |
| 20 | + #[DataProvider('provideTruncFieldCases')] |
| 21 | + #[Test] |
| 22 | + public function can_truncate_to_field(string $field, string $expected): void |
| 23 | + { |
| 24 | + $dql = \sprintf( |
| 25 | + "SELECT DATE_TRUNC('%s', t.datetime1) as result |
| 26 | + FROM Fixtures\\MartinGeorgiev\\Doctrine\\Entity\\ContainsDates t |
| 27 | + WHERE t.id = 1", |
| 28 | + $field |
| 29 | + ); |
| 30 | + $result = $this->executeDqlQuery($dql); |
| 31 | + $this->assertSame($expected, $result[0]['result']); |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * @return \Generator<string, array{string, string}> |
| 36 | + */ |
| 37 | + public static function provideTruncFieldCases(): \Generator |
| 38 | + { |
| 39 | + // Test data: datetime1 = '2023-06-15 10:30:00' |
| 40 | + yield 'microseconds' => ['microseconds', '2023-06-15 10:30:00']; |
| 41 | + yield 'milliseconds' => ['milliseconds', '2023-06-15 10:30:00']; |
| 42 | + yield 'second' => ['second', '2023-06-15 10:30:00']; |
| 43 | + yield 'minute' => ['minute', '2023-06-15 10:30:00']; |
| 44 | + yield 'hour' => ['hour', '2023-06-15 10:00:00']; |
| 45 | + yield 'day' => ['day', '2023-06-15 00:00:00']; |
| 46 | + yield 'week' => ['week', '2023-06-12 00:00:00']; // Monday of that week |
| 47 | + yield 'month' => ['month', '2023-06-01 00:00:00']; |
| 48 | + yield 'quarter' => ['quarter', '2023-04-01 00:00:00']; |
| 49 | + yield 'year' => ['year', '2023-01-01 00:00:00']; |
| 50 | + yield 'decade' => ['decade', '2020-01-01 00:00:00']; |
| 51 | + yield 'century' => ['century', '2001-01-01 00:00:00']; |
| 52 | + yield 'millennium' => ['millennium', '2001-01-01 00:00:00']; |
| 53 | + } |
| 54 | + |
| 55 | + #[Test] |
| 56 | + public function can_truncate_timestamptz_with_timezone(): void |
| 57 | + { |
| 58 | + $dql = "SELECT DATE_TRUNC('day', t.datetimetz1, 'Australia/Adelaide') as result |
| 59 | + FROM Fixtures\\MartinGeorgiev\\Doctrine\\Entity\\ContainsDates t |
| 60 | + WHERE t.id = 1"; |
| 61 | + $result = $this->executeDqlQuery($dql); |
| 62 | + // The input is '2023-06-15 10:30:00+00' (UTC) |
| 63 | + // In Australia/Adelaide (UTC+9:30), this is '2023-06-15 20:00:00' |
| 64 | + // Truncated to day in Adelaide timezone gives '2023-06-15 00:00:00+09:30' |
| 65 | + // Converted back to UTC: '2023-06-14 14:30:00+00' |
| 66 | + $this->assertSame('2023-06-14 14:30:00+00', $result[0]['result']); |
| 67 | + } |
| 68 | +} |
0 commit comments