Skip to content

Commit b1f79e8

Browse files
ArturGoldynsebastianbergmann
authored andcommitted
"TestCaseClass" template for testing entire class in isolation.
1 parent 6ebee52 commit b1f79e8

File tree

3 files changed

+119
-12
lines changed

3 files changed

+119
-12
lines changed

src/Framework/TestCase.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -770,15 +770,23 @@ public function run(TestResult $result = null)
770770
!$this->handleDependencies()) {
771771
return;
772772
}
773+
774+
$runEntireClass = $this->runClassInSeparateProcess && !$this->runTestInSeparateProcess;
773775

774776
if (($this->runTestInSeparateProcess === true || $this->runClassInSeparateProcess === true) &&
775777
$this->inIsolation !== true &&
776778
!$this instanceof PhptTestCase) {
777779
$class = new ReflectionClass($this);
778780

779-
$template = new Text_Template(
780-
__DIR__ . '/../Util/PHP/Template/TestCaseMethod.tpl'
781-
);
781+
if ($runEntireClass) {
782+
$template = new Text_Template(
783+
__DIR__ . '/../Util/PHP/Template/TestCaseClass.tpl'
784+
);
785+
} else {
786+
$template = new Text_Template(
787+
__DIR__ . '/../Util/PHP/Template/TestCaseMethod.tpl'
788+
);
789+
}
782790

783791
if ($this->preserveGlobalState) {
784792
$constants = GlobalState::getConstantsAsString();
@@ -836,14 +844,11 @@ public function run(TestResult $result = null)
836844

837845
$configurationFilePath = $GLOBALS['__PHPUNIT_CONFIGURATION_FILE'] ?? '';
838846

839-
$runEntireClass = $this->runClassInSeparateProcess && !$this->runTestInSeparateProcess;
840-
841847
$var = [
842848
'composerAutoload' => $composerAutoload,
843849
'phar' => $phar,
844850
'filename' => $class->getFileName(),
845851
'className' => $class->getName(),
846-
'runEntireClass' => $runEntireClass,
847852
'collectCodeCoverageInformation' => $coverage,
848853
'data' => $data,
849854
'dataName' => $dataName,
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
<?php
2+
use SebastianBergmann\CodeCoverage\CodeCoverage;
3+
4+
if (!defined('STDOUT')) {
5+
// php://stdout does not obey output buffering. Any output would break
6+
// unserialization of child process results in the parent process.
7+
define('STDOUT', fopen('php://temp', 'w+b'));
8+
define('STDERR', fopen('php://stderr', 'wb'));
9+
}
10+
11+
{iniSettings}
12+
ini_set('display_errors', 'stderr');
13+
set_include_path('{include_path}');
14+
15+
$composerAutoload = {composerAutoload};
16+
$phar = {phar};
17+
18+
ob_start();
19+
20+
if ($composerAutoload) {
21+
require_once $composerAutoload;
22+
define('PHPUNIT_COMPOSER_INSTALL', $composerAutoload);
23+
} else if ($phar) {
24+
require $phar;
25+
}
26+
27+
function __phpunit_run_isolated_test()
28+
{
29+
if (!class_exists('{className}')) {
30+
require_once '{filename}';
31+
}
32+
33+
$result = new PHPUnit\Framework\TestResult;
34+
35+
if ({collectCodeCoverageInformation}) {
36+
$result->setCodeCoverage(
37+
new CodeCoverage(
38+
null,
39+
unserialize('{codeCoverageFilter}')
40+
)
41+
);
42+
}
43+
44+
$result->beStrictAboutTestsThatDoNotTestAnything({isStrictAboutTestsThatDoNotTestAnything});
45+
$result->beStrictAboutOutputDuringTests({isStrictAboutOutputDuringTests});
46+
$result->enforceTimeLimit({enforcesTimeLimit});
47+
$result->beStrictAboutTodoAnnotatedTests({isStrictAboutTodoAnnotatedTests});
48+
$result->beStrictAboutResourceUsageDuringSmallTests({isStrictAboutResourceUsageDuringSmallTests});
49+
50+
$test = new {className}(null, unserialize('{data}'), '{dataName}');
51+
$test->setDependencyInput(unserialize('{dependencyInput}'));
52+
$test->setInIsolation(TRUE);
53+
54+
ob_end_clean();
55+
$test->run($result);
56+
$output = '';
57+
if (!$test->hasExpectationOnOutput()) {
58+
$output = $test->getActualOutput();
59+
}
60+
61+
@rewind(STDOUT); /* @ as not every STDOUT target stream is rewindable */
62+
if ($stdout = stream_get_contents(STDOUT)) {
63+
$output = $stdout . $output;
64+
$streamMetaData = stream_get_meta_data(STDOUT);
65+
if (!empty($streamMetaData['stream_type']) && 'STDIO' === $streamMetaData['stream_type']) {
66+
@ftruncate(STDOUT, 0);
67+
@rewind(STDOUT);
68+
}
69+
}
70+
71+
print serialize(
72+
array(
73+
'testResult' => $test->getResult(),
74+
'numAssertions' => $test->getNumAssertions(),
75+
'result' => $result,
76+
'output' => $output
77+
)
78+
);
79+
}
80+
81+
$configurationFilePath = '{configurationFilePath}';
82+
83+
if ('' !== $configurationFilePath) {
84+
$configuration = PHPUnit\Util\Configuration::getInstance($configurationFilePath);
85+
$configuration->handlePHPConfiguration();
86+
unset($configuration);
87+
}
88+
89+
function __phpunit_error_handler($errno, $errstr, $errfile, $errline, $errcontext)
90+
{
91+
return true;
92+
}
93+
94+
set_error_handler("__phpunit_error_handler");
95+
96+
{constants}
97+
{included_files}
98+
{globals}
99+
100+
restore_error_handler();
101+
102+
if (isset($GLOBALS['__PHPUNIT_BOOTSTRAP'])) {
103+
require_once $GLOBALS['__PHPUNIT_BOOTSTRAP'];
104+
unset($GLOBALS['__PHPUNIT_BOOTSTRAP']);
105+
}
106+
107+
__phpunit_run_isolated_test();

src/Util/PHP/Template/TestCaseMethod.tpl.dist

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,7 @@ function __phpunit_run_isolated_test()
4747
$result->beStrictAboutTodoAnnotatedTests({isStrictAboutTodoAnnotatedTests});
4848
$result->beStrictAboutResourceUsageDuringSmallTests({isStrictAboutResourceUsageDuringSmallTests});
4949

50-
if (isset($runEntireClass) && $runEntireClass) {
51-
$test = new {className}(null, unserialize('{data}'), '{dataName}');
52-
} else {
53-
$test = new {className}('{methodName}', unserialize('{data}'), '{dataName}');
54-
}
55-
50+
$test = new {className}('{methodName}', unserialize('{data}'), '{dataName}');
5651
$test->setDependencyInput(unserialize('{dependencyInput}'));
5752
$test->setInIsolation(TRUE);
5853

0 commit comments

Comments
 (0)