packages/sql-semantics-mysql/tests/Unit/Rules/Routine/EventClausesTest.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\Rendering\Lexical;
12use SqlSemantics\Platform\MySql\Model\Rendering\Output;
13use SqlSemantics\Platform\MySql\Rendering\Codec;
14use SqlSemantics\Platform\MySql\Rules\Routine\EventClauses;
15use SqlSemantics\Platform\MySql\Semantics;
16use SqlSemantics\Platform\MySql\Statement\Literal\Text;
17use SqlSemantics\Platform\MySql\Statement\Routine\Event\Completion;
18use SqlSemantics\Platform\MySql\Statement\Routine\Event\EventStatus;
19
20#[CoversClass(EventClauses::class)]
21#[Medium]
22final class EventClausesTest extends TestCase
23{
24    public function testWriteWritesTheClausesInGrammarOrder(): void
25    {
26        $out = new Output(new Codec(GrammarRelease::MySql910));
27        (new EventClauses())->write($out, Completion::NotPreserve, EventStatus::DisableOnReplica, new Text('c'));
28
29        self::assertSame("ON COMPLETION NOT PRESERVE DISABLE ON REPLICA COMMENT 'c'", (new Lexical())->join($out->pieces()));
30    }
31
32    public function testWriteKeepsEachStatusSpellingAndOmitsAbsentClauses(): void
33    {
34        $out = new Output(new Codec(GrammarRelease::MySql910));
35        (new EventClauses())->write($out, Completion::Preserve, EventStatus::DisableOnSlave, null);
36        (new EventClauses())->write($out, null, EventStatus::Enable, null);
37        (new EventClauses())->write($out, null, EventStatus::Disable, null);
38        (new EventClauses())->write($out, null, null, null);
39
40        self::assertSame('ON COMPLETION PRESERVE DISABLE ON SLAVE ENABLE DISABLE', (new Lexical())->join($out->pieces()));
41    }
42
43    public function testWriteRendersTheClausesOfCreateAndAlterEvent(): void
44    {
45        $semantics = new Semantics();
46
47        self::assertSame("CREATE EVENT db.e ON SCHEDULE EVERY 1 DAY ON COMPLETION NOT PRESERVE DISABLE ON REPLICA COMMENT 'c' DO SELECT 1", $semantics->analyze("create event db.e on schedule every 1 day on completion not preserve disable on replica comment 'c' do select 1")->toString());
48        self::assertSame("ALTER EVENT e ON COMPLETION PRESERVE DISABLE ON SLAVE COMMENT 'x'", $semantics->analyze("alter event e on completion preserve disable on slave comment 'x'")->toString());
49        self::assertSame('ALTER EVENT e ENABLE', (new Semantics('mysql-5.7.44'))->analyze('alter event e enable')->toString());
50    }
51}
52