packages/sql-semantics-mysql/tests/Unit/Statement/Expression/Conversion/CastAtLocalTest.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\Invalid;
15use SqlSemantics\Platform\MySql\Platform;
16use SqlSemantics\Platform\MySql\Statement\Expression\Conversion\CastAtLocal;
17use SqlSemantics\Platform\MySql\Statement\Literal\NullLiteral;
18use SqlSemantics\Platform\MySql\Statement\Literal\NumberLiteral;
19use SqlSemantics\Platform\MySql\Statement\Type\CastTarget;
20use SqlSemantics\Platform\MySql\Statement\Type\Kind\CastKind;
21
22#[CoversClass(CastAtLocal::class)]
23#[Medium]
24final class CastAtLocalTest extends TestCase
25{
26    public function testDeriveScalarReportsTheUnsupportedForm(): void
27    {
28        $platform = new Platform();
29        $derivation = new Derivation($platform->context($platform->profile('mysql-8.4.7', null, ParameterStyle::Native), null, [], true));
30        $fact = $derivation->scalar(new CastAtLocal(new NumberLiteral('1'), new CastTarget(CastKind::DateTime)), $derivation->environment());
31
32        self::assertInstanceOf(Invalid::class, $fact->type);
33        self::assertSame("This version of MySQL doesn't yet support 'AT LOCAL'.", $derivation->facts()->diagnostics[0]->message());
34    }
35
36    public function testRenderWritesAtLocal(): void
37    {
38        $platform = new Platform();
39        $out = new Output($platform->codec($platform->profile('mysql-8.4.7', null, ParameterStyle::Native)));
40        (new CastAtLocal(new NullLiteral(), new CastTarget(CastKind::Signed), true))->render($out);
41
42        self::assertSame('CAST(NULL AT LOCAL AS SIGNED ARRAY)', (new Lexical())->join($out->pieces()));
43    }
44}
45