packages/sql-semantics-mysql/tests/Unit/Rules/Routine/ReturnSearchTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Rules\Routine;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\DataProvider;
9use PHPUnit\Framework\Attributes\Small;
10use PHPUnit\Framework\TestCase;
11use SqlSemantics\Platform\MySql\Model\Statement\Identifier\Name;
12use SqlSemantics\Platform\MySql\Model\Statement\Node;
13use SqlSemantics\Platform\MySql\Rules\Routine\ReturnSearch;
14use SqlSemantics\Platform\MySql\Statement\Literal\NumberLiteral;
15use SqlSemantics\Platform\MySql\Statement\Routine\Condition\ConditionClass;
16use SqlSemantics\Platform\MySql\Statement\Routine\Condition\GeneralCondition;
17use SqlSemantics\Platform\MySql\Statement\Routine\Program\Block;
18use SqlSemantics\Platform\MySql\Statement\Routine\Program\Flow\ConditionalBranch;
19use SqlSemantics\Platform\MySql\Statement\Routine\Program\Flow\IfStatement;
20use SqlSemantics\Platform\MySql\Statement\Routine\Program\Flow\Leave;
21use SqlSemantics\Platform\MySql\Statement\Routine\Program\Flow\Loop;
22use SqlSemantics\Platform\MySql\Statement\Routine\Program\Flow\RepeatLoop;
23use SqlSemantics\Platform\MySql\Statement\Routine\Program\Flow\ReturnStatement;
24use SqlSemantics\Platform\MySql\Statement\Routine\Program\Flow\SearchedCase;
25use SqlSemantics\Platform\MySql\Statement\Routine\Program\Flow\SimpleCase;
26use SqlSemantics\Platform\MySql\Statement\Routine\Program\Flow\WhileLoop;
27use SqlSemantics\Platform\MySql\Statement\Routine\Program\HandlerAction;
28use SqlSemantics\Platform\MySql\Statement\Routine\Program\HandlerDeclaration;
29use SqlSemantics\Platform\MySql\Statement\Routine\Program\VariableDeclaration;
30use SqlSemantics\Platform\MySql\Statement\Type\Integral;
31use SqlSemantics\Platform\MySql\Statement\Type\Kind\IntegralKind;
32use SqlSemantics\Platform\MySql\Statement\Utility\Set\SetVariables;
33use SqlSemantics\Platform\MySql\Statement\Utility\Set\UserAssignment;
34use SqlSemantics\Platform\MySql\Statement\Variable\UserVariable;
35
36#[CoversClass(ReturnSearch::class)]
37#[Small]
38final class ReturnSearchTest extends TestCase
39{
40 #[DataProvider('providerFoundFindsANestedReturn')]
41 public function testFoundFindsANestedReturn(Node $statement): void
42 {
43 self::assertTrue((new ReturnSearch())->found($statement));
44 }
45
46 /**
47 * @return iterable<string, array{Node}>
48 */
49 public static function providerFoundFindsANestedReturn(): iterable
50 {
51 $set = new SetVariables([new UserAssignment(new UserVariable(new Name('a')), new NumberLiteral('1'))]);
52 $return = new ReturnStatement(new NumberLiteral('1'));
53
54 yield 'a RETURN itself' => [$return];
55 yield 'a RETURN in a block' => [new Block([], [$set, $return])];
56 yield 'a RETURN in a handler of a block' => [new Block([new HandlerDeclaration(HandlerAction::Continue, [new GeneralCondition(ConditionClass::SqlException)], $return)], [$set])];
57 yield 'a RETURN in a branch of IF' => [new IfStatement([new ConditionalBranch(new NumberLiteral('1'), [$set]), new ConditionalBranch(new NumberLiteral('0'), [$return])])];
58 yield 'a RETURN in the ELSE of IF' => [new IfStatement([new ConditionalBranch(new NumberLiteral('1'), [$set])], [$return])];
59 yield 'a RETURN in a simple CASE' => [new SimpleCase(new NumberLiteral('1'), [new ConditionalBranch(new NumberLiteral('1'), [$return])])];
60 yield 'a RETURN in the ELSE of a searched CASE' => [new SearchedCase([new ConditionalBranch(new NumberLiteral('1'), [$set])], [$return])];
61 yield 'a RETURN in a LOOP' => [new Loop([$return])];
62 yield 'a RETURN in a WHILE loop' => [new WhileLoop(new NumberLiteral('1'), [$return])];
63 yield 'a RETURN in a REPEAT loop' => [new RepeatLoop(new NumberLiteral('1'), [$set, $return])];
64 yield 'a RETURN deep in nested statements' => [new Block([], [new Loop([new IfStatement([new ConditionalBranch(new NumberLiteral('1'), [new Block([], [$return])])])])])];
65 }
66
67 #[DataProvider('providerFoundIsFalseWithoutReturn')]
68 public function testFoundIsFalseWithoutReturn(Node $statement): void
69 {
70 self::assertFalse((new ReturnSearch())->found($statement));
71 }
72
73 /**
74 * @return iterable<string, array{Node}>
75 */
76 public static function providerFoundIsFalseWithoutReturn(): iterable
77 {
78 $set = new SetVariables([new UserAssignment(new UserVariable(new Name('a')), new NumberLiteral('1'))]);
79
80 yield 'an SQL statement' => [$set];
81 yield 'an empty block' => [new Block()];
82 yield 'a block with a variable declaration' => [new Block([new VariableDeclaration([new Name('v')], new Integral(IntegralKind::Int))], [$set])];
83 yield 'a handler without RETURN' => [new Block([new HandlerDeclaration(HandlerAction::Exit, [new GeneralCondition(ConditionClass::NotFound)], $set)])];
84 yield 'conditionals and loops without RETURN' => [new Block([], [new IfStatement([new ConditionalBranch(new NumberLiteral('1'), [$set])], [$set]), new WhileLoop(new NumberLiteral('1'), [$set]), new Loop([new Leave(new Name('l'))], new Name('l'))])];
85 }
86
87 public function testNestedAnswersTheStatementsAndHandlerStatementsOfABlock(): void
88 {
89 $set = new SetVariables([new UserAssignment(new UserVariable(new Name('a')), new NumberLiteral('1'))]);
90 $return = new ReturnStatement(new NumberLiteral('1'));
91 $block = new Block([new HandlerDeclaration(HandlerAction::Continue, [new GeneralCondition(ConditionClass::SqlWarning)], $return)], [$set]);
92
93 self::assertSame([$set, $return], (new ReturnSearch())->nested($block));
94 }
95
96 public function testNestedAnswersTheElseStatementsBeforeTheBranchStatements(): void
97 {
98 $first = new SetVariables([new UserAssignment(new UserVariable(new Name('a')), new NumberLiteral('1'))]);
99 $second = new SetVariables([new UserAssignment(new UserVariable(new Name('b')), new NumberLiteral('2'))]);
100 $otherwise = new ReturnStatement(new NumberLiteral('3'));
101 $branches = [new ConditionalBranch(new NumberLiteral('1'), [$first]), new ConditionalBranch(new NumberLiteral('2'), [$second])];
102 $search = new ReturnSearch();
103
104 self::assertSame([$otherwise, $first, $second], $search->nested(new IfStatement($branches, [$otherwise])));
105 self::assertSame([$otherwise, $first, $second], $search->nested(new SimpleCase(new NumberLiteral('1'), $branches, [$otherwise])));
106 self::assertSame([$first, $second], $search->nested(new SearchedCase($branches)));
107 }
108
109 public function testNestedAnswersTheStatementsOfALoop(): void
110 {
111 $return = new ReturnStatement(new NumberLiteral('1'));
112 $leave = new Leave(new Name('l'));
113 $search = new ReturnSearch();
114
115 self::assertSame([$leave, $return], $search->nested(new Loop([$leave, $return], new Name('l'))));
116 self::assertSame([$return], $search->nested(new WhileLoop(new NumberLiteral('1'), [$return])));
117 self::assertSame([$return], $search->nested(new RepeatLoop(new NumberLiteral('1'), [$return])));
118 }
119
120 public function testNestedAnswersNothingForAStatementWithoutNestedStatements(): void
121 {
122 $search = new ReturnSearch();
123
124 self::assertSame([], $search->nested(new ReturnStatement(new NumberLiteral('1'))));
125 self::assertSame([], $search->nested(new SetVariables([new UserAssignment(new UserVariable(new Name('a')), new NumberLiteral('1'))])));
126 self::assertSame([], $search->nested(new NumberLiteral('1')));
127 }
128}
129