Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Codeception 5 #47

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "codeception/phpbuiltinserver",
"name": "robchett/phpbuiltinserver",
"description": "PhpBuiltinServer extension for Codeception",
"keywords": ["codeception"],
"minimum-stability": "stable",
Expand All @@ -8,11 +8,15 @@
{
"name": "tiger-seo",
"email": "tiger.seo@gmail.com"
},
{
"name": "Rob Chett",
"email": "robchett@gmail.com"
}
],
"require": {
"php": ">=5.6.0",
"codeception/codeception": "^3.0 || ^4.0"
"codeception/codeception": "^5.0"
},
"require-dev": {
"phpunit/phpunit": "5.*"
Expand Down
46 changes: 32 additions & 14 deletions src/Codeception/Extension/PhpBuiltinServer.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,26 @@

use Codeception\Configuration;
use Codeception\Exception\ModuleConfigException;
use Codeception\Extension;
use Codeception\Exception\ExtensionException;
use Codeception\Module;

class PhpBuiltinServer extends Extension
class PhpBuiltinServer extends Module
{
static $events = [
'suite.before' => 'beforeSuite'
];

private $requiredFields = ['hostname', 'port', 'documentRoot'];
protected array $requiredFields = ['hostname', 'documentRoot'];
private $resource;
private $pipes;

public function __construct($config, $options)
public function __construct(\Codeception\Lib\ModuleContainer $container, $config)
{
if (version_compare(PHP_VERSION, '5.4', '<')) {
throw new ExtensionException($this, 'Requires PHP built-in web server, available since PHP 5.4.0.');
}

parent::__construct($config, $options);
parent::__construct($container, $config);
$this->validateConfig();

if (
Expand Down Expand Up @@ -71,7 +71,7 @@ private function getCommand()
if ($this->isRemoteDebug()) {
$parameters .= ' -dxdebug.remote_enable=1';
}
$parameters .= ' -dcodecept.access_log="' . Configuration::logDir() . 'phpbuiltinserver.access_log.txt' . '"';
$parameters .= ' -dcodecept.access_log="' . Configuration::baseDir() . 'tests/_output/phpbuiltinserver.access_log.txt' . '"';

if (PHP_OS !== 'WINNT' && PHP_OS !== 'WIN32') {
// Platform uses POSIX process handling. Use exec to avoid
Expand All @@ -82,24 +82,42 @@ private function getCommand()
$exec = '';
}

$port = $this->findFreePort();
$url = "{$this->config['hostname']}:$port";
foreach ($this->getModules() as $module) {
foreach ($module->_getConfig() as $key => $value) {
if (is_string($value) && str_contains($value, '%webserver%')) {
$module->_setConfig([$key => str_replace('%webserver%', 'http://' . $url, $value)]);
}
}
}

$command = sprintf(
$exec . PHP_BINARY . ' %s -S %s:%s -t "%s" "%s"',
$exec . PHP_BINARY . ' %s -S %s -t "%s" "%s"',
$parameters,
$this->config['hostname'],
$this->config['port'],
$url,
realpath($this->config['documentRoot']),
__DIR__ . '/Router.php'
);

return $command;
}

private function findFreePort(): int
{
$sock = socket_create_listen(0);
socket_getsockname($sock, $addr, $port);
socket_close($sock);

return $port;
}

private function isRemoteDebug()
{
return Configuration::isExtensionEnabled('Codeception\Extension\RemoteDebug');
}

private function validateConfig()
protected function validateConfig(): void
{
$fields = array_keys($this->config);
if (array_intersect($this->requiredFields, $fields) != $this->requiredFields) {
Expand Down Expand Up @@ -138,16 +156,16 @@ public function startServer()
$command = $this->getCommand();
$descriptorSpec = [
['pipe', 'r'],
['file', Configuration::logDir() . 'phpbuiltinserver.output.txt', 'w'],
['file', Configuration::logDir() . 'phpbuiltinserver.errors.txt', 'a']
['file', Configuration::baseDir() . 'tests/_output/phpbuiltinserver.output.txt', 'w'],
['file', Configuration::baseDir() . 'tests/_output/phpbuiltinserver.errors.txt', 'a']
];
$this->resource = proc_open($command, $descriptorSpec, $this->pipes, null, null, ['bypass_shell' => true]);
if (!is_resource($this->resource)) {
throw new ExtensionException($this, 'Failed to start server.');
throw new ExtensionException($this, 'Failed to start server (no resource)');
}
if (!proc_get_status($this->resource)['running']) {
proc_close($this->resource);
throw new ExtensionException($this, 'Failed to start server.');
throw new ExtensionException($this, 'Failed to start server (not running)');
}

$resource = $this->resource;
Expand Down