packages/sql-semantics-mysql/tests/Unit/Lowering/Replication/ReplicaRuleTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Lowering\Replication;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlParser\Lexer\Token;
11use SqlSemantics\Platform\MySql\Lowering\Lowering;
12use SqlSemantics\Platform\MySql\Lowering\Replication\ReplicaRule;
13use SqlSemantics\Platform\MySql\Model\Contract\ParameterStyle;
14use SqlSemantics\Platform\MySql\Model\Lowering\Leaves;
15use SqlSemantics\Platform\MySql\Platform;
16use SqlSemantics\Platform\MySql\Semantics;
17
18#[CoversClass(ReplicaRule::class)]
19#[Medium]
20final class ReplicaRuleTest extends TestCase
21{
22 public function testStatementLowersEveryGeneration(): void
23 {
24 self::assertSame('STOP SLAVE IO_THREAD', (new Semantics('mysql-5.6.51'))->analyze('stop slave io_thread')->toString());
25 self::assertSame('START REPLICA', (new Semantics('mysql-9.1.0'))->analyze('start replica')->toString());
26 }
27
28 public function testLegacyStartLowersTheSplitParts(): void
29 {
30 self::assertSame("START SLAVE SQL_THREAD UNTIL SQL_AFTER_MTS_GAPS FOR CHANNEL 'c'", (new Semantics('mysql-5.7.44'))->analyze("start slave sql_thread until sql_after_mts_gaps for channel 'c'")->toString());
31 }
32
33 public function testStartLowersTheConnectionOptions(): void
34 {
35 self::assertSame("START SLAVE PASSWORD = 'p' PLUGIN_DIR = 'd'", (new Semantics('mysql-5.6.51'))->analyze("start slave password = 'p' plugin_dir = 'd'")->toString());
36 }
37
38 public function testConnectionAnswersTheFourOptions(): void
39 {
40 self::assertSame("START SLAVE USER = 'u' DEFAULT_AUTH = 'a'", (new Semantics('mysql-5.7.44'))->analyze("start slave user = 'u' default_auth = 'a'")->toString());
41 }
42
43 public function testOptionLowersAWrittenOption(): void
44 {
45 self::assertSame("START REPLICA PLUGIN_DIR = 'd'", (new Semantics())->analyze("start replica plugin_dir = 'd'")->toString());
46 }
47
48 public function testThreadsLowersTheListInOrder(): void
49 {
50 self::assertSame('START REPLICA IO_THREAD, SQL_THREAD, IO_THREAD', (new Semantics())->analyze('start replica io_thread, sql_thread, relay_thread')->toString());
51 }
52
53 public function testNodeRejectsAToken(): void
54 {
55 $platform = new Platform();
56 $profile = $platform->profile(null, null, ParameterStyle::Native);
57 $rule = new ReplicaRule(new Lowering($platform->productions($profile), new Leaves(), $profile));
58
59 $this->expectExceptionMessage('a START statement part that is not a nonterminal');
60
61 $rule->node(new Token(0, 'USER', 'USER', 0));
62 }
63}
64