diff --git a/src/MySQLReplication/Config/Config.php b/src/MySQLReplication/Config/Config.php index 916f434..a7ecea1 100644 --- a/src/MySQLReplication/Config/Config.php +++ b/src/MySQLReplication/Config/Config.php @@ -105,15 +105,23 @@ public function validate(): void public function checkDataBasesOnly(string $database): bool { - return ($this->databasesOnly !== [] && !in_array($database, $this->databasesOnly, true)) - || ($this->databasesRegex !== [] && !self::matchNames($database, $this->databasesRegex)); + if ($this->databasesOnly === [] && $this->databasesRegex === []) { + return false; + } + + return !in_array($database, $this->databasesOnly, true) + && !self::matchNames($database, $this->databasesRegex); } public function checkTablesOnly(string $table): bool { - return ($this->tablesOnly !== [] && !in_array($table, $this->tablesOnly, true)) - || ($this->tablesRegex !== [] && !self::matchNames($table, $this->tablesRegex)); + if ($this->tablesOnly === [] && $this->tablesRegex === []) { + return false; + } + + return !in_array($table, $this->tablesOnly, true) + && !self::matchNames($table, $this->tablesRegex); } public function checkEvent(int $type): bool diff --git a/tests/Unit/Config/ConfigTest.php b/tests/Unit/Config/ConfigTest.php index 6f3924f..1d5b9c2 100644 --- a/tests/Unit/Config/ConfigTest.php +++ b/tests/Unit/Config/ConfigTest.php @@ -102,6 +102,20 @@ public function testShouldCheckDataBasesOnly(): void self::assertFalse($config->checkDataBasesOnly('foo_123')); } + public function testShouldCheckDataBasesOnlyListAndRegex(): void + { + $config = (new ConfigBuilder()) + ->withDatabasesOnly(['foo']) + ->withDatabasesRegex(['/^instance\d+$/']) + ->build(); + self::assertFalse($config->checkDataBasesOnly('foo')); + self::assertFalse($config->checkDataBasesOnly('instance1')); + self::assertFalse($config->checkDataBasesOnly('instance123')); + self::assertTrue($config->checkDataBasesOnly('bar')); + self::assertTrue($config->checkDataBasesOnly('instance')); + self::assertTrue($config->checkDataBasesOnly('instanceX')); + } + public function testShouldCheckTablesOnly(): void { $config = (new ConfigBuilder())->build(); @@ -120,6 +134,20 @@ public function testShouldCheckTablesOnly(): void self::assertFalse($config->checkTablesOnly('foo_123')); } + public function testShouldCheckTablesOnlyListAndRegex(): void + { + $config = (new ConfigBuilder()) + ->withTablesOnly(['foo']) + ->withTablesRegex(['/^instance\d+$/']) + ->build(); + self::assertFalse($config->checkTablesOnly('foo')); + self::assertFalse($config->checkTablesOnly('instance1')); + self::assertFalse($config->checkTablesOnly('instance123')); + self::assertTrue($config->checkTablesOnly('bar')); + self::assertTrue($config->checkTablesOnly('instance')); + self::assertTrue($config->checkTablesOnly('instanceX')); + } + public function testShouldCheckEvent(): void { $config = (new ConfigBuilder())->build();