Christer Edvartsen
Written by Christer Edvartsen
Published 2013-07-19

Using PHP’s built-in web server in your test suites

As of PHP-5.4.0 the CLI SAPI provides a built-in web server. The web server is designed for development purposes, and serves requests sequentially. This web server can come in really handy when the need for an httpd arises during (integration) tests.

In this post I’ll use PHPUnit as the testing framework, and I’ll show you how to start the web server during the bootstrap process, and how to shut it down when the test suite is finished.

Entry image

PHPUnit lets you specify a bootstrap file that is executed before the tests. This bootstrap file will contain the code needed to start and stop the web server. First, specify a bootstrap file in PHPUnit’s XML configuration file:

The three constants we define holds the host and the port that the server will listen on (localhost:1349) and the path to the document root (./public). These constants will be used in our bootstrap.php script when starting up the web server.

Now, on to the bootstrap script:

With this bootstrap script the web server will be started before the tests execute, and when the test suite is finished the shutdown function will kill the web server process.

If you execute PHPUnit with the above XML configuration and the bootstrap file you should see something like this:


If you have any other use cases for the built-in web server, feel free to leave a comment.

Written by Christer Edvartsen
Published 2013-07-19