Skip to content

Commit

Permalink
Improve error handling on MockWebServer
Browse files Browse the repository at this point in the history
  • Loading branch information
donatj committed Jun 9, 2024
1 parent 91b5741 commit 9790fa3
Showing 1 changed file with 51 additions and 17 deletions.
68 changes: 51 additions & 17 deletions src/MockWebServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace donatj\MockWebServer;

use donatj\MockWebServer\Exceptions\RuntimeException;

class MockWebServer {

public const VND = 'VND.DonatStudios.MockWebServer';
Expand Down Expand Up @@ -190,17 +192,23 @@ private function getTmpDir() : string {

$tmpPath = $tmpDir . DIRECTORY_SEPARATOR . 'MockWebServer';
if( !is_dir($tmpPath) ) {
mkdir($tmpPath);
if( !mkdir($tmpPath) && !is_dir($tmpPath) ) {
throw new \RuntimeException(sprintf('Directory "%s" was not created', $tmpPath));
}
}

$tmpPath .= DIRECTORY_SEPARATOR . $this->port;
if( !is_dir($tmpPath) ) {
mkdir($tmpPath);
if( !mkdir($tmpPath) && !is_dir($tmpPath) ) {
throw new \RuntimeException(sprintf('Directory "%s" was not created', $tmpPath));
}
}

$tmpPath .= DIRECTORY_SEPARATOR . md5(microtime(true) . ':' . rand(0, 100000));
if( !is_dir($tmpPath) ) {
mkdir($tmpPath);
if( !mkdir($tmpPath) && !is_dir($tmpPath) ) {
throw new \RuntimeException(sprintf('Directory "%s" was not created', $tmpPath));
}
}

return $tmpPath;
Expand Down Expand Up @@ -229,17 +237,25 @@ public function getLastRequest() : ?RequestInfo {
* If offset is negative, the request will be that from the end of the requests.
*/
public function getRequestByOffset( int $offset ) : ?RequestInfo {
$reqs = glob($this->tmpDir . DIRECTORY_SEPARATOR . 'request.*');
$reqs = glob($this->tmpDir . DIRECTORY_SEPARATOR . 'request.*') ?: [];
natsort($reqs);

$item = array_slice($reqs, $offset, 1);
if( !$item ) {
return null;
}

$path = reset($item);
$path = reset($item);
if( !$path ) {
return null;
}

$content = file_get_contents($path);
$data = @unserialize($content);
if( $content === false ) {
throw new RuntimeException("failed to read request from '{$path}'");
}

$data = @unserialize($content);
if( $data instanceof RequestInfo ) {
return $data;
}
Expand All @@ -266,10 +282,13 @@ public function getPort() : int {
*/
private function findOpenPort() : int {
$sock = socket_create(AF_INET, SOCK_STREAM, 0);
if( $sock === false ) {
throw new RuntimeException('Failed to create socket');
}

// Bind the socket to an address/port
if( !socket_bind($sock, $this->getHost(), 0) ) {
throw new Exceptions\RuntimeException('Could not bind to address');
throw new RuntimeException('Could not bind to address');
}

socket_getsockname($sock, $checkAddress, $checkPort);
Expand All @@ -279,15 +298,15 @@ private function findOpenPort() : int {
return $checkPort;
}

throw new Exceptions\RuntimeException('Failed to find open port');
throw new RuntimeException('Failed to find open port');
}

private function isWindowsPlatform() : bool {
return defined('PHP_WINDOWS_VERSION_MAJOR');
}

/**
* @return array{resource, resource[]}
* @return array{resource,array{resource,resource,resource}}
*/
private function startServer( string $fullCmd ) : array {
if( !$this->isWindowsPlatform() ) {
Expand All @@ -300,24 +319,39 @@ private function startServer( string $fullCmd ) : array {
$cwd = null;

$stdoutf = tempnam(sys_get_temp_dir(), 'MockWebServer.stdout');
if( $stdoutf === false ) {
throw new RuntimeException('error creating stdout temp file');
}
$stderrf = tempnam(sys_get_temp_dir(), 'MockWebServer.stderr');
if( $stderrf === false ) {
throw new RuntimeException('error creating stderr temp file');
}

$stdin = fopen('php://stdin', 'rb');
if( $stdin === false ) {
throw new RuntimeException('error opening stdin');
}
$stdout = fopen($stdoutf, 'ab');
if( $stdout === false ) {
throw new RuntimeException('error opening stdout');
}
$stderr = fopen($stderrf, 'ab');
if( $stderr === false ) {
throw new RuntimeException('error opening stderr');
}

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

$process = proc_open($fullCmd, $descriptorSpec, $pipes, $cwd, $env, [
'suppress_errors' => false,
'bypass_shell' => true,
]);

if( is_resource($process) ) {
return [ $process, $descriptorSpec ];
if( $process === false ) {
throw new Exceptions\ServerException('Error starting server');
}

throw new Exceptions\ServerException('Error starting server');
return [ $process, $descriptorSpec ];
}

}

0 comments on commit 9790fa3

Please sign in to comment.