packages/sql-semantics-mysql/tests/Unit/Statement/Literal/SignedLiteralTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Literal;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Small;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\MySql\Lowering\Lowering;
11use SqlSemantics\Platform\MySql\Model\Construction\Derivation;
12use SqlSemantics\Platform\MySql\Model\Contract\GrammarRelease;
13use SqlSemantics\Platform\MySql\Model\Contract\ParameterStyle;
14use SqlSemantics\Platform\MySql\Model\Lowering\Leaves;
15use SqlSemantics\Platform\MySql\Model\Rendering\Output;
16use SqlSemantics\Platform\MySql\Model\Rendering\PieceKind;
17use SqlSemantics\Platform\MySql\Model\Statement\Type\Known;
18use SqlSemantics\Platform\MySql\Model\Statement\Type\Nullability;
19use SqlSemantics\Platform\MySql\Platform;
20use SqlSemantics\Platform\MySql\Rendering\Codec;
21use SqlSemantics\Platform\MySql\Statement\Literal\NumberLiteral;
22use SqlSemantics\Platform\MySql\Statement\Literal\SignedLiteral;
23use SqlSemantics\Platform\MySql\Statement\Type\Decimal;
24use SqlSemantics\Platform\MySql\Statement\Type\Floating;
25use SqlSemantics\Platform\MySql\Statement\Type\Integral;
26use SqlSemantics\Platform\MySql\Statement\Type\Kind\FloatingKind;
27use SqlSemantics\Platform\MySql\Statement\Type\Kind\IntegralKind;
28
29#[CoversClass(SignedLiteral::class)]
30#[Small]
31final class SignedLiteralTest extends TestCase
32{
33    public function testDeriveScalarKeepsTheTypeOfTheNumber(): void
34    {
35        $platform = new Platform();
36        $derivation = new Derivation($platform->context($platform->profile(null, null, ParameterStyle::Native), null, [], false));
37        $integer = (new SignedLiteral(true, new NumberLiteral('5')))->deriveScalar($derivation, $derivation->environment());
38        $decimal = (new SignedLiteral(false, new NumberLiteral('1.5')))->deriveScalar($derivation, $derivation->environment());
39        $float = (new SignedLiteral(true, new NumberLiteral('1e3')))->deriveScalar($derivation, $derivation->environment());
40
41        self::assertInstanceOf(Known::class, $integer->type);
42        self::assertInstanceOf(Integral::class, $integer->type->descriptor);
43        self::assertSame(IntegralKind::BigInt, $integer->type->descriptor->kind);
44        self::assertFalse($integer->type->descriptor->unsigned());
45        self::assertSame(Nullability::NotNull, $integer->nullability);
46        self::assertInstanceOf(Known::class, $decimal->type);
47        self::assertInstanceOf(Decimal::class, $decimal->type->descriptor);
48        self::assertInstanceOf(Known::class, $float->type);
49        self::assertInstanceOf(Floating::class, $float->type->descriptor);
50        self::assertSame(FloatingKind::Double, $float->type->descriptor->kind);
51    }
52
53    public function testDeriveScalarAnswersDecimalForANegatedIntegerBeyondTheSignedRange(): void
54    {
55        $platform = new Platform();
56        $derivation = new Derivation($platform->context($platform->profile(null, null, ParameterStyle::Native), null, [], false));
57        $smallest = (new SignedLiteral(true, new NumberLiteral('9223372036854775808')))->deriveScalar($derivation, $derivation->environment());
58        $beyond = (new SignedLiteral(true, new NumberLiteral('9223372036854775809')))->deriveScalar($derivation, $derivation->environment());
59        $padded = (new SignedLiteral(true, new NumberLiteral('0009223372036854775808')))->deriveScalar($derivation, $derivation->environment());
60
61        self::assertInstanceOf(Known::class, $smallest->type);
62        self::assertInstanceOf(Decimal::class, $smallest->type->descriptor);
63        self::assertSame(Nullability::NotNull, $smallest->nullability);
64        self::assertInstanceOf(Known::class, $beyond->type);
65        self::assertInstanceOf(Decimal::class, $beyond->type->descriptor);
66        self::assertInstanceOf(Known::class, $padded->type);
67        self::assertInstanceOf(Decimal::class, $padded->type->descriptor);
68    }
69
70    public function testDeriveScalarKeepsBigIntForTheLargestNegativeIntegerThatFits(): void
71    {
72        $platform = new Platform();
73        $derivation = new Derivation($platform->context($platform->profile(null, null, ParameterStyle::Native), null, [], false));
74        $fact = (new SignedLiteral(true, new NumberLiteral('9223372036854775807')))->deriveScalar($derivation, $derivation->environment());
75
76        self::assertInstanceOf(Known::class, $fact->type);
77        self::assertInstanceOf(Integral::class, $fact->type->descriptor);
78        self::assertSame(IntegralKind::BigInt, $fact->type->descriptor->kind);
79        self::assertFalse($fact->type->descriptor->unsigned());
80    }
81
82    public function testDeriveScalarKeepsTheUnsignedTypeOfAPositiveIntegerBeyondTheSignedRange(): void
83    {
84        $platform = new Platform();
85        $derivation = new Derivation($platform->context($platform->profile(null, null, ParameterStyle::Native), null, [], false));
86        $fact = (new SignedLiteral(false, new NumberLiteral('9223372036854775808')))->deriveScalar($derivation, $derivation->environment());
87
88        self::assertInstanceOf(Known::class, $fact->type);
89        self::assertInstanceOf(Integral::class, $fact->type->descriptor);
90        self::assertSame(IntegralKind::BigInt, $fact->type->descriptor->kind);
91        self::assertTrue($fact->type->descriptor->unsigned());
92    }
93
94    public function testRenderWritesTheSignAndTheNumber(): void
95    {
96        $out = new Output(new Codec(GrammarRelease::MySql847));
97        (new SignedLiteral(true, new NumberLiteral('5')))->render($out);
98        (new SignedLiteral(false, new NumberLiteral('1.5')))->render($out);
99        $pieces = $out->pieces();
100
101        self::assertSame([PieceKind::Symbol, PieceKind::Literal, PieceKind::Symbol, PieceKind::Literal], array_column($pieces, 'kind'));
102        self::assertSame(['-', '5', '+', '1.5'], array_column($pieces, 'text'));
103    }
104
105    public function testLoweredDefaultKeepsTheSignAndTheUnsignedText(): void
106    {
107        $platform = new Platform();
108        $profile = $platform->profile('mysql-8.4.7', null, ParameterStyle::Native);
109        $parser = $platform->parser($profile);
110        $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
111        $negative = $lowering->literals->literal($parser->parse('CREATE TABLE t (c INT DEFAULT -5)')->find('signed_literal')[0]);
112        $positive = $lowering->literals->literal($parser->parse('CREATE TABLE t (c INT DEFAULT +1.5)')->find('signed_literal')[0]);
113
114        self::assertInstanceOf(SignedLiteral::class, $negative);
115        self::assertTrue($negative->negative);
116        self::assertSame('5', $negative->number->text);
117        self::assertInstanceOf(SignedLiteral::class, $positive);
118        self::assertFalse($positive->negative);
119        self::assertSame('1.5', $positive->number->text);
120    }
121}
122