Skip to content

Commit e445958

Browse files
committed
Inject ForwardCompatibiliy in TestCase
1 parent 215ba8a commit e445958

6 files changed

+52
-139
lines changed

src/Symfony/Bridge/PhpUnit/ForwardCompatTestTrait.php

+2-3
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,13 @@ trait ForwardCompatTestTrait
2222
{
2323
use Legacy\ForwardCompatTestTraitForV5;
2424
}
25-
} elseif ($r->getMethod('tearDown')->hasReturnType()) {
25+
} elseif (!$r->getMethod('tearDown')->hasReturnType()) {
2626
trait ForwardCompatTestTrait
2727
{
28-
use Legacy\ForwardCompatTestTraitForV8;
28+
use Legacy\ForwardCompatTestTraitForV7;
2929
}
3030
} else {
3131
trait ForwardCompatTestTrait
3232
{
33-
use Legacy\ForwardCompatTestTraitForV7;
3433
}
3534
}

src/Symfony/Bridge/PhpUnit/Legacy/ForwardCompatTestTraitForV5.php

+4-69
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
namespace Symfony\Bridge\PhpUnit\Legacy;
1313

1414
use PHPUnit\Framework\MockObject\MockObject;
15-
use PHPUnit\Framework\TestCase;
1615

1716
/**
1817
* @internal
@@ -22,70 +21,6 @@ trait ForwardCompatTestTraitForV5
2221
private $forwardCompatExpectedExceptionMessage = '';
2322
private $forwardCompatExpectedExceptionCode = null;
2423

25-
/**
26-
* @return void
27-
*/
28-
public static function setUpBeforeClass()
29-
{
30-
self::doSetUpBeforeClass();
31-
}
32-
33-
/**
34-
* @return void
35-
*/
36-
public static function tearDownAfterClass()
37-
{
38-
self::doTearDownAfterClass();
39-
}
40-
41-
/**
42-
* @return void
43-
*/
44-
protected function setUp()
45-
{
46-
self::doSetUp();
47-
}
48-
49-
/**
50-
* @return void
51-
*/
52-
protected function tearDown()
53-
{
54-
self::doTearDown();
55-
}
56-
57-
/**
58-
* @return void
59-
*/
60-
private static function doSetUpBeforeClass()
61-
{
62-
parent::setUpBeforeClass();
63-
}
64-
65-
/**
66-
* @return void
67-
*/
68-
private static function doTearDownAfterClass()
69-
{
70-
parent::tearDownAfterClass();
71-
}
72-
73-
/**
74-
* @return void
75-
*/
76-
private function doSetUp()
77-
{
78-
parent::setUp();
79-
}
80-
81-
/**
82-
* @return void
83-
*/
84-
private function doTearDown()
85-
{
86-
parent::tearDown();
87-
}
88-
8924
/**
9025
* @param string $originalClassName
9126
*
@@ -222,7 +157,7 @@ public static function assertIsIterable($actual, $message = '')
222157
*/
223158
public function expectException($exception)
224159
{
225-
if (method_exists(TestCase::class, 'expectException')) {
160+
if (\is_callable('parent::expectException')) {
226161
parent::expectException($exception);
227162

228163
return;
@@ -236,7 +171,7 @@ public function expectException($exception)
236171
*/
237172
public function expectExceptionCode($code)
238173
{
239-
if (method_exists(TestCase::class, 'expectExceptionCode')) {
174+
if (\is_callable('parent::expectExceptionCode')) {
240175
parent::expectExceptionCode($code);
241176

242177
return;
@@ -253,7 +188,7 @@ public function expectExceptionCode($code)
253188
*/
254189
public function expectExceptionMessage($message)
255190
{
256-
if (method_exists(TestCase::class, 'expectExceptionMessage')) {
191+
if (\is_callable('parent::expectExceptionMessage')) {
257192
parent::expectExceptionMessage($message);
258193

259194
return;
@@ -270,7 +205,7 @@ public function expectExceptionMessage($message)
270205
*/
271206
public function expectExceptionMessageRegExp($messageRegExp)
272207
{
273-
if (method_exists(TestCase::class, 'expectExceptionMessageRegExp')) {
208+
if (\is_callable('parent::expectExceptionMessageRegExp')) {
274209
parent::expectExceptionMessageRegExp($messageRegExp);
275210

276211
return;

src/Symfony/Bridge/PhpUnit/Legacy/ForwardCompatTestTraitForV7.php

-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
*/
1919
trait ForwardCompatTestTraitForV7
2020
{
21-
use ForwardCompatTestTraitForV5;
22-
2321
/**
2422
* @param string|string[] $originalClassName
2523
*/

src/Symfony/Bridge/PhpUnit/Legacy/ForwardCompatTestTraitForV8.php

-58
This file was deleted.

src/Symfony/Bridge/PhpUnit/Tests/ClockMockTest.php

+2-5
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Bridge\PhpUnit\ClockMock;
16-
use Symfony\Bridge\PhpUnit\ForwardCompatTestTrait;
1716

1817
/**
1918
* @author Dominic Tubach <dominic.tubach@to.com>
@@ -22,14 +21,12 @@
2221
*/
2322
class ClockMockTest extends TestCase
2423
{
25-
use ForwardCompatTestTrait;
26-
27-
private static function doSetUpBeforeClass()
24+
public static function setUpBeforeClass()
2825
{
2926
ClockMock::register(__CLASS__);
3027
}
3128

32-
private function doSetUp()
29+
protected function setUp()
3330
{
3431
ClockMock::withClockMock(1234567890.125);
3532
}

src/Symfony/Bridge/PhpUnit/bin/simple-phpunit.php

+44-2
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@
6363
$PHPUNIT_VERSION = '4.8';
6464
}
6565

66+
$PHPUNIT_INJECT_FORWARD_COMPAT = $getEnvVar('SYMFONY_PHPUNIT_INJECT_FORWARD_COMPAT', '1') === '1';
67+
$PHPUNIT_REMOVE_RETURN_TYPEHINT = $getEnvVar('SYMFONY_PHPUNIT_REMOVE_RETURN_TYPEHINT', '0') === '1';
68+
6669
$COMPOSER_JSON = getenv('COMPOSER') ?: 'composer.json';
6770

6871
$root = __DIR__;
@@ -100,8 +103,9 @@
100103

101104

102105
$SYMFONY_PHPUNIT_REMOVE = $getEnvVar('SYMFONY_PHPUNIT_REMOVE', 'phpspec/prophecy'.($PHPUNIT_VERSION < 6.0 ? ' symfony/yaml': ''));
106+
$configurationHash = implode(PHP_EOL, [md5_file(__FILE__), $SYMFONY_PHPUNIT_REMOVE, (int) $PHPUNIT_INJECT_FORWARD_COMPAT, (int) $PHPUNIT_REMOVE_RETURN_TYPEHINT]);
103107

104-
if (!file_exists("$PHPUNIT_DIR/phpunit-$PHPUNIT_VERSION/phpunit") || md5_file(__FILE__)."\n".$SYMFONY_PHPUNIT_REMOVE !== @file_get_contents("$PHPUNIT_DIR/.$PHPUNIT_VERSION.md5")) {
108+
if (!file_exists("$PHPUNIT_DIR/phpunit-$PHPUNIT_VERSION/phpunit") || $configurationHash !== @file_get_contents("$PHPUNIT_DIR/.$PHPUNIT_VERSION.md5")) {
105109
// Build a standalone phpunit without symfony/yaml nor prophecy by default
106110

107111
@mkdir($PHPUNIT_DIR, 0777, true);
@@ -139,6 +143,44 @@
139143
if ($exit) {
140144
exit($exit);
141145
}
146+
147+
// Mutate TestsCase code
148+
if ($PHPUNIT_INJECT_FORWARD_COMPAT || $PHPUNIT_REMOVE_RETURN_TYPEHINT) {
149+
$testCaseFile = implode(DIRECTORY_SEPARATOR, ['src', 'Framework', 'TestCase.php']);
150+
$testCaseCode = file_get_contents($testCaseFile);
151+
if ($PHPUNIT_REMOVE_RETURN_TYPEHINT) {
152+
$testCaseCode = preg_replace('/^ ((?:protected|public)(?: static)? function \w+\(\)): void/m', ' $1', $testCaseCode);
153+
}
154+
155+
if ($PHPUNIT_INJECT_FORWARD_COMPAT) {
156+
$injectedTrait = '';
157+
if ($PHPUNIT_VERSION < 7) {
158+
$injectedTrait = 'ForwardCompatTestTraitForV5';
159+
} elseif ($PHPUNIT_VERSION < 8) {
160+
$injectedTrait = 'ForwardCompatTestTraitForV7';
161+
}
162+
}
163+
164+
if ($injectedTrait) {
165+
$testCaseCode = preg_replace(
166+
'/abstract class (?:TestCase|PHPUnit_Framework_TestCase)[^\{]+\{/',
167+
'$0 '.PHP_EOL." use \Symfony\Bridge\PhpUnit\Legacy\\$injectedTrait;",
168+
$testCaseCode
169+
);
170+
$traitFile = "vendor/symfony/phpunit-bridge/Legacy/$injectedTrait.php";
171+
file_put_contents(
172+
$traitFile,
173+
str_replace(
174+
' * @internal',
175+
'',
176+
file_get_contents($traitFile)
177+
)
178+
);
179+
}
180+
181+
file_put_contents($testCaseFile, $testCaseCode);
182+
}
183+
142184
file_put_contents('phpunit', <<<'EOPHP'
143185
<?php
144186
@@ -161,7 +203,7 @@ class SymfonyBlacklistPhpunit {}
161203
EOPHP
162204
);
163205
chdir('..');
164-
file_put_contents(".$PHPUNIT_VERSION.md5", md5_file(__FILE__)."\n".$SYMFONY_PHPUNIT_REMOVE);
206+
file_put_contents(".$PHPUNIT_VERSION.md5", $configurationHash);
165207
chdir($oldPwd);
166208

167209
}

0 commit comments

Comments
 (0)