Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 82aace3

Browse files
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 ------- abcd45a Add polyfill for TestCase::createMock()
2 parents 1fca6c2 + abcd45a commit 82aace3

File tree

5 files changed

+71
-12
lines changed

5 files changed

+71
-12
lines changed

src/Symfony/Bridge/PhpUnit/ForwardCompatTestTrait.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,21 @@
1515

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

18-
if (method_exists(\ReflectionMethod::class, 'hasReturnType') && (new \ReflectionMethod(TestCase::class, 'tearDown'))->hasReturnType()) {
18+
$r = new \ReflectionClass(TestCase::class);
19+
20+
if (\PHP_VERSION_ID < 70000 || !$r->hasMethod('createMock') || !$r->getMethod('createMock')->hasReturnType()) {
21+
trait ForwardCompatTestTrait
22+
{
23+
use Legacy\ForwardCompatTestTraitForV5;
24+
}
25+
} elseif ($r->getMethod('tearDown')->hasReturnType()) {
1926
trait ForwardCompatTestTrait
2027
{
2128
use Legacy\ForwardCompatTestTraitForV8;
2229
}
2330
} else {
2431
trait ForwardCompatTestTrait
2532
{
26-
use Legacy\ForwardCompatTestTraitForV5;
33+
use Legacy\ForwardCompatTestTraitForV7;
2734
}
2835
}

src/Symfony/Bridge/PhpUnit/Legacy/ForwardCompatTestTraitForV5.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Symfony\Bridge\PhpUnit\Legacy;
1313

14+
use PHPUnit\Framework\MockObject\MockObject;
15+
1416
/**
1517
* @internal
1618
*/
@@ -80,6 +82,25 @@ private function doTearDown()
8082
parent::tearDown();
8183
}
8284

85+
/**
86+
* @param string $originalClassName
87+
*
88+
* @return MockObject
89+
*/
90+
protected function createMock($originalClassName)
91+
{
92+
$mock = $this->getMockBuilder($originalClassName)
93+
->disableOriginalConstructor()
94+
->disableOriginalClone()
95+
->disableArgumentCloning();
96+
97+
if (method_exists($mock, 'disallowMockingUnknownTypes')) {
98+
$mock = $mock->disallowMockingUnknownTypes();
99+
}
100+
101+
return $mock->getMock();
102+
}
103+
83104
/**
84105
* @param string $message
85106
*
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bridge\PhpUnit\Legacy;
13+
14+
use PHPUnit\Framework\MockObject\MockObject;
15+
16+
/**
17+
* @internal
18+
*/
19+
trait ForwardCompatTestTraitForV7
20+
{
21+
use ForwardCompatTestTraitForV5;
22+
23+
/**
24+
* @param string|string[] $originalClassName
25+
*/
26+
protected function createMock($originalClassName): MockObject
27+
{
28+
return $this->getMockBuilder($originalClassName)
29+
->disableOriginalConstructor()
30+
->disableOriginalClone()
31+
->disableArgumentCloning()
32+
->disallowMockingUnknownTypes()
33+
->getMock();
34+
}
35+
}

src/Symfony/Component/Validator/Tests/DataCollector/ValidatorDataCollectorTest.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Validator\Tests\DataCollector;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Bridge\PhpUnit\ForwardCompatTestTrait;
1516
use Symfony\Component\Validator\ConstraintViolation;
1617
use Symfony\Component\Validator\ConstraintViolationList;
1718
use Symfony\Component\Validator\DataCollector\ValidatorDataCollector;
@@ -20,6 +21,8 @@
2021

2122
class ValidatorDataCollectorTest extends TestCase
2223
{
24+
use ForwardCompatTestTrait;
25+
2326
public function testCollectsValidatorCalls()
2427
{
2528
$originalValidator = $this->createMock(ValidatorInterface::class);
@@ -71,9 +74,4 @@ public function testReset()
7174
$this->assertCount(0, $collector->getCalls());
7275
$this->assertSame(0, $collector->getViolationsCount());
7376
}
74-
75-
protected function createMock($classname)
76-
{
77-
return $this->getMockBuilder($classname)->disableOriginalConstructor()->getMock();
78-
}
7977
}

src/Symfony/Component/Validator/Tests/Validator/TraceableValidatorTest.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Validator\Tests\Validator;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Bridge\PhpUnit\ForwardCompatTestTrait;
1516
use Symfony\Component\Validator\Constraint;
1617
use Symfony\Component\Validator\ConstraintViolation;
1718
use Symfony\Component\Validator\ConstraintViolationList;
@@ -23,6 +24,8 @@
2324

2425
class TraceableValidatorTest extends TestCase
2526
{
27+
use ForwardCompatTestTrait;
28+
2629
public function testValidate()
2730
{
2831
$originalValidator = $this->createMock(ValidatorInterface::class);
@@ -95,9 +98,4 @@ public function testForwardsToOriginalValidator()
9598
$expects('validatePropertyValue')->willReturn($expected = new ConstraintViolationList());
9699
$this->assertSame($expected, $validator->validatePropertyValue(new \stdClass(), 'property', 'value'), 'returns original validator validatePropertyValue() result');
97100
}
98-
99-
protected function createMock($classname)
100-
{
101-
return $this->getMockBuilder($classname)->disableOriginalConstructor()->getMock();
102-
}
103101
}

0 commit comments

Comments
 (0)