Search by

libresign / behat-builtin-extension

libresign

Behat extension to run php built-in web server

Package info

github.com/LibreSign/behat-builtin-extension

pkg:composer/libresign/behat-builtin-extension

Statistics

Installs: 73 693

Dependents: 1

Suggesters: 0

Stars: 3

Open Issues: 1

v1.0.0 2026-11-05 17:17 UTC

README

Behat

Extension to use built-in PHP server on Behat tests

Instalation

composer require libresign/behat-builtin-extension
vendor/bin/behat --init

Configuration

Add the extension to your behat.php (Behat 4 and PHP 8.2 or later):

<?php

use Behat\Config\Config;
use Behat\Config\Extension;
use Behat\Config\Profile;
use PhpBuiltin\Server;

return (new Config())
    ->withProfile((new Profile('default'))
        ->withExtension(new Extension(Server::class, [
            'verbose' => false,
            'rootDir' => '/var/www/html',
            'host' => 'localhost',
        ])));

Config values

config default Environment Description
verbose false BEHAT_VERBOSE Enables/disables verbose mode
rootDir /var/www/html BEHAT_ROOT_DIR Specifies http root dir
host localhost BEHAT_HOST Host domain or IP
runAs BEHAT_RUN_AS The username to be used to run the built-in server
workers 0 BEHAT_WORKERS The quantity of workers to use. More informations here searching by PHP_CLI_SERVER_WORKERS

You can also use -v option to enable verbose mode. Example

vendor/bin/behat -v

The verbose mode will show:

  • The rootDir, host, runAs and workers used
  • When the PHP built-in server starts: PID, host, port, worker count and log path
  • PHP built-in server stdout/stderr captured for the suite
  • Process exit status when the server terminates
  • A clear message when teardown finds that the server process is already gone (instead of only kill: No such process)

Usage example

<?php

use Behat\Behat\Context\Context;
use PhpBuiltin\RunServerListener;

/**
 * Defines application features from the specific context.
 */
class FeatureContext implements Context
{
    private string $baseUrl;
    public function __construct()
    {
        $this->baseUrl = RunServerListener::getServerRoot();
    }

    public function sendRequest(string $verb, string $url, ?array $body = null, array $headers = []): void
    {
        $client = new Client();

        $fullUrl = $this->baseUrl . ltrim($url, '/');

        $options['headers'] = $headers;

        if (is_array($body)) {
            $options['form_params'] = $body;
        }

        try {
            $this->response = $client->{$verb}($fullUrl, $options);
        } catch (ClientException $e) {
            $this->response = $e->getResponse();
        } catch (ServerException $e) {
            $this->response = $e->getResponse();
        }
    }
}