Skip to content

Commit

Permalink
fix: use provided event dispatcher (#67)
Browse files Browse the repository at this point in the history
misaert authored Sep 3, 2020
1 parent 49833ee commit 35a1354
Showing 2 changed files with 53 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/MySQLReplication/MySQLReplicationFactory.php
Original file line number Diff line number Diff line change
@@ -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();
}
50 changes: 50 additions & 0 deletions tests/Integration/BasicTest.php
Original file line number Diff line number Diff line change
@@ -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());
}
}

0 comments on commit 35a1354

Please sign in to comment.