packages/sql-semantics-mysql/tests/Unit/Statement/Expression/RowTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Expression;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
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\Type\Known;
15use SqlSemantics\Platform\MySql\Model\Statement\Type\Nullability;
16use SqlSemantics\Platform\MySql\Platform;
17use SqlSemantics\Platform\MySql\Statement\Expression\Row;
18use SqlSemantics\Platform\MySql\Statement\Expression\Tuple;
19use SqlSemantics\Platform\MySql\Statement\Literal\NullLiteral;
20use SqlSemantics\Platform\MySql\Statement\Literal\NumberLiteral;
21
22#[CoversClass(Row::class)]
23#[Medium]
24final class RowTest extends TestCase
25{
26    public function testDeriveScalarIsARowOfTheWidth(): void
27    {
28        $platform = new Platform();
29        $derivation = new Derivation($platform->context($platform->profile('mysql-8.4.7', null, ParameterStyle::Native), null, [], true));
30        $fact = $derivation->scalar(new Row([new NumberLiteral('1'), new NullLiteral(), new NumberLiteral('3')]), $derivation->environment());
31
32        self::assertInstanceOf(Known::class, $fact->type);
33        self::assertInstanceOf(Tuple::class, $fact->type->descriptor);
34        self::assertSame([3, Nullability::Nullable], [$fact->type->descriptor->width, $fact->nullability]);
35    }
36
37    public function testRenderWritesTheElementsInParentheses(): void
38    {
39        $platform = new Platform();
40        $out = new Output($platform->codec($platform->profile('mysql-8.4.7', null, ParameterStyle::Native)));
41        (new Row([new NumberLiteral('1'), new NumberLiteral('2')]))->render($out);
42
43        self::assertSame('(1, 2)', (new Lexical())->join($out->pieces()));
44    }
45
46    public function testASingleElementIsRejected(): void
47    {
48        $this->expectExceptionMessage('A row constructor has at least two elements.');
49
50        new Row([new NumberLiteral('1')]);
51    }
52}
53