Skip to content

Instantly share code, notes, and snippets.

@Mulkave
Last active March 20, 2021 05:31
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Mulkave/5696612 to your computer and use it in GitHub Desktop.
Save Mulkave/5696612 to your computer and use it in GitHub Desktop.
This is a way to leverage the PHP built-in web server in your tests. Uses the new @beforeClass and @afterclass annotation introduced in phpunit v3.8.x.
<?php
Class MyTest extends PHPUnit_Framework_TestCase {
static $pidfile = './.pidfile';
static $serverHost = 'localhost';
static $serverPort = '6767';
public static function serverURL()
{
return static::$serverHost.':'.static::$serverPort;
}
/**
* @beforeClass
*/
public static function bootUpBuiltInServer()
{
shell_exec('php -S '.static::serverURL().' -t ./tests/server > /dev/null 2>&1 & echo $! >> '.static::$pidfile);
// let's wait a couple of seconds for the server to be up
sleep(2);
}
/**
* @afterClass
*/
public static function turnDownBuiltInServer()
{
$filename = static::$pidfile;
if(file_exists($filename))
{
$fileContent = file($filename);
$pid = array_pop($fileContent);
shell_exec('kill -9 '.$pid);
unlink($filename);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment