packages/sql-semantics-mysql/tests/Unit/Rules/Expression/NumericContextTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Rules\Expression;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Small;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\MySql\Rules\Expression\NumericClass;
11use SqlSemantics\Platform\MySql\Rules\Expression\NumericContext;
12use SqlSemantics\Platform\MySql\Statement\Expression\Grouped;
13use SqlSemantics\Platform\MySql\Statement\Literal\NumberLiteral;
14use SqlSemantics\Platform\MySql\Statement\Literal\Radix;
15use SqlSemantics\Platform\MySql\Statement\Literal\RadixLiteral;
16use SqlSemantics\Platform\MySql\Statement\Type\Binary;
17use SqlSemantics\Platform\MySql\Statement\Type\Character;
18use SqlSemantics\Platform\MySql\Statement\Type\Decimal;
19use SqlSemantics\Platform\MySql\Statement\Type\Elementary;
20use SqlSemantics\Platform\MySql\Statement\Type\Integral;
21use SqlSemantics\Platform\MySql\Statement\Type\Kind\BinaryKind;
22use SqlSemantics\Platform\MySql\Statement\Type\Kind\CharacterKind;
23use SqlSemantics\Platform\MySql\Statement\Type\Kind\ElementaryKind;
24use SqlSemantics\Platform\MySql\Statement\Type\Kind\IntegralKind;
25use SqlSemantics\Platform\MySql\Statement\Type\Kind\NumericModifier;
26use SqlSemantics\Platform\MySql\Statement\Type\Kind\TemporalKind;
27use SqlSemantics\Platform\MySql\Statement\Type\Temporal;
28
29#[CoversClass(NumericContext::class)]
30#[Small]
31final class NumericContextTest extends TestCase
32{
33    public function testClassifyFollowsTheTypeAndTreatsAHexadecimalLiteralAsAnUnsignedInteger(): void
34    {
35        $context = new NumericContext();
36        $number = new NumberLiteral('1');
37
38        self::assertSame(
39            [NumericClass::Unsigned, NumericClass::Signed, NumericClass::Decimal, NumericClass::Double, NumericClass::Double, NumericClass::Unsigned],
40            [
41                $context->classify($number, new Integral(IntegralKind::Int, null, [NumericModifier::Unsigned])),
42                $context->classify($number, new Integral(IntegralKind::Int)),
43                $context->classify($number, new Decimal()),
44                $context->classify($number, new Character(CharacterKind::VarChar)),
45                $context->classify($number, null),
46                $context->classify(new Grouped(new RadixLiteral(Radix::Hexadecimal, '10')), new Binary(BinaryKind::VarBinary)),
47            ],
48        );
49    }
50
51    public function testTemporalIsAnIntegerWithoutFractionalSeconds(): void
52    {
53        $context = new NumericContext();
54
55        self::assertSame([NumericClass::Signed, NumericClass::Signed, NumericClass::Decimal], [$context->temporal(new Temporal(TemporalKind::Date)), $context->temporal(new Temporal(TemporalKind::DateTime, '0')), $context->temporal(new Temporal(TemporalKind::Time, '3'))]);
56    }
57
58    public function testElementaryClassifiesBoolBitAndJson(): void
59    {
60        $context = new NumericContext();
61
62        self::assertSame([NumericClass::Signed, NumericClass::Unsigned, NumericClass::Double], [$context->elementary(new Elementary(ElementaryKind::Boolean)), $context->elementary(new Elementary(ElementaryKind::Bit)), $context->elementary(new Elementary(ElementaryKind::Json))]);
63    }
64}
65