baywa-re-lusy/behat-contexts

BayWa r.e. Behat Contexts

2.6.0 2024-04-10 13:20 UTC

README

CircleCI

This repository provides you with different Behat Contexts containing common test steps that can be reused across different projects.

Installation

Install the package via Composer:

$ composer require --dev lusy/behat-contexts

In your behat.yml, add the following:

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

HalContext

A Context to parse & test API responses in HAL format:

In your FeatureContext, add the following:

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>);
        ...
    }
}

And when you receive a reponse from your API, pass it to the context:

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

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

You can add placeholders to your URL by writing:

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

And add the corresponding value with:

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

AuthContext

A Context to login to a generic Auth Server (OpenID Connect/OAuth2) with Login/Password or as a Machine-to-Machine client. The HalContext needs to be initialized first.

In your FeatureContext, add the following:

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

A Context to use AWS SQS compatible queues like e.g. ElasticMQ

In your FeatureContext, add the following:

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));
        ...
    }
}

To clear the queues before each Scenario, use the following code:

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

ConsoleContext

A Context containing steps to test console routes

In your FeatureContext, add the following:

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);
        ...
    }
}