dev-master
9999999-dev https://github.com/alexdesignworks/behat-phpserverDrupal extension for Behat
GPL-2.0+
The Requires
by Alex Skrypnyk
server behat
Wallogit.com
2017 © Pedro Peláez
Drupal extension for Behat
PhpServerContext context
to start and stop PHP server:
ApiServerContext context
to serve queued API responses for API mocking:
composer require --dev drevops/behat-phpserver
PhpServerContextUsed to serve assets from a pre-defined document root., (*1)
default:
suites:
default:
contexts:
- DrevOps\BehatPhpServer\PhpServerContext:
webroot: '%paths.base%/tests/behat/fixtures' # Path to the PHP server document root
protocol: http # PHP server protocol
host: 0.0.0.0 # PHP server host
port: 8888 # PHP server port
debug: false # Enable debug mode for verbose output
ApiServerContextUsed to serve a pre-set API responses from a pre-defined document root., (*2)
default:
suites:
default:
contexts:
- DrevOps\BehatPhpServer\ApiServerContext:
webroot: '%paths.base%/apiserver' # Path to the API server document root
protocol: http # API PHP server protocol
host: 0.0.0.0 # API PHP server host
port: 8889 # API PHP server port
debug: false # API Enable debug mode for verbose output
paths: # Path(s) to fixture files for API responses
- '%paths.base%/tests/behat/fixtures'
- '%paths.base%/tests/behat/fixtures2'
API responses can be queued up in the API server server by sending
PUT requests to /admin/responses as an array of the expected responses
using following JSON format:, (*3)
[
{
"code": 200,
"reason": "OK",
"headers": {},
"body": ""
},
{
"code": 404,
"reason": "Not found",
"headers": {
},
"body": ""
}
]
The ApiServerContext provides several step definitions to make it easier to
work with the API server:, (*4)
# Check if the API server is running.
Given the API server is running
# Queue up a single API response.
Given API will respond with:
"""
{
"code": 200,
"headers": {
"Content-Type": "application/json"
},
"body": {
"Id": "test-id-1",
"Slug": "test-slug-1"
}
}
"""
# Queue up a single API response with minimal configuration.
Given API will respond with:
"""
{
"code": 200
}
"""
# Queue up a single API response with JSON body.
Given API will respond with JSON:
"""
{
"Id": "test-id-1",
"Slug": "test-slug-1"
}
"""
# Queue up a single API response with JSON body and expected code.
Given API will respond with JSON and 201 code:
"""
{
"Id": "test-id-2",
"Slug": "test-slug-2"
}
"""
# Reset the API server by clearing all responses and requests.
Given the API server is reset
# Queue up a file response with automatic content type detection.
Given API will respond with file "test_data.json"
# Queue up a file response with a custom response code.
Given API will respond with file "test_content.xml" and 201 code
# Assert the number of requests received by the API server.
Then the API server should have 3 received requests
# Assert the number of responses queued in the API server.
Then the API server should have 0 queued responses
See this test feature for more examples., (*5)
The apiWillRespondWithFile step definition allows you to respond with the contents of a file
from one of the configured fixture paths. The context will automatically detect the appropriate
content type based on the file extension:, (*6)
.json → application/json
.xml → application/xml
.html, .htm → text/html
.txt → text/plain
application/octet-stream
Multiple fixture paths can be configured in the behat.yml file. The context will search for the
file in each path in the order specified until it finds a match., (*7)
The resetApi step definition allows you to clear all queued responses and request history in the API server.
This is useful for ensuring a clean state between test steps, especially when multiple scenarios
interact with the API server:, (*8)
# Clear existing state before setting up a new test Given API server is reset And API will respond with file "test_data.json" When I send a GET request to "/"
For more information on supported RESTful API endpoints, see the API server implementation., (*9)
If you need to access the API server URL from your context to update the base
URL of your API client, you can do so by using beforeScenario in your
FeatureContext class:, (*10)
<?php
declare(strict_types=1);
use Behat\Behat\Context\Context;
use Behat\Behat\Context\Environment\InitializedContextEnvironment;
use Behat\Behat\Hook\Scope\BeforeScenarioScope;
use DrevOps\BehatPhpServer\ApiServerContext;
use DrevOps\BehatPhpServer\PhpServerContext;
class FeatureContext implements Context {
/**
* The PHP server URL.
*/
protected string $phpServerUrl;
/**
* The API server URL.
*/
protected string $apiServerUrl;
/**
* Initialize the context.
*
* @beforeScenario
*/
public function beforeScenarioInit(BeforeScenarioScope $scope): void {
$environment = $scope->getEnvironment();
if (!$environment instanceof InitializedContextEnvironment) {
throw new \Exception('Environment is not initialized');
}
$context = $environment->getContext(PhpServerContext::class);
$this->phpServerUrl = $context->getServerUrl();
$context = $environment->getContext(ApiServerContext::class);
$this->apiServerUrl = $context->getServerUrl();
}
}
composer lint composer lint-fix
composer test composer test-bdd
This repository was created using the Scaffold project template, (*11)
Drupal extension for Behat
GPL-2.0+
server behat