pccomponentes / open-api-messaging-context
Behat context for validating messages with OpenApi schema
Installs: 52 231
Dependents: 0
Suggesters: 0
Security: 0
Stars: 9
Watchers: 5
Forks: 3
Open Issues: 1
Requires
- php: ^8.0
- ext-json: *
- 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
- pccomponentes/ddd: ^3.0 || ^4.0
- symfony/cache: ^5.0 || ^6.0 || ^7.0
- symfony/messenger: ^5.0 || ^6.0 || ^7.0
- symfony/yaml: ^5.0 || ^6.0 || ^7.0
Requires (Dev)
- pccomponentes/coding-standard: ^3.0
- phpunit/phpunit: ^9.0
- symfony/var-dumper: ^5.0 || ^6.0 || ^7.0
- dev-master
- v3.3.0
- v3.2.0
- v3.1.0
- v3.0.0
- v2.11.0
- v2.10.0
- v2.9.0
- v2.8.1
- v2.8.0
- v2.7.0
- v2.6.0
- v2.5.0
- v2.4.0
- v2.3.0
- v2.2.0
- v2.1.0
- v2.0.1
- v2.0.0
- v1.0.0
- v0.1.1
- v0.1.0
- v0.0.8
- v0.0.7
- v0.0.6
- v0.0.5
- v0.0.4
- v0.0.3
- v0.0.2
- v0.0.1
- dev-feature/add-request-validation-using-openapi
- dev-feature/cache-schemas
- dev-feature/add-failing-messages-validation
This package is auto-updated.
Last update: 2024-10-24 15:16:50 UTC
README
Little context in behat for validate published messages and http responses according to an AsyncApi and OpenApi specification.
Installation
composer require --dev pccomponentes/open-api-messaging-context
Configuration
This package uses Friends Of Behat Symfony Extension, you must see how to configure your behat with this extension.
How to use
Define SpyMiddleware as a service in your test environment:
services: spy.message.middleware: class: PcComponentes\OpenApiMessagingContext\Messaging\SpyMiddleware
Add SpyMiddleware as a middleware in messenger.yaml
configuration in test
environment.
framework: messenger: buses: messenger.bus.event-broker: middleware: - spy.message.middleware messenger.bus.command: middleware: - spy.message.middleware
Configure the context in your behat.yml
default: suites: default: contexts: - PcComponentes\OpenApiMessagingContext\Behat\MessageValidatorOpenApiContext: - '%%kernel.project_dir%%' - '@spy.message.middleware'
And use the Then
statement for validate messages:
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.
Available for version schemas 1.2.0
and 2.0.0
.
Available contexts
MessageValidatorOpenApiContext
Check the content of the dispatched message that matches with your asyncapi file:
Then the published message "pccomponentes.example.1.domain_event.resource.resource_created" should be valid according to swagger "docs/asyncapi.yml"
Check if message has dispatched:
Then the message "pccomponentes.example.1.domain_event.resource.resource_created" should be dispatched
Check if message has not been dispatched:
Then the message "pccomponentes.example.1.domain_event.resource.resource_created" should not be dispatched
Configuration:
- PcComponentes\OpenApiMessagingContext\Behat\MessageValidatorOpenApiContext: - '%%kernel.project_dir%%' - '@spy.message.middleware'
ResponseValidatorOpenApiContext
Check if http requests are documented in your openapi file:
Then the request should be valid according to OpenApi "docs/openapi.yml" with path "/your/openapi/path/"
Check if http responses are documented in your openapi file:
Then the response should be valid according to OpenApi "docs/openapi.yml" with path "/your/openapi/path/"
Additionally, you can check both the request and response with:
Then the request and response should be valid according to OpenApi "docs/openapi.yml" with path "/your/openapi/path/"
Configuration:
- PcComponentes\OpenApiMessagingContext\Behat\ResponseValidatorOpenApiContext: - '%%kernel.project_dir%%'
SimpleMessageContext
When
step for SimpleMessage input:
When I receive a simple message with payload: """ { "data": { "message_id": "d2439fd8-be54-4233-ba59-fe3187620505", "type": "pccomponentes.example.1.command.resource.create_resource", "attributes": { "id": "3c44e76e-1369-4a95-84ac-3a78f9c1f354", "my_awesome_data": "foo", } } } """ 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
Configuration:
- PcComponentes\OpenApiMessagingContext\Behat\SimpleMessageContext: - '@messenger.bus.command' ##Your command bus with spy.message.middleware - '@your.simple_message_deserializer.service'
TIP If you are using pccomponentes/messenger-bundle you can use @pccom.messenger_bundle.simple_message.serializer.stream_deserializer
for deserializer service
AggregateMessageContext
When
step for AggregateMessage input:
When I receive an aggregate message with payload: """ { "data": { "message_id": "2b8c7e00-219e-4d12-8b0e-dac2cc432410", "type": "pccomponentes.example.1.domain_event.resource.resource_created", "occurred_on": "1554900327", "attributes": { "aggregate_id": "0e7c57f8-d679-4605-ba27-3008b9636a0a", "status": "OPEN" } } } """ Then the message "pccomponentes.example.1.command.resource.create_resource_projection" should be dispatched
This is useful to combine it with Then
step in MessageValidatorOpenApiContext
Configuration:
- PcComponentes\OpenApiMessagingContext\Behat\AggregateMessageContext: - '@messenger.bus.events' ## Your event bus with spy.message.middleware - '@your.aggregate_message_deserializer.service'
TIP If you are using pccomponentes/messenger-bundle you can use @pccom.messenger_bundle.aggregate_message.serializer.stream_deserializer
for deserializer service