Skip to content

Commit

Permalink
test: Add test for worker dispatcher
Browse files Browse the repository at this point in the history
  • Loading branch information
morrislaptop committed Jun 26, 2019
1 parent 5bc3d0e commit 7ce435b
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/EventStoreWorker.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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;
Expand Down
15 changes: 15 additions & 0 deletions tests/Fixtures/TestEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace DigitalRisks\LaravelEventStore\Tests\Fixtures;

use Rxnet\EventStore\Record\EventRecord;

class TestEvent
{
public $event;

public function __construct(EventRecord $event)
{
$this->event = $event;
}
}
76 changes: 76 additions & 0 deletions tests/WorkerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

namespace DigitalRisks\LaravelEventStore\Tests;

use DigitalRisks\LaravelEventStore\ShouldBeEventStored;
use DigitalRisks\LaravelEventStore\Tests\Traits\InteractsWithEventStore;
use DigitalRisks\LaravelEventStore\SendsToEventStore;
use DigitalRisks\LaravelEventStore\Tests\Traits\MakesEventRecords;
use DigitalRisks\LaravelEventStore\EventStoreWorker;
use Illuminate\Support\Facades\Event;
use Rxnet\EventStore\Record\EventRecord;
use DigitalRisks\LaravelEventStore\Tests\Fixtures\TestEvent;

class WorkerTest extends TestCase
{
use InteractsWithEventStore, MakesEventRecords;

/** @test */
public function it_dispatches_an_event_from_a_subscribed_event()
{
// Arrange.
Event::fake();
$worker = resolve(EventStoreWorker::class);
$event = $this->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;
});
}
}

0 comments on commit 7ce435b

Please sign in to comment.