Skip to content

Commit

Permalink
feat: Update dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
zoilomora committed Jul 28, 2023
1 parent 064ae7f commit 1f15105
Show file tree
Hide file tree
Showing 21 changed files with 131 additions and 150 deletions.
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@ default:
And use the `Then` statement for validate messages:

```gherkin
Scenario: My awesome scenario
Given the environment is clean
When I send a "POST" request to "/resource/" with body:
"""
{
"my-awesome-data": "foo",
}
"""
Then the published message "pccomponentes.example.1.domain_event.resource.resource_created" should be valid according to swagger "docs/asyncapi.yml"
Scenario: My awesome scenario
Given the environment is clean
When I send a "POST" request to "/resource/" with body:
"""
{
"my-awesome-data": "foo",
}
"""
Then the published message "pccomponentes.example.1.domain_event.resource.resource_created" should be valid according to swagger "docs/asyncapi.yml"
```

Your schema must be writen according to https://www.asyncapi.com/ specification.
Expand Down
29 changes: 15 additions & 14 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,29 @@
}
},
"require": {
"php": "^8.0",
"ext-json": "*",
"php": "^7.4 || ^8.0",
"symfony/yaml": "^4.4 || ^5.0 || ^6.0",
"symfony/messenger": "^4.4 || ^5.0 || ^6.0",
"beberlei/assert": "^3.3",
"behat/behat": "^3.13",
"friends-of-behat/mink": "^1.10",
"friends-of-behat/mink-browserkit-driver": "^1.6",
"friends-of-behat/mink-extension": "^2.7",
"friends-of-behat/symfony-extension": "^2.4",
"justinrainbow/json-schema": "^5.2",
"behat/behat": "^3.5",
"pccomponentes/ddd": "^2.0 || ^3.0 || ^4.0",
"beberlei/assert": "^3.2",
"friends-of-behat/symfony-extension": "^2.0",
"friends-of-behat/mink": "^1.8",
"friends-of-behat/mink-extension": "^2.4",
"friends-of-behat/mink-browserkit-driver": "^1.4"
"pccomponentes/ddd": "^3.0 || ^4.0",
"symfony/cache-contracts": "^3.0",
"symfony/messenger": "^5.0 || ^6.0",
"symfony/yaml": "^5.0 || ^6.0"
},
"require-dev": {
"pccomponentes/ganchudo": "^1.0",
"phpunit/phpunit": "^9.0",
"symfony/var-dumper": "^4.4|^5.0|^6.0",
"pccomponentes/coding-standard": "^1.0"
"symfony/var-dumper": "^5.0 || ^6.0",
"pccomponentes/coding-standard": "^3.0"
},
"config": {
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true
}
},
"sort-packages": true
}
}
23 changes: 15 additions & 8 deletions src/AsyncApi/AsyncApiParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@

