|
22 | 22 | use function defined;
|
23 | 23 | use function error_clear_last;
|
24 | 24 | use function explode;
|
| 25 | +use function fclose; |
25 | 26 | use function getcwd;
|
26 | 27 | use function implode;
|
27 | 28 | use function in_array;
|
| 29 | +use function ini_set; |
28 | 30 | use function is_array;
|
29 | 31 | use function is_callable;
|
30 | 32 | use function is_int;
|
|
44 | 46 | use function set_exception_handler;
|
45 | 47 | use function sprintf;
|
46 | 48 | use function str_contains;
|
| 49 | +use function stream_get_contents; |
| 50 | +use function stream_get_meta_data; |
| 51 | +use function tmpfile; |
47 | 52 | use function trim;
|
48 | 53 | use AssertionError;
|
49 | 54 | use DeepCopy\DeepCopy;
|
@@ -174,7 +179,13 @@ abstract class TestCase extends Assert implements Reorderable, SelfDescribing, T
|
174 | 179 | private bool $outputBufferingActive = false;
|
175 | 180 | private int $outputBufferingLevel;
|
176 | 181 | private bool $outputRetrievedForAssertion = false;
|
177 |
| - private bool $doesNotPerformAssertions = false; |
| 182 | + private string $errorOutput = ''; |
| 183 | + private ?string $errorOutputExpectedRegex = null; |
| 184 | + private ?string $errorOutputPrevious = null; |
| 185 | + |
| 186 | + /** @var null|resource */ |
| 187 | + private $errorOutputResource; |
| 188 | + private bool $doesNotPerformAssertions = false; |
178 | 189 |
|
179 | 190 | /**
|
180 | 191 | * @var list<Comparator>
|
@@ -1002,6 +1013,11 @@ final protected function expectOutputString(string $expectedString): void
|
1002 | 1013 | $this->outputExpectedString = $expectedString;
|
1003 | 1014 | }
|
1004 | 1015 |
|
| 1016 | + final protected function expectErrorOutputRegex(string $expectedRegex): void |
| 1017 | + { |
| 1018 | + $this->errorOutputExpectedRegex = $expectedRegex; |
| 1019 | + } |
| 1020 | + |
1005 | 1021 | /**
|
1006 | 1022 | * @param class-string<Throwable> $exception
|
1007 | 1023 | */
|
@@ -1443,6 +1459,10 @@ private function startOutputBuffering(): void
|
1443 | 1459 |
|
1444 | 1460 | $this->outputBufferingActive = true;
|
1445 | 1461 | $this->outputBufferingLevel = ob_get_level();
|
| 1462 | + |
| 1463 | + $capture = tmpfile(); |
| 1464 | + $this->errorOutputPrevious = ini_set('error_log', stream_get_meta_data($capture)['uri']); |
| 1465 | + $this->errorOutputResource = $capture; |
1446 | 1466 | }
|
1447 | 1467 |
|
1448 | 1468 | private function stopOutputBuffering(): bool
|
@@ -1470,6 +1490,15 @@ private function stopOutputBuffering(): bool
|
1470 | 1490 | return false;
|
1471 | 1491 | }
|
1472 | 1492 |
|
| 1493 | + if ($this->errorOutputResource !== null) { |
| 1494 | + $this->errorOutput = stream_get_contents($this->errorOutputResource); |
| 1495 | + fclose($this->errorOutputResource); |
| 1496 | + } |
| 1497 | + |
| 1498 | + if ($this->errorOutputPrevious !== null) { |
| 1499 | + ini_set('error_log', $this->errorOutputPrevious); |
| 1500 | + } |
| 1501 | + |
1473 | 1502 | $this->output = ob_get_clean();
|
1474 | 1503 |
|
1475 | 1504 | $this->outputBufferingActive = false;
|
@@ -1866,6 +1895,8 @@ private function performAssertionsOnOutput(): void
|
1866 | 1895 | $this->assertMatchesRegularExpression($this->outputExpectedRegex, $this->output);
|
1867 | 1896 | } elseif ($this->outputExpectedString !== null) {
|
1868 | 1897 | $this->assertSame($this->outputExpectedString, $this->output);
|
| 1898 | + } elseif ($this->errorOutputExpectedRegex !== null) { |
| 1899 | + $this->assertMatchesRegularExpression($this->errorOutputExpectedRegex, $this->errorOutput); |
1869 | 1900 | }
|
1870 | 1901 | } catch (ExpectationFailedException $e) {
|
1871 | 1902 | $this->status = TestStatus::failure($e->getMessage());
|
@@ -2145,7 +2176,7 @@ private function isRegisteredFailure(Throwable $t): bool
|
2145 | 2176 | */
|
2146 | 2177 | private function hasExpectationOnOutput(): bool
|
2147 | 2178 | {
|
2148 |
| - return is_string($this->outputExpectedString) || is_string($this->outputExpectedRegex); |
| 2179 | + return is_string($this->outputExpectedString) || is_string($this->outputExpectedRegex) || is_string($this->errorOutputExpectedRegex); |
2149 | 2180 | }
|
2150 | 2181 |
|
2151 | 2182 | private function requirementsNotSatisfied(): bool
|
|
0 commit comments