packages/sql-semantics-mysql/tests/Unit/Rules/Routine/ProgramScopeTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Rules\Routine;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\MySql\Model\Contract\AnalysisContext;
11use SqlSemantics\Platform\MySql\Model\Resolution\Environment;
12use SqlSemantics\Platform\MySql\Model\Statement\Fact\Diagnostic;
13use SqlSemantics\Platform\MySql\Model\Statement\Identifier\Name;
14use SqlSemantics\Platform\MySql\Rules\Routine\ProgramScope;
15use SqlSemantics\Platform\MySql\Semantics;
16use SqlSemantics\Platform\MySql\Statement\Literal\Numeral;
17use SqlSemantics\Platform\MySql\Statement\Literal\Text;
18use SqlSemantics\Platform\MySql\Statement\Routine\Condition\ErrorCode;
19use SqlSemantics\Platform\MySql\Statement\Routine\Condition\SqlState;
20use SqlSemantics\Platform\MySql\Statement\Routine\Program\ConditionDeclaration;
21use SqlSemantics\Platform\MySql\Statement\Routine\ProgramKind;
22use SqlSemantics\Platform\MySql\Statement\Routine\Trigger\TriggerEvent;
23use SqlSemantics\Platform\MySql\Statement\Routine\Trigger\TriggerTime;
24
25#[CoversClass(ProgramScope::class)]
26#[Medium]
27final class ProgramScopeTest extends TestCase
28{
29    public function testHoldsComparesNamesWithoutRegardToLetterCase(): void
30    {
31        $scope = new ProgramScope(new Environment(new AnalysisContext((new Semantics())->profile(), [new Name('db')])));
32
33        self::assertTrue($scope->holds([new Name('a'), new Name('Label')], new Name('LABEL')));
34        self::assertFalse($scope->holds([new Name('a')], new Name('b')));
35        self::assertFalse($scope->holds([], new Name('a')));
36    }
37
38    public function testVariableIsFalseInAnEnvironmentWithoutVariables(): void
39    {
40        $scope = new ProgramScope(new Environment(new AnalysisContext((new Semantics())->profile(), [new Name('db')])), ProgramKind::Procedure);
41
42        self::assertFalse($scope->variable(new Name('x')));
43    }
44
45    public function testVariableFindsAParameterInAnyLetterCase(): void
46    {
47        $semantics = new Semantics();
48
49        self::assertSame([], $semantics->inspect('CREATE PROCEDURE p(x INT) GET DIAGNOSTICS X = NUMBER')->facts->diagnostics);
50        self::assertSame(['Undeclared variable: y'], array_map(static fn (Diagnostic $problem): string => $problem->message(), $semantics->inspect('CREATE PROCEDURE p(x INT) GET DIAGNOSTICS y = NUMBER')->facts->diagnostics));
51    }
52
53    public function testConditionFindsTheInnermostDeclarationOfTheName(): void
54    {
55        $outer = new ConditionDeclaration(new Name('c'), new ErrorCode(new Numeral('1051')));
56        $inner = new ConditionDeclaration(new Name('C'), new SqlState(new Text('45000')));
57        $scope = new ProgramScope(new Environment(new AnalysisContext((new Semantics())->profile(), [new Name('db')])), ProgramKind::Procedure, conditions: [$outer, $inner]);
58
59        self::assertSame($inner, $scope->condition(new Name('c')));
60        self::assertNull($scope->condition(new Name('d')));
61    }
62
63    public function testConditionLetsAnInnerBlockHideAnOuterCondition(): void
64    {
65        $semantics = new Semantics();
66        $hidden = $semantics->inspect("CREATE PROCEDURE p() BEGIN DECLARE c CONDITION FOR 1051; BEGIN DECLARE c CONDITION FOR SQLSTATE '45000'; SIGNAL c; END; END");
67        $hiding = $semantics->inspect("CREATE PROCEDURE p() BEGIN DECLARE c CONDITION FOR SQLSTATE '45000'; BEGIN DECLARE c CONDITION FOR 1051; SIGNAL c; END; END");
68
69        self::assertSame([], $hidden->facts->diagnostics);
70        self::assertSame(['SIGNAL/RESIGNAL can only use a CONDITION defined with SQLSTATE'], array_map(static fn (Diagnostic $problem): string => $problem->message(), $hiding->facts->diagnostics));
71    }
72
73    public function testLabeledAddsTheLabelOfABlockOrALoop(): void
74    {
75        $scope = new ProgramScope(new Environment(new AnalysisContext((new Semantics())->profile(), [new Name('db')])), ProgramKind::Procedure);
76        $block = $scope->labeled(new Name('b'), false);
77        $loop = $block->labeled(new Name('l'), true);
78
79        self::assertEquals([new Name('b')], $block->blocks);
80        self::assertSame([], $block->loops);
81        self::assertEquals([new Name('b')], $loop->blocks);
82        self::assertEquals([new Name('l')], $loop->loops);
83        self::assertSame(ProgramKind::Procedure, $loop->kind);
84        self::assertSame($scope->environment, $loop->environment);
85    }
86
87    public function testLabeledKeepsTheScopeWithoutALabel(): void
88    {
89        $scope = new ProgramScope(new Environment(new AnalysisContext((new Semantics())->profile(), [new Name('db')])));
90
91        self::assertSame($scope, $scope->labeled(null, true));
92    }
93
94    public function testDeclaredReplacesTheEnvironmentConditionsAndCursors(): void
95    {
96        $context = new AnalysisContext((new Semantics())->profile(), [new Name('db')]);
97        $condition = new ConditionDeclaration(new Name('c'), new ErrorCode(new Numeral('1051')));
98        $scope = (new ProgramScope(new Environment($context), ProgramKind::Trigger, time: TriggerTime::Before, event: TriggerEvent::Insert))->labeled(new Name('b'), false);
99        $inner = new Environment($context, $scope->environment);
100        $declared = $scope->declared($inner, [$condition], [new Name('cur')]);
101
102        self::assertSame($inner, $declared->environment);
103        self::assertSame([$condition], $declared->conditions);
104        self::assertEquals([new Name('cur')], $declared->cursors);
105        self::assertEquals([new Name('b')], $declared->blocks);
106        self::assertSame(ProgramKind::Trigger, $declared->kind);
107        self::assertSame(TriggerTime::Before, $declared->time);
108        self::assertSame(TriggerEvent::Insert, $declared->event);
109    }
110
111    public function testHandlerSeesNoLabelOutsideTheHandler(): void
112    {
113        $condition = new ConditionDeclaration(new Name('c'), new ErrorCode(new Numeral('1051')));
114        $scope = (new ProgramScope(new Environment(new AnalysisContext((new Semantics())->profile(), [new Name('db')])), ProgramKind::Procedure, conditions: [$condition], cursors: [new Name('cur')]))->labeled(new Name('b'), false)->labeled(new Name('l'), true);
115        $handler = $scope->handler();
116
117        self::assertSame([], $handler->blocks);
118        self::assertSame([], $handler->loops);
119        self::assertSame([$condition], $handler->conditions);
120        self::assertEquals([new Name('cur')], $handler->cursors);
121        self::assertSame(['LEAVE with no matching label: l'], array_map(static fn (Diagnostic $problem): string => $problem->message(), (new Semantics())->inspect('CREATE PROCEDURE p() l: BEGIN DECLARE CONTINUE HANDLER FOR SQLEXCEPTION LEAVE l; END')->facts->diagnostics));
122    }
123}
124