Skip to content

Commit 8cb4a2f

Browse files
committed
feature symfony#58467 [PhpUnitBridge] support ClockMock and DnsMock with PHPUnit 10+ (xabbuh)
This PR was merged into the 7.2 branch. Discussion ---------- [PhpUnitBridge] support `ClockMock` and `DnsMock` with PHPUnit 10+ | Q | A | ------------- | --- | Branch? | 7.2 | Bug fix? | no | New feature? | yes | Deprecations? | no | Issues | Fix symfony#49069 | License | MIT Commits ------- aeb8400 support ClockMock and DnsMock with PHPUnit 10+
2 parents eb8b5e7 + aeb8400 commit 8cb4a2f

28 files changed

+861
-2
lines changed

.github/workflows/unit-tests.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,3 +239,11 @@ jobs:
239239
mkdir -p /opt/php/lib
240240
echo memory_limit=-1 > /opt/php/lib/php.ini
241241
./build/php/bin/php ./phpunit --colors=always src/Symfony/Component/Process
242+
243+
- name: Run PhpUnitBridge tests with PHPUnit 11
244+
if: '! matrix.mode'
245+
run: |
246+
./phpunit src/Symfony/Bridge/PhpUnit
247+
env:
248+
SYMFONY_PHPUNIT_VERSION: '11.3'
249+
SYMFONY_DEPRECATIONS_HELPER: 'disabled'

src/Symfony/Bridge/PhpUnit/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ CHANGELOG
44
7.2
55
---
66

