packages/sql-semantics-mysql/tests/Unit/Statement/Expression/Conversion/AtTimeZoneTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Expression\Conversion;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\MySql\Model\Construction\Derivation;
11use SqlSemantics\Platform\MySql\Model\Contract\ParameterStyle;
12use SqlSemantics\Platform\MySql\Model\Rendering\Lexical;
13use SqlSemantics\Platform\MySql\Model\Rendering\Output;
14use SqlSemantics\Platform\MySql\Model\Statement\Type\Known;
15use SqlSemantics\Platform\MySql\Model\Statement\Type\Nullability;
16use SqlSemantics\Platform\MySql\Platform;
17use SqlSemantics\Platform\MySql\Statement\Expression\Conversion\AtTimeZone;
18use SqlSemantics\Platform\MySql\Statement\Literal\EscapeRule;
19use SqlSemantics\Platform\MySql\Statement\Literal\NullLiteral;
20use SqlSemantics\Platform\MySql\Statement\Literal\NumberLiteral;
21use SqlSemantics\Platform\MySql\Statement\Literal\Radix;
22use SqlSemantics\Platform\MySql\Statement\Literal\Text;
23use SqlSemantics\Platform\MySql\Statement\Type\Kind\TemporalKind;
24use SqlSemantics\Platform\MySql\Statement\Type\Temporal;
25
26#[CoversClass(AtTimeZone::class)]
27#[Medium]
28final class AtTimeZoneTest extends TestCase
29{
30    public function testDeriveScalarIsADateTimeOfThePrecision(): void
31    {
32        $platform = new Platform();
33        $derivation = new Derivation($platform->context($platform->profile('mysql-8.4.7', null, ParameterStyle::Native), null, [], true));
34        $fact = $derivation->scalar(new AtTimeZone(new NullLiteral(), new Text('UTC'), false, '6'), $derivation->environment());
35
36        self::assertEquals(new Known(new Temporal(TemporalKind::DateTime, '6')), $fact->type);
37        self::assertSame(Nullability::Nullable, $fact->nullability);
38    }
39
40    public function testRenderKeepsTheIntervalKeyword(): void
41    {
42        $platform = new Platform();
43        $out = new Output($platform->codec($platform->profile('mysql-8.4.7', null, ParameterStyle::Native)));
44        (new AtTimeZone(new NumberLiteral('1'), new Text('+00:00'), true))->render($out);
45
46        self::assertSame("CAST(1 AT TIME ZONE INTERVAL '+00:00' AS DATETIME)", (new Lexical())->join($out->pieces()));
47    }
48
49    public function testAHexadecimalZoneIsRejected(): void
50    {
51        $this->expectExceptionMessage('A time zone is a quoted string.');
52
53        new AtTimeZone(new NumberLiteral('1'), new Text('00', EscapeRule::Backslash, Radix::Hexadecimal));
54    }
55}
56