final class AsyncApiParser
{
private array $originalContent;
private array $versionExtractor;

public function __construct(array $originalContent)
public function __construct(private array $originalContent)
{
$this->originalContent = $originalContent;
$this->versionExtractor = [
'1.2' => new V12ChannelExtractor(),
'2.0' => new V20ChannelExtractor(),
Expand Down Expand Up @@ -38,19 +36,25 @@ private function extractVersion(): ChannelExtractor
return $this->versionExtractor['2.0'];
}

throw new \InvalidArgumentException(\sprintf('%s async api version not supported', $this->originalContent['asyncapi']));
throw new \InvalidArgumentException(
\sprintf('%s async api version not supported', $this->originalContent['asyncapi']),
);
}

private function extractData(array $data): array
{
$aux = [];

foreach ($data as $key => $elem) {
if ($key === '$ref') {
if ('$ref' === $key) {
$aux = $this->findDefinition($elem);

continue;
}

if (\is_array($elem)) {
$aux[$key] = $this->extractData($elem);

continue;
}

Expand All @@ -72,9 +76,12 @@ private function findDefinition(string $def): array
}

$explodedDef = \explode('/', $cleanDef);
$foundDef = \array_reduce($explodedDef, function ($last, $elem) {
return null === $last ? $this->originalContent[$elem] : $last[$elem];
});
$foundDef = \array_reduce(
$explodedDef,
fn ($last, $elem) => null === $last
? $this->originalContent[$elem]
: $last[$elem],
);

return $this->extractData(\array_key_exists('payload', $foundDef) ? $foundDef['payload'] : $foundDef);
}
Expand Down
3 changes: 2 additions & 1 deletion src/AsyncApi/ChannelExtractor.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php declare(strict_types=1);
<?php
declare(strict_types=1);

namespace PcComponentes\OpenApiMessagingContext\AsyncApi;

Expand Down
8 changes: 6 additions & 2 deletions src/AsyncApi/V12ChannelExtractor.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php declare(strict_types=1);
<?php
declare(strict_types=1);

namespace PcComponentes\OpenApiMessagingContext\AsyncApi;

Expand All @@ -7,7 +8,10 @@ final class V12ChannelExtractor implements ChannelExtractor
public function extract(array $originalContent, string $channel): array
{
$topicName = $channel;
$baseTopic = \array_key_exists('baseTopic', $originalContent) ? $originalContent['baseTopic'] : '';
$baseTopic = \array_key_exists('baseTopic', $originalContent)
? $originalContent['baseTopic']
: '';

if ('' !== $baseTopic) {
$topicName = \preg_replace('/^' . $baseTopic . '\./', '', $topicName);
}
Expand Down
3 changes: 2 additions & 1 deletion src/AsyncApi/V20ChannelExtractor.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php declare(strict_types=1);
<?php
declare(strict_types=1);

namespace PcComponentes\OpenApiMessagingContext\AsyncApi;

Expand Down
15 changes: 4 additions & 11 deletions src/Behat/AggregateMessageContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,17 @@

final class AggregateMessageContext implements Context
{
private MessageBusInterface $bus;
private AggregateMessageUnserializable $aggregateMessageUnserializable;

public function __construct(
MessageBusInterface $bus,
AggregateMessageUnserializable $aggregateMessageUnserializable
private MessageBusInterface $bus,
private AggregateMessageUnserializable $aggregateMessageUnserializable,
) {
$this->bus = $bus;
$this->aggregateMessageUnserializable = $aggregateMessageUnserializable;
}

/**
* @When I receive an aggregate message with payload:
*/
/** @When I receive an aggregate message with payload: */
public function dispatchMessage(PyStringNode $payload): void
{
$message = $this->aggregateMessageUnserializable->unserialize(
$this->payloadToStream($payload->getRaw())
$this->payloadToStream($payload->getRaw()),
);
$this->bus->dispatch($message);
}
Expand Down
43 changes: 17 additions & 26 deletions src/Behat/MessageValidatorOpenApiContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,20 @@

final class MessageValidatorOpenApiContext extends ValidatorApiContext implements Context
{
private string $rootPath;
private SpyMiddleware $spyMiddleware;

public function __construct(string $rootPath, SpyMiddleware $spyMiddleware)
public function __construct(private string $rootPath, private SpyMiddleware $spyMiddleware)
{
$this->rootPath = $rootPath;
$this->spyMiddleware = $spyMiddleware;
}

/**
* @BeforeScenario
*/
/** @BeforeScenario */
public function bootstrapEnvironment(): void
{
$this->spyMiddleware->reset();
}

/**
* @Then the published message :name should be valid according to swagger :dumpPath
*/
/** @Then the published message :name should be valid according to swagger :dumpPath */
public function theMessageShouldBeValidAccordingToTheSwagger($name, $dumpPath): void
{
$path = realpath($this->rootPath . '/' . $dumpPath);
$path = \realpath($this->rootPath . '/' . $dumpPath);
$this->checkSchemaFile($path);

$jsonMessages = $this->spyMiddleware->getMessagesFromName($name);
Expand All @@ -53,46 +44,46 @@ public function theMessageShouldBeValidAccordingToTheSwagger($name, $dumpPath):
}

$jsonValidation = new JsonValidationCollection(...$validations);

if ($jsonValidation->hasAnyError()) {
throw new JsonValidationException($jsonValidation->buildErrorMessage());
}
}

/**
* @Then the message :name should be dispatched
*/
/** @Then the message :name should be dispatched */
public function theMessageShouldBeDispatched(string $name): void
{
if (false === $this->spyMiddleware->hasMessage($name)) {
throw new \Exception(sprintf('Message %s was expected to dispatch, actually not dispatched', $name));
throw new \Exception(
\sprintf('Message %s was expected to dispatch, actually not dispatched', $name),
);
}
}

/**
* @Then the message :name should be dispatched :times times
*/
/** @Then the message :name should be dispatched :times times */
public function theMessageShouldBeDispatchedManyTimes(string $name, int $times): void
{
$countMessages = $this->spyMiddleware->countMessagesFromName($name);

if ($times !== $countMessages) {
throw new \Exception(
sprintf(
\sprintf(
'Message %s was expected to dispatch %d times, actually dispatched %d times.',
$name,
$times,
$countMessages
$countMessages,
),
);
}
}

/**
* @Then the message :name should not be dispatched
*/
/** @Then the message :name should not be dispatched */
public function theMessageShouldNotBeDispatched(string $name): void
{
if (true === $this->spyMiddleware->hasMessage($name)) {
throw new \Exception(sprintf('Message %s was not expected to be dispatched', $name));
throw new \Exception(
\sprintf('Message %s was not expected to be dispatched', $name),
);
}
}
}
13 changes: 3 additions & 10 deletions src/Behat/ResponseValidatorOpenApiContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,11 @@ abstract class ResponseValidatorOpenApiContext extends ValidatorApiContext imple
{
private const HTTP_NO_CONTENT_CODE = 204;

private string $rootPath;

public function __construct(string $rootPath)
public function __construct(private string $rootPath)
{
$this->rootPath = $rootPath;
}

/**
* @Then the JSON response should be valid according to OpenApi :dumpPath schema :schema
*/
/** @Then the JSON response should be valid according to OpenApi :dumpPath schema :schema */
public function theJsonResponseShouldBeValidAccordingToOpenApiSchema($dumpPath, $schema): void
{
$path = \realpath($this->rootPath . '/' . $dumpPath);
Expand All @@ -46,9 +41,7 @@ public function theJsonResponseShouldBeValidAccordingToOpenApiSchema($dumpPath,
}
}

/**
* @Then the response should be valid according to OpenApi :dumpPath with path :openApiPath
*/
/** @Then the response should be valid according to OpenApi :dumpPath with path :openApiPath */
public function theResponseShouldBeValidAccordingToOpenApiWithPath(string $dumpPath, string $openApiPath): void
{
$path = \realpath($this->rootPath . '/' . $dumpPath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ final class MinkResponseValidatorOpenApiContext extends ResponseValidatorOpenApi

private MinkContext $minkContext;

/**
* @BeforeScenario
*/
/** @BeforeScenario */
public function bootstrapEnvironment(BeforeScenarioScope $scope): void
{
$this->minkContext = $scope->getEnvironment()->getContext(MinkContext::class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,9 @@ final class RequestHistoryResponseValidatorOpenApiContext extends ResponseValida
{
private const CONTENT_TYPE_RESPONSE_HEADER_KEY = 'content-type';

private RequestHistory $requestHistory;

public function __construct(string $rootPath, RequestHistory $requestHistory)
public function __construct(string $rootPath, private RequestHistory $requestHistory)
{
parent::__construct($rootPath);

$this->requestHistory = $requestHistory;
}

protected function extractMethod(): string
Expand Down
15 changes: 4 additions & 11 deletions src/Behat/SimpleMessageContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,17 @@

final class SimpleMessageContext implements Context
{
private MessageBusInterface $bus;
private SimpleMessageUnserializable $simpleMessageUnserializable;

public function __construct(
MessageBusInterface $bus,
SimpleMessageUnserializable $simpleMessageUnserializable
private MessageBusInterface $bus,
private SimpleMessageUnserializable $simpleMessageUnserializable,
) {
$this->bus = $bus;
$this->simpleMessageUnserializable = $simpleMessageUnserializable;
}

/**
* @When I receive a simple message with payload:
*/
/** @When I receive a simple message with payload: */
public function dispatchMessage(PyStringNode $payload): void
{
$message = $this->simpleMessageUnserializable->unserialize(
$this->payloadToStream($payload->getRaw())
$this->payloadToStream($payload->getRaw()),
);
$this->bus->dispatch($message);
}
Expand Down
Loading

0 comments on commit 1f15105

Please sign in to comment.