packages/sql-semantics-mysql/tests/Unit/Statement/Routine/Problem/ProgramProblemTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Routine\Problem;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\DataProvider;
9use PHPUnit\Framework\Attributes\Small;
10use PHPUnit\Framework\TestCase;
11use SqlSemantics\Platform\MySql\Statement\Routine\Problem\ProgramProblem;
12use SqlSemantics\Platform\MySql\Statement\Routine\Problem\ProgramRule;
13
14#[CoversClass(ProgramProblem::class)]
15#[Small]
16final class ProgramProblemTest extends TestCase
17{
18    #[DataProvider('providerMessageWritesTheSubjectIntoTheServerText')]
19    public function testMessageWritesTheSubjectIntoTheServerText(ProgramRule $rule, string $subject, string $expected): void
20    {
21        $problem = new ProgramProblem($rule, $subject);
22
23        self::assertSame($expected, $problem->message());
24        self::assertSame($rule, $problem->rule);
25        self::assertSame($subject, $problem->subject);
26    }
27
28    /**
29     * @return iterable<string, array{ProgramRule, string, string}>
30     */
31    public static function providerMessageWritesTheSubjectIntoTheServerText(): iterable
32    {
33        yield 'a duplicate parameter' => [ProgramRule::DuplicateParameter, 'x', 'Duplicate parameter: x'];
34        yield 'a duplicate variable' => [ProgramRule::DuplicateVariable, 'v', 'Duplicate variable: v'];
35        yield 'a duplicate condition' => [ProgramRule::DuplicateCondition, 'c', 'Duplicate condition: c'];
36        yield 'a duplicate cursor' => [ProgramRule::DuplicateCursor, 'cur', 'Duplicate cursor: cur'];
37        yield 'a redefined label' => [ProgramRule::RedefinedLabel, 'l', 'Redefining label l'];
38        yield 'an end label without match' => [ProgramRule::EndLabelMismatch, 'm', 'End-label m without match'];
39        yield 'a LEAVE without label' => [ProgramRule::LeaveWithoutLabel, 'l', 'LEAVE with no matching label: l'];
40        yield 'an ITERATE without label' => [ProgramRule::IterateWithoutLabel, 'l', 'ITERATE with no matching label: l'];
41        yield 'an undefined condition' => [ProgramRule::UndefinedCondition, 'c', 'Undefined CONDITION: c'];
42        yield 'an undefined cursor' => [ProgramRule::UndefinedCursor, 'cur', 'Undefined CURSOR: cur'];
43        yield 'an undeclared variable' => [ProgramRule::UndeclaredVariable, 'y', 'Undeclared variable: y'];
44        yield 'a missing RETURN' => [ProgramRule::MissingReturn, 'f', 'No RETURN found in FUNCTION f'];
45        yield 'a bad SQLSTATE' => [ProgramRule::BadSqlState, '00000', "Bad SQLSTATE: '00000'"];
46        yield 'a duplicate signal item' => [ProgramRule::DuplicateSignalItem, 'MESSAGE_TEXT', "Duplicate condition information item 'MESSAGE_TEXT'"];
47        yield 'a bad statement' => [ProgramRule::BadStatement, 'LOCK', 'LOCK is not allowed in stored procedures'];
48        yield 'a recursive create' => [ProgramRule::RecursiveCreate, 'PROCEDURE', "Can't create a PROCEDURE from within another stored routine"];
49        yield 'a nested alter or drop' => [ProgramRule::NestedAlterOrDrop, 'FUNCTION', "Can't drop or alter a FUNCTION from within another stored routine"];
50        yield 'a result set' => [ProgramRule::ResultSet, 'trigger', 'Not allowed to return a result set from a trigger'];
51        yield 'a function statement' => [ProgramRule::FunctionStatement, 'Dynamic SQL', 'Dynamic SQL is not allowed in stored function or trigger'];
52    }
53
54    #[DataProvider('providerMessageWritesTheServerTextOfARuleWithoutSubject')]
55    public function testMessageWritesTheServerTextOfARuleWithoutSubject(ProgramRule $rule, string $expected): void
56    {
57        $problem = new ProgramProblem($rule);
58
59        self::assertSame($expected, $problem->message());
60        self::assertNull($problem->subject);
61    }
62
63    /**
64     * @return iterable<string, array{ProgramRule, string}>
65     */
66    public static function providerMessageWritesTheServerTextOfARuleWithoutSubject(): iterable
67    {
68        yield 'a RETURN outside a function' => [ProgramRule::ReturnOutsideFunction, 'RETURN is only allowed in a FUNCTION'];
69        yield 'a declaration after a cursor or handler' => [ProgramRule::DeclarationAfterCursorOrHandler, 'Variable or condition declaration after cursor or handler declaration'];
70        yield 'an error code 0' => [ProgramRule::ZeroErrorCode, "Incorrect CONDITION value: '0'"];
71        yield 'a duplicate handler' => [ProgramRule::DuplicateHandler, 'Duplicate handler declared in the same block'];
72        yield 'a cursor after a handler' => [ProgramRule::CursorAfterHandler, 'Cursor declaration after handler declaration'];
73        yield 'a signal of a condition without SQLSTATE' => [ProgramRule::SignalConditionKind, 'SIGNAL/RESIGNAL can only use a CONDITION defined with SQLSTATE'];
74        yield 'an event recursion' => [ProgramRule::EventRecursion, 'Recursion of EVENT DDL statements is forbidden when body is present'];
75        yield 'a commit in a function' => [ProgramRule::CommitInFunction, 'Explicit or implicit commit is not allowed in stored function or trigger.'];
76        yield 'an update of the OLD row' => [ProgramRule::OldRowUpdate, 'Updating of OLD row is not allowed in trigger'];
77        yield 'an update of the NEW row after' => [ProgramRule::AfterRowUpdate, 'Updating of NEW row is not allowed in after trigger'];
78        yield 'a NEW row in a DELETE trigger' => [ProgramRule::NoNewRow, 'There is no NEW row in on DELETE trigger'];
79    }
80
81    public function testMessageKeepsTheSubjectVerbatim(): void
82    {
83        self::assertSame('Duplicate parameter: Weird %s `name`', (new ProgramProblem(ProgramRule::DuplicateParameter, 'Weird %s `name`'))->message());
84    }
85
86    public function testProblemsWithTheSameRuleAndSubjectAreEqual(): void
87    {
88        self::assertEquals(new ProgramProblem(ProgramRule::UndefinedCursor, 'c'), new ProgramProblem(ProgramRule::UndefinedCursor, 'c'));
89        self::assertNotEquals(new ProgramProblem(ProgramRule::UndefinedCursor, 'c'), new ProgramProblem(ProgramRule::UndefinedCursor, 'd'));
90    }
91
92    public function testARuleAboutANameWithoutSubjectIsRejected(): void
93    {
94        $this->expectExceptionMessage('A problem names a subject exactly when its rule is about a name.');
95
96        new ProgramProblem(ProgramRule::UndefinedCursor);
97    }
98
99    public function testARuleWithoutNameWithASubjectIsRejected(): void
100    {
101        $this->expectExceptionMessage('A problem names a subject exactly when its rule is about a name.');
102
103        new ProgramProblem(ProgramRule::CommitInFunction, 'COMMIT');
104    }
105}
106