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 8173daf commit ff8253e
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 138 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ env:
global:
- MIN_PHP=5.5.9
- SYMFONY_PROCESS_PHP_TEST_BINARY=~/.phpenv/versions/5.6/bin/php
- PHPUNIT_INJECT_FORWARD_COMPAT=1

matrix:
include:
- php: 5.5
env: php_extra="5.6 7.0 7.1"
env: php_extra="5.6 7.0 7.1" PHPUNIT_REMOVE_RETURN_TYPEHINT=1
- php: 7.2
env: deps=high
- php: 7.3
Expand Down
7 changes: 1 addition & 6 deletions src/Symfony/Bridge/PhpUnit/ForwardCompatTestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,7 @@ trait ForwardCompatTestTrait
{
use Legacy\ForwardCompatTestTraitForV5;
}
} elseif ($r->getMethod('tearDown')->hasReturnType()) {
trait ForwardCompatTestTrait
{
use Legacy\ForwardCompatTestTraitForV8;
}
} else {
} elseif (!$r->getMethod('tearDown')->hasReturnType()) {
trait ForwardCompatTestTrait
{
use Legacy\ForwardCompatTestTraitForV7;
Expand Down
64 changes: 0 additions & 64 deletions src/Symfony/Bridge/PhpUnit/Legacy/ForwardCompatTestTraitForV5.php
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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.

49 changes: 46 additions & 3 deletions src/Symfony/Bridge/PhpUnit/bin/simple-phpunit
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#!/usr/bin/env php
<?php

/*
Expand Down Expand Up @@ -26,6 +25,9 @@ if (PHP_VERSION_ID >= 70200) {
$PHPUNIT_VERSION = '4.8';
}

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

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

$root = __DIR__;
Expand Down Expand Up @@ -65,7 +67,9 @@ if (false === $SYMFONY_PHPUNIT_REMOVE = getenv('SYMFONY_PHPUNIT_REMOVE')) {
$SYMFONY_PHPUNIT_REMOVE = 'phpspec/prophecy symfony/yaml';
}

if (!file_exists("$PHPUNIT_DIR/phpunit-$PHPUNIT_VERSION/phpunit") || md5_file(__FILE__)."\n".$SYMFONY_PHPUNIT_REMOVE !== @file_get_contents("$PHPUNIT_DIR/.$PHPUNIT_VERSION.md5")) {
$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") || $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 @@ -103,6 +107,45 @@ if (!file_exists("$PHPUNIT_DIR/phpunit-$PHPUNIT_VERSION/phpunit") || md5_file(__
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 <= 5) {
$injectedTrait = '\Symfony\Bridge\PhpUnit\Legacy\ForwardCompatTestTraitForV5';
} elseif ($PHPUNIT_VERSION <= 7) {
$injectedTrait = '\Symfony\Bridge\PhpUnit\Legacy\ForwardCompatTestTraitForV7';
}
}

if ($injectedTrait) {
$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 @@ -125,7 +168,7 @@ Symfony\Bridge\PhpUnit\TextUI\Command::main();
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
5 changes: 1 addition & 4 deletions src/Symfony/Bridge/Twig/Tests/AppVariableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,19 @@
namespace Symfony\Bridge\Twig\Tests;

use PHPUnit\Framework\TestCase;
use Symfony\Bridge\PhpUnit\ForwardCompatTestTrait;
use Symfony\Bridge\Twig\AppVariable;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Session\Flash\FlashBag;
use Symfony\Component\HttpFoundation\Session\Session;

class AppVariableTest extends TestCase
{
use ForwardCompatTestTrait;

/**
* @var AppVariable
*/
protected $appVariable;

private function doSetUp()
protected function setUp()
{
$this->appVariable = new AppVariable();
}
Expand Down

0 comments on commit ff8253e

Please sign in to comment.