Skip to content

Fix TestCase::expectErrorLog() with open_basedir php.ini #6208

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

Open
wants to merge 3 commits into
base: 12.1
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 35 additions & 14 deletions src/Framework/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
use function is_int;
use function is_object;
use function is_string;
use function is_writable;
use function libxml_clear_errors;
use function method_exists;
use function ob_end_clean;
Expand Down Expand Up @@ -1252,27 +1253,40 @@ private 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']);
$capture = tmpfile();

if ($capture !== false) {
$capturePath = stream_get_meta_data($capture)['uri'];

if (@is_writable($capturePath)) {
$errorLogPrevious = ini_set('error_log', $capturePath);
} else {
$capture = false;
}
}

try {
/** @phpstan-ignore method.dynamicName */
$testResult = $this->{$this->methodName}(...$testArguments);

$errorLogOutput = stream_get_contents($capture);
if ($capture !== false) {
$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);
if ($this->expectErrorLog) {
$this->assertNotEmpty($errorLogOutput, 'Test did not call error_log().');
} else {
print $this->stripDateFromErrorLog($errorLogOutput);
}
} elseif ($this->expectErrorLog) {
$this->markTestIncomplete('Could not create writable error_log file.');
}
} catch (Throwable $exception) {
if (!$this->expectErrorLog) {
$errorLogOutput = stream_get_contents($capture);
if ($capture !== false) {
if (!$this->expectErrorLog) {
$errorLogOutput = stream_get_contents($capture);

// strip date from logged error, see https://github.com/php/php-src/blob/c696087e323263e941774ebbf902ac249774ec9f/main/main.c#L905
print preg_replace('/\[.+\] /', '', $errorLogOutput);
print $this->stripDateFromErrorLog($errorLogOutput);
}
}

if (!$this->shouldExceptionExpectationsBeVerified($exception)) {
Expand All @@ -1285,16 +1299,23 @@ private function runTest(): mixed
} finally {
if ($capture !== false) {
fclose($capture);
}

ini_set('error_log', $errorLogPrevious);
/** @phpstan-ignore variable.undefined (https://github.com/phpstan/phpstan/issues/12992) */
ini_set('error_log', $errorLogPrevious);
}
}

$this->expectedExceptionWasNotRaised();

return $testResult;
}

private function stripDateFromErrorLog(string $log): string
{
// https://github.com/php/php-src/blob/c696087e323263e941774ebbf902ac249774ec9f/main/main.c#L905
return preg_replace('/\[\d+-\w+-\d+ \d+:\d+:\d+ [^\r\n[\]]+?\] /', '', $log);
}

/**
* @throws ExpectationFailedException
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
--TEST--
https://github.com/sebastianbergmann/phpunit/issues/6197
--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';

/*
* Expected result should match the result of expect-error-log-fail-with-open_basedir.phpt test,
* but at least one of these feature requests needs to be implemented in php-src:
* - https://github.com/php/php-src/issues/17817
* - https://github.com/php/php-src/issues/18530
*
* Until then, mark the test result as incomplete when TestCase::expectErrorLog() was called and an error_log file
* could not be created (because of open_basedir php.ini in effect, readonly filesystem...).
*/

ini_set('open_basedir', (ini_get('open_basedir') ? ini_get('open_basedir') . PATH_SEPARATOR : '') . dirname(__DIR__, 3));

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

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

Runtime: %s

I 1 / 1 (100%)

Time: %s, Memory: %s

Expect Error Log Fail (PHPUnit\TestFixture\ExpectNoErrorLog\ExpectErrorLogFail)
∅ One
│ Could not create writable error_log file.


OK, but there were issues!
Tests: 1, Assertions: 1, Incomplete: 1.
33 changes: 33 additions & 0 deletions tests/end-to-end/generic/expect-error-log-with-open_basedir.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
--TEST--
https://github.com/sebastianbergmann/phpunit/issues/6197
--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';

ini_set('open_basedir', (ini_get('open_basedir') ? ini_get('open_basedir') . PATH_SEPARATOR : '') . dirname(__DIR__, 3));

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

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

Runtime: %s

logged a side effect
I 1 / 1 (100%)

Time: %s, Memory: %s

Expect Error Log (PHPUnit\TestFixture\ExpectErrorLog\ExpectErrorLog)
∅ One
│ Could not create writable error_log file.


OK, but there were issues!
Tests: 1, Assertions: 1, Incomplete: 1.