baywa-re-lusy / behat-contexts
BayWa r.e. Behat Contexts
Requires
- php: >=8.1
- ext-curl: *
- ext-json: *
- ext-openssl: *
Requires (Dev)
- composer-runtime-api: ^2.0
- baywa-re-lusy/queue: ^1.1
- behat/behat: ^3.8
- guzzlehttp/guzzle: ^7.4
- mtdowling/jmespath.php: ^2.6
- phpstan/phpstan: ^1.5
- ramsey/uuid: ^4.3
- squizlabs/php_codesniffer: ^3.6
- dev-main
- 2.x-dev
- 2.8.0
- 2.7.0
- 2.6.1
- 2.6.0
- 2.5.2
- 2.5.1
- 2.5.0
- 2.4.1
- 2.4.0
- 2.3.1
- 2.3.0
- 2.2.0
- 2.1.0
- 2.0.2
- 2.0.1
- 2.0.0
- 1.13.0
- 1.12.1
- 1.12.0
- 1.11.0
- 1.10.0
- 1.9.2
- 1.9.1
- 1.9.0
- 1.8.2
- 1.8.1
- 1.8.0
- 1.7.0
- 1.6.1
- 1.6.0
- 1.5.0
- 1.4.2
- 1.4.1
- 1.4.0
- 1.3.0
- 1.2.0
- 1.1.1
- 1.1.0
- 1.0.9
- 1.0.8
- 1.0.7
- 1.0.6
- 1.0.5
- 1.0.4
- 1.0.3
- 1.0.2
- 1.0.1
- 1.0.0
- dev-feature/NSEMEA-1367-missing-test-steps
- dev-feature/inputArguments
This package is auto-updated.
Last update: 2025-02-20 11:11:33 UTC
README
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:
- https://de.wikipedia.org/wiki/Hypertext_Application_Language
- https://stateless.group/hal_specification.html
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); ... } }