packages/sql-semantics-mysql/tests/Unit/Statement/Call/Weight/WeightStringParametersTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Call\Weight;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Small;
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\Identifier\Name;
15use SqlSemantics\Platform\MySql\Model\Statement\Type\Known;
16use SqlSemantics\Platform\MySql\Platform;
17use SqlSemantics\Platform\MySql\Rules\Call\TypeClass;
18use SqlSemantics\Platform\MySql\Statement\Call\Weight\WeightStringParameters;
19use SqlSemantics\Platform\MySql\Statement\Literal\Numeral;
20use SqlSemantics\Platform\MySql\Statement\Literal\StringLiteral;
21use SqlSemantics\Platform\MySql\Statement\Name\ColumnUse;
22
23#[CoversClass(WeightStringParameters::class)]
24#[Small]
25final class WeightStringParametersTest extends TestCase
26{
27    public function testDeriveScalarAnswersABinaryString(): void
28    {
29        $platform = new Platform();
30        $profile = $platform->profile('mysql-8.4.7', null, ParameterStyle::Native);
31        $derivation = new Derivation($platform->context($profile, null, [], false));
32        $fact = $derivation->scalar(new WeightStringParameters(new StringLiteral(['x']), new Numeral('0'), new Numeral('1'), new Numeral('2')), $derivation->environment());
33
34        self::assertEquals(new Known(TypeClass::Binary->descriptor()), $fact->type);
35    }
36
37    public function testRenderWritesTheNumbers(): void
38    {
39        $platform = new Platform();
40        $profile = $platform->profile('mysql-8.4.7', null, ParameterStyle::Native);
41        $out = new Output($platform->codec($profile));
42        (new WeightStringParameters(new ColumnUse(new Name('a')), new Numeral('0'), new Numeral('1'), new Numeral('2')))->render($out);
43
44        self::assertSame('WEIGHT_STRING(a, 0, 1, 2)', (new Lexical())->join($out->pieces()));
45    }
46}
47