Skip to content

Commit 2292f50

Browse files
Handle fsockopen() failure
1 parent e6aa593 commit 2292f50

File tree

4 files changed

+51
-1
lines changed

4 files changed

+51
-1
lines changed

.psalm/baseline.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -899,6 +899,10 @@
899899
<code><![CDATA[nameAndVersion]]></code>
900900
</InternalMethod>
901901
<MissingThrowsDocblock>
902+
<code><![CDATA[DefaultPrinter::from(
903+
$configuration->logfileTeamcity(),
904+
)]]></code>
905+
<code><![CDATA[OutputFacade::printerFor($configuration->logfileJunit())]]></code>
902906
<code><![CDATA[atLeastVersion]]></code>
903907
<code><![CDATA[build]]></code>
904908
<code><![CDATA[configurationFile]]></code>
@@ -1153,6 +1157,9 @@
11531157
<MissingThrowsDocblock>
11541158
<code><![CDATA[DefaultPrinter::standardError()]]></code>
11551159
<code><![CDATA[DefaultPrinter::standardError()]]></code>
1160+
<code><![CDATA[DefaultPrinter::standardError()]]></code>
1161+
<code><![CDATA[DefaultPrinter::standardOutput()]]></code>
1162+
<code><![CDATA[DefaultPrinter::standardOutput()]]></code>
11561163
<code><![CDATA[DefaultPrinter::standardOutput()]]></code>
11571164
<code><![CDATA[DefaultPrinter::standardOutput()]]></code>
11581165
<code><![CDATA[DefaultPrinter::standardOutput()]]></code>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* This file is part of PHPUnit.
4+
*
5+
* (c) Sebastian Bergmann <sebastian@phpunit.de>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
namespace PHPUnit\TextUI;
11+
12+
use function sprintf;
13+
use RuntimeException;
14+
15+
/**
16+
* @internal This class is not covered by the backward compatibility promise for PHPUnit
17+
*/
18+
final class CannotOpenSocketException extends RuntimeException implements Exception
19+
{
20+
public function __construct(string $hostname, int $port)
21+
{
22+
parent::__construct(
23+
sprintf(
24+
'Cannot open socket %s:%d',
25+
$hostname,
26+
$port,
27+
),
28+
);
29+
}
30+
}

src/TextUI/Output/Facade.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use PHPUnit\Logging\TestDox\TestResultCollection;
1818
use PHPUnit\Runner\DirectoryDoesNotExistException;
1919
use PHPUnit\TestRunner\TestResult\TestResult;
20+
use PHPUnit\TextUI\CannotOpenSocketException;
2021
use PHPUnit\TextUI\Configuration\Configuration;
2122
use PHPUnit\TextUI\InvalidSocketException;
2223
use PHPUnit\TextUI\Output\Default\ProgressPrinter\ProgressPrinter as DefaultProgressPrinter;
@@ -101,6 +102,7 @@ public static function printResult(TestResult $result, ?array $testDoxResult, Du
101102
}
102103

103104
/**
105+
* @throws CannotOpenSocketException
104106
* @throws DirectoryDoesNotExistException
105107
* @throws InvalidSocketException
106108
*/

src/TextUI/Output/Printer/DefaultPrinter.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use function str_replace;
2121
use function str_starts_with;
2222
use PHPUnit\Runner\DirectoryDoesNotExistException;
23+
use PHPUnit\TextUI\CannotOpenSocketException;
2324
use PHPUnit\TextUI\InvalidSocketException;
2425
use PHPUnit\Util\Filesystem;
2526

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

3839
/**
40+
* @throws CannotOpenSocketException
3941
* @throws DirectoryDoesNotExistException
4042
* @throws InvalidSocketException
4143
*/
@@ -45,6 +47,7 @@ public static function from(string $out): self
4547
}
4648

4749
/**
50+
* @throws CannotOpenSocketException
4851
* @throws DirectoryDoesNotExistException
4952
* @throws InvalidSocketException
5053
*/
@@ -54,6 +57,7 @@ public static function standardOutput(): self
5457
}
5558

5659
/**
60+
* @throws CannotOpenSocketException
5761
* @throws DirectoryDoesNotExistException
5862
* @throws InvalidSocketException
5963
*/
@@ -63,6 +67,7 @@ public static function standardError(): self
6367
}
6468

6569
/**
70+
* @throws CannotOpenSocketException
6671
* @throws DirectoryDoesNotExistException
6772
* @throws InvalidSocketException
6873
*/
@@ -77,7 +82,13 @@ private function __construct(string $out)
7782
throw new InvalidSocketException($out);
7883
}
7984

80-
$this->stream = fsockopen($tmp[0], (int) $tmp[1]);
85+
$stream = @fsockopen($tmp[0], (int) $tmp[1]);
86+
87+
if ($stream === false) {
88+
throw new CannotOpenSocketException($tmp[0], (int) $tmp[1]);
89+
}
90+
91+
$this->stream = $stream;
8192
$this->isOpen = true;
8293

8394
return;

0 commit comments

Comments
 (0)