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

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Call;
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\Model\Statement\Type\Nullability;
17use SqlSemantics\Platform\MySql\Platform;
18use SqlSemantics\Platform\MySql\Rules\Call\TypeClass;
19use SqlSemantics\Platform\MySql\Statement\Call\CharCall;
20use SqlSemantics\Platform\MySql\Statement\Literal\NumberLiteral;
21use SqlSemantics\Platform\MySql\Statement\Name\CharsetName;
22
23#[CoversClass(CharCall::class)]
24#[Small]
25final class CharCallTest extends TestCase
26{
27    public function testDeriveScalarAnswersABinaryStringWithoutUsing(): 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 CharCall([new NumberLiteral('1')]), $derivation->environment());
33
34        self::assertEquals(new Known(TypeClass::Binary->descriptor()), $fact->type);
35        self::assertSame(Nullability::NotNull, $fact->nullability);
36    }
37
38    public function testRenderWritesTheCharacterSet(): void
39    {
40        $platform = new Platform();
41        $profile = $platform->profile('mysql-8.4.7', null, ParameterStyle::Native);
42        $out = new Output($platform->codec($profile));
43        (new CharCall([new NumberLiteral('1'), new NumberLiteral('1')], new CharsetName(new Name('utf8mb4'))))->render($out);
44
45        self::assertSame('CHAR(1, 1 USING utf8mb4)', (new Lexical())->join($out->pieces()));
46    }
47}
48