Skip to content

Commit

Permalink
minor #32882 [PhpUnitBridge] Inject ForwardCompatibiliy in TestCase (…
Browse files Browse the repository at this point in the history
…jderusse)

This PR was merged into the 4.4 branch.

Discussion
----------

[PhpUnitBridge] Inject ForwardCompatibiliy in TestCase

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | not yet
| Fixed tickets | #32844
| License       | MIT
| Doc PR        | NA

This PR replace the previous trait `ForwardCompatTestTrait` by injecting forward compatibility code in phpunit itself which allow to use the polyfill methods in tests without changing the code.

when using `simple_phpunit` the env variable  `$PHPUNIT_REMOVE_RETURN_TYPEHINT=1` (default 0) removes the `: void` typehint in public and protected methods (allow to use 7.4 8 in 3.4 branch)

note: once merged, all tests have to be fixed. see #32889

Commits
-------

016bd8d Inject ForwardCompatibiliy in TestCase
  • Loading branch information
nicolas-grekas committed Aug 3, 2019
2 parents 9614f48 + 016bd8d commit 9279f04
Show file tree
Hide file tree
Showing 8 changed files with 222 additions and 265 deletions.
50 changes: 0 additions & 50 deletions src/Symfony/Bridge/PhpUnit/Legacy/ForwardCompatTestTraitForV7.php

This file was deleted.

Original file line number Diff line number Diff line change
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);
}
}
109 changes: 109 additions & 0 deletions src/Symfony/Bridge/PhpUnit/Legacy/PolyfillTestCaseTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bridge\PhpUnit\Legacy;

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

/**
* @internal
*/
trait PolyfillTestCaseTrait
{
/**
* @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 string $exception
*
* @return void
*/
public function expectException($exception)
{
$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)
{
$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)
{
$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)
{
$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 9279f04

Please sign in to comment.