packages/sql-semantics-mysql/tests/Unit/Rules/Routine/ProgramNamesTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Rules\Routine;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\MySql\Model\Contract\GrammarRelease;
11use SqlSemantics\Platform\MySql\Model\Contract\NameUse;
12use SqlSemantics\Platform\MySql\Model\Rendering\Lexical;
13use SqlSemantics\Platform\MySql\Model\Rendering\Output;
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\Rules\Routine\ProgramNames;
18use SqlSemantics\Platform\MySql\Semantics;
19use SqlSemantics\Platform\MySql\Statement\Routine\CreateFunction;
20
21#[CoversClass(ProgramNames::class)]
22#[Medium]
23final class ProgramNamesTest extends TestCase
24{
25    public function testQualifiedWritesTheDatabaseQualifierWhenPresent(): void
26    {
27        $out = new Output(new Codec(GrammarRelease::MySql910));
28        (new ProgramNames())->qualified($out, new QualifiedName(new Name('p'), new Name('db')));
29        (new ProgramNames())->qualified($out, new QualifiedName(new Name('select')));
30        (new ProgramNames())->qualified($out, new QualifiedName(new Name('t'), new Name('db')), NameUse::Relation);
31
32        self::assertSame('db.p `select` db.t', (new Lexical())->join($out->pieces()));
33    }
34
35    public function testLabelWritesTheLabelFollowedByAColon(): void
36    {
37        $out = new Output(new Codec(GrammarRelease::MySql910));
38        (new ProgramNames())->label($out, new Name('l'));
39        (new ProgramNames())->label($out, null);
40        (new ProgramNames())->label($out, new Name('select l'));
41
42        self::assertSame('l: `select l`:', (new Lexical())->join($out->pieces()));
43    }
44
45    public function testEndWritesTheEndLabelWhenPresent(): void
46    {
47        $out = new Output(new Codec(GrammarRelease::MySql910));
48        $out->keyword('END');
49        (new ProgramNames())->end($out, new Name('l'));
50        (new ProgramNames())->end($out, null);
51
52        self::assertSame('END l', (new Lexical())->join($out->pieces()));
53        self::assertSame('CREATE PROCEDURE p() l: BEGIN END l', (new Semantics())->analyze('create procedure p() l: begin end l')->toString());
54    }
55
56    public function testTypeWritesTheCollateClauseWhenPresent(): void
57    {
58        $function = (new Semantics())->analyze('CREATE FUNCTION f() RETURNS CHAR(1) COLLATE utf8mb4_bin RETURN 1')->statement;
59        self::assertInstanceOf(CreateFunction::class, $function);
60        $out = new Output(new Codec(GrammarRelease::MySql910));
61        (new ProgramNames())->type($out, $function->returns, $function->collation);
62        (new ProgramNames())->type($out, $function->returns, null);
63
64        self::assertSame('CHAR(1) COLLATE utf8mb4_bin CHAR(1)', (new Lexical())->join($out->pieces()));
65    }
66}
67