From 35a1354da2c632d950e48abf222287fd60f61048 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micka=C3=ABl=20Isaert?= Date: Thu, 3 Sep 2020 13:09:28 +0200 Subject: [PATCH] fix: use provided event dispatcher (#67) --- .../MySQLReplicationFactory.php | 6 +-- tests/Integration/BasicTest.php | 50 +++++++++++++++++++ 2 files changed, 53 insertions(+), 3 deletions(-) diff --git a/src/MySQLReplication/MySQLReplicationFactory.php b/src/MySQLReplication/MySQLReplicationFactory.php index 5ed6d57..271305a 100644 --- a/src/MySQLReplication/MySQLReplicationFactory.php +++ b/src/MySQLReplication/MySQLReplicationFactory.php @@ -66,9 +66,9 @@ public function __construct( if (null === $cache) { $cache = new ArrayCache(); } - if (null === $eventDispatcher) { - $this->eventDispatcher = new EventDispatcher(); - } + + $this->eventDispatcher = $eventDispatcher ?: new EventDispatcher(); + if (null === $socket) { $socket = new Socket(); } diff --git a/tests/Integration/BasicTest.php b/tests/Integration/BasicTest.php index b218ef5..2f5b55f 100644 --- a/tests/Integration/BasicTest.php +++ b/tests/Integration/BasicTest.php @@ -7,12 +7,16 @@ use MySQLReplication\BinLog\BinLogServerInfo; use MySQLReplication\Definitions\ConstEventType; use MySQLReplication\Event\DTO\DeleteRowsDTO; +use MySQLReplication\Event\DTO\FormatDescriptionEventDTO; use MySQLReplication\Event\DTO\QueryDTO; use MySQLReplication\Event\DTO\RotateDTO; use MySQLReplication\Event\DTO\TableMapDTO; use MySQLReplication\Event\DTO\UpdateRowsDTO; use MySQLReplication\Event\DTO\WriteRowsDTO; use MySQLReplication\Event\DTO\XidDTO; +use MySQLReplication\MySQLReplicationFactory; +use Symfony\Component\EventDispatcher\EventDispatcher; +use Symfony\Component\EventDispatcher\EventDispatcherInterface; class BasicTest extends BaseTest { @@ -352,4 +356,50 @@ public function shouldRoteLog(): void $this->getEvent()->getEventInfo()->getBinLogCurrent()->getBinFileName() ); } + + /** + * @test + */ + public function shouldUseProvidedEventDispatcher(): void + { + $this->disconnect(); + + $testEventSubscribers = new TestEventSubscribers($this); + + $eventDispatcher = new EventDispatcher(); + $eventDispatcher->addSubscriber($testEventSubscribers); + + $this->connectWithProvidedEventDispatcher($eventDispatcher); + + $this->connection->exec( + $createExpected = 'CREATE TABLE test (id INT NOT NULL AUTO_INCREMENT, data VARCHAR (50) NOT NULL, PRIMARY KEY (id))' + ); + + /** @var QueryDTO $event */ + $event = $this->getEvent(); + self::assertInstanceOf(QueryDTO::class, $event); + self::assertEquals($createExpected, $event->getQuery()); + } + + private function connectWithProvidedEventDispatcher(EventDispatcherInterface $eventDispatcher): void + { + $this->mySQLReplicationFactory = new MySQLReplicationFactory( + $this->configBuilder->build(), + null, + null, + $eventDispatcher + ); + + $this->connection = $this->mySQLReplicationFactory->getDbConnection(); + + $this->connection->exec('SET SESSION time_zone = "UTC"'); + $this->connection->exec('DROP DATABASE IF EXISTS ' . $this->database); + $this->connection->exec('CREATE DATABASE ' . $this->database); + $this->connection->exec('USE ' . $this->database); + $this->connection->exec('SET SESSION sql_mode = \'\';'); + + self::assertInstanceOf(FormatDescriptionEventDTO::class, $this->getEvent()); + self::assertInstanceOf(QueryDTO::class, $this->getEvent()); + self::assertInstanceOf(QueryDTO::class, $this->getEvent()); + } } \ No newline at end of file