Skip to content

Commit e008eb3

Browse files
committed
Fix TestCase::expectErrorLog() with open_basedir php.ini
1 parent 1314136 commit e008eb3

File tree

3 files changed

+93
-14
lines changed

3 files changed

+93
-14
lines changed

src/Framework/TestCase.php

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
use function is_int;
3333
use function is_object;
3434
use function is_string;
35+
use function is_writable;
3536
use function libxml_clear_errors;
3637
use function method_exists;
3738
use function ob_end_clean;
@@ -1252,27 +1253,40 @@ private function runTest(): mixed
12521253
{
12531254
$testArguments = array_merge($this->data, array_values($this->dependencyInput));
12541255

1255-
$capture = tmpfile();
1256-
$errorLogPrevious = ini_set('error_log', stream_get_meta_data($capture)['uri']);
1256+
$capture = tmpfile();
1257+
1258+
if ($capture !== false) {
1259+
$capturePath = stream_get_meta_data($capture)['uri'];
1260+
1261+
if (@is_writable($capturePath)) {
1262+
$errorLogPrevious = ini_set('error_log', $capturePath);
1263+
} else {
1264+
$capture = false;
1265+
}
1266+
}
12571267

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

1262-
$errorLogOutput = stream_get_contents($capture);
1272+
if ($capture !== false) {
1273+
$errorLogOutput = stream_get_contents($capture);
12631274

1264-
if ($this->expectErrorLog) {
1265-
$this->assertNotEmpty($errorLogOutput, 'Test did not call error_log().');
1266-
} else {
1267-
// strip date from logged error, see https://github.com/php/php-src/blob/c696087e323263e941774ebbf902ac249774ec9f/main/main.c#L905
1268-
print preg_replace('/\[.+\] /', '', $errorLogOutput);
1275+
if ($this->expectErrorLog) {
1276+
$this->assertNotEmpty($errorLogOutput, 'Test did not call error_log().');
1277+
} else {
1278+
// strip date from logged error, see https://github.com/php/php-src/blob/c696087e323263e941774ebbf902ac249774ec9f/main/main.c#L905
1279+
print preg_replace('/\[.+\] /', '', $errorLogOutput);
1280+
}
12691281
}
12701282
} catch (Throwable $exception) {
1271-
if (!$this->expectErrorLog) {
1272-
$errorLogOutput = stream_get_contents($capture);
1283+
if ($capture !== false) {
1284+
if (!$this->expectErrorLog) {
1285+
$errorLogOutput = stream_get_contents($capture);
12731286

1274-
// strip date from logged error, see https://github.com/php/php-src/blob/c696087e323263e941774ebbf902ac249774ec9f/main/main.c#L905
1275-
print preg_replace('/\[.+\] /', '', $errorLogOutput);
1287+
// strip date from logged error, see https://github.com/php/php-src/blob/c696087e323263e941774ebbf902ac249774ec9f/main/main.c#L905
1288+
print preg_replace('/\[.+\] /', '', $errorLogOutput);
1289+
}
12761290
}
12771291

12781292
if (!$this->shouldExceptionExpectationsBeVerified($exception)) {
@@ -1285,9 +1299,10 @@ private function runTest(): mixed
12851299
} finally {
12861300
if ($capture !== false) {
12871301
fclose($capture);
1288-
}
12891302

1290-
ini_set('error_log', $errorLogPrevious);
1303+
/** @phpstan-ignore variable.undefined (https://github.com/phpstan/phpstan/issues/12992) */
1304+
ini_set('error_log', $errorLogPrevious);
1305+
}
12911306
}
12921307

12931308
$this->expectedExceptionWasNotRaised();
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
--TEST--
2+
https://github.com/sebastianbergmann/phpunit/issues/6197
3+
--FILE--
4+
<?php declare(strict_types=1);
5+
$_SERVER['argv'][] = '--do-not-cache-result';
6+
$_SERVER['argv'][] = '--no-configuration';
7+
$_SERVER['argv'][] = '--testdox';
8+
$_SERVER['argv'][] = __DIR__ . '/_files/ExpectErrorLogFailTest.php';
9+
10+
/*
11+
* Expected result should math expect-error-log-fail-with-open_basedir.phpt test,
12+
* but at least one of these feature requests needs to be implemented in php-src:
13+
* - https://github.com/php/php-src/issues/17817
14+
* - https://github.com/php/php-src/issues/18530
15+
*
16+
* Until then, ignore TestCase::expectErrorLog() if open_basedir php.ini is in effect.
17+
*/
18+
19+
ini_set('open_basedir', (ini_get('open_basedir') ? ini_get('open_basedir') . PATH_SEPARATOR : '') . dirname(__DIR__, 3));
20+
21+
require_once __DIR__ . '/../../bootstrap.php';
22+
23+
(new PHPUnit\TextUI\Application)->run($_SERVER['argv']);
24+
--EXPECTF--
25+
PHPUnit %s by Sebastian Bergmann and contributors.
26+
27+
Runtime: %s
28+
29+
. 1 / 1 (100%)
30+
31+
Time: %s, Memory: %s
32+
33+
Expect Error Log Fail (PHPUnit\TestFixture\ExpectNoErrorLog\ExpectErrorLogFail)
34+
✔ One
35+
36+
OK (1 test, 1 assertion)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
--TEST--
2+
https://github.com/sebastianbergmann/phpunit/issues/6197
3+
--FILE--
4+
<?php declare(strict_types=1);
5+
$_SERVER['argv'][] = '--do-not-cache-result';
6+
$_SERVER['argv'][] = '--no-configuration';
7+
$_SERVER['argv'][] = '--testdox';
8+
$_SERVER['argv'][] = __DIR__ . '/_files/ExpectErrorLogTest.php';
9+
10+
ini_set('open_basedir', (ini_get('open_basedir') ? ini_get('open_basedir') . PATH_SEPARATOR : '') . dirname(__DIR__, 3));
11+
12+
require_once __DIR__ . '/../../bootstrap.php';
13+
14+
(new PHPUnit\TextUI\Application)->run($_SERVER['argv']);
15+
--EXPECTF--
16+
PHPUnit %s by Sebastian Bergmann and contributors.
17+
18+
Runtime: %s
19+
20+
logged a side effect
21+
. 1 / 1 (100%)
22+
23+
Time: %s, Memory: %s
24+
25+
Expect Error Log (PHPUnit\TestFixture\ExpectErrorLog\ExpectErrorLog)
26+
✔ One
27+
28+
OK (1 test, 1 assertion)

0 commit comments

Comments
 (0)