Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ When I receive a simple message with payload:
"""
Then the message "pccomponentes.example.1.domain_event.resource.resource_created" should be dispatched
```
This is useful to combine it with `Then` step in `MessageValidatorOpenApiContext`
This is useful to combine it with `Then` step in `MessageValidatorOpenApiContext`, also you can combine them with expected message exceptions.

Configuration:
```yaml
Expand Down
37 changes: 37 additions & 0 deletions src/Behat/SimpleMessageContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ final class SimpleMessageContext implements Context
{
private MessageBusInterface $bus;
private SimpleMessageUnserializable $simpleMessageUnserializable;
private \Throwable $lastException;

public function __construct(
MessageBusInterface $bus,
Expand All @@ -34,6 +35,42 @@ public function dispatchMessage(PyStringNode $payload): void
$this->bus->dispatch($message);
}

/**
* @When I receive a simple failing message with payload:
*/
public function dispatchFailingMessage(PyStringNode $payload): void
{
try {
$this->dispatchMessage($payload);
} catch (\Throwable $exception) {
$this->lastException = $exception;

return;
}

$message = 'Expecting a failed message';

throw new \Exception($message);
}

/**
* @Then exception class for the last message should be :exceptionType
*/
public function checkExceptionClass($exceptionType): void
{
if (get_class($this->lastException) === $exceptionType) {
return;
}

$message = \sprintf(
'Expecting a failed message with exception \'%s\' but got \'%s\'',
$exceptionType,
get_class($this->lastException),
);

throw new \Exception($message);
}

private function payloadToStream(string $rawPayload): SimpleMessageStream
{
$payload = \json_decode($rawPayload, true, 512, \JSON_THROW_ON_ERROR);
Expand Down