From 6b859d56f38e1fe199464138b80b8594632b1823 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Gilberto=20Mullor?= Date: Sun, 10 Oct 2021 14:32:31 +0200 Subject: [PATCH 1/2] Add new features to test failing messages --- README.md | 2 +- src/Behat/SimpleMessageContext.php | 37 ++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 19e6520..fc808de 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/Behat/SimpleMessageContext.php b/src/Behat/SimpleMessageContext.php index 3e17f96..40fb9b2 100644 --- a/src/Behat/SimpleMessageContext.php +++ b/src/Behat/SimpleMessageContext.php @@ -14,6 +14,7 @@ final class SimpleMessageContext implements Context { private MessageBusInterface $bus; private SimpleMessageUnserializable $simpleMessageUnserializable; + private \Throwable $lastException; public function __construct( MessageBusInterface $bus, @@ -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 dispatchMessage(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); From e1df622b6574cc745f267f6917ce4c32c35b70f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Gilberto=20Mullor?= Date: Sun, 10 Oct 2021 14:37:31 +0200 Subject: [PATCH 2/2] Fix name issue --- src/Behat/SimpleMessageContext.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Behat/SimpleMessageContext.php b/src/Behat/SimpleMessageContext.php index 40fb9b2..8b29056 100644 --- a/src/Behat/SimpleMessageContext.php +++ b/src/Behat/SimpleMessageContext.php @@ -38,7 +38,7 @@ public function dispatchMessage(PyStringNode $payload): void /** * @When I receive a simple failing message with payload: */ - public function dispatchMessage(PyStringNode $payload): void + public function dispatchFailingMessage(PyStringNode $payload): void { try { $this->dispatchMessage($payload);