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 f1801a4 commit b7d9d95
Show file tree
Hide file tree
Showing 9 changed files with 239 additions and 264 deletions.
24 changes: 3 additions & 21 deletions src/Symfony/Bridge/PhpUnit/ForwardCompatTestTrait.php
Expand Up @@ -11,25 +11,7 @@

namespace Symfony\Bridge\PhpUnit;

use PHPUnit\Framework\TestCase;

// A trait to provide forward compatibility with newest PHPUnit versions

$r = new \ReflectionClass(TestCase::class);

if (\PHP_VERSION_ID < 70000 || !$r->hasMethod('createMock') || !$r->getMethod('createMock')->hasReturnType()) {
trait ForwardCompatTestTrait
{
use Legacy\ForwardCompatTestTraitForV5;
}
} elseif ($r->getMethod('tearDown')->hasReturnType()) {
trait ForwardCompatTestTrait
{
use Legacy\ForwardCompatTestTraitForV8;
}
} else {
trait ForwardCompatTestTrait
{
use Legacy\ForwardCompatTestTraitForV7;
}
trait ForwardCompatTestTrait
{
use SetUpTearDownTrait;
}
50 changes: 0 additions & 50 deletions src/Symfony/Bridge/PhpUnit/Legacy/ForwardCompatTestTraitForV7.php

This file was deleted.

Expand Up @@ -15,106 +15,12 @@
use PHPUnit\Framework\Constraint\LogicalNot;
use PHPUnit\Framework\Constraint\StringContains;
use PHPUnit\Framework\Constraint\TraversableContains;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

/**
* @internal
*/
trait ForwardCompatTestTraitForV5
trait PolyfillAssertTrait
{
/**
* @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();
}

private static function doSetUpBeforeClass()
{
parent::setUpBeforeClass();
}

private static function doTearDownAfterClass()
{
parent::tearDownAfterClass();
}

private function doSetUp()
{
parent::setUp();
}

private function doTearDown()
{
parent::tearDown();
}

/**
* @param string|string[] $originalClassName
*
* @return MockObject
*/
protected function createMock($originalClassName)
{
$mock = $this->getMockBuilder($originalClassName)
->disableOriginalConstructor()
->disableOriginalClone()
->disableArgumentCloning();

if (method_exists($mock, 'disallowMockingUnknownTypes')) {
$mock = $mock->disallowMockingUnknownTypes();
}

return $mock->getMock();
}

/**
* @param string|string[] $originalClassName
* @param string[] $methods
*
* @return MockObject
*/
protected function createPartialMock($originalClassName, array $methods)
{
$mock = $this->getMockBuilder($originalClassName)
->disableOriginalConstructor()
->disableOriginalClone()
->disableArgumentCloning()
->setMethods(empty($methods) ? null : $methods);

if (method_exists($mock, 'disallowMockingUnknownTypes')) {
$mock = $mock->disallowMockingUnknownTypes();
}

return $mock->getMock();
}

/**
* @param float $delta
* @param string $message
Expand Down Expand Up @@ -320,12 +226,6 @@ public static function assertStringNotContainsStringIgnoringCase($needle, $hayst
*/
public static function assertFinite($actual, $message = '')
{
if (method_exists(TestCase::class, 'assertFinite')) {
parent::assertFinite($actual, $message);

return;
}

static::assertInternalType('float', $actual, $message);
static::assertTrue(is_finite($actual), $message ? $message : "Failed asserting that $actual is finite.");
}
Expand All @@ -337,12 +237,6 @@ public static function assertFinite($actual, $message = '')
*/
public static function assertInfinite($actual, $message = '')
{
if (method_exists(TestCase::class, 'assertInfinite')) {
parent::assertInfinite($actual, $message);

return;
}

static::assertInternalType('float', $actual, $message);
static::assertTrue(is_infinite($actual), $message ? $message : "Failed asserting that $actual is infinite.");
}
Expand All @@ -354,85 +248,7 @@ public static function assertInfinite($actual, $message = '')
*/
public static function assertNan($actual, $message = '')
{
if (method_exists(TestCase::class, 'assertNan')) {
parent::assertNan($actual, $message);

return;
}

static::assertInternalType('float', $actual, $message);
static::assertTrue(is_nan($actual), $message ? $message : "Failed asserting that $actual is nan.");
}

/**
* @param string $exception
*
* @return void
*/
public function expectException($exception)
{
if (method_exists(TestCase::class, 'expectException')) {
parent::expectException($exception);

return;
}

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

/**
* @param int|string $code
*
* @return void
*/
public function expectExceptionCode($code)
{
if (method_exists(TestCase::class, 'expectExceptionCode')) {
parent::expectExceptionCode($code);

return;
}

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

/**
* @param string $message
*
* @return void
*/
public function expectExceptionMessage($message)
{
if (method_exists(TestCase::class, 'expectExceptionMessage')) {
parent::expectExceptionMessage($message);

return;
}

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

/**
* @param string $messageRegExp
*
* @return void
*/
public function expectExceptionMessageRegExp($messageRegExp)
{
if (method_exists(TestCase::class, 'expectExceptionMessageRegExp')) {
parent::expectExceptionMessageRegExp($messageRegExp);

return;
}

$property = new \ReflectionProperty(class_exists('PHPUnit_Framework_TestCase') ? 'PHPUnit_Framework_TestCase' : TestCase::class, 'expectedExceptionMessageRegExp');
$property->setAccessible(true);
$property->setValue($this, $messageRegExp);
}
}

0 comments on commit b7d9d95

Please sign in to comment.