packages/sql-semantics-mysql/tests/Unit/Statement/Query/ValuesQueryTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Query;
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\Rendering\Lexical;
12use SqlSemantics\Platform\MySql\Model\Rendering\Output;
13use SqlSemantics\Platform\MySql\Model\Statement\Shape\Field;
14use SqlSemantics\Platform\MySql\Model\Statement\Type\Nullability;
15use SqlSemantics\Platform\MySql\Rendering\Codec;
16use SqlSemantics\Platform\MySql\Semantics;
17use SqlSemantics\Platform\MySql\Statement\Literal\NullLiteral;
18use SqlSemantics\Platform\MySql\Statement\Literal\NumberLiteral;
19use SqlSemantics\Platform\MySql\Statement\Query\Problem\CountedList;
20use SqlSemantics\Platform\MySql\Statement\Query\Problem\CountMismatch;
21use SqlSemantics\Platform\MySql\Statement\Query\ValueRow;
22use SqlSemantics\Platform\MySql\Statement\Query\ValuesQuery;
23
24#[CoversClass(ValuesQuery::class)]
25#[Medium]
26final class ValuesQueryTest extends TestCase
27{
28    public function testDeriveStatementRecordsTheRowsAsTheOutput(): void
29    {
30        $semantics = new Semantics();
31        $values = new ValuesQuery([new ValueRow([new NumberLiteral('1')])]);
32        $derivation = new Derivation($semantics->context());
33        $derivation->statement($values);
34
35        self::assertNotNull($derivation->facts()->output);
36        self::assertSame('column_0', $derivation->facts()->output->fields()?->at(0)->name?->value);
37    }
38
39    public function testDeriveQueryNamesTheColumnsAndCombinesTheRows(): void
40    {
41        $semantics = new Semantics();
42        $values = new ValuesQuery([new ValueRow([new NumberLiteral('1'), new NumberLiteral('2')]), new ValueRow([new NullLiteral(), new NumberLiteral('3')])]);
43        $derivation = new Derivation($semantics->context());
44        $fact = $derivation->query($values, $derivation->environment());
45
46        self::assertSame(['column_0', 'column_1'], array_map(static fn (Field $field): ?string => $field->name?->value, $fact->fields()->items ?? []));
47        self::assertNotNull($fact->fields());
48        self::assertSame(Nullability::Nullable, $fact->fields()->at(0)->nullability);
49        self::assertSame(Nullability::NotNull, $fact->fields()->at(1)->nullability);
50        self::assertSame([], $derivation->facts()->diagnostics);
51    }
52
53    public function testDeriveQueryReportsRowsOfDifferentLengths(): void
54    {
55        $semantics = new Semantics();
56        $values = new ValuesQuery([new ValueRow([new NumberLiteral('1'), new NumberLiteral('2')]), new ValueRow([new NumberLiteral('3')])]);
57        $derivation = new Derivation($semantics->context());
58        $derivation->query($values, $derivation->environment());
59
60        self::assertCount(1, $derivation->facts()->diagnostics);
61        self::assertInstanceOf(CountMismatch::class, $derivation->facts()->diagnostics[0]);
62        self::assertSame(CountedList::ValueRows, $derivation->facts()->diagnostics[0]->list);
63    }
64
65    public function testRenderWritesTheRows(): void
66    {
67        $semantics = new Semantics();
68        $values = new ValuesQuery([new ValueRow([new NumberLiteral('1')]), new ValueRow([new NumberLiteral('2')])]);
69        $out = new Output(new Codec($semantics->profile()->grammar));
70        $values->render($out);
71
72        self::assertSame('VALUES ROW(1), ROW(2)', (new Lexical())->join($out->pieces()));
73    }
74
75    public function testAStatementWithoutRowsIsRejected(): void
76    {
77        $this->expectExceptionMessage('VALUES holds at least one row.');
78
79        new ValuesQuery([]);
80    }
81}
82