Skip to content

Commit

Permalink
Inject ForwardCompatibiliy in TestCase
Browse files Browse the repository at this point in the history
  • Loading branch information
jderusse committed Aug 2, 2019
1 parent 215ba8a commit 588b4eb
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 137 deletions.
5 changes: 2 additions & 3 deletions src/Symfony/Bridge/PhpUnit/ForwardCompatTestTrait.php
Expand Up @@ -22,14 +22,13 @@ trait ForwardCompatTestTrait
{
use Legacy\ForwardCompatTestTraitForV5;
}
} elseif ($r->getMethod('tearDown')->hasReturnType()) {
} elseif (!$r->getMethod('tearDown')->hasReturnType()) {
trait ForwardCompatTestTrait
{
use Legacy\ForwardCompatTestTraitForV8;
use Legacy\ForwardCompatTestTraitForV7;
}
} else {
trait ForwardCompatTestTrait
{
use Legacy\ForwardCompatTestTraitForV7;
}
}
72 changes: 4 additions & 68 deletions src/Symfony/Bridge/PhpUnit/Legacy/ForwardCompatTestTraitForV5.php
Expand Up @@ -22,70 +22,6 @@ trait ForwardCompatTestTraitForV5
private $forwardCompatExpectedExceptionMessage = '';
private $forwardCompatExpectedExceptionCode = null;

/**
* @return void
*/
public static function setUpBeforeClass()
{
self::doSetUpBeforeClass();
}

/**
* @return void
*/
public static function tearDownAfterClass()
{
self::doTearDownAfterClass();
}

/**
* @return void
*/
protected function setUp()
{
self::doSetUp();
}

/**
* @return void
*/
protected function tearDown()
{
self::doTearDown();
}

/**
* @return void
*/
private static function doSetUpBeforeClass()
{
parent::setUpBeforeClass();
}

/**
* @return void
*/
private static function doTearDownAfterClass()
{
parent::tearDownAfterClass();
}

/**
* @return void
*/
private function doSetUp()
{
parent::setUp();
}

/**
* @return void
*/
private function doTearDown()
{
parent::tearDown();
}

/**
* @param string $originalClassName
*
Expand Down Expand Up @@ -222,7 +158,7 @@ public static function assertIsIterable($actual, $message = '')
*/
public function expectException($exception)
{
if (method_exists(TestCase::class, 'expectException')) {
if (is_callable('parent::expectException')) {
parent::expectException($exception);

return;
Expand All @@ -236,7 +172,7 @@ public function expectException($exception)
*/
public function expectExceptionCode($code)
{
if (method_exists(TestCase::class, 'expectExceptionCode')) {
if (is_callable('parent::expectExceptionCode')) {
parent::expectExceptionCode($code);

return;
Expand All @@ -253,7 +189,7 @@ public function expectExceptionCode($code)
*/
public function expectExceptionMessage($message)
{
if (method_exists(TestCase::class, 'expectExceptionMessage')) {
if (is_callable('parent::expectExceptionMessage')) {
parent::expectExceptionMessage($message);

return;
Expand All @@ -270,7 +206,7 @@ public function expectExceptionMessage($message)
*/
public function expectExceptionMessageRegExp($messageRegExp)
{
if (method_exists(TestCase::class, 'expectExceptionMessageRegExp')) {
if (is_callable('parent::expectExceptionMessageRegExp')) {
parent::expectExceptionMessageRegExp($messageRegExp);

return;
Expand Down
Expand Up @@ -18,8 +18,6 @@
*/
trait ForwardCompatTestTraitForV7
{
use ForwardCompatTestTraitForV5;

/**
* @param string|string[] $originalClassName
*/
Expand Down
58 changes: 0 additions & 58 deletions src/Symfony/Bridge/PhpUnit/Legacy/ForwardCompatTestTraitForV8.php

This file was deleted.

6 changes: 2 additions & 4 deletions src/Symfony/Bridge/PhpUnit/Tests/ClockMockTest.php
Expand Up @@ -22,14 +22,12 @@
*/
class ClockMockTest extends TestCase
{
use ForwardCompatTestTrait;

private static function doSetUpBeforeClass()
public static function setUpBeforeClass()
{
ClockMock::register(__CLASS__);
}

private function doSetUp()
protected function setUp()
{
ClockMock::withClockMock(1234567890.125);
}
Expand Down
48 changes: 46 additions & 2 deletions src/Symfony/Bridge/PhpUnit/bin/simple-phpunit.php
Expand Up @@ -63,6 +63,9 @@
$PHPUNIT_VERSION = '4.8';
}

$PHPUNIT_INJECT_FORWARD_COMPAT = $getEnvVar('SYMFONY_PHPUNIT_INJECT_FORWARD_COMPAT', '1') === '1';
$PHPUNIT_REMOVE_RETURN_TYPEHINT = $getEnvVar('SYMFONY_PHPUNIT_REMOVE_RETURN_TYPEHINT', '0') === '1';

$COMPOSER_JSON = getenv('COMPOSER') ?: 'composer.json';

$root = __DIR__;
Expand Down Expand Up @@ -100,8 +103,9 @@


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

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

@mkdir($PHPUNIT_DIR, 0777, true);
Expand Down Expand Up @@ -139,6 +143,46 @@
if ($exit) {
exit($exit);
}

// Mutate TestsCase code
if ($PHPUNIT_INJECT_FORWARD_COMPAT || $PHPUNIT_REMOVE_RETURN_TYPEHINT) {
$testCaseFile = implode(DIRECTORY_SEPARATOR, ['src', 'Framework', 'TestCase.php']);
$testCaseCode = file_get_contents($testCaseFile);
if ($PHPUNIT_REMOVE_RETURN_TYPEHINT) {
$testCaseCode = preg_replace('/^ ((?:protected|public)(?: static)? function \w+\(\)): void/m', ' $1', $testCaseCode);
}

if ($PHPUNIT_INJECT_FORWARD_COMPAT) {
$injectedTrait = '';
if ($PHPUNIT_VERSION < 7) {
$injectedTrait = '\Symfony\Bridge\PhpUnit\Legacy\ForwardCompatTestTraitForV5';
} elseif ($PHPUNIT_VERSION < 8) {
$injectedTrait = '\Symfony\Bridge\PhpUnit\Legacy\ForwardCompatTestTraitForV7';
}
}

if ($injectedTrait) {
echo "--DEBUG-- injecting $injectedTrait in testCase";
$originalClassName = '';
$testCaseCode = preg_replace_callback('/^abstract class ([^\s]+)/m', function($match) use (&$originalClassName) {
$originalClassName = $match[1];
return 'abstract class SymfonyAlteredTestCase';
}, $testCaseCode, 1);
if (!$originalClassName) {
throw new \RuntimeException('Unexpected class name in PhpUnit TestCase');
}

$testCaseCode .= "
abstract class $originalClassName extends SymfonyAlteredTestCase {
use $injectedTrait;
}
";
}

file_put_contents($testCaseFile, $testCaseCode);
}

file_put_contents('phpunit', <<<'EOPHP'
<?php
Expand All @@ -161,7 +205,7 @@ class SymfonyBlacklistPhpunit {}
EOPHP
);
chdir('..');
file_put_contents(".$PHPUNIT_VERSION.md5", md5_file(__FILE__)."\n".$SYMFONY_PHPUNIT_REMOVE);
file_put_contents(".$PHPUNIT_VERSION.md5", $configurationHash);
chdir($oldPwd);

}
Expand Down

0 comments on commit 588b4eb

Please sign in to comment.