minor #32865 Add polyfill for TestCase::createMock() (nicolas-grekas)
This PR was merged into the 3.4 branch.
Discussion
----------
Add polyfill for TestCase::createMock()
| Q | A
| ------------- | ---
| Branch? | 3.4
| Bug fix? | no
| New feature? | no
| BC breaks? | no
| Deprecations? | no
| Tests pass? | yes
| Fixed tickets | #32844
| License | MIT
| Doc PR | -
Commits
-------
abcd45a587
Add polyfill for TestCase::createMock()
This commit is contained in:
commit
82aace3228
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
*
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
Reference in New Issue