7+
* Add a PHPUnit extension that registers the clock mock and DNS mock and the `DebugClassLoader` from the ErrorHandler component if present
78
* Add `ExpectUserDeprecationMessageTrait` with a polyfill of PHPUnit's `expectUserDeprecationMessage()`
89
* Use `total` for asserting deprecation count when a group is not defined
910

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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\Extension;
13+
14+
use PHPUnit\Event\Code\TestMethod;
15+
use PHPUnit\Event\Test\Finished;
16+
use PHPUnit\Event\Test\FinishedSubscriber;
17+
use PHPUnit\Metadata\Group;
18+
use Symfony\Bridge\PhpUnit\ClockMock;
19+
20+
/**
21+
* @internal
22+
*/
23+
class DisableClockMockSubscriber implements FinishedSubscriber
24+
{
25+
public function notify(Finished $event): void
26+
{
27+
$test = $event->test();
28+
29+
if (!$test instanceof TestMethod) {
30+
return;
31+
}
32+
33+
foreach ($test->metadata() as $metadata) {
34+
if ($metadata instanceof Group && 'time-sensitive' === $metadata->groupName()) {
35+
ClockMock::withClockMock(false);
36+
}
37+
}
38+
}
39+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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\Extension;
13+
14+
use PHPUnit\Event\Code\TestMethod;
15+
use PHPUnit\Event\Test\Finished;
16+
use PHPUnit\Event\Test\FinishedSubscriber;
17+
use PHPUnit\Metadata\Group;
18+
use Symfony\Bridge\PhpUnit\DnsMock;
19+
20+
/**
21+
* @internal
22+
*/
23+
class DisableDnsMockSubscriber implements FinishedSubscriber
24+
{
25+
public function notify(Finished $event): void
26+
{
27+
$test = $event->test();
28+
29+
if (!$test instanceof TestMethod) {
30+
return;
31+
}
32+
33+
foreach ($test->metadata() as $metadata) {
34+
if ($metadata instanceof Group && 'dns-sensitive' === $metadata->groupName()) {
35+
DnsMock::withMockedHosts([]);
36+
}
37+
}
38+
}
39+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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\Extension;
13+
14+
use PHPUnit\Event\Code\TestMethod;
15+
use PHPUnit\Event\Test\PreparationStarted;
16+
use PHPUnit\Event\Test\PreparationStartedSubscriber;
17+
use PHPUnit\Metadata\Group;
18+
use Symfony\Bridge\PhpUnit\ClockMock;
19+
20+
/**
21+
* @internal
22+
*/
23+
class EnableClockMockSubscriber implements PreparationStartedSubscriber
24+
{
25+
public function notify(PreparationStarted $event): void
26+
{
27+
$test = $event->test();
28+
29+
if (!$test instanceof TestMethod) {
30+
return;
31+
}
32+
33+
foreach ($test->metadata() as $metadata) {
34+
if ($metadata instanceof Group && 'time-sensitive' === $metadata->groupName()) {
35+
ClockMock::withClockMock(true);
36+
}
37+
}
38+
}
39+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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\Extension;
13+
14+
use PHPUnit\Event\Code\TestMethod;
15+
use PHPUnit\Event\TestSuite\Loaded;
16+
use PHPUnit\Event\TestSuite\LoadedSubscriber;
17+
use PHPUnit\Metadata\Group;
18+
use Symfony\Bridge\PhpUnit\ClockMock;
19+
20+
/**
21+
* @internal
22+
*/
23+
class RegisterClockMockSubscriber implements LoadedSubscriber
24+
{
25+
public function notify(Loaded $event): void
26+
{
27+
foreach ($event->testSuite()->tests() as $test) {
28+
if (!$test instanceof TestMethod) {
29+
continue;
30+
}
31+
32+
foreach ($test->metadata() as $metadata) {
33+
if ($metadata instanceof Group && 'time-sensitive' === $metadata->groupName()) {
34+
ClockMock::register($test->className());
35+
}
36+
}
37+
}
38+
}
39+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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\Extension;
13+
14+
use PHPUnit\Event\Code\TestMethod;
15+
use PHPUnit\Event\TestSuite\Loaded;
16+
use PHPUnit\Event\TestSuite\LoadedSubscriber;
17+
use PHPUnit\Metadata\Group;
18+
use Symfony\Bridge\PhpUnit\DnsMock;
19+
20+
/**
21+
* @internal
22+
*/
23+
class RegisterDnsMockSubscriber implements LoadedSubscriber
24+
{
25+
public function notify(Loaded $event): void
26+
{
27+
foreach ($event->testSuite()->tests() as $test) {
28+
if (!$test instanceof TestMethod) {
29+
continue;
30+
}
31+
32+
foreach ($test->metadata() as $metadata) {
33+
if ($metadata instanceof Group && 'dns-sensitive' === $metadata->groupName()) {
34+
DnsMock::register($test->className());
35+
}
36+
}
37+
}
38+
}
39+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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;
13+
14+
use PHPUnit\Runner\Extension\Extension;
15+
use PHPUnit\Runner\Extension\Facade;
16+
use PHPUnit\Runner\Extension\ParameterCollection;
17+
use PHPUnit\TextUI\Configuration\Configuration;
18+
use Symfony\Bridge\PhpUnit\Extension\DisableClockMockSubscriber;
19+
use Symfony\Bridge\PhpUnit\Extension\DisableDnsMockSubscriber;
20+
use Symfony\Bridge\PhpUnit\Extension\EnableClockMockSubscriber;
21+
use Symfony\Bridge\PhpUnit\Extension\RegisterClockMockSubscriber;
22+
use Symfony\Bridge\PhpUnit\Extension\RegisterDnsMockSubscriber;
23+
use Symfony\Component\ErrorHandler\DebugClassLoader;
24+
25+
class SymfonyExtension implements Extension
26+
{
27+
public function bootstrap(Configuration $configuration, Facade $facade, ParameterCollection $parameters): void
28+
{
29+
if (class_exists(DebugClassLoader::class)) {
30+
DebugClassLoader::enable();
31+
}
32+
33+
if ($parameters->has('clock-mock-namespaces')) {
34+
foreach (explode(',', $parameters->get('clock-mock-namespaces')) as $namespace) {
35+
ClockMock::register($namespace.'\DummyClass');
36+
}
37+
}
38+
39+
$facade->registerSubscriber(new RegisterClockMockSubscriber());
40+
$facade->registerSubscriber(new EnableClockMockSubscriber());
41+
$facade->registerSubscriber(new DisableClockMockSubscriber());
42+
43+
if ($parameters->has('dns-mock-namespaces')) {
44+
foreach (explode(',', $parameters->get('dns-mock-namespaces')) as $namespace) {
45+
DnsMock::register($namespace.'\DummyClass');
46+
}
47+
}
48+
49+
$facade->registerSubscriber(new RegisterDnsMockSubscriber());
50+
$facade->registerSubscriber(new DisableDnsMockSubscriber());
51+
}
52+
}

src/Symfony/Bridge/PhpUnit/Tests/CoverageListenerTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515

16+
/**
17+
* @requires PHPUnit < 10
18+
*/
1619
class CoverageListenerTest extends TestCase
1720
{
1821
public function test()

src/Symfony/Bridge/PhpUnit/Tests/DeprecationErrorHandler/ConfigurationTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,9 @@ public function testExistingBaselineAndGeneration()
463463
$this->assertEquals(json_encode($expected, \JSON_PRETTY_PRINT | \JSON_UNESCAPED_SLASHES), file_get_contents($filename));
464464
}
465465

466+
/**
467+
* @requires PHPUnit < 10
468+
*/
466469
public function testBaselineGenerationWithDeprecationTriggeredByDebugClassLoader()
467470
{
468471
$filename = $this->createFile();

0 commit comments

Comments
 (0)