Skip to content

Commit

Permalink
Merge pull request #3 from PcComponentes/feature/add-aggregate-and-si…
Browse files Browse the repository at this point in the history
…mple-message-context

Feature/add aggregate and simple message context
  • Loading branch information
AaronBernabeu authored Apr 23, 2020
2 parents f2d862f + d7b9838 commit 5cdd41f
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
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 \
Expand Down
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
},
"require": {
"ext-json": "*",
"php": "^7.2",
"php": "^7.3",
"symfony/yaml": "^4.0",
"symfony/messenger": "^4.0",
"justinrainbow/json-schema": "^5.2",
Expand All @@ -30,7 +30,8 @@
"behat/mink-browserkit-driver": "^1.3",
"behat/symfony2-extension": "^2.1",
"behat/mink-extension": "^2.3",
"pccomponentes/ddd": "^1.2"
"pccomponentes/ddd": "^1.2",
"beberlei/assert": "^3.2"
},
"require-dev": {
"pccomponentes/ganchudo": "^1.0",
Expand Down
75 changes: 75 additions & 0 deletions src/Behat/AggregateMessageContext.php
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();
}
}
66 changes: 66 additions & 0 deletions src/Behat/SimpleMessageContext.php
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();
}
}

0 comments on commit 5cdd41f

Please sign in to comment.