packages/sql-semantics-mysql/tests/Unit/Rules/Expression/CastResultTest.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\Model\Statement\Type\Choice;
11use SqlSemantics\Platform\MySql\Model\Statement\Type\Known;
12use SqlSemantics\Platform\MySql\Model\Statement\Type\Nullability;
13use SqlSemantics\Platform\MySql\Rules\Expression\CastResult;
14use SqlSemantics\Platform\MySql\Statement\Type\CastTarget;
15use SqlSemantics\Platform\MySql\Statement\Type\Decimal;
16use SqlSemantics\Platform\MySql\Statement\Type\Floating;
17use SqlSemantics\Platform\MySql\Statement\Type\Integral;
18use SqlSemantics\Platform\MySql\Statement\Type\Kind\CastKind;
19use SqlSemantics\Platform\MySql\Statement\Type\Kind\FloatingKind;
20use SqlSemantics\Platform\MySql\Statement\Type\Kind\IntegralKind;
21use SqlSemantics\Platform\MySql\Statement\Type\Kind\NumericModifier;
22use SqlSemantics\Platform\MySql\Statement\Type\Kind\SpatialKind;
23use SqlSemantics\Platform\MySql\Statement\Type\Spatial;
24
25#[CoversClass(CastResult::class)]
26#[Small]
27final class CastResultTest extends TestCase
28{
29    public function testTypeFollowsTheTarget(): void
30    {
31        $results = new CastResult();
32
33        self::assertEquals(
34            [new Known(new Integral(IntegralKind::BigInt, null, [NumericModifier::Unsigned])), new Known(new Decimal('5', '2')), new Known(new Floating(FloatingKind::Double)), new Known(new Floating(FloatingKind::Float)), new Known(new Spatial(SpatialKind::MultiPoint)), new Choice([new Floating(FloatingKind::Float), new Floating(FloatingKind::Double)])],
35            [$results->type(new CastTarget(CastKind::Unsigned)), $results->type(new CastTarget(CastKind::Decimal, '5', '2')), $results->type(new CastTarget(CastKind::Float, '30')), $results->type(new CastTarget(CastKind::Float)), $results->type(new CastTarget(CastKind::MultiPoint)), $results->type(new CastTarget(CastKind::Real))],
36        );
37    }
38
39    public function testNullabilityMakesATemporalCastNullable(): void
40    {
41        $results = new CastResult();
42
43        self::assertSame([Nullability::Nullable, Nullability::NotNull], [$results->nullability(new CastTarget(CastKind::Year), Nullability::NotNull), $results->nullability(new CastTarget(CastKind::Signed), Nullability::NotNull)]);
44    }
45}
46