{"id":19140264,"url":"https://github.com/drevops/behat-phpserver","last_synced_at":"2026-01-22T10:03:08.095Z","repository":{"id":48424492,"uuid":"77816170","full_name":"drevops/behat-phpserver","owner":"drevops","description":"🧪 PHP and API server for Behat tests","archived":false,"fork":false,"pushed_at":"2025-12-25T02:06:59.000Z","size":126,"stargazers_count":3,"open_issues_count":1,"forks_count":2,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-12-30T05:29:55.738Z","etag":null,"topics":["api","behat","behat-context","behat-extension","php","rest","test"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/drevops.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null},"funding":{"github":"drevops","patreon":"drevops"}},"created_at":"2017-01-02T06:51:28.000Z","updated_at":"2025-12-25T02:07:02.000Z","dependencies_parsed_at":"2024-05-16T23:28:38.504Z","dependency_job_id":"f389e027-3bb6-4361-9eb0-d10b60988e6a","html_url":"https://github.com/drevops/behat-phpserver","commit_stats":{"total_commits":19,"total_committers":3,"mean_commits":6.333333333333333,"dds":0.4736842105263158,"last_synced_commit":"82b1e5cbee3d7a68577f428b3d7506047fd27cb8"},"previous_names":[],"tags_count":14,"template":false,"template_full_name":null,"purl":"pkg:github/drevops/behat-phpserver","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/drevops%2Fbehat-phpserver","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/drevops%2Fbehat-phpserver/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/drevops%2Fbehat-phpserver/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/drevops%2Fbehat-phpserver/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/drevops","download_url":"https://codeload.github.com/drevops/behat-phpserver/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/drevops%2Fbehat-phpserver/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28661024,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-22T01:17:37.254Z","status":"online","status_checked_at":"2026-01-22T02:00:07.137Z","response_time":144,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["api","behat","behat-context","behat-extension","php","rest","test"],"created_at":"2024-11-09T07:16:57.629Z","updated_at":"2026-01-22T10:03:08.078Z","avatar_url":"https://github.com/drevops.png","language":"PHP","funding_links":["https://github.com/sponsors/drevops","https://patreon.com/drevops"],"categories":[],"sub_categories":[],"readme":"\u003cdiv align=\"center\"\u003e\n  \u003ca href=\"\" rel=\"noopener\"\u003e\n  \u003cimg width=200px height=200px src=\"https://placehold.jp/000000/ffffff/200x200.png?text=Behat+PHP+server\u0026css=%7B%22border-radius%22%3A%22%20100px%22%7D\" alt=\"Yourproject logo\"\u003e\u003c/a\u003e\n\u003c/div\u003e\n\n\u003ch1 align=\"center\"\u003ePHP and API server for Behat tests\u003c/h1\u003e\n\u003cdiv align=\"center\"\u003e\n\n[![GitHub Issues](https://img.shields.io/github/issues/drevops/behat-phpserver.svg)](https://github.com/drevops/behat-phpserver/issues)\n[![GitHub Pull Requests](https://img.shields.io/github/issues-pr/drevops/behat-phpserver.svg)](https://github.com/drevops/behat-phpserver/pulls)\n[![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)\n[![codecov](https://codecov.io/gh/drevops/behat-phpserver/branch/main/graph/badge.svg?token=KZCCZXN5C4)](https://codecov.io/gh/drevops/behat-phpserver)\n![GitHub release (latest by date)](https://img.shields.io/github/v/release/drevops/behat-phpserver)\n[![Total Downloads](https://poser.pugx.org/drevops/behat-phpserver/downloads)](https://packagist.org/packages/drevops/behat-phpserver)\n![LICENSE](https://img.shields.io/github/license/drevops/behat-phpserver)\n![Renovate](https://img.shields.io/badge/renovate-enabled-green?logo=renovatebot)\n\n\u003c/div\u003e\n\n## Features\n\n- [`PhpServerContext`](src/DrevOps/BehatPhpServer/ApiServerContext.php) context\n  to start and stop PHP server:\n  - Automatically start and stop PHP server for each scenario.\n  - Serve files from a configurable document root.\n  - Configurable PHP server protocol, host and port.\n- [`ApiServerContext`](src/DrevOps/BehatPhpServer/PhpServerContext.php) context\n  to serve queued API responses for API mocking:\n  - A RESTful [API server](apiserver/index.php) used to queue up expected API\n    responses.\n  - Step definition to queue up API responses.\n  - Automatically start and stop PHP server for each scenario.\n  - Serve files from a configurable document root.\n  - Configurable PHP server protocol, host and port.\n\n## Installation\n\n    composer require --dev drevops/behat-phpserver\n\n## Usage\n\n### `PhpServerContext`\n\nUsed to serve assets from a pre-defined document root.\n\n```yaml\ndefault:\n  suites:\n    default:\n      contexts:\n        - DrevOps\\BehatPhpServer\\PhpServerContext:\n            webroot: '%paths.base%/tests/behat/fixtures' # Path to the PHP server document root\n            protocol: http  # PHP server protocol\n            host: 0.0.0.0   # PHP server host\n            port: 8888      # PHP server port\n            debug: false    # Enable debug mode for verbose output\n```\n\n### `ApiServerContext`\n\nUsed to serve a pre-set API responses from a pre-defined document root.\n\n```yaml\ndefault:\n  suites:\n    default:\n      contexts:\n        - DrevOps\\BehatPhpServer\\ApiServerContext:\n            webroot: '%paths.base%/apiserver' # Path to the API server document root\n            protocol: http  # API PHP server protocol\n            host: 0.0.0.0   # API PHP server host\n            port: 8889      # API PHP server port\n            debug: false    # API Enable debug mode for verbose output\n            paths:          # Path(s) to fixture files for API responses\n              - '%paths.base%/tests/behat/fixtures'\n              - '%paths.base%/tests/behat/fixtures2'\n```\n\nAPI responses can be queued up in the API server server by sending\n`PUT` requests to `/admin/responses` as an array of the expected responses\nusing following JSON format:\n\n```json\n[\n  {\n    \"code\": 200,\n    \"reason\": \"OK\",\n    \"headers\": {},\n    \"body\": \"\"\n  },\n  {\n    \"code\": 404,\n    \"reason\": \"Not found\",\n    \"headers\": {\n    },\n    \"body\": \"\"\n  }\n]\n```\n\nThe `ApiServerContext` provides several step definitions to make it easier to\nwork with the API server:\n\n```gherkin\n# Check if the API server is running.\nGiven the API server is running\n\n# Queue up a single API response.\nGiven API will respond with:\n  \"\"\"\n  {\n    \"code\": 200,\n    \"headers\": {\n      \"Content-Type\": \"application/json\"\n    },\n    \"body\": {\n      \"Id\": \"test-id-1\",\n      \"Slug\": \"test-slug-1\"\n    }\n  }\n  \"\"\"\n\n# Queue up a single API response with minimal configuration.\nGiven API will respond with:\n  \"\"\"\n  {\n    \"code\": 200\n  }\n  \"\"\"\n\n# Queue up a single API response with JSON body.\nGiven API will respond with JSON:\n  \"\"\"\n  {\n    \"Id\": \"test-id-1\",\n    \"Slug\": \"test-slug-1\"\n  }\n  \"\"\"\n\n# Queue up a single API response with JSON body and expected code.\nGiven API will respond with JSON and 201 code:\n  \"\"\"\n  {\n    \"Id\": \"test-id-2\",\n    \"Slug\": \"test-slug-2\"\n  }\n  \"\"\"\n\n# Reset the API server by clearing all responses and requests.\nGiven the API server is reset\n\n# Queue up a file response with automatic content type detection.\nGiven API will respond with file \"test_data.json\"\n\n# Queue up a file response with a custom response code.\nGiven API will respond with file \"test_content.xml\" and 201 code\n\n# Assert the number of requests received by the API server.\nThen the API server should have 3 received requests\n\n# Assert the number of responses queued in the API server.\nThen the API server should have 0 queued responses\n```\n\nSee this [test feature](tests/behat/features/apiserver.feature) for more\nexamples.\n\n### Using File Responses\n\nThe `apiWillRespondWithFile` step definition allows you to respond with the contents of a file\nfrom one of the configured fixture paths. The context will automatically detect the appropriate\ncontent type based on the file extension:\n\n- `.json` → `application/json`\n- `.xml` → `application/xml`\n- `.html`, `.htm` → `text/html`\n- `.txt` → `text/plain`\n- All other extensions → `application/octet-stream`\n\nMultiple fixture paths can be configured in the `behat.yml` file. The context will search for the\nfile in each path in the order specified until it finds a match.\n\n### Resetting the API Server\n\nThe `resetApi` step definition allows you to clear all queued responses and request history in the API server.\nThis is useful for ensuring a clean state between test steps, especially when multiple scenarios\ninteract with the API server:\n\n```gherkin\n# Clear existing state before setting up a new test\nGiven API server is reset\nAnd API will respond with file \"test_data.json\"\nWhen I send a GET request to \"/\"\n```\n\nFor more information on supported RESTful API endpoints, see\nthe [API server](apiserver/index.php) implementation.\n\n#### Accessing the API server URL from your contexts\n\nIf you need to access the API server URL from your context to update the base\nURL of your API client, you can do so by using `beforeScenario` in your\n`FeatureContext` class:\n\n```php\n\u003c?php\n\ndeclare(strict_types=1);\n\nuse Behat\\Behat\\Context\\Context;\nuse Behat\\Behat\\Context\\Environment\\InitializedContextEnvironment;\nuse Behat\\Behat\\Hook\\Scope\\BeforeScenarioScope;\nuse DrevOps\\BehatPhpServer\\ApiServerContext;\nuse DrevOps\\BehatPhpServer\\PhpServerContext;\n\nclass FeatureContext implements Context {\n\n  /**\n   * The PHP server URL.\n   */\n  protected string $phpServerUrl;\n\n  /**\n   * The API server URL.\n   */\n  protected string $apiServerUrl;\n\n  /**\n   * Initialize the context.\n   *\n   * @beforeScenario\n   */\n  public function beforeScenarioInit(BeforeScenarioScope $scope): void {\n    $environment = $scope-\u003egetEnvironment();\n\n    if (!$environment instanceof InitializedContextEnvironment) {\n      throw new \\Exception('Environment is not initialized');\n    }\n\n    $context = $environment-\u003egetContext(PhpServerContext::class);\n    $this-\u003ephpServerUrl = $context-\u003egetServerUrl();\n\n    $context = $environment-\u003egetContext(ApiServerContext::class);\n    $this-\u003eapiServerUrl = $context-\u003egetServerUrl();\n  }\n\n}\n\n```\n\n## Maintenance\n\n### Lint code\n\n```bash\ncomposer lint\ncomposer lint-fix\n```\n\n### Run tests\n\n```bash\ncomposer test\ncomposer test-bdd\n```\n\n---\n_This repository was created using the [Scaffold](https://getscaffold.dev/) project template_\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdrevops%2Fbehat-phpserver","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdrevops%2Fbehat-phpserver","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdrevops%2Fbehat-phpserver/lists"}