baywa-re-lusy/behat-contexts

BayWa r.e. Behat Contexts

2.7.0 2024-08-28 10:27 UTC

README

CircleCI

此仓库提供不同的 Behat Contexts,其中包含常见的测试步骤,这些步骤可以在不同的项目中重用。

安装

通过 Composer 安装此包

$ composer require --dev lusy/behat-contexts

在您的 behat.yml 文件中,添加以下内容:

default:
  ...
  suites:
    api_features:
      contexts:
        ...
        - BayWaReLusy\BehatContext\HalContext
        - BayWaReLusy\BehatContext\SqsContext
        - BayWaReLusy\BehatContext\ConsoleContext
        ...

HalContext

一个用于解析和测试 HAL 格式 API 响应的上下文

在您的 FeatureContext 中,添加以下内容:

use BayWaReLusy\BehatContext\HalContext\HalContextAwareTrait;
use BayWaReLusy\BehatContext\HalContext\HalContextAwareInterface;

class FeatureContext implements
    ...
    HalContextAwareInterface
    ...
{
    use HalContextAwareTrait;
    
    ...
    
    /**
     * @BeforeScenario
     */
    public function gatherContexts(\Behat\Behat\Hook\Scope\BeforeScenarioScope $scope)
    {
        ...
        $this->gatherHalContext($scope);
        $this->getHalContext()
            ->setJsonFilesPath(<path to directory with JSON files>)
            ->setBaseUrl(<API Base URL>)
            ->setBearerToken(<API Bearer Token>);
        ...
    }
}

当您从您的 API 收到响应时,将其传递给上下文

/** @var Psr\Http\Message\ResponseInterface */
$apiResponse = ...

$this->getHalContext()->setLastResponse($apiResponse);

您可以通过编写以下内容在 URL 中添加占位符:

When I send a "GET" request to "/resource-url/{MY_PLACEHOLDER}"

并通过以下方式添加相应的值:

$this->getHalContext()->addPlaceholder('MY_PLACEHOLDER', '<placeholder value>');

AuthContext

一个用于使用通用身份验证服务器(OpenID Connect/OAuth2)登录的上下文,通过用户名/密码或作为机器对机器客户端。需要先初始化 HalContext

在您的 FeatureContext 中,添加以下内容:

use BayWaReLusy\BehatContext\AuthContext\AuthContextAwareTrait;
use BayWaReLusy\BehatContext\AuthContext\AuthContextAwareInterface;
use BayWaReLusy\BehatContext\AuthContext\MachineToMachineCredentials;
use BayWaReLusy\BehatContext\AuthContext\UserCredentials;

class FeatureContext implements
    ...
    AuthContextAwareInterface
    ...
{
    use AuthContextAwareTrait;
    
    ...
    
    /**
     * @BeforeScenario
     */
    public function gatherContexts(\Behat\Behat\Hook\Scope\BeforeScenarioScope $scope)
    {
        ...
        $this->gatherAuthContext($scope);
        $this->getAuthContext()
            ->setHalContext($this->getHalContext())
            ->setServerAddress(<Auth Server address>)
            ->setTokenEndpoint(<Auth Server Token endpoint>)
            ->setTokenEndpoint(<Auth Server Token endpoint>)
            ->addMachineToMachineCredentials(new MachineToMachineCredentials(
                '<Client name describing the client>',
                '<Auth Client ID>',
                '<Auth Client Secret>'
            ))
            ->addUserCredentials(new UserCredentials(
                '<User Login>',
                '<User Password>',
                '<Auth Client ID>'
            ));
        ...
    }
}

SqsContext

一个用于使用 AWS SQS 兼容队列(如 ElasticMQ)的上下文

在您的 FeatureContext 中,添加以下内容:

use BayWaReLusy\BehatContext\SqsContext\SqsContextAwareTrait;
use BayWaReLusy\BehatContext\SqsContext\SqsContextAwareInterface;
use BayWaReLusy\BehatContext\SqsContext\QueueUrl;

class FeatureContext implements
    ...
    SqsContextAwareInterface
    ...
{
    use SqsContextAwareTrait;
    
    ...
    
    /**
     * @BeforeScenario
     */
    public function gatherContexts(\Behat\Behat\Hook\Scope\BeforeScenarioScope $scope)
    {
        ...
        $queueService = ... // <== instance of BayWaReLusy\QueueTools\QueueService
        
        $this->gatherSqsContext($scope);
        $this->getSqsContext()
            ->setQueueService($queueService)
            ->setSqsEndpoint($sqsEndpoint) // <== optional Hostname of the SQS endpoint (e.g. "http://baywa_tms_elasticmq:9324")
            ->setAwsRegion(<AWS Region>)
            ->setAwsKey(<AWS Key>)
            ->setAwsSecret(<AWS Secret>)
            ->addQueue(new QueueUrl('queueName', $queueUrl));
        ...
    }
}

在每个场景之前清除队列,请使用以下代码:

/**
 * @BeforeScenario
 */
public function clearAllQueues(): void
{
    // Clear all queues
    $this->sqsContext->clearAllQueues();
}

ConsoleContext

一个包含测试控制台路由步骤的上下文

在您的 FeatureContext 中,添加以下内容:

use BayWaReLusy\BehatContext\ConsoleContext\ConsoleContextAwareTrait;
use BayWaReLusy\BehatContext\ConsoleContext\ConsoleContextAwareInterface;

class FeatureContext implements
    ...
    ConsoleContextAwareInterface
    ...
{
    use ConsoleContextAwareTrait;
    
    ...
    
    /**
     * @BeforeScenario
     */
    public function gatherContexts(\Behat\Behat\Hook\Scope\BeforeScenarioScope $scope)
    {
        ...
        $this->gatherConsoleContext($scope);
        ...
    }
}