Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d26b40d
commit 322ca87
Showing
3 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
namespace EventSauce\EventSourcing; | ||
|
||
use function end; | ||
use function explode; | ||
use function get_class; | ||
use function method_exists; | ||
|
||
class EventHandlingMessageConsumer implements Consumer | ||
{ | ||
public function handle(Message $message) | ||
{ | ||
$event = $message->event(); | ||
$parts = explode('\\', get_class($event)); | ||
$method = 'handle' . end($parts); | ||
|
||
if (method_exists($this, $method)) { | ||
$this->{$method}($event, $message); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace EventSauce\EventSourcing\Integration\EventHandling; | ||
|
||
use EventSauce\EventSourcing\EventHandlingMessageConsumer; | ||
use EventSauce\EventSourcing\Message; | ||
use EventSauce\EventSourcing\PayloadStub; | ||
|
||
class DummyEventHandler extends EventHandlingMessageConsumer | ||
{ | ||
/** | ||
* @var PayloadStub | ||
*/ | ||
public $event; | ||
|
||
/** | ||
* @var Message | ||
*/ | ||
public $message; | ||
|
||
protected function handlePayloadStub(PayloadStub $event, Message $message) | ||
{ | ||
$this->event = $event; | ||
$this->message = $message; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
namespace EventSauce\EventSourcing\Integration\EventHandling; | ||
|
||
use EventSauce\EventSourcing\Message; | ||
use EventSauce\EventSourcing\PayloadStub; | ||
use PHPStan\Testing\TestCase; | ||
|
||
class EventHandlingTest extends TestCase | ||
{ | ||
/** | ||
* @test | ||
*/ | ||
public function handling_events_from_messages() | ||
{ | ||
$handler = new DummyEventHandler(); | ||
$event = PayloadStub::create('value'); | ||
$message = new Message($event); | ||
$handler->handle($message); | ||
self::assertEquals($event, $handler->event); | ||
self::assertEquals($message, $handler->message); | ||
} | ||
} |