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

时间:2012-10-25 16:11:40

标签: php jenkins behat

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

基本上我需要运行:

php -S localhost:8000

在我的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);
}

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

2 个答案:

答案 0 :(得分:4)

只需在构建过程中启动服务器即可。创建一个ant任务,它将在behat运行之前启动服务器,并在完成后终止它。

我已成功使用此方法启动和停止selenium服务器。

答案 1 :(得分:3)

我自己解决了这个问题。我创建了两种方法。我在运行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);
    }
}
相关问题