Skip to content

Commit

Permalink
Merge pull request #60 from donatj/fix/db-bypass-finals-compat
Browse files Browse the repository at this point in the history
Fix descriptor opening for bad stream wrappers
  • Loading branch information
donatj authored Oct 30, 2023
2 parents dc86bf2 + 16a49d6 commit 19ace5d
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions src/MockWebServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ class MockWebServer {
*/
private $process;

/**
* Contains the descriptors for the process after it has been started
*
* @var resource[]
*/
private $descriptors = [];

/**
* TestWebServer constructor.
*
Expand Down Expand Up @@ -67,7 +74,7 @@ public function start() : void {

InternalServer::incrementRequestCounter($this->tmpDir, 0);

$this->process = $this->startServer($fullCmd);
[ $this->process, $this->descriptors ] = $this->startServer($fullCmd);

for( $i = 0; $i <= 20; $i++ ) {
usleep(100000);
Expand Down Expand Up @@ -123,6 +130,10 @@ public function stop() : void {
usleep(10000);
}
}

foreach( $this->descriptors as $descriptor ) {
@fclose($descriptor);
}
}

/**
Expand Down Expand Up @@ -274,9 +285,9 @@ private function isWindowsPlatform() : bool {
}

/**
* @return resource
* @return array{resource, resource[]}
*/
private function startServer( string $fullCmd ) {
private function startServer( string $fullCmd ) : array {
if( !$this->isWindowsPlatform() ) {
// We need to prefix exec to get the correct process http://php.net/manual/ru/function.proc-get-status.php#93382
$fullCmd = 'exec ' . $fullCmd;
Expand All @@ -286,14 +297,13 @@ private function startServer( string $fullCmd ) {
$env = null;
$cwd = null;

$stdin = fopen('php://stdin', 'rb');
$stdoutf = tempnam(sys_get_temp_dir(), 'MockWebServer.stdout');
$stderrf = tempnam(sys_get_temp_dir(), 'MockWebServer.stderr');

$descriptorSpec = [
0 => $stdin,
1 => [ 'file', $stdoutf, 'a' ],
2 => [ 'file', $stderrf, 'a' ],
0 => fopen('php://stdin', 'rb'),
1 => fopen($stdoutf, 'a'),
2 => fopen($stderrf, 'a'),
];

$process = proc_open($fullCmd, $descriptorSpec, $pipes, $cwd, $env, [
Expand All @@ -302,7 +312,7 @@ private function startServer( string $fullCmd ) {
]);

if( is_resource($process) ) {
return $process;
return [ $process, $descriptorSpec ];
}

throw new Exceptions\ServerException('Error starting server');
Expand Down

0 comments on commit 19ace5d

Please sign in to comment.