-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Appended a test to check supporting of multiple middlewares
- Loading branch information
1 parent
46dde60
commit fd358e2
Showing
1 changed file
with
117 additions
and
0 deletions.
There are no files selected for viewing
117 changes: 117 additions & 0 deletions
117
...Buses/EventBus/Middleware/Multiple/EventBusMultipleMiddlewareReactionAfterHandlerTest.php
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,117 @@ | ||
<?php | ||
|
||
declare(strict_types=1); // strict mode | ||
|
||
namespace AwdStudio\Tests\ServiceBuses\EventBus\Middleware\Multiple; | ||
|
||
use AwdStudio\ServiceBuses\EventBus\EventBus; | ||
use AwdStudio\ServiceBuses\Implementation\Handling\InMemoryHandlerLocator; | ||
use AwdStudio\ServiceBuses\Implementation\Middleware\Chain; | ||
use PHPStan\Testing\TestCase; | ||
|
||
final class EventBusMultipleMiddlewareReactionAfterHandlerTest extends TestCase | ||
{ | ||
|
||
public function testMustSupportMultipleMiddleware() | ||
{ | ||
$eventBus = new EventBus( | ||
new InMemoryHandlerLocator([Event::class => [new EventHandler()]]), | ||
new Chain(new EventMiddleware1(), new EventMiddleware2()) | ||
); | ||
|
||
$event = new Event(); | ||
|
||
$eventBus->handle($event); | ||
|
||
$this->assertTrue($event->isFilledByHandler()); | ||
$this->assertTrue($event->isFilledByMiddleware1()); | ||
$this->assertTrue($event->isFilledByMiddleware2()); | ||
} | ||
|
||
} | ||
|
||
class Event | ||
{ | ||
/** @var bool */ | ||
private $filledByHandler = false; | ||
|
||
/** @var bool */ | ||
private $filledByMiddleware1 = false; | ||
|
||
/** @var bool */ | ||
private $filledByMiddleware2 = false; | ||
|
||
public function fillByHandler(EventHandler $handler): void | ||
{ | ||
$this->filledByHandler = $handler->check($this); | ||
} | ||
|
||
public function fillByMiddleware1(EventMiddleware1 $middleware): void | ||
{ | ||
$this->filledByMiddleware1 = $middleware->check($this); | ||
} | ||
|
||
public function fillByMiddleware2(EventMiddleware2 $middleware): void | ||
{ | ||
$this->filledByMiddleware2 = $middleware->check($this); | ||
} | ||
|
||
public function isFilledByHandler(): bool | ||
{ | ||
return $this->filledByHandler; | ||
} | ||
|
||
public function isFilledByMiddleware1(): bool | ||
{ | ||
return $this->filledByMiddleware1; | ||
} | ||
|
||
public function isFilledByMiddleware2(): bool | ||
{ | ||
return $this->filledByMiddleware2; | ||
} | ||
} | ||
|
||
abstract class EventComparator | ||
{ | ||
/** @var Event */ | ||
protected $event; | ||
|
||
public function check(Event $event): bool | ||
{ | ||
return $this->event === $event; | ||
} | ||
} | ||
|
||
class EventHandler extends EventComparator | ||
{ | ||
public function __invoke(Event $event): void | ||
{ | ||
$this->event = $event; | ||
|
||
$event->fillByHandler($this); | ||
} | ||
} | ||
|
||
class EventMiddleware1 extends EventComparator | ||
{ | ||
public function __invoke(Event $event, callable $next) | ||
{ | ||
$this->event = $event; | ||
|
||
$result = $next($event); | ||
$event->fillByMiddleware1($this); | ||
return $result; | ||
} | ||
} | ||
|
||
class EventMiddleware2 extends EventComparator | ||
{ | ||
public function __invoke(Event $event, callable $next) | ||
{ | ||
$this->event = $event; | ||
|
||
$event->fillByMiddleware2($this); | ||
return $next($event); | ||
} | ||
} |