-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from PcComponentes/feature/add-aggregate-and-si…
…mple-message-context Feature/add aggregate and simple message context
- Loading branch information
Showing
4 changed files
with
145 additions
and
3 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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
FROM php:7.2-cli-alpine | ||
FROM php:7.3-cli-alpine | ||
|
||
RUN apk update && \ | ||
apk add --no-cache \ | ||
|
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
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,75 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Pccomponentes\OpenApiMessagingContext\Behat; | ||
|
||
use Assert\Assert; | ||
use Behat\Behat\Context\Context; | ||
use Behat\Gherkin\Node\PyStringNode; | ||
use Pccomponentes\Ddd\Util\Message\Serialization\AggregateMessageUnserializable; | ||
use Pccomponentes\Ddd\Util\Message\Serialization\JsonApi\AggregateMessageStream; | ||
use Symfony\Component\Messenger\MessageBusInterface; | ||
|
||
final class AggregateMessageContext implements Context | ||
{ | ||
private $bus; | ||
private $aggregateMessageUnserializable; | ||
|
||
public function __construct( | ||
MessageBusInterface $bus, | ||
AggregateMessageUnserializable $aggregateMessageUnserializable | ||
) { | ||
$this->bus = $bus; | ||
$this->aggregateMessageUnserializable = $aggregateMessageUnserializable; | ||
} | ||
|
||
/** | ||
* @When I receive an aggregate message with payload: | ||
*/ | ||
public function dispatchMessage(PyStringNode $payload): void | ||
{ | ||
$message = $this->aggregateMessageUnserializable->unserialize( | ||
$this->payloadToStream($payload->getRaw()) | ||
); | ||
$this->bus->dispatch($message); | ||
} | ||
|
||
private function payloadToStream(string $rawPayload): AggregateMessageStream | ||
{ | ||
$payload = \json_decode($rawPayload, true, 512, \JSON_THROW_ON_ERROR); | ||
$this->assertContent($payload); | ||
|
||
$body = $payload['data']; | ||
|
||
return new AggregateMessageStream( | ||
$body['message_id'], | ||
$body['attributes']['aggregate_id'], | ||
(int) $body['occurred_on'], | ||
$body['type'], | ||
0, | ||
\json_encode($body['attributes'], \JSON_THROW_ON_ERROR, 512), | ||
); | ||
} | ||
|
||
private function assertContent(array $content): void | ||
{ | ||
Assert::lazy()->tryAll() | ||
->that($content['data'], 'data')->isArray() | ||
->keyExists('message_id') | ||
->keyExists('type') | ||
->keyExists('occurred_on') | ||
->keyExists('attributes') | ||
->verifyNow(); | ||
|
||
Assert::lazy()->tryAll() | ||
->that($content['data']['message_id'], 'message_id')->uuid() | ||
->that($content['data']['type'], 'type')->string()->notEmpty() | ||
->that($content['data']['occurred_on'], 'occurred_on')->notEmpty() | ||
->that($content['data']['attributes'], 'attributes')->isArray()->keyExists('aggregate_id') | ||
->verifyNow(); | ||
|
||
Assert::lazy()->tryAll() | ||
->that($content['data']['attributes']['aggregate_id'], 'aggregate_id')->uuid() | ||
->verifyNow(); | ||
} | ||
} |
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,66 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Pccomponentes\OpenApiMessagingContext\Behat; | ||
|
||
use Assert\Assert; | ||
use Behat\Behat\Context\Context; | ||
use Behat\Gherkin\Node\PyStringNode; | ||
use Pccomponentes\Ddd\Util\Message\Serialization\JsonApi\SimpleMessageStream; | ||
use Pccomponentes\Ddd\Util\Message\Serialization\SimpleMessageUnserializable; | ||
use Symfony\Component\Messenger\MessageBusInterface; | ||
|
||
final class SimpleMessageContext implements Context | ||
{ | ||
private $bus; | ||
private $simpleMessageUnserializable; | ||
|
||
public function __construct( | ||
MessageBusInterface $bus, | ||
SimpleMessageUnserializable $simpleMessageUnserializable | ||
) { | ||
$this->bus = $bus; | ||
$this->simpleMessageUnserializable = $simpleMessageUnserializable; | ||
} | ||
|
||
/** | ||
* @When I receive a simple message with payload: | ||
*/ | ||
public function dispatchMessage(PyStringNode $payload): void | ||
{ | ||
$message = $this->simpleMessageUnserializable->unserialize( | ||
$this->payloadToStream($payload->getRaw()) | ||
); | ||
$this->bus->dispatch($message); | ||
} | ||
|
||
private function payloadToStream(string $rawPayload): SimpleMessageStream | ||
{ | ||
$payload = \json_decode($rawPayload, true, 512, \JSON_THROW_ON_ERROR); | ||
$this->assertContent($payload); | ||
|
||
$body = $payload['data']; | ||
|
||
return new SimpleMessageStream( | ||
$body['message_id'], | ||
$body['type'], | ||
\json_encode($body['attributes'], \JSON_THROW_ON_ERROR, 512), | ||
); | ||
} | ||
|
||
private function assertContent(array $content): void | ||
{ | ||
Assert::lazy()->tryAll() | ||
->that($content['data'], 'data')->isArray() | ||
->keyExists('message_id') | ||
->keyExists('type') | ||
->keyExists('attributes') | ||
->verifyNow(); | ||
|
||
Assert::lazy()->tryAll() | ||
->that($content['data']['message_id'], 'message_id')->uuid() | ||
->that($content['data']['type'], 'type')->string()->notEmpty() | ||
->that($content['data']['attributes'], 'attributes')->isArray() | ||
->verifyNow(); | ||
} | ||
} |