Skip to content

Commit

Permalink
Allow assertions on error_log() output
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm committed Feb 7, 2025
1 parent fa8961d commit b7ce972
Showing 1 changed file with 33 additions and 2 deletions.
35 changes: 33 additions & 2 deletions src/Framework/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@
use function defined;
use function error_clear_last;
use function explode;
use function fclose;
use function getcwd;
use function implode;
use function in_array;
use function ini_set;
use function is_array;
use function is_callable;
use function is_int;
Expand All @@ -44,6 +46,9 @@
use function set_exception_handler;
use function sprintf;
use function str_contains;
use function stream_get_contents;
use function stream_get_meta_data;
use function tmpfile;
use function trim;
use AssertionError;
use DeepCopy\DeepCopy;
Expand Down Expand Up @@ -174,7 +179,13 @@ abstract class TestCase extends Assert implements Reorderable, SelfDescribing, T
private bool $outputBufferingActive = false;
private int $outputBufferingLevel;
private bool $outputRetrievedForAssertion = false;
private bool $doesNotPerformAssertions = false;
private string $errorOutput = '';
private ?string $errorOutputExpectedRegex = null;
private ?string $errorOutputPrevious = null;

/** @var null|resource */
private $errorOutputResource;
private bool $doesNotPerformAssertions = false;

/**
* @var list<Comparator>
Expand Down Expand Up @@ -1002,6 +1013,11 @@ final protected function expectOutputString(string $expectedString): void
$this->outputExpectedString = $expectedString;
}

final protected function expectErrorOutputRegex(string $expectedRegex): void
{
$this->errorOutputExpectedRegex = $expectedRegex;
}

/**
* @param class-string<Throwable> $exception
*/
Expand Down Expand Up @@ -1443,6 +1459,10 @@ private function startOutputBuffering(): void

$this->outputBufferingActive = true;
$this->outputBufferingLevel = ob_get_level();

$capture = tmpfile();
$this->errorOutputPrevious = ini_set('error_log', stream_get_meta_data($capture)['uri']);
$this->errorOutputResource = $capture;
}

private function stopOutputBuffering(): bool
Expand Down Expand Up @@ -1470,6 +1490,15 @@ private function stopOutputBuffering(): bool
return false;
}

if ($this->errorOutputResource !== null) {
$this->errorOutput = stream_get_contents($this->errorOutputResource);
fclose($this->errorOutputResource);
}

if ($this->errorOutputPrevious !== null) {
ini_set('error_log', $this->errorOutputPrevious);
}

$this->output = ob_get_clean();

$this->outputBufferingActive = false;
Expand Down Expand Up @@ -1866,6 +1895,8 @@ private function performAssertionsOnOutput(): void
$this->assertMatchesRegularExpression($this->outputExpectedRegex, $this->output);
} elseif ($this->outputExpectedString !== null) {
$this->assertSame($this->outputExpectedString, $this->output);
} elseif ($this->errorOutputExpectedRegex !== null) {
$this->assertMatchesRegularExpression($this->errorOutputExpectedRegex, $this->errorOutput);
}
} catch (ExpectationFailedException $e) {
$this->status = TestStatus::failure($e->getMessage());
Expand Down Expand Up @@ -2145,7 +2176,7 @@ private function isRegisteredFailure(Throwable $t): bool
*/
private function hasExpectationOnOutput(): bool
{
return is_string($this->outputExpectedString) || is_string($this->outputExpectedRegex);
return is_string($this->outputExpectedString) || is_string($this->outputExpectedRegex) || is_string($this->errorOutputExpectedRegex);
}

private function requirementsNotSatisfied(): bool
Expand Down

0 comments on commit b7ce972

Please sign in to comment.