如何在运行测试之前启动PHP内置Web服务器,并在测试运行后关闭它

Ric*_*nop 7 php jenkins behat

我正在尝试使用Behat进行BDD测试.在Jenkins上运行构建时,我希望Behat在Web服务器中打开PHP的构建,然后在运行测试后关闭它.怎么做?

基本上我需要运行:

php -S localhost:8000
Run Code Online (Sandbox Code Playgroud)

在我的BDD测试中,我试过:

/**
 * @Given /^I call "([^"]*)" with email and password$/
 */
public function iCallWithPostData($uri)
{
    echo exec('php -S localhost:8000');
    $client = new Guzzle\Service\Client();
    $request = $client->post('http://localhost:8000' . $uri, array(), '{"email":"a","password":"a"}')->send();
    $this->response = $request->getBody(true);
}
Run Code Online (Sandbox Code Playgroud)

但是当运行Behat时它会被卡住而没有任何消息.

Ric*_*nop 4

我自己解决了这个问题。我创建了两种方法。我在运行 BDD 测试之前调用第一个,在运行测试后调用第二个:

private function _startDevelopmentServer($pidfile)
{
    $cmd = 'cd ../../public && php -S 127.0.0.1:8027 index.php';
    $outputfile = '/dev/null';
    shell_exec(sprintf("%s > %s 2>&1 & echo $! >> %s", $cmd, $outputfile, $pidfile));
    sleep(1);
}

private function _killDevelopmentServer($pidfile)
{
    if (file_exists($pidfile)) {
        $pids = file($pidfile);
        foreach ($pids as $pid) {
            shell_exec('kill -9 ' . $pid);
        }
        unlink($pidfile);
    }
}
Run Code Online (Sandbox Code Playgroud)