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

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Routine\Characteristic;
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\Rendering\Codec;
14use SqlSemantics\Platform\MySql\Semantics;
15use SqlSemantics\Platform\MySql\Statement\Literal\EscapeRule;
16use SqlSemantics\Platform\MySql\Statement\Literal\Radix;
17use SqlSemantics\Platform\MySql\Statement\Literal\Text;
18use SqlSemantics\Platform\MySql\Statement\Routine\AlterRoutine;
19use SqlSemantics\Platform\MySql\Statement\Routine\Characteristic\RoutineComment;
20
21#[CoversClass(RoutineComment::class)]
22#[Medium]
23final class RoutineCommentTest extends TestCase
24{
25    /**
26     * @return iterable<string, array{string, string, string, string}>
27     */
28    public static function providerRenderWritesTheCommentText(): iterable
29    {
30        yield 'MySQL 5.6 plain text' => ['mysql-5.6.51', "alter procedure p comment 'adds'", "ALTER PROCEDURE p COMMENT 'adds'", 'adds'];
31        yield 'MySQL 8.0 doubled quote' => ['mysql-8.0.44', "alter function f comment 'it''s'", "ALTER FUNCTION f COMMENT 'it''s'", "it's"];
32        yield 'MySQL 9.1 double-quoted text' => ['mysql-9.1.0', 'alter function f comment "two words"', "ALTER FUNCTION f COMMENT 'two words'", 'two words'];
33    }
34
35    #[DataProvider('providerRenderWritesTheCommentText')]
36    public function testRenderWritesTheCommentText(string $release, string $sql, string $expected, string $text): void
37    {
38        $alter = (new Semantics($release))->analyze($sql);
39        $statement = $alter->statement;
40        self::assertInstanceOf(AlterRoutine::class, $statement);
41        $characteristic = $statement->characteristics[0] ?? null;
42        self::assertInstanceOf(RoutineComment::class, $characteristic);
43
44        self::assertSame($text, $characteristic->text->value);
45        self::assertSame($expected, $alter->toString());
46    }
47
48    public function testRenderWritesAConstructedCharacteristic(): void
49    {
50        $out = new Output(new Codec((new Semantics())->profile()->grammar));
51        (new RoutineComment(new Text('adds')))->render($out);
52
53        self::assertSame("COMMENT 'adds'", (new Lexical())->join($out->pieces()));
54    }
55
56    public function testAHexadecimalTextIsRejected(): void
57    {
58        $this->expectExceptionMessage('A comment is written as a quoted string.');
59
60        new RoutineComment(new Text('41', EscapeRule::Backslash, Radix::Hexadecimal));
61    }
62
63    public function testABitTextIsRejected(): void
64    {
65        $this->expectExceptionMessage('A comment is written as a quoted string.');
66
67        new RoutineComment(new Text('01', EscapeRule::Backslash, Radix::Bit));
68    }
69}
70