How to run PHP built into the web server before running the test and close it after running the test

I am trying to use Behat to test BDD. When running the build on Jenkins, I would like Behat to open the PHP build on the web server and then close it after running the tests. How to do it?

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 you start Behat, it gets stuck without any message.

+6
source share
2 answers

I decided it myself. I created two methods. First I call the first before running the BDD tests, and the second after running 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); } } 
+3
source

Just start the server as part of the build process. Create ant tasks that would start the server before it starts, and will kill it as soon as it ends.

I have successfully used this approach to start and stop the selenium server.

+4
source

All Articles