Skip to content

Commit 5af9431

Browse files
Extract methods
1 parent 61f3cf9 commit 5af9431

File tree

1 file changed

+63
-42
lines changed

1 file changed

+63
-42
lines changed

src/Framework/TestCase.php

Lines changed: 63 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,12 @@ abstract class TestCase extends Assert implements Reorderable, SelfDescribing, T
219219
* @var list<non-empty-string>
220220
*/
221221
private array $expectedUserDeprecationMessageRegularExpression = [];
222-
private false|string $previousErrorLogTarget = false;
222+
223+
/**
224+
* @var false|resource
225+
*/
226+
private mixed $errorLogCapture = false;
227+
private false|string $previousErrorLogTarget = false;
223228

224229
/**
225230
* @param non-empty-string $name
@@ -1299,35 +1304,15 @@ private function runTest(): mixed
12991304
{
13001305
$testArguments = array_merge($this->data, array_values($this->dependencyInput));
13011306

1302-
$capture = $this->startErrorLogCapture();
1307+
$this->startErrorLogCapture();
13031308

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

1308-
if ($capture !== false) {
1309-
$errorLogOutput = stream_get_contents($capture);
1310-
1311-
if ($this->expectErrorLog) {
1312-
$this->assertNotEmpty($errorLogOutput, 'Test did not call error_log().');
1313-
} else {
1314-
if ($errorLogOutput !== false) {
1315-
print $this->stripDateFromErrorLog($errorLogOutput);
1316-
}
1317-
}
1318-
} elseif ($this->expectErrorLog) {
1319-
$this->markTestIncomplete('Could not create writable error_log file.');
1320-
}
1313+
$this->verifyErrorLogExpectation();
13211314
} catch (Throwable $exception) {
1322-
if ($capture !== false) {
1323-
if (!$this->expectErrorLog) {
1324-
$errorLogOutput = stream_get_contents($capture);
1325-
1326-
if ($errorLogOutput !== false) {
1327-
print $this->stripDateFromErrorLog($errorLogOutput);
1328-
}
1329-
}
1330-
}
1315+
$this->handleErrorLogError();
13311316

13321317
if (!$this->shouldExceptionExpectationsBeVerified($exception)) {
13331318
throw $exception;
@@ -1337,15 +1322,7 @@ private function runTest(): mixed
13371322

13381323
return null;
13391324
} finally {
1340-
if ($capture !== false) {
1341-
ShutdownHandler::resetMessage();
1342-
1343-
fclose($capture);
1344-
1345-
if ($this->previousErrorLogTarget !== false) {
1346-
ini_set('error_log', $this->previousErrorLogTarget);
1347-
}
1348-
}
1325+
$this->stopErrorLogCapture();
13491326
}
13501327

13511328
$this->expectedExceptionWasNotRaised();
@@ -2336,28 +2313,72 @@ private function handleExceptionFromInvokedCountMockObjectRule(Throwable $t): vo
23362313
}
23372314
}
23382315

2339-
/**
2340-
* @return false|resource
2341-
*/
2342-
private function startErrorLogCapture(): mixed
2316+
private function startErrorLogCapture(): void
23432317
{
23442318
if (ini_get('display_errors') === '0') {
23452319
ShutdownHandler::setMessage('Fatal error: Premature end of PHP process. Use display_errors=On to see the error message.');
23462320
}
23472321

2348-
$capture = tmpfile();
2322+
$errorLogCapture = tmpfile();
23492323

2350-
if ($capture !== false) {
2351-
$capturePath = stream_get_meta_data($capture)['uri'];
2324+
if ($errorLogCapture !== false) {
2325+
$capturePath = stream_get_meta_data($errorLogCapture)['uri'];
23522326

23532327
if (@is_writable($capturePath)) {
23542328
$this->previousErrorLogTarget = ini_set('error_log', $capturePath);
23552329
} else {
2356-
$capture = false;
2330+
$errorLogCapture = false;
2331+
}
2332+
}
2333+
2334+
$this->errorLogCapture = $errorLogCapture;
2335+
}
2336+
2337+
private function verifyErrorLogExpectation(): void
2338+
{
2339+
if ($this->errorLogCapture !== false) {
2340+
$errorLogOutput = stream_get_contents($this->errorLogCapture);
2341+
2342+
if ($this->expectErrorLog) {
2343+
$this->assertNotEmpty($errorLogOutput, 'Test did not call error_log().');
2344+
} else {
2345+
if ($errorLogOutput !== false) {
2346+
print $this->stripDateFromErrorLog($errorLogOutput);
2347+
}
23572348
}
2349+
} elseif ($this->expectErrorLog) {
2350+
$this->markTestIncomplete('Could not create writable error_log file.');
23582351
}
2352+
}
2353+
2354+
private function handleErrorLogError(): void
2355+
{
2356+
if ($this->errorLogCapture !== false) {
2357+
if (!$this->expectErrorLog) {
2358+
$errorLogOutput = stream_get_contents($this->errorLogCapture);
23592359

2360-
return $capture;
2360+
if ($errorLogOutput !== false) {
2361+
print $this->stripDateFromErrorLog($errorLogOutput);
2362+
}
2363+
}
2364+
}
2365+
}
2366+
2367+
private function stopErrorLogCapture(): void
2368+
{
2369+
if ($this->errorLogCapture !== false) {
2370+
ShutdownHandler::resetMessage();
2371+
2372+
fclose($this->errorLogCapture);
2373+
2374+
$this->errorLogCapture = false;
2375+
2376+
if ($this->previousErrorLogTarget !== false) {
2377+
ini_set('error_log', $this->previousErrorLogTarget);
2378+
2379+
$this->previousErrorLogTarget = false;
2380+
}
2381+
}
23612382
}
23622383

23632384
/**

0 commit comments

Comments
 (0)