PHPackages                             drevops/behat-phpserver - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. drevops/behat-phpserver

ActiveLibrary[API Development](/categories/api)

drevops/behat-phpserver
=======================

Behat Context to enable PHP server for tests

2.3.0(3mo ago)39.0k↓36.9%2[1 issues](https://github.com/drevops/behat-phpserver/issues)8GPL-2.0-or-laterPHPPHP &gt;=8.2CI passing

Since Apr 19Pushed 1mo ago3 watchersCompare

[ Source](https://github.com/drevops/behat-phpserver)[ Packagist](https://packagist.org/packages/drevops/behat-phpserver)[ Docs](https://github.com/drevops/behat-phpserver)[ GitHub Sponsors](https://github.com/drevops)[ Patreon](https://www.patreon.com/drevops)[ RSS](/packages/drevops-behat-phpserver/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (16)Versions (19)Used By (8)

  ![Yourproject logo](https://camo.githubusercontent.com/84c139639fadbd81f389d89b1ae33928798c027dc2e93bd777fbc50b5b7fcaa2/68747470733a2f2f706c616365686f6c642e6a702f3030303030302f6666666666662f323030783230302e706e673f746578743d42656861742b5048502b736572766572266373733d253742253232626f726465722d7261646975732532322533412532322532303130307078253232253744)

PHP and API server for Behat tests
==================================

[](#php-and-api-server-for-behat-tests)

[![GitHub Issues](https://camo.githubusercontent.com/f20f011b8b279abe9d515365b87ba65f3e943a2ac0f22545aab9de9aad3f0354/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6973737565732f647265766f70732f62656861742d7068707365727665722e737667)](https://github.com/drevops/behat-phpserver/issues)[![GitHub Pull Requests](https://camo.githubusercontent.com/d06a51e1c597279bce440f81d0c6ce00cb5120a39c0590661f8b55a1219b5006/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6973737565732d70722f647265766f70732f62656861742d7068707365727665722e737667)](https://github.com/drevops/behat-phpserver/pulls)[![Test PHP](https://github.com/drevops/behat-phpserver/actions/workflows/test-php.yml/badge.svg)](https://github.com/drevops/behat-phpserver/actions/workflows/test-php.yml)[![codecov](https://camo.githubusercontent.com/a8b1d318c252bdd69504d74e27ac71305992485548fff5d2b51dcbbde9ad0d03/68747470733a2f2f636f6465636f762e696f2f67682f647265766f70732f62656861742d7068707365727665722f6272616e63682f6d61696e2f67726170682f62616467652e7376673f746f6b656e3d4b5a43435a584e354334)](https://codecov.io/gh/drevops/behat-phpserver)[![GitHub release (latest by date)](https://camo.githubusercontent.com/46e91dcde3f3b2f25cb9c860c01cc5ea88e31c9a2f9b81308a32e55a7a960dcc/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f762f72656c656173652f647265766f70732f62656861742d706870736572766572)](https://camo.githubusercontent.com/46e91dcde3f3b2f25cb9c860c01cc5ea88e31c9a2f9b81308a32e55a7a960dcc/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f762f72656c656173652f647265766f70732f62656861742d706870736572766572)[![Total Downloads](https://camo.githubusercontent.com/57c679b34ad806977c93c42b4c02def58fb87d5381e1a3056786a38cf094f25b/68747470733a2f2f706f7365722e707567782e6f72672f647265766f70732f62656861742d7068707365727665722f646f776e6c6f616473)](https://packagist.org/packages/drevops/behat-phpserver)[![LICENSE](https://camo.githubusercontent.com/c5a4f0953b4299d4353a66b348008f074f38d4606c8c67b23d21fe1efd5c69a2/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f647265766f70732f62656861742d706870736572766572)](https://camo.githubusercontent.com/c5a4f0953b4299d4353a66b348008f074f38d4606c8c67b23d21fe1efd5c69a2/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f647265766f70732f62656861742d706870736572766572)[![Renovate](https://camo.githubusercontent.com/35389190ce58a3690fe850342c1c3fd4f54e4c10ba8996741c8558ee24bf50dc/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f72656e6f766174652d656e61626c65642d677265656e3f6c6f676f3d72656e6f76617465626f74)](https://camo.githubusercontent.com/35389190ce58a3690fe850342c1c3fd4f54e4c10ba8996741c8558ee24bf50dc/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f72656e6f766174652d656e61626c65642d677265656e3f6c6f676f3d72656e6f76617465626f74)

[![Vortex Ecosystem](https://camo.githubusercontent.com/08d2ce6f52424a739d03c7df3f08b096f591c5198618d51756c11b2b69bde397/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f2546302539462538432538302d566f7274657825323045636f73797374656d2d3243354136383f7374796c653d666f722d7468652d6261646765266c6162656c436f6c6f723d363541434243)](https://github.com/drevops/vortex)

Features
--------

[](#features)

- [`PhpServerContext`](src/DrevOps/BehatPhpServer/ApiServerContext.php) context to start and stop PHP server:
    - Automatically start and stop PHP server for each scenario.
    - Serve files from a configurable document root.
    - Configurable PHP server protocol, host and port.
- [`ApiServerContext`](src/DrevOps/BehatPhpServer/PhpServerContext.php) context to serve queued API responses for API mocking:
    - A RESTful [API server](apiserver/index.php) used to queue up expected API responses.
    - Step definition to queue up API responses.
    - Automatically start and stop PHP server for each scenario.
    - Serve files from a configurable document root.
    - Configurable PHP server protocol, host and port.

Installation
------------

[](#installation)

```
composer require --dev drevops/behat-phpserver

```

Usage
-----

[](#usage)

### `PhpServerContext`

[](#phpservercontext)

Used to serve assets from a pre-defined document root.

```
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
```

### `ApiServerContext`

[](#apiservercontext)

Used to serve a pre-set API responses from a pre-defined document root.

```
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:

```
[
  {
    "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:

```
# 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](tests/behat/features/apiserver.feature) for more examples.

### Using File Responses

[](#using-file-responses)

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:

- `.json` → `application/json`
- `.xml` → `application/xml`
- `.html`, `.htm` → `text/html`
- `.txt` → `text/plain`
- All other extensions → `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.

### Resetting the API Server

[](#resetting-the-api-server)

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:

```
# 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](apiserver/index.php) implementation.

#### Accessing the API server URL from your contexts

[](#accessing-the-api-server-url-from-your-contexts)

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:

```
