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

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Query\Into;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\MySql\Semantics;
11use SqlSemantics\Platform\MySql\Statement\Literal\NumberLiteral;
12use SqlSemantics\Platform\MySql\Statement\Query\Clause\ProgramVariable;
13use SqlSemantics\Platform\MySql\Statement\Query\Into\IntoVariables;
14use SqlSemantics\Platform\MySql\Statement\Query\Problem\CountMismatch;
15use SqlSemantics\Platform\MySql\Statement\Query\Select;
16use SqlSemantics\Platform\MySql\Statement\Variable\UserVariable;
17
18#[CoversClass(IntoVariables::class)]
19#[Medium]
20final class IntoVariablesTest extends TestCase
21{
22    public function testRenderWritesTheTargets(): void
23    {
24        $operation = (new Semantics())->inspect('select a, b from t into @x, y');
25
26        self::assertSame('SELECT a, b FROM t INTO @x, y', $operation->toString());
27        self::assertInstanceOf(Select::class, $operation->statement);
28        self::assertInstanceOf(IntoVariables::class, $operation->statement->into);
29        self::assertInstanceOf(UserVariable::class, $operation->statement->into->targets[0]);
30        self::assertInstanceOf(ProgramVariable::class, $operation->statement->into->targets[1]);
31    }
32
33    public function testADifferentNumberOfColumnsIsReported(): void
34    {
35        $operation = (new Semantics())->inspect('SELECT 1, 2 INTO @x');
36
37        self::assertCount(1, $operation->facts->diagnostics);
38        self::assertInstanceOf(CountMismatch::class, $operation->facts->diagnostics[0]);
39        self::assertSame([2, 1], [$operation->facts->diagnostics[0]->expected, $operation->facts->diagnostics[0]->actual]);
40    }
41
42    public function testAnEmptyTargetListIsRejected(): void
43    {
44        $this->expectExceptionMessage('INTO names at least one variable.');
45
46        new IntoVariables([]);
47    }
48
49    public function testAnotherScalarIsRejected(): void
50    {
51        $this->expectExceptionMessage('An INTO target is a user variable or a stored program variable.');
52
53        new IntoVariables([new NumberLiteral('1')]);
54    }
55}
56