{"id":16809469,"url":"https://github.com/giberti/phpunit-local-server","last_synced_at":"2025-10-03T17:56:57.861Z","repository":{"id":62510658,"uuid":"117283925","full_name":"giberti/phpunit-local-server","owner":"giberti","description":"Provides an HTTP server test case for PHPUnit; powered by PHP's built in server.","archived":false,"fork":false,"pushed_at":"2023-12-09T20:07:10.000Z","size":46,"stargazers_count":6,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-08-24T11:24:48.184Z","etag":null,"topics":["php","php7","php8","phpunit"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/giberti.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2018-01-12T20:16:27.000Z","updated_at":"2023-05-22T20:16:07.000Z","dependencies_parsed_at":"2023-12-09T21:23:07.095Z","dependency_job_id":"9340b02e-fd24-4f5a-9f2e-1c936a83c2c9","html_url":"https://github.com/giberti/phpunit-local-server","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"purl":"pkg:github/giberti/phpunit-local-server","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/giberti%2Fphpunit-local-server","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/giberti%2Fphpunit-local-server/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/giberti%2Fphpunit-local-server/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/giberti%2Fphpunit-local-server/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/giberti","download_url":"https://codeload.github.com/giberti/phpunit-local-server/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/giberti%2Fphpunit-local-server/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":273656782,"owners_count":25145002,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-09-04T02:00:08.968Z","response_time":61,"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":["php","php7","php8","phpunit"],"created_at":"2024-10-13T10:12:49.866Z","updated_at":"2025-10-03T17:56:57.784Z","avatar_url":"https://github.com/giberti.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# PHPUnit Local Server\n[![Build and Test](https://github.com/giberti/phpunit-local-server/actions/workflows/test-php.yml/badge.svg)](https://github.com/giberti/phpunit-local-server/actions/workflows/test-php.yml)\n\nProvides an HTTP server test case for PHPUnit. The server is powered by PHP's built-in server for testing of network related calls.\n\n### Installing\n\nThis library requires PHP 7.3 or newer, including PHP 8.0, 8,1, and 8.2. It will run with PHPUnit versions 8 and 9.\n\n```\ncomposer require giberti/phpunit-local-server\n```\n\n### Usage\n\n* Create a directory that will contain the code you want to execute\n* Extend the `\\Giberti\\PHPUnitLocalServer\\LocalServerTestCase` as if you were extending `\\PHPUnit\\Framework\\TestCase`\n* Start a server in the test method or for the entire class\n* Make requests against the server\n\n#### Usage Tips\n\n* Whenever possible, re-use the existing server. Frequent restarts will slow down your tests.\n* You can provide a different `php` binary by overriding the static `$phpBinary` property on the class.\n\n##### A single test\n\nCall either the `createServerWithDocroot()` or `createServerWithRouter()` helper method and then execute your test.\n\n```php\nuse Giberti\\PHPUnitLocalServer\\LocalServerTestCase;\n\nclass Test extends LocalServerTestCase\n{\n    public function testFoo() {\n        static::createServerWithDocroot('./tests/localhost');\n        $url = $this-\u003egetLocalServerUrl() . '/foo';\n\n        $content = file_get_contents($url);\n\n        $this-\u003eassertEquals('...', $content, 'Content mismatch');\n    }\n}\n```\n\n##### Several tests using the same configuration\n\nTo optimize performance of your tests, it's best to re-use the server whenever possible. To make this easier, simply start the server at the beginning of the class by defining a `setupBeforeClass()` method with your desired configuration.\n\n```php\nuse Giberti\\PHPUnitLocalServer\\LocalServerTestCase;\n\nclass Test extends LocalServerTestCase\n{\n    public static function setupBeforeClass() {\n        static::createServerWithDocroot('./tests/localhost');\n    }\n\n    public function testFoo() {\n        $url = $this-\u003egetLocalServer() . '/foo';\n        $content = file_get_contents($url);\n\n        $this-\u003eassertEquals('...', $content, 'Content mismatch');\n    }\n\n    public function testBar() {\n        $url = $this-\u003egetLocalServer() . '/bar';\n        $content = file_get_contents($url);\n\n        $this-\u003eassertEquals('...', $content, 'Content mismatch');\n    }\n}\n```\n\n##### Modifying the server runtime version\n\nIt's possible to run the server under a different PHP runtime than the version running your test suite. This can help with testing your code under multiple versions of PHP. In the example below, the server will start with the PHP 7.3 and 8.1 executable in `/usr/local/bin/` on the host test system. Your path may be different.\n\n```php\nuse Giberti\\PHPUnitLocalServer\\LocalServerTestCase;\n\nclass Test73 extends LocalServerTestCase\n{\n    static $phpBinary = '/usr/local/bin/php73';\n\n    public function testFoo() {\n        static::createServerWithDocroot('./tests/localhost');\n\n        $url = $this-\u003egetLocalServer() . '/foo';\n        $content = file_get_contents($url);\n\n        $this-\u003eassertEquals('...', $content, 'Content mismatch');\n    }\n}\n\nclass Test81 extends LocalServerTestCase\n{\n    static $phpBinary = '/usr/local/bin/php81';\n\n    public function testFoo() {\n        static::createServerWithDocroot('./tests/localhost');\n\n        $url = $this-\u003egetLocalServer() . '/foo';\n        $content = file_get_contents($url);\n\n        $this-\u003eassertEquals('...', $content, 'Content mismatch');\n    }\n}\n```\n\n### Methods\n\nThe following methods are provided to interact with the local server.\n\n#### public bool LocalServerTestCase::createServerWithDocroot(string $docroot)\n\nCreates a local server using a document root.\n\n```php\nstatic::createServerWithDocroot('./path/to/site/files');\n```\n\n#### public bool LocalServerTestCase::createServerWithRouter(string $router)\n\nCreates a local server using a router file. If you are using a framework, this is most likely the `index.php` file in your document route.\n\n```php\nstatic::createServerWithRouter('./path/to/router.php');\n```\n\n#### public void LocalServerTestCase::destroyServer(void)\n\nRemoves the local server. Useful to reset the session state. This is automatically called in the `tearDownAfterClass()` lifecycle method.\n\n```php\nstatic::destroyServer();\n```\n\n#### public string LocalServerTestCase::getServerUrl(void)\n\nThe port for the server will _usually_ be `8000`, however, it is dynamically assigned in the event of a conflict. The safest way to access the host is to call the `getServerUrl()` method and use that as the root for any Url construction.\n\n```php\n$schemeHost = $this-\u003egetServerUrl();\n$fullUrl    = $schemeHost . \"/path/to/file/to/access\";\n\necho $fullUrl; // http://localhost:8000/path/to/file/to/access\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgiberti%2Fphpunit-local-server","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgiberti%2Fphpunit-local-server","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgiberti%2Fphpunit-local-server/lists"}