Add polyfill for TestCase::createMock()

This commit is contained in:
Nicolas Grekas 2019-08-01 14:45:14 +02:00
parent c06454827d
commit abcd45a587
5 changed files with 71 additions and 12 deletions

View File

@ -15,7 +15,14 @@ use PHPUnit\Framework\TestCase;
// A trait to provide forward compatibility with newest PHPUnit versions
if (method_exists(\ReflectionMethod::class, 'hasReturnType') && (new \ReflectionMethod(TestCase::class, 'tearDown'))->hasReturnType()) {
$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;
@ -23,6 +30,6 @@ if (method_exists(\ReflectionMethod::class, 'hasReturnType') && (new \Reflection
} else {
trait ForwardCompatTestTrait
{
use Legacy\ForwardCompatTestTraitForV5;
use Legacy\ForwardCompatTestTraitForV7;
}
}

View File

@ -11,6 +11,8 @@
namespace Symfony\Bridge\PhpUnit\Legacy;
use PHPUnit\Framework\MockObject\MockObject;
/**
* @internal
*/
@ -80,6 +82,25 @@ trait ForwardCompatTestTraitForV5
parent::tearDown();
}
/**
* @param 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 $message
*

View File

@ -0,0 +1,35 @@
<?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;
/**
* @internal
*/
trait ForwardCompatTestTraitForV7
{
use ForwardCompatTestTraitForV5;
/**
* @param string|string[] $originalClassName
*/
protected function createMock($originalClassName): MockObject
{
return $this->getMockBuilder($originalClassName)
->disableOriginalConstructor()
->disableOriginalClone()
->disableArgumentCloning()
->disallowMockingUnknownTypes()
->getMock();
}
}

View File

@ -12,6 +12,7 @@
namespace Symfony\Component\Validator\Tests\DataCollector;
use PHPUnit\Framework\TestCase;
use Symfony\Bridge\PhpUnit\ForwardCompatTestTrait;
use Symfony\Component\Validator\ConstraintViolation;
use Symfony\Component\Validator\ConstraintViolationList;
use Symfony\Component\Validator\DataCollector\ValidatorDataCollector;
@ -20,6 +21,8 @@ use Symfony\Component\Validator\Validator\ValidatorInterface;
class ValidatorDataCollectorTest extends TestCase
{
use ForwardCompatTestTrait;
public function testCollectsValidatorCalls()
{
$originalValidator = $this->createMock(ValidatorInterface::class);
@ -71,9 +74,4 @@ class ValidatorDataCollectorTest extends TestCase
$this->assertCount(0, $collector->getCalls());
$this->assertSame(0, $collector->getViolationsCount());
}
protected function createMock($classname)
{
return $this->getMockBuilder($classname)->disableOriginalConstructor()->getMock();
}
}

View File

@ -12,6 +12,7 @@
namespace Symfony\Component\Validator\Tests\Validator;
use PHPUnit\Framework\TestCase;
use Symfony\Bridge\PhpUnit\ForwardCompatTestTrait;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintViolation;
use Symfony\Component\Validator\ConstraintViolationList;
@ -23,6 +24,8 @@ use Symfony\Component\Validator\Validator\ValidatorInterface;
class TraceableValidatorTest extends TestCase
{
use ForwardCompatTestTrait;
public function testValidate()
{
$originalValidator = $this->createMock(ValidatorInterface::class);
@ -95,9 +98,4 @@ class TraceableValidatorTest extends TestCase
$expects('validatePropertyValue')->willReturn($expected = new ConstraintViolationList());
$this->assertSame($expected, $validator->validatePropertyValue(new \stdClass(), 'property', 'value'), 'returns original validator validatePropertyValue() result');
}
protected function createMock($classname)
{
return $this->getMockBuilder($classname)->disableOriginalConstructor()->getMock();
}
}