Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to start a PHP built in web server before running a test and close it after test has run

Tags:

php

jenkins

behat

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.

like image 424
Richard Knop Avatar asked Oct 25 '12 16:10

Richard Knop


2 Answers

Just start the server as a part of your build process. Create an ant tasks which would start the server before behat is run and would kill it once behat is finished.

I've been successfully using this approach to start and stop the selenium server.

like image 95
Jakub Zalas Avatar answered Sep 23 '22 18:09

Jakub Zalas


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);
    }
}
like image 27
Richard Knop Avatar answered Sep 23 '22 18:09

Richard Knop