douzhao7445 2012-10-25 16:11
浏览 38
已采纳

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

I am trying to use Behat for BDD testing. When running a build on Jenkins, I would like Behat to open PHP's build in web server and then close it after running the tests. How to do that?

Basically I need to run:

php -S localhost:8000

In my BDD tests I tried:

/**
 * @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);
}

But then when running Behat it gets stuck without any message.

  • 写回答

2条回答 默认 最新

  • donglu2761 2012-10-30 11:21
    关注

    Solved this myself. I have create two methods. I call the first one before running my BDD tests and the second one after I ran the tests:

    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);
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