Skip to content

Commit 35a1354

Browse files
authored
fix: use provided event dispatcher (#67)
1 parent 49833ee commit 35a1354

File tree

2 files changed

+53
-3
lines changed

2 files changed

+53
-3
lines changed

src/MySQLReplication/MySQLReplicationFactory.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ public function __construct(
6666
if (null === $cache) {
6767
$cache = new ArrayCache();
6868
}
69-
if (null === $eventDispatcher) {
70-
$this->eventDispatcher = new EventDispatcher();
71-
}
69+
70+
$this->eventDispatcher = $eventDispatcher ?: new EventDispatcher();
71+
7272
if (null === $socket) {
7373
$socket = new Socket();
7474
}

tests/Integration/BasicTest.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,16 @@
77
use MySQLReplication\BinLog\BinLogServerInfo;
88
use MySQLReplication\Definitions\ConstEventType;
99
use MySQLReplication\Event\DTO\DeleteRowsDTO;
10+
use MySQLReplication\Event\DTO\FormatDescriptionEventDTO;
1011
use MySQLReplication\Event\DTO\QueryDTO;
1112
use MySQLReplication\Event\DTO\RotateDTO;
1213
use MySQLReplication\Event\DTO\TableMapDTO;
1314
use MySQLReplication\Event\DTO\UpdateRowsDTO;
1415
use MySQLReplication\Event\DTO\WriteRowsDTO;
1516
use MySQLReplication\Event\DTO\XidDTO;
17+
use MySQLReplication\MySQLReplicationFactory;
18+
use Symfony\Component\EventDispatcher\EventDispatcher;
19+
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
1620

1721
class BasicTest extends BaseTest
1822
{
@@ -352,4 +356,50 @@ public function shouldRoteLog(): void
352356
$this->getEvent()->getEventInfo()->getBinLogCurrent()->getBinFileName()
353357
);
354358
}
359+
360+
/**
361+
* @test
362+
*/
363+
public function shouldUseProvidedEventDispatcher(): void
364+
{
365+
$this->disconnect();
366+
367+
$testEventSubscribers = new TestEventSubscribers($this);
368+
369+
$eventDispatcher = new EventDispatcher();
370+
$eventDispatcher->addSubscriber($testEventSubscribers);
371+
372+
$this->connectWithProvidedEventDispatcher($eventDispatcher);
373+
374+
$this->connection->exec(
375+
$createExpected = 'CREATE TABLE test (id INT NOT NULL AUTO_INCREMENT, data VARCHAR (50) NOT NULL, PRIMARY KEY (id))'
376+
);
377+
378+
/** @var QueryDTO $event */
379+
$event = $this->getEvent();
380+
self::assertInstanceOf(QueryDTO::class, $event);
381+
self::assertEquals($createExpected, $event->getQuery());
382+
}
383+
384+
private function connectWithProvidedEventDispatcher(EventDispatcherInterface $eventDispatcher): void
385+
{
386+
$this->mySQLReplicationFactory = new MySQLReplicationFactory(
387+
$this->configBuilder->build(),
388+
null,
389+
null,
390+
$eventDispatcher
391+
);
392+
393+
$this->connection = $this->mySQLReplicationFactory->getDbConnection();
394+
395+
$this->connection->exec('SET SESSION time_zone = "UTC"');
396+
$this->connection->exec('DROP DATABASE IF EXISTS ' . $this->database);
397+
$this->connection->exec('CREATE DATABASE ' . $this->database);
398+
$this->connection->exec('USE ' . $this->database);
399+
$this->connection->exec('SET SESSION sql_mode = \'\';');
400+
401+
self::assertInstanceOf(FormatDescriptionEventDTO::class, $this->getEvent());
402+
self::assertInstanceOf(QueryDTO::class, $this->getEvent());
403+
self::assertInstanceOf(QueryDTO::class, $this->getEvent());
404+
}
355405
}

0 commit comments

Comments
 (0)