/** * Starts the CLI web server for UI tests * * Add an 'httpd: { port: 8888, root: %paths.base% }' suite setting to your `behat.yml` file. * * @BeforeSuite */ public static function startWebServer(BeforeSuiteScope $scope) { if ($scope->getSuite()->hasSetting('httpd')) { $config = $scope->getSuite()->getSetting('httpd'); $port = $config['port']; $root = $config['root']; $host = 'localhost'; $routerString = isset($config['router']) ? ' ' . $config['router'] : ''; // launch if not already up if (!self::serverIsUp($host, $port)) { $command = "php -S {$host}:{$port} -t {$root}{$routerString} >/dev/null 2>&1 & echo \$!"; $output = trim(shell_exec($command)); self::$httpdPid = is_numeric($output) ? intval($output) : null; } // check that the server is running, wait up to 2 seconds $attempts = 0; do { $up = self::serverIsUp($host, $port); $attempts++; usleep(100000); // 0.1 sec } while (!$up && $attempts < 20); if (!$up) { self::stopProcess(self::$httpdPid); // just in case it *did* start but did not respond in time throw new \RuntimeException("Could not start web server at {$host}:{$port}"); } } }