Skip to content

Commit

Permalink
Handle fsockopen() failure
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianbergmann committed May 7, 2024
1 parent e6aa593 commit 2292f50
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 1 deletion.
7 changes: 7 additions & 0 deletions .psalm/baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -899,6 +899,10 @@
<code><![CDATA[nameAndVersion]]></code>
</InternalMethod>
<MissingThrowsDocblock>
<code><![CDATA[DefaultPrinter::from(
$configuration->logfileTeamcity(),
)]]></code>
<code><![CDATA[OutputFacade::printerFor($configuration->logfileJunit())]]></code>
<code><![CDATA[atLeastVersion]]></code>
<code><![CDATA[build]]></code>
<code><![CDATA[configurationFile]]></code>
Expand Down Expand Up @@ -1153,6 +1157,9 @@
<MissingThrowsDocblock>
<code><![CDATA[DefaultPrinter::standardError()]]></code>
<code><![CDATA[DefaultPrinter::standardError()]]></code>
<code><![CDATA[DefaultPrinter::standardError()]]></code>
<code><![CDATA[DefaultPrinter::standardOutput()]]></code>
<code><![CDATA[DefaultPrinter::standardOutput()]]></code>
<code><![CDATA[DefaultPrinter::standardOutput()]]></code>
<code><![CDATA[DefaultPrinter::standardOutput()]]></code>
<code><![CDATA[DefaultPrinter::standardOutput()]]></code>
Expand Down
30 changes: 30 additions & 0 deletions src/TextUI/Exception/CannotOpenSocketException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php declare(strict_types=1);
/*
* This file is part of PHPUnit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PHPUnit\TextUI;

use function sprintf;
use RuntimeException;

/**
* @internal This class is not covered by the backward compatibility promise for PHPUnit
*/
final class CannotOpenSocketException extends RuntimeException implements Exception
{
public function __construct(string $hostname, int $port)
{
parent::__construct(
sprintf(
'Cannot open socket %s:%d',
$hostname,
$port,
),
);
}
}
2 changes: 2 additions & 0 deletions src/TextUI/Output/Facade.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use PHPUnit\Logging\TestDox\TestResultCollection;
use PHPUnit\Runner\DirectoryDoesNotExistException;
use PHPUnit\TestRunner\TestResult\TestResult;
use PHPUnit\TextUI\CannotOpenSocketException;
use PHPUnit\TextUI\Configuration\Configuration;
use PHPUnit\TextUI\InvalidSocketException;
use PHPUnit\TextUI\Output\Default\ProgressPrinter\ProgressPrinter as DefaultProgressPrinter;
Expand Down Expand Up @@ -101,6 +102,7 @@ public static function printResult(TestResult $result, ?array $testDoxResult, Du
}

/**
* @throws CannotOpenSocketException
* @throws DirectoryDoesNotExistException
* @throws InvalidSocketException
*/
Expand Down
13 changes: 12 additions & 1 deletion src/TextUI/Output/Printer/DefaultPrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use function str_replace;
use function str_starts_with;
use PHPUnit\Runner\DirectoryDoesNotExistException;
use PHPUnit\TextUI\CannotOpenSocketException;
use PHPUnit\TextUI\InvalidSocketException;
use PHPUnit\Util\Filesystem;

Expand All @@ -36,6 +37,7 @@ final class DefaultPrinter implements Printer
private bool $isOpen;

/**
* @throws CannotOpenSocketException
* @throws DirectoryDoesNotExistException
* @throws InvalidSocketException
*/
Expand All @@ -45,6 +47,7 @@ public static function from(string $out): self
}

/**
* @throws CannotOpenSocketException
* @throws DirectoryDoesNotExistException
* @throws InvalidSocketException
*/
Expand All @@ -54,6 +57,7 @@ public static function standardOutput(): self
}

/**
* @throws CannotOpenSocketException
* @throws DirectoryDoesNotExistException
* @throws InvalidSocketException
*/
Expand All @@ -63,6 +67,7 @@ public static function standardError(): self
}

/**
* @throws CannotOpenSocketException
* @throws DirectoryDoesNotExistException
* @throws InvalidSocketException
*/
Expand All @@ -77,7 +82,13 @@ private function __construct(string $out)
throw new InvalidSocketException($out);
}

$this->stream = fsockopen($tmp[0], (int) $tmp[1]);
$stream = @fsockopen($tmp[0], (int) $tmp[1]);

if ($stream === false) {
throw new CannotOpenSocketException($tmp[0], (int) $tmp[1]);
}

$this->stream = $stream;
$this->isOpen = true;

return;
Expand Down

0 comments on commit 2292f50

Please sign in to comment.