fittinq/symfony-behat

此包的最新版本(5.1.3)没有可用的许可证信息。

5.1.3 2024-09-16 17:22 UTC

README

简介

欢迎使用 Symfony Behat Bundle!这个强大的工具简化了使用 Behat 在 Symfony 应用程序中进行身份验证、服务和服务模拟的测试。本 README 将指导您如何安装、配置和使用此包。

目录

安装

要将此包添加到您的 Symfony 项目中,请运行以下 Composer 命令

composer require fittinq/symfony-behat

认证器上下文

认证器配置

为了配置您的项目以进行认证器测试,将认证器包添加到 Behat 设置中。更新您的 behat.ymlbehat.yaml 如下

default:
   default:
   paths:
      - behat/features
   contexts:
    - Fittinq\Symfony\Behat\Authenticator\Context\AuthenticatorApiContext
    - Fittinq\Symfony\Behat\Authenticator\Context\AuthenticatorDatabaseContext
    - Fittinq\Symfony\Behat\Authenticator\Context\AuthenticatorUserContext
    - Fittinq\Symfony\Behat\Authenticator\Context\AuthenticatorRoleContext
    - Fittinq\Symfony\Behat\Authenticator\Context\AuthenticatorFrontendContext

认证器使用案例

认证器角色上下文

认证器角色上下文专注于管理角色,并提供添加角色和验证其存在的步骤。以下是一个示例用法

Given there are roles
  | name       |
  | ROLE_USER  |
  | ROLE_ADMIN |
  | ROLE_EDITOR|

认证器用户上下文

此上下文是为管理与用户相关的 Behat 步骤而设计的。它包括添加用户和通过 API 进行身份验证的步骤。示例用法

Given there are users
  | username | password | roles           |
  | user1    | secret   | ROLE_USER       |
  | admin    | password | ROLE_ADMIN      |
  | editor   | 123456   | ROLE_EDITOR     |

认证器前端上下文

认证器前端上下文用于测试前端交互,如用户登录和检查 HTTP 状态码及页面内容。示例用法

Given user user1 logs in to the frontend app
Then the current page should contain text "Welcome, user1!"

服务上下文

服务配置

为了配置您的项目以进行服务测试,更新您的 Behat 设置如下

default:
   default:
   paths:
      - behat/features
   contexts:
      - Fittinq\Symfony\Behat\Service\Context\ServiceContext

服务使用案例

添加服务

您可以使用类似以下 Gherkin 场景添加服务

Given there are services
   | name     | url                 |
   | service1 | http://service1.com |
   | service2 | http://service2.com |

标记服务不可用

使用类似以下 Gherkin 场景模拟服务不可用

Given service1 is unavailable

服务模拟上下文

服务模拟配置

为了配置您的项目以进行服务模拟,更新您的 Behat 设置如下

default:
   default:
   paths:
      - behat/features
   contexts:
      - Fittinq\Symfony\Behat\ServiceMock\Context\ServiceMockContext

服务模拟使用案例

设置服务响应

当调用服务时,您可以使其响应特定的数据。示例

When service1 service responds
"""
{
  "method": "POST",
  "host": "http://service-mock",
  "uri": "/api/testing",
  "headers": [],
  "httpStatusCode": 200,
  "body": {
    "content": "The request was successful"
    }
}
"""

检查是否向服务发出请求

您可以使用特定数据验证是否向服务发出请求。示例

Then a request should have been made to service2
"""
 {
   "method": "POST",
   "host": "http://service-mock",
   "uri": "/api/create",
   "invocationType": "once",
   "headers": [],
   "requests": [
      {
          "headers": {},
          "body": {
            "name": "test"
          }
      }
   ]
  }
"""