kielabokkie/jsonapi-behat-extension

用于测试 JSON API 的 Behat 扩展

v4.0.0 2022-06-29 08:02 UTC

README

Author Packagist Version Software License

JSON API Behat 扩展为针对 JSON API 的常见测试场景提供步骤定义。它提供了通过 OAuth 处理认证的简单方法。

要求

  • PHP >= 5.4

安装

推荐通过运行 composer require 命令进行安装。这将安装此扩展的最新稳定版本。

composer require kielabokkie/jsonapi-behat-extension --dev

配置

要使用此扩展,您需要在您的 behat.yml 文件中将其添加到 extensions 下。

default:
    extensions:
        Kielabokkie\BehatJsonApi: ~

默认参数

开箱即用,此扩展具有以下默认参数

您可以在 behat.yml 文件中根据需要覆盖任何这些参数。

default:
    extensions:
        Kielabokkie\BehatJsonApi:
            base_url: http://api.yourapp.dev
            parameters:
                oauth:
                    uri: /v1/oauth/token
                    client_id: myClientId
                    client_secret: myClientSecret
                    use_bearer_token: true
                    password_grant_requires_client_credentials: true

可选参数

为了避免在每次 API 调用时都使用 OAuth 获取访问令牌,您还可以在参数中指定可选的 access_token

default:
    extensions:
        Kielabokkie\BehatJsonApi:
            parameters:
                access_token: 90dabed99acef998fd3e35280f2a0a3c30c00c8d

使用方法

要使用此扩展提供的步骤定义,只需在您的套件中加载上下文类

default:
  suites:
    default:
      contexts:
        - Kielabokkie\BehatJsonApi\Context\JsonApiContext

然后您将能够访问以下步骤定义

@Given I use the access token
@Given I use access token :token
@Given I oauth with :username and :password
@Given I oauth with :username and :password and scope :scope
@Given I oauth using the client credentials grant
@Given I oauth using the client credentials grant with scope :scope
@Given I oauth using the client credentials grant with :id and :secret
@Given I oauth using the client credentials grant with :id and :secret and scope :scope
@Given I add a :header header with the value :value
@Given I have the payload:
@When /^I request "(GET|PUT|PATCH|POST|DELETE) ([^"]*)"$/
@Then I get a :statuscode response
@Then scope into the :scope property
@Then scope into the first :scope element
@Then the structure matches:
@Then the :field property is an object
@Then the :field property is an array
@Then the :field property is an array with :count items
@Then the :field property is an empty array
@Then the :field property is an integer
@Then the :field property is a integer equaling/equalling :expected
@Then the :field property is a string
@Then the :field property is a string equaling/equalling :expected
@Then the :field property is a boolean
@Then the :field property is a boolean equaling/equalling :expected
@Then /^echo last request$/
@Then /^echo last response$

注意:最后两个定义仅用于调试目的,并且仅在您使用 Behat 的 pretty 格式化选项时显示输出,即 ./vendor/bin/behat -f pretty

要获取所有可用步骤定义(包括示例)的列表,可以运行以下命令

$ vendor/bin/behat -di

覆盖基本 URL

在某些情况下,您可能希望覆盖特定套件的基本 URL。以下是一个 behat.yml 文件的示例。在此示例中,自定义 URL http://hooks.yourapp.dev 被传递到 hooks 套件中的 FeatureContext。

default:
    autoload:
        - %paths.base%/tests/Behat/features/bootstrap
    suites:
        api:
            paths:
                - %paths.base%/tests/Behat/features/api
            contexts:
                - Kielabokkie\BehatJsonApi\Context\JsonApiContext: ~
        hooks:
            paths:
                - %paths.base%/tests/Behat/features/hooks
            contexts:
                - Kielabokkie\BehatJsonApi\Context\JsonApiContext:
                    - http://hooks.yourapp.dev
    extensions:
        Kielabokkie\BehatJsonApi: ~