pccomponentes/open-api-messaging-context

用于验证与 OpenApi 架构消息的 Behat 上下文

v3.3.0 2024-09-24 15:04 UTC

README

在 Behat 中几乎没有上下文来验证根据 AsyncApi 和 OpenApi 规范发布的消息和 HTTP 响应。

安装

composer require --dev pccomponentes/open-api-messaging-context

配置

此包使用 Friends Of Behat Symfony 扩展,您必须查看如何使用此扩展配置您的 Behat。

如何使用

在测试环境中将 SpyMiddleware 定义为服务

services:
  spy.message.middleware:
    class: PcComponentes\OpenApiMessagingContext\Messaging\SpyMiddleware

test 环境的 messenger.yaml 配置中添加 SpyMiddleware 作为中间件。

framework:
  messenger:
    buses:
      messenger.bus.event-broker:
        middleware:
          - spy.message.middleware
      messenger.bus.command:
        middleware:
          - spy.message.middleware

在您的 behat.yml 中配置上下文

default:
  suites:
    default:
      contexts:
        - PcComponentes\OpenApiMessagingContext\Behat\MessageValidatorOpenApiContext:
          - '%%kernel.project_dir%%'
          - '@spy.message.middleware'

然后使用 Then 语句来验证消息

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"

您的架构必须根据 https://www.asyncapi.com/ 规范编写。

适用于版本架构 1.2.02.0.0

可用上下文

MessageValidatorOpenApiContext

检查与您的 asyncapi 文件匹配的已发送消息的内容

Then the published message "pccomponentes.example.1.domain_event.resource.resource_created" should be valid according to swagger "docs/asyncapi.yml"

检查消息是否已发送

Then the message "pccomponentes.example.1.domain_event.resource.resource_created" should be dispatched

检查消息是否未发送

Then the message "pccomponentes.example.1.domain_event.resource.resource_created" should not be dispatched

配置

- PcComponentes\OpenApiMessagingContext\Behat\MessageValidatorOpenApiContext:
  - '%%kernel.project_dir%%'
  - '@spy.message.middleware'

ResponseValidatorOpenApiContext

检查您的 openapi 文件中是否记录了 HTTP 请求

Then the request should be valid according to OpenApi "docs/openapi.yml" with path "/your/openapi/path/"

检查您的 openapi 文件中是否记录了 HTTP 响应

Then the response should be valid according to OpenApi "docs/openapi.yml" with path "/your/openapi/path/"

此外,您可以使用以下方式同时检查请求和响应

Then the request and response should be valid according to OpenApi "docs/openapi.yml" with path "/your/openapi/path/"

配置

- PcComponentes\OpenApiMessagingContext\Behat\ResponseValidatorOpenApiContext:
  - '%%kernel.project_dir%%'

SimpleMessageContext

SimpleMessage 输入的 When 步骤

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

这可以与 MessageValidatorOpenApiContext 中的 Then 步骤结合使用

配置

- PcComponentes\OpenApiMessagingContext\Behat\SimpleMessageContext:
  - '@messenger.bus.command' ##Your command bus with spy.message.middleware
  - '@your.simple_message_deserializer.service'

提示 如果您正在使用 pccomponentes/messenger-bundle,则可以使用 @pccom.messenger_bundle.simple_message.serializer.stream_deserializer 作为反序列化服务

AggregateMessageContext

AggregateMessage 输入的 When 步骤

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

这可以与 MessageValidatorOpenApiContext 中的 Then 步骤结合使用

配置

- PcComponentes\OpenApiMessagingContext\Behat\AggregateMessageContext:
  - '@messenger.bus.events' ## Your event bus with spy.message.middleware
  - '@your.aggregate_message_deserializer.service'

提示 如果您正在使用 pccomponentes/messenger-bundle,则可以使用 @pccom.messenger_bundle.aggregate_message.serializer.stream_deserializer 作为反序列化服务