packages/sql-semantics-mysql/tests/Unit/Statement/Replication/Source/ChangeReplicationSourceTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Replication\Source;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\MySql\Model\Statement\Operation;
11use SqlSemantics\Platform\MySql\Semantics;
12use SqlSemantics\Platform\MySql\Statement\Literal\Numeral;
13use SqlSemantics\Platform\MySql\Statement\Literal\Text;
14use SqlSemantics\Platform\MySql\Statement\Replication\Problem\RefusedSetting;
15use SqlSemantics\Platform\MySql\Statement\Replication\Problem\ReplicationError;
16use SqlSemantics\Platform\MySql\Statement\Replication\Source\ChangeReplicationSource;
17use SqlSemantics\Platform\MySql\Statement\Replication\Source\SourceOption;
18use SqlSemantics\Platform\MySql\Statement\Replication\Source\SourceOptionKind;
19use SqlSemantics\Platform\MySql\Statement\Replication\Terminology;
20
21#[CoversClass(ChangeReplicationSource::class)]
22#[Medium]
23final class ChangeReplicationSourceTest extends TestCase
24{
25    public function testDeriveStatementReportsRefusedSettings(): void
26    {
27        $change = (new Semantics())->inspect("CHANGE REPLICATION SOURCE TO SOURCE_PASSWORD = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', SOURCE_DELAY = 2147483648, SOURCE_HEARTBEAT_PERIOD = 5000000, REQUIRE_ROW_FORMAT = 2, SOURCE_CONNECTION_AUTO_FAILOVER = 1.5, ASSIGN_GTIDS_TO_ANONYMOUS_TRANSACTIONS = 'x' FOR CHANNEL 'a\\nb'");
28
29        self::assertEquals([
30            new RefusedSetting(ReplicationError::PasswordTooLong), new RefusedSetting(ReplicationError::DelayOutOfRange), new RefusedSetting(ReplicationError::HeartbeatOutOfRange),
31            new RefusedSetting(ReplicationError::RowFormatValue), new RefusedSetting(ReplicationError::FractionalNumber), new RefusedSetting(ReplicationError::InvalidUuid),
32            new RefusedSetting(ReplicationError::LineFeed),
33        ], $change->facts->diagnostics);
34    }
35
36    public function testDeriveStatementRejectsTheVocabularyOfAnotherRelease(): void
37    {
38        $semantics = new Semantics('mysql-8.4.7');
39        $change = new ChangeReplicationSource(Terminology::Legacy, [new SourceOption(Terminology::Legacy, SourceOptionKind::Host, new Text('h'))]);
40
41        $this->expectExceptionMessage('CHANGE REPLICATION SOURCE is written in the vocabulary of the release.');
42
43        new Operation($semantics->context([]), $change);
44    }
45
46    public function testDeriveStatementRejectsAnOptionOfALaterRelease(): void
47    {
48        $semantics = new Semantics('mysql-5.7.44');
49        $change = new ChangeReplicationSource(Terminology::Legacy, [new SourceOption(Terminology::Legacy, SourceOptionKind::GtidOnly, new Numeral('1'))]);
50
51        $this->expectExceptionMessage('GTID_ONLY is not an option of this release.');
52
53        new Operation($semantics->context([]), $change);
54    }
55
56    public function testRenderWritesTheLegacyVocabulary(): void
57    {
58        self::assertSame("CHANGE MASTER TO MASTER_HOST = 'h', RELAY_LOG_POS = 4 FOR CHANNEL 'c'", (new Semantics('mysql-5.7.44'))->inspect("change master to master_host='h', relay_log_pos=4 for channel 'c'")->toString());
59    }
60
61    public function testRenderWritesTheCurrentVocabulary(): void
62    {
63        self::assertSame("CHANGE REPLICATION SOURCE TO SOURCE_LOG_FILE = 'f', SOURCE_LOG_POS = 4", (new Semantics('mysql-8.3.0'))->inspect("change replication source to master_log_file='f', source_log_pos=4")->toString());
64    }
65}
66