Last active
March 20, 2021 05:31
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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