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 3, 2019
1 parent 215ba8a commit ab61e52
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 170 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;
}
}
116 changes: 16 additions & 100 deletions src/Symfony/Bridge/PhpUnit/Legacy/ForwardCompatTestTraitForV5.php
Expand Up @@ -12,7 +12,6 @@
namespace Symfony\Bridge\PhpUnit\Legacy;

use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

/**
* @internal
Expand All @@ -22,70 +21,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,28 +157,31 @@ 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;
}

parent::setExpectedException($exception, $this->forwardCompatExpectedExceptionMessage, $this->forwardCompatExpectedExceptionCode);
$property = new \ReflectionProperty(\class_exists('PHPUnit_Framework_TestCase') ? 'PHPUnit_Framework_TestCase' : 'TestCase', 'expectedException');
$property->setAccessible(true);
$property->setValue($this, $exception);
}

/**
* @return void
*/
public function expectExceptionCode($code)
{
if (method_exists(TestCase::class, 'expectExceptionCode')) {
if (\is_callable('parent::expectExceptionCode')) {
parent::expectExceptionCode($code);

return;
}

$this->forwardCompatExpectedExceptionCode = $code;
parent::setExpectedException(parent::getExpectedException(), $this->forwardCompatExpectedExceptionMessage, $this->forwardCompatExpectedExceptionCode);
$property = new \ReflectionProperty(\class_exists('PHPUnit_Framework_TestCase') ? 'PHPUnit_Framework_TestCase' : 'TestCase', 'expectedExceptionCode');
$property->setAccessible(true);
$property->setValue($this, $code);
}

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

return;
}

$this->forwardCompatExpectedExceptionMessage = $message;
parent::setExpectedException(parent::getExpectedException(), $this->forwardCompatExpectedExceptionMessage, $this->forwardCompatExpectedExceptionCode);
$property = new \ReflectionProperty(\class_exists('PHPUnit_Framework_TestCase') ? 'PHPUnit_Framework_TestCase' : 'TestCase', 'expectedExceptionMessage');
$property->setAccessible(true);
$property->setValue($this, $message);
}

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

return;
}

parent::setExpectedExceptionRegExp(parent::getExpectedException(), $messageRegExp, $this->forwardCompatExpectedExceptionCode);
}

/**
* @param string $exceptionMessage
*
* @return void
*/
public function setExpectedException($exceptionName, $exceptionMessage = '', $exceptionCode = null)
{
$this->forwardCompatExpectedExceptionMessage = $exceptionMessage;
$this->forwardCompatExpectedExceptionCode = $exceptionCode;

parent::setExpectedException($exceptionName, $exceptionMessage, $exceptionCode);
}

/**
* @param string $exceptionMessageRegExp
*
* @return void
*/
public function setExpectedExceptionRegExp($exceptionName, $exceptionMessageRegExp = '', $exceptionCode = null)
{
$this->forwardCompatExpectedExceptionCode = $exceptionCode;

parent::setExpectedExceptionRegExp($exceptionName, $exceptionMessageRegExp, $exceptionCode);
$property = new \ReflectionProperty(\class_exists('PHPUnit_Framework_TestCase') ? 'PHPUnit_Framework_TestCase' : 'TestCase', 'expectedExceptionMessageRegExp');
$property->setAccessible(true);
$property->setValue($this, $messageRegExp);
}
}
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.

7 changes: 2 additions & 5 deletions src/Symfony/Bridge/PhpUnit/Tests/ClockMockTest.php
Expand Up @@ -13,7 +13,6 @@

use PHPUnit\Framework\TestCase;
use Symfony\Bridge\PhpUnit\ClockMock;
use Symfony\Bridge\PhpUnit\ForwardCompatTestTrait;

/**
* @author Dominic Tubach <dominic.tubach@to.com>
Expand All @@ -22,14 +21,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
46 changes: 44 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,44 @@
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 = 'ForwardCompatTestTraitForV5';
} elseif ($PHPUNIT_VERSION < 8) {
$injectedTrait = 'ForwardCompatTestTraitForV7';
}
}

if ($injectedTrait) {
$testCaseCode = preg_replace(
'/abstract class (?:TestCase|PHPUnit_Framework_TestCase)[^\{]+\{/',
'$0 '.PHP_EOL." use \Symfony\Bridge\PhpUnit\Legacy\\$injectedTrait;",
$testCaseCode
);
$traitFile = "vendor/symfony/phpunit-bridge/Legacy/$injectedTrait.php";
file_put_contents(
$traitFile,
str_replace(
' * @internal',
'',
file_get_contents($traitFile)
)
);
}

file_put_contents($testCaseFile, $testCaseCode);
}

file_put_contents('phpunit', <<<'EOPHP'
<?php
Expand All @@ -161,7 +203,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 ab61e52

Please sign in to comment.