packages/sql-semantics-mysql/tests/Unit/Statement/Routine/AlterRoutineTest.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\Identifier\Name;
14use SqlSemantics\Platform\MySql\Model\Statement\Identifier\QualifiedName;
15use SqlSemantics\Platform\MySql\Rendering\Codec;
16use SqlSemantics\Platform\MySql\Semantics;
17use SqlSemantics\Platform\MySql\Statement\Routine\AlterRoutine;
18use SqlSemantics\Platform\MySql\Statement\Routine\Characteristic\AccessLevel;
19use SqlSemantics\Platform\MySql\Statement\Routine\Characteristic\DataAccess;
20use SqlSemantics\Platform\MySql\Statement\Routine\Characteristic\Determinism;
21use SqlSemantics\Platform\MySql\Statement\Routine\Characteristic\SecurityContext;
22use SqlSemantics\Platform\MySql\Statement\Routine\Characteristic\SqlSecurity;
23use SqlSemantics\Platform\MySql\Statement\Routine\ProgramKind;
24
25#[CoversClass(AlterRoutine::class)]
26#[Medium]
27final class AlterRoutineTest extends TestCase
28{
29    public function testDeriveStatementChangesNoContext(): void
30    {
31        $semantics = new Semantics();
32        $table = $semantics->analyze('CREATE TABLE t (a INT)');
33        $alter = $semantics->analyze("ALTER PROCEDURE shop.p COMMENT 'c' MODIFIES SQL DATA", [$table]);
34
35        self::assertSame([], $alter->facts->diagnostics);
36        self::assertSame([], $alter->facts->declarations);
37        self::assertNull($alter->facts->output);
38    }
39
40    public function testDeriveStatementReadsTheKindTheNameAndTheCharacteristics(): void
41    {
42        $alter = (new Semantics())->analyze('ALTER FUNCTION shop.f NO SQL SQL SECURITY INVOKER');
43        $statement = $alter->statement;
44        self::assertInstanceOf(AlterRoutine::class, $statement);
45
46        self::assertSame(ProgramKind::Function, $statement->kind);
47        self::assertSame('shop', $statement->name->schema?->value);
48        self::assertSame('f', $statement->name->name->value);
49        self::assertEquals([new DataAccess(AccessLevel::NoSql), new SqlSecurity(SecurityContext::Invoker)], $statement->characteristics);
50    }
51
52    /**
53     * @return iterable<string, array{string, string, string}>
54     */
55    public static function providerRenderWritesTheAlteration(): iterable
56    {
57        yield 'MySQL 5.6 every characteristic' => [
58            'mysql-5.6.51',
59            "alter procedure shop.p comment 'x' language sql sql security invoker contains sql",
60            "ALTER PROCEDURE shop.p COMMENT 'x' LANGUAGE SQL SQL SECURITY INVOKER CONTAINS SQL",
61        ];
62        yield 'MySQL 5.7 function' => ['mysql-5.7.44', 'alter function f modifies sql data', 'ALTER FUNCTION f MODIFIES SQL DATA'];
63        yield 'MySQL 8.0 without characteristics' => ['mysql-8.0.44', 'alter procedure p', 'ALTER PROCEDURE p'];
64        yield 'MySQL 9.1 two access levels' => ['mysql-9.1.0', 'alter function shop.f no sql reads sql data', 'ALTER FUNCTION shop.f NO SQL READS SQL DATA'];
65    }
66
67    #[DataProvider('providerRenderWritesTheAlteration')]
68    public function testRenderWritesTheAlteration(string $release, string $sql, string $expected): void
69    {
70        self::assertSame($expected, (new Semantics($release))->analyze($sql)->toString());
71    }
72
73    public function testRenderWritesAConstructedAlteration(): void
74    {
75        $semantics = new Semantics();
76        $out = new Output(new Codec($semantics->profile()->grammar));
77        (new AlterRoutine(ProgramKind::Procedure, new QualifiedName(new Name('p'), new Name('shop')), [new SqlSecurity(SecurityContext::Definer)]))->render($out);
78
79        self::assertSame('ALTER PROCEDURE shop.p SQL SECURITY DEFINER', (new Lexical())->join($out->pieces()));
80    }
81
82    public function testATriggerIsRejected(): void
83    {
84        $this->expectExceptionMessage('ALTER of a routine names a procedure or a function.');
85
86        new AlterRoutine(ProgramKind::Trigger, new QualifiedName(new Name('tr')));
87    }
88
89    public function testAnEventIsRejected(): void
90    {
91        $this->expectExceptionMessage('ALTER of a routine names a procedure or a function.');
92
93        new AlterRoutine(ProgramKind::Event, new QualifiedName(new Name('e')));
94    }
95
96    public function testANameWithACatalogIsRejected(): void
97    {
98        $this->expectExceptionMessage('A routine name has at most a database qualifier.');
99
100        new AlterRoutine(ProgramKind::Function, new QualifiedName(new Name('f'), new Name('shop'), new Name('def')));
101    }
102
103    public function testADeterministicCharacteristicIsRejected(): void
104    {
105        $this->expectExceptionMessage('ALTER of a routine has no DETERMINISTIC characteristic.');
106
107        new AlterRoutine(ProgramKind::Function, new QualifiedName(new Name('f')), [new DataAccess(AccessLevel::NoSql), new Determinism(true)]);
108    }
109}
110