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

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Routine;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\DataProvider;
9use PHPUnit\Framework\Attributes\Medium;
10use PHPUnit\Framework\TestCase;
11use SqlSemantics\Platform\MySql\Model\Rendering\Lexical;
12use SqlSemantics\Platform\MySql\Model\Rendering\Output;
13use SqlSemantics\Platform\MySql\Model\Statement\Fact\Diagnostic;
14use SqlSemantics\Platform\MySql\Model\Statement\Identifier\Name;
15use SqlSemantics\Platform\MySql\Model\Statement\Identifier\QualifiedName;
16use SqlSemantics\Platform\MySql\Rendering\Codec;
17use SqlSemantics\Platform\MySql\Semantics;
18use SqlSemantics\Platform\MySql\Statement\Literal\EscapeRule;
19use SqlSemantics\Platform\MySql\Statement\Literal\Radix;
20use SqlSemantics\Platform\MySql\Statement\Literal\Text;
21use SqlSemantics\Platform\MySql\Statement\Routine\AlterEvent;
22use SqlSemantics\Platform\MySql\Statement\Routine\Event\EventStatus;
23use SqlSemantics\Platform\MySql\Statement\Routine\Parameter;
24use SqlSemantics\Platform\MySql\Statement\Type\Integral;
25use SqlSemantics\Platform\MySql\Statement\Type\Kind\IntegralKind;
26
27#[CoversClass(AlterEvent::class)]
28#[Medium]
29final class AlterEventTest extends TestCase
30{
31    public function testDeriveStatementResolvesTheLocalVariablesOfTheBody(): void
32    {
33        $semantics = new Semantics();
34        $table = $semantics->inspect('CREATE TABLE t (a INT, b INT)');
35        $alter = $semantics->inspect('ALTER EVENT e ON SCHEDULE EVERY 1 HOUR DO BEGIN DECLARE v INT DEFAULT 1; UPDATE t SET b = v WHERE a = v; END', [$table]);
36
37        self::assertSame([], $alter->facts->diagnostics);
38    }
39
40    public function testDeriveStatementReportsUnknownNamesOfTheScheduleAndTheBody(): void
41    {
42        $semantics = new Semantics();
43        $table = $semantics->inspect('CREATE TABLE t (a INT, b INT)');
44        $alter = $semantics->inspect('ALTER EVENT e ON SCHEDULE AT a DO DELETE FROM t WHERE zz = 1', [$table]);
45
46        self::assertSame(['Column a does not exist.', 'Column zz does not exist.'], array_map(static fn (Diagnostic $diagnostic): string => $diagnostic->message(), $alter->facts->diagnostics));
47    }
48
49    public function testDeriveStatementAcceptsAnAlterationWithoutClauses(): void
50    {
51        $alter = (new Semantics())->inspect('ALTER EVENT e');
52        $statement = $alter->statement;
53        self::assertInstanceOf(AlterEvent::class, $statement);
54
55        self::assertNull($statement->schedule);
56        self::assertNull($statement->body);
57        self::assertSame([], $alter->facts->diagnostics);
58    }
59
60    /**
61     * @return iterable<string, array{string, string}>
62     */
63    public static function providerDeriveStatementReportsAStatementAStoredProgramMayNotContain(): iterable
64    {
65        yield 'ALTER EVENT with a body' => ['ALTER EVENT f DO SELECT 1', 'Recursion of EVENT DDL statements is forbidden when body is present'];
66        yield 'LOCK TABLES in a block' => ['BEGIN DECLARE v INT; SET @x = v; LOCK TABLES t READ; END', 'LOCK is not allowed in stored procedures'];
67    }
68
69    #[DataProvider('providerDeriveStatementReportsAStatementAStoredProgramMayNotContain')]
70    public function testDeriveStatementReportsAStatementAStoredProgramMayNotContain(string $body, string $message): void
71    {
72        $alter = (new Semantics())->inspect('ALTER EVENT e DO ' . $body);
73
74        self::assertSame([$message], array_map(static fn (Diagnostic $diagnostic): string => $diagnostic->message(), $alter->facts->diagnostics));
75    }
76
77    public function testDeriveStatementAcceptsANestedAlterationWithoutABody(): void
78    {
79        self::assertSame([], (new Semantics())->inspect('ALTER EVENT e DO ALTER EVENT f ENABLE')->facts->diagnostics);
80    }
81
82    /**
83     * @return iterable<string, array{string, string, string}>
84     */
85    public static function providerRenderWritesTheChanges(): iterable
86    {
87        yield 'MySQL 5.6 status only' => ['mysql-5.6.51', 'alter event e disable on slave', 'ALTER EVENT e DISABLE ON SLAVE'];
88        yield 'MySQL 5.7 rename' => ['mysql-5.7.44', 'alter event e rename to shop.f enable', 'ALTER EVENT e RENAME TO shop.f ENABLE'];
89        yield 'MySQL 8.0 every clause' => [
90            'mysql-8.0.44',
91            "alter definer = current_user event shop.e on schedule at '2030-01-01' + interval 1 day on completion not preserve rename to shop.f enable comment 'n' do update t set a = 1",
92            "ALTER DEFINER = CURRENT_USER EVENT shop.e ON SCHEDULE AT '2030-01-01' + INTERVAL 1 DAY ON COMPLETION NOT PRESERVE RENAME TO shop.f ENABLE COMMENT 'n' DO UPDATE t SET a = 1",
93        ];
94        yield 'MySQL 8.4 DISABLE ON REPLICA' => ['mysql-8.4.7', 'alter event e disable on replica', 'ALTER EVENT e DISABLE ON REPLICA'];
95        yield 'MySQL 9.1 nothing' => ['mysql-9.1.0', 'alter definer = current_user event e', 'ALTER DEFINER = CURRENT_USER EVENT e'];
96    }
97
98    #[DataProvider('providerRenderWritesTheChanges')]
99    public function testRenderWritesTheChanges(string $release, string $sql, string $expected): void
100    {
101        self::assertSame($expected, (new Semantics($release))->inspect($sql)->toString());
102    }
103
104    public function testRenderWritesAConstructedAlteration(): void
105    {
106        $semantics = new Semantics();
107        $out = new Output(new Codec($semantics->profile()->grammar));
108        (new AlterEvent(new QualifiedName(new Name('e')), null, null, null, EventStatus::Disable))->render($out);
109
110        self::assertSame('ALTER EVENT e DISABLE', (new Lexical())->join($out->pieces()));
111        self::assertSame('ALTER EVENT e', $semantics->inspect('ALTER EVENT e')->toString());
112    }
113
114    public function testANewNameWithACatalogIsRejected(): void
115    {
116        $this->expectExceptionMessage('An event name has at most a database qualifier.');
117
118        new AlterEvent(new QualifiedName(new Name('e')), null, null, new QualifiedName(new Name('f'), new Name('shop'), new Name('def')));
119    }
120
121    public function testAHexadecimalCommentIsRejected(): void
122    {
123        $this->expectExceptionMessage('A comment is written as a quoted string.');
124
125        new AlterEvent(new QualifiedName(new Name('e')), null, null, null, null, new Text('41', EscapeRule::Backslash, Radix::Hexadecimal));
126    }
127
128    public function testABodyOfAnotherClassIsRejected(): void
129    {
130        $this->expectExceptionMessage('A member of a stored program is a program statement or an SQL statement.');
131
132        new AlterEvent(new QualifiedName(new Name('e')), null, null, null, null, null, new Parameter(new Name('x'), new Integral(IntegralKind::Int)));
133    }
134}
135