packages/sql-semantics-mysql/tests/Unit/Statement/Utility/Set/SetVariablesTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Utility\Set;
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\Utility\Set\SetVariables;
12use SqlSemantics\Platform\MySql\Statement\Variable\VariableScope;
13
14#[CoversClass(SetVariables::class)]
15#[Medium]
16final class SetVariablesTest extends TestCase
17{
18    public function testScopeOfAnswersTheInheritedScope(): void
19    {
20        $set = (new Semantics())->inspect('SET PERSIST a = 1, b = 2, @@SESSION.c = 3, @d = 4, e = 5');
21        self::assertInstanceOf(SetVariables::class, $set->statement);
22        self::assertSame(VariableScope::Persist, $set->statement->scopeOf(1));
23        self::assertSame(VariableScope::Session, $set->statement->scopeOf(2));
24        self::assertNull($set->statement->scopeOf(3));
25        self::assertSame(VariableScope::Persist, $set->statement->scopeOf(4));
26    }
27
28    public function testScopeOfRefusesAPositionOutsideTheItems(): void
29    {
30        $set = (new Semantics())->inspect('SET a = 1');
31        self::assertInstanceOf(SetVariables::class, $set->statement);
32        $this->expectExceptionMessage('The position is one of the items.');
33        $set->statement->scopeOf(1);
34    }
35
36    public function testDeriveStatementDerivesEveryItem(): void
37    {
38        $set = (new Semantics())->inspect('SET @a = b, @@x = c', []);
39        self::assertInstanceOf(SetVariables::class, $set->statement);
40        self::assertCount(1, $set->facts->diagnostics);
41        self::assertNull($set->shape());
42    }
43
44    public function testRenderWritesTheItemsInOrder(): void
45    {
46        self::assertSame('SET SESSION a = 1, @b = 2', (new Semantics('mysql-5.7.44'))->inspect('set local a = 1, @b := 2')->toString());
47    }
48
49    public function testRefusesAnEmptyList(): void
50    {
51        $this->expectExceptionMessage('SET assigns at least one item.');
52        new SetVariables([]);
53    }
54}
55