PHPackages                             giberti/phpunit-local-server - 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. [Testing &amp; Quality](/categories/testing)
4. /
5. giberti/phpunit-local-server

ActiveLibrary[Testing &amp; Quality](/categories/testing)

giberti/phpunit-local-server
============================

A local HTTP server for PHPUnit tests

3.0.1(5mo ago)631712MITPHPPHP ^7.3|^8.0CI failing

Since Jan 13Pushed 5mo ago1 watchersCompare

[ Source](https://github.com/giberti/phpunit-local-server)[ Packagist](https://packagist.org/packages/giberti/phpunit-local-server)[ RSS](/packages/giberti-phpunit-local-server/feed)WikiDiscussions main Synced 2w ago

READMEChangelog (8)Dependencies (1)Versions (9)Used By (2)

PHPUnit Local Server
====================

[](#phpunit-local-server)

[![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)

Provides an HTTP server test case for PHPUnit. The server is powered by PHP's built-in server for testing of network related calls.

### Installing

[](#installing)

This library requires PHP 7.3 or newer, including PHP 8.0 through 8.5. It will run with PHPUnit versions 8, 9, 10, and 11.

```
composer require giberti/phpunit-local-server

```

### Usage

[](#usage)

- Create a directory that will contain the code you want to execute
- Extend the `\Giberti\PHPUnitLocalServer\LocalServerTestCase` as if you were extending `\PHPUnit\Framework\TestCase`
- Start a server in the test method or for the entire class
- Make requests against the server

#### Usage Tips

[](#usage-tips)

- Whenever possible, re-use the existing server. Frequent restarts will slow down your tests.
- You can provide a different `php` binary by overriding the static `$phpBinary` property on the class.

##### A single test

[](#a-single-test)

Call either the `createServerWithDocroot()` or `createServerWithRouter()` helper method and then execute your test.

```
use Giberti\PHPUnitLocalServer\LocalServerTestCase;

class Test extends LocalServerTestCase
{
    public function testFoo() {
        static::createServerWithDocroot('./tests/localhost');
        $url = $this->getLocalServerUrl() . '/foo';

        $content = file_get_contents($url);

        $this->assertEquals('...', $content, 'Content mismatch');
    }
}
```

##### Several tests using the same configuration

[](#several-tests-using-the-same-configuration)

To 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.

```
use Giberti\PHPUnitLocalServer\LocalServerTestCase;

class Test extends LocalServerTestCase
{
    public static function setupBeforeClass() {
        static::createServerWithDocroot('./tests/localhost');
    }

    public function testFoo() {
        $url = $this->getLocalServer() . '/foo';
        $content = file_get_contents($url);

        $this->assertEquals('...', $content, 'Content mismatch');
    }

    public function testBar() {
        $url = $this->getLocalServer() . '/bar';
        $content = file_get_contents($url);

        $this->assertEquals('...', $content, 'Content mismatch');
    }
}
```

##### Modifying the server runtime version

[](#modifying-the-server-runtime-version)

It'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.

```
use Giberti\PHPUnitLocalServer\LocalServerTestCase;

class Test73 extends LocalServerTestCase
{
    static $phpBinary = '/usr/local/bin/php73';

    public function testFoo() {
        static::createServerWithDocroot('./tests/localhost');

        $url = $this->getLocalServer() . '/foo';
        $content = file_get_contents($url);

        $this->assertEquals('...', $content, 'Content mismatch');
    }
}

class Test81 extends LocalServerTestCase
{
    static $phpBinary = '/usr/local/bin/php81';

    public function testFoo() {
        static::createServerWithDocroot('./tests/localhost');

        $url = $this->getLocalServer() . '/foo';
        $content = file_get_contents($url);

        $this->assertEquals('...', $content, 'Content mismatch');
    }
}
```

### Methods

[](#methods)

The following methods are provided to interact with the local server.

#### public bool LocalServerTestCase::createServerWithDocroot(string $docroot)

[](#public-bool-localservertestcasecreateserverwithdocrootstring-docroot)

Creates a local server using a document root.

```
static::createServerWithDocroot('./path/to/site/files');
```

#### public bool LocalServerTestCase::createServerWithRouter(string $router)

[](#public-bool-localservertestcasecreateserverwithrouterstring-router)

Creates 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.

```
static::createServerWithRouter('./path/to/router.php');
```

#### public void LocalServerTestCase::destroyServer(void)

[](#public-void-localservertestcasedestroyservervoid)

Removes the local server. Useful to reset the session state. This is automatically called in the `tearDownAfterClass()` lifecycle method.

```
static::destroyServer();
```

#### public string LocalServerTestCase::getServerUrl(void)

[](#public-string-localservertestcasegetserverurlvoid)

The 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.

```
$schemeHost = $this->getServerUrl();
$fullUrl    = $schemeHost . "/path/to/file/to/access";

echo $fullUrl; // http://localhost:8000/path/to/file/to/access
```

###  Health Score

48

—

FairBetter than 94% of packages

Maintenance73

Regular maintenance activity

Popularity18

Limited adoption so far

Community14

Small or concentrated contributor base

Maturity73

Established project with proven stability

 Bus Factor1

Top contributor holds 98.6% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~419 days

Recently: every ~593 days

Total

8

Last Release

150d ago

Major Versions

v0.1.2 → v1.0.02019-08-03

v1.0.0 → v2.0.02020-01-18

v2.1.0 → v3.0.02022-05-01

PHP version history (6 changes)0.1.0PHP ^7.0

v1.0.0PHP ^7.1

v2.0.0PHP ^7.2

v2.1.0PHP ^7.2 || ^8.0

v3.0.0PHP ^7.3 || ^8.0

3.0.1PHP ^7.3|^8.0

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/771060?v=4)[Erik Giberti](/maintainers/giberti)[@giberti](https://github.com/giberti)

---

Top Contributors

[![giberti](https://avatars.githubusercontent.com/u/771060?v=4)](https://github.com/giberti "giberti (69 commits)")[![m6w6](https://avatars.githubusercontent.com/u/1265282?v=4)](https://github.com/m6w6 "m6w6 (1 commits)")

---

Tags

phpphp7php8phpunit

### Embed Badge

![Health badge](/badges/giberti-phpunit-local-server/health.svg)

```
[![Health](https://phpackages.com/badges/giberti-phpunit-local-server/health.svg)](https://phpackages.com/packages/giberti-phpunit-local-server)
```

###  Alternatives

[brianium/paratest

Parallel testing for PHP

2.5k129.9M913](/packages/brianium-paratest)[orchestra/testbench

Laravel Testing Helper for Packages Development

2.2k41.3M38.9k](/packages/orchestra-testbench)[drupal/core-dev

require-dev dependencies from drupal/drupal; use in addition to drupal/core-recommended to run tests from drupal/core.

2022.0M321](/packages/drupal-core-dev)[webmozarts/strict-phpunit

Enables type-safe comparisons of objects in PHPUnit

30300.1k6](/packages/webmozarts-strict-phpunit)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
