Skip to content

Commit

Permalink
minor #32865 Add polyfill for TestCase::createMock() (nicolas-grekas)
Browse files Browse the repository at this point in the history
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
-------

abcd45a Add polyfill for TestCase::createMock()
  • Loading branch information
nicolas-grekas committed Aug 1, 2019
2 parents 1fca6c2 + abcd45a commit 82aace3
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 12 deletions.
11 changes: 9 additions & 2 deletions src/Symfony/Bridge/PhpUnit/ForwardCompatTestTrait.php
Expand Up @@ -15,14 +15,21 @@

// 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;
}
} else {
trait ForwardCompatTestTrait
{
use Legacy\ForwardCompatTestTraitForV5;
use Legacy\ForwardCompatTestTraitForV7;
}
}
21 changes: 21 additions & 0 deletions src/Symfony/Bridge/PhpUnit/Legacy/ForwardCompatTestTraitForV5.php
Expand Up @@ -11,6 +11,8 @@

namespace Symfony\Bridge\PhpUnit\Legacy;

use PHPUnit\Framework\MockObject\MockObject;

/**
* @internal
*/
Expand Down Expand Up @@ -80,6 +82,25 @@ private function doTearDown()
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
*
Expand Down
35 changes: 35 additions & 0 deletions src/Symfony/Bridge/PhpUnit/Legacy/ForwardCompatTestTraitForV7.php
@@ -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();
}
}
Expand Up @@ -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;
Expand All @@ -20,6 +21,8 @@

class ValidatorDataCollectorTest extends TestCase
{
use ForwardCompatTestTrait;

public function testCollectsValidatorCalls()
{
$originalValidator = $this->createMock(ValidatorInterface::class);
Expand Down Expand Up @@ -71,9 +74,4 @@ public function testReset()
$this->assertCount(0, $collector->getCalls());
$this->assertSame(0, $collector->getViolationsCount());
}

protected function createMock($classname)
{
return $this->getMockBuilder($classname)->disableOriginalConstructor()->getMock();
}
}
Expand Up @@ -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;
Expand All @@ -23,6 +24,8 @@

class TraceableValidatorTest extends TestCase
{
use ForwardCompatTestTrait;

public function testValidate()
{
$originalValidator = $this->createMock(ValidatorInterface::class);
Expand Down Expand Up @@ -95,9 +98,4 @@ public function testForwardsToOriginalValidator()
$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();
}
}

0 comments on commit 82aace3

Please sign in to comment.