packages/sql-semantics-mysql/tests/Unit/Statement/Expression/Conversion/CastTest.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\Cast;
18use SqlSemantics\Platform\MySql\Statement\Literal\NumberLiteral;
19use SqlSemantics\Platform\MySql\Statement\Literal\StringLiteral;
20use SqlSemantics\Platform\MySql\Statement\Type\CastTarget;
21use SqlSemantics\Platform\MySql\Statement\Type\Elementary;
22use SqlSemantics\Platform\MySql\Statement\Type\Kind\CastKind;
23use SqlSemantics\Platform\MySql\Statement\Type\Kind\ElementaryKind;
24use SqlSemantics\Platform\MySql\Statement\Type\Kind\TemporalKind;
25use SqlSemantics\Platform\MySql\Statement\Type\Temporal;
26
27#[CoversClass(Cast::class)]
28#[Medium]
29final class CastTest extends TestCase
30{
31 public function testDeriveScalarHasTheTargetTypeAndADateCanBeNull(): void
32 {
33 $platform = new Platform();
34 $derivation = new Derivation($platform->context($platform->profile('mysql-8.4.7', null, ParameterStyle::Native), null, [], true));
35 $fact = $derivation->scalar(new Cast(new StringLiteral(['x']), new CastTarget(CastKind::DateTime, '3')), $derivation->environment());
36
37 self::assertEquals(new Known(new Temporal(TemporalKind::DateTime, '3')), $fact->type);
38 self::assertSame(Nullability::Nullable, $fact->nullability);
39 }
40
41 public function testDeriveScalarIsJsonForAnArray(): void
42 {
43 $platform = new Platform();
44 $derivation = new Derivation($platform->context($platform->profile('mysql-8.0.44', null, ParameterStyle::Native), null, [], true));
45
46 self::assertEquals(new Known(new Elementary(ElementaryKind::Json)), $derivation->scalar(new Cast(new NumberLiteral('1'), new CastTarget(CastKind::Unsigned), true), $derivation->environment())->type);
47 }
48
49 public function testDeriveScalarRejectsAnArrayBeforeMySql80(): void
50 {
51 $platform = new Platform();
52 $derivation = new Derivation($platform->context($platform->profile('mysql-5.7.44', null, ParameterStyle::Native), null, [], true));
53
54 $this->expectExceptionMessage('A cast to an array needs MySQL 8.0 or later.');
55
56 $derivation->scalar(new Cast(new NumberLiteral('1'), new CastTarget(CastKind::Unsigned), true), $derivation->environment());
57 }
58
59 public function testRenderGluesTheParenthesisToCast(): void
60 {
61 $platform = new Platform();
62 $out = new Output($platform->codec($platform->profile('mysql-8.4.7', null, ParameterStyle::Native)));
63 (new Cast(new NumberLiteral('1'), new CastTarget(CastKind::Char, '2'), true))->render($out);
64
65 self::assertSame('CAST(1 AS CHAR(2) ARRAY)', (new Lexical())->join($out->pieces()));
66 }
67}
68