Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add expectErrorLog() for expecting error_log() output #6118

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
30 changes: 30 additions & 0 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 @@ -38,12 +40,16 @@
use function ob_get_level;
use function ob_start;
use function preg_match;
use function preg_replace;
use function restore_error_handler;
use function restore_exception_handler;
use function set_error_handler;
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 @@ -175,6 +181,7 @@ abstract class TestCase extends Assert implements Reorderable, SelfDescribing, T
private int $outputBufferingLevel;
private bool $outputRetrievedForAssertion = false;
private bool $doesNotPerformAssertions = false;
private bool $expectErrorLog = false;

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

final protected function expectErrorLog(): void
{
$this->expectErrorLog = true;
}

/**
* @param class-string<Throwable> $exception
*/
Expand Down Expand Up @@ -1104,8 +1116,20 @@ final protected function runTest(): mixed
{
$testArguments = array_merge($this->data, array_values($this->dependencyInput));

$capture = tmpfile();
$errorLogPrevious = ini_set('error_log', stream_get_meta_data($capture)['uri']);

try {
$testResult = $this->{$this->methodName}(...$testArguments);

$errorLogOutput = stream_get_contents($capture);

if ($this->expectErrorLog) {
$this->assertNotEmpty($errorLogOutput, 'Test did not call error_log().');
} else {
// strip date from logged error, see https://github.com/php/php-src/blob/c696087e323263e941774ebbf902ac249774ec9f/main/main.c#L905
print preg_replace('/\[.+\] /', '', $errorLogOutput);
}
} catch (Throwable $exception) {
if (!$this->shouldExceptionExpectationsBeVerified($exception)) {
throw $exception;
Expand All @@ -1114,6 +1138,12 @@ final protected function runTest(): mixed
$this->verifyExceptionExpectations($exception);

return null;
} finally {
if ($capture !== false) {
fclose($capture);
}

ini_set('error_log', $errorLogPrevious);
}

$this->expectedExceptionWasNotRaised();
Expand Down
31 changes: 31 additions & 0 deletions tests/end-to-end/generic/_files/ExpectErrorLogFailTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php declare(strict_types=1);
/*
* This file is part of PHPUnit.
*
* (c) Sebastian Bergmann <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PHPUnit\TestFixture\ExpectNoErrorLog;

use PHPUnit\Framework\TestCase;

class FooBar
{
public function doFoo()
{
return '';
}
}

final class ExpectErrorLogFailTest extends TestCase
{
public function testOne(): void
{
$foo = new FooBar;

$this->assertSame('', $foo->doFoo());
$this->expectErrorLog();
}
}
34 changes: 34 additions & 0 deletions tests/end-to-end/generic/_files/ExpectErrorLogTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php declare(strict_types=1);
/*
* This file is part of PHPUnit.
*
* (c) Sebastian Bergmann <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PHPUnit\TestFixture\ExpectErrorLog;

use function error_log;
use PHPUnit\Framework\TestCase;

class Foo
{
public function doFoo()
{
error_log('logged a side effect');

return '';
}
}

final class ExpectErrorLogTest extends TestCase
{
public function testOne(): void
{
$foo = new Foo;

$this->assertSame('', $foo->doFoo());
$this->expectErrorLog();
}
}
31 changes: 31 additions & 0 deletions tests/end-to-end/generic/expect-error-log-fail.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
--TEST--
https://github.com/sebastianbergmann/phpunit/issues/2155
--FILE--
<?php declare(strict_types=1);
$_SERVER['argv'][] = '--do-not-cache-result';
$_SERVER['argv'][] = '--no-configuration';
$_SERVER['argv'][] = '--testdox';
$_SERVER['argv'][] = __DIR__ . '/_files/ExpectErrorLogFailTest.php';

require_once __DIR__ . '/../../bootstrap.php';

(new PHPUnit\TextUI\Application)->run($_SERVER['argv']);
--EXPECTF--
PHPUnit %s by Sebastian Bergmann and contributors.

Runtime: %s

F 1 / 1 (100%)

Time: %s, Memory: %s

Expect Error Log Fail (PHPUnit\TestFixture\ExpectNoErrorLog\ExpectErrorLogFail)
✘ One
│ Test did not call error_log().
│ Failed asserting that a string is not empty.


FAILURES!
Tests: 1, Assertions: 2, Failures: 1.
25 changes: 25 additions & 0 deletions tests/end-to-end/generic/expect-error-log.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
--TEST--
https://github.com/sebastianbergmann/phpunit/issues/2155
--FILE--
<?php declare(strict_types=1);
$_SERVER['argv'][] = '--do-not-cache-result';
$_SERVER['argv'][] = '--no-configuration';
$_SERVER['argv'][] = '--testdox';
$_SERVER['argv'][] = __DIR__ . '/_files/ExpectErrorLogTest.php';

require_once __DIR__ . '/../../bootstrap.php';

(new PHPUnit\TextUI\Application)->run($_SERVER['argv']);
--EXPECTF--
PHPUnit %s by Sebastian Bergmann and contributors.

Runtime: %s

. 1 / 1 (100%)

Time: %s, Memory: %s

Expect Error Log (PHPUnit\TestFixture\ExpectErrorLog\ExpectErrorLog)
✔ One

OK (1 test, 2 assertions)