From 7ce435bcf476b1b70063c6beff4edd372af30ad8 Mon Sep 17 00:00:00 2001 From: Craig Morris Date: Wed, 26 Jun 2019 13:21:26 +0200 Subject: [PATCH] test: Add test for worker dispatcher --- src/EventStoreWorker.php | 3 +- tests/Fixtures/TestEvent.php | 15 +++++++ tests/WorkerTest.php | 76 ++++++++++++++++++++++++++++++++++++ 3 files changed, 93 insertions(+), 1 deletion(-) create mode 100644 tests/Fixtures/TestEvent.php create mode 100644 tests/WorkerTest.php diff --git a/src/EventStoreWorker.php b/src/EventStoreWorker.php index e970630..be15a12 100644 --- a/src/EventStoreWorker.php +++ b/src/EventStoreWorker.php @@ -7,6 +7,7 @@ use EventLoop\EventLoop; use Rxnet\EventStore\EventStore; use Rxnet\EventStore\Record\AcknowledgeableEventRecord; +use Rxnet\EventStore\Record\EventRecord; class EventStoreWorker extends Command { @@ -87,7 +88,7 @@ private function processStream($eventStore, string $stream) }, 'report'); } - protected function dispatch(AcknowledgeableEventRecord $event) + public function dispatch(EventRecord $event) { $type = $event->getType(); $class = config('eventstore.namespace') . '\\' . $type; diff --git a/tests/Fixtures/TestEvent.php b/tests/Fixtures/TestEvent.php new file mode 100644 index 0000000..305d7f0 --- /dev/null +++ b/tests/Fixtures/TestEvent.php @@ -0,0 +1,15 @@ +event = $event; + } +} diff --git a/tests/WorkerTest.php b/tests/WorkerTest.php new file mode 100644 index 0000000..86339f3 --- /dev/null +++ b/tests/WorkerTest.php @@ -0,0 +1,76 @@ +makeEventRecord('event_with_no_class', ['hello' => 'world']); + + // Act. + $worker->dispatch($event); + + // Assert. + Event::assertDispatched('event_with_no_class', function ($type, EventRecord $event) { + $this->assertEquals(['hello' => 'world'], $event->getData()); + + return true; + }); + } + + /** @test */ + public function it_dispatches_a_classed_event_from_a_subscribed_event() + { + // Arrange. + Event::fake(); + $worker = resolve(EventStoreWorker::class); + $event = $this->makeEventRecord('TestEvent', ['hello' => 'world']); + config(['eventstore.namespace' => 'DigitalRisks\LaravelEventStore\Tests\Fixtures']); + + // Act. + $worker->dispatch($event); + + // Assert. + Event::assertDispatched(TestEvent::class, function (TestEvent $event) { + $this->assertEquals(['hello' => 'world'], $event->event->getData()); + + return true; + }); + } + + /** @test */ + public function it_dispatches_a_classed_event_from_a_parked_event() + { + // Arrange. + Event::fake(); + $worker = resolve(EventStoreWorker::class); + $event = $this->makeEventRecord('TestEvent', ['hello' => 'world']); + config(['eventstore.namespace' => 'DigitalRisks\LaravelEventStore\Tests\Fixtures']); + + // Act. + $worker->dispatch($event); + + // Assert. + Event::assertDispatched(TestEvent::class, function (TestEvent $event) { + $this->assertEquals(['hello' => 'world'], $event->event->getData()); + + return true; + }); + } +}