Skip to content

Commit 078398e

Browse files
committed
support ClockMock and DnsMock with PHPUnit 10+
1 parent 2faee18 commit 078398e

27 files changed

+853
-2
lines changed

CHANGELOG.md

+1
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

+39
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 <[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\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+
}
+39
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 <[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\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+
}
+39
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 <[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\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+
}
+39
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 <[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\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+
}
+39
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 <[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\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+
}

SymfonyExtension.php

+52
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 <[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;
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+
}

Tests/CoverageListenerTest.php

+3
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()

Tests/DeprecationErrorHandler/ConfigurationTest.php

+3
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();

Tests/DeprecationErrorHandler/log_file.phpt

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
--TEST--
22
Test DeprecationErrorHandler with log file
3+
--SKIPIF--
4+
<?php if (!getenv('SYMFONY_PHPUNIT_VERSION') || version_compare(getenv('SYMFONY_PHPUNIT_VERSION'), '10.0', '>=')) die('Skipping on PHPUnit 10+');
35
--FILE--
46
<?php
57
$filename = tempnam(sys_get_temp_dir(), 'sf-');

Tests/ExpectDeprecationTraitTest.php

+3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
1616

17+
/**
18+
* @requires PHPUnit < 10
19+
*/
1720
final class ExpectDeprecationTraitTest extends TestCase
1821
{
1922
use ExpectDeprecationTrait;

Tests/ExpectedDeprecationAnnotationTest.php

+3
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
final class ExpectedDeprecationAnnotationTest extends TestCase
1720
{
1821
/**

Tests/FailTests/ExpectDeprecationTraitTestFail.php

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
*
2020
* This class is deliberately suffixed with *TestFail.php so that it is ignored
2121
* by PHPUnit. This test is designed to fail. See ../expectdeprecationfail.phpt.
22+
*
23+
* @requires PHPUnit < 10
2224
*/
2325
final class ExpectDeprecationTraitTestFail extends TestCase
2426
{

Tests/FailTests/NoAssertionsTestNotRisky.php

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
/**
1818
* This class is deliberately suffixed with *TestRisky.php so that it is ignored
1919
* by PHPUnit. This test is designed to fail. See ../expectnotrisky.phpt.
20+
*
21+
* @requires PHPUnit < 10
2022
*/
2123
final class NoAssertionsTestNotRisky extends TestCase
2224
{

Tests/FailTests/NoAssertionsTestRisky.php

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
/**
1818
* This class is deliberately suffixed with *TestRisky.php so that it is ignored
1919
* by PHPUnit. This test is designed to fail. See ../expectrisky.phpt.
20+
*
21+
* @requires PHPUnit < 10
2022
*/
2123
final class NoAssertionsTestRisky extends TestCase
2224
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/11.3/phpunit.xsd"
4+
backupGlobals="false"
5+
colors="true"
6+
bootstrap="tests/bootstrap.php"
7+
failOnRisky="true"
8+
failOnWarning="true"
9+
cacheDirectory=".phpunit.cache"
10+
>
11+
<testsuites>
12+
<testsuite name="Fixtures/symfonyextension Test Suite">
13+
<directory>tests</directory>
14+
</testsuite>
15+
</testsuites>
16+
17+
<source ignoreSuppressionOfDeprecations="true">
18+
<include>
19+
<directory>src</directory>
20+
</include>
21+
</source>
22+
23+
<extensions>
24+
<bootstrap class="Symfony\Bridge\PhpUnit\SymfonyExtension">
25+
<parameter name="clock-mock-namespaces" value="App" />
26+
<parameter name="dns-mock-namespaces" value="App" />
27+
</bootstrap>
28+
</extensions>
29+
</phpunit>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/11.3/phpunit.xsd"
4+
backupGlobals="false"
5+
colors="true"
6+
bootstrap="tests/bootstrap.php"
7+
failOnRisky="true"
8+
failOnWarning="true"
9+
cacheDirectory=".phpunit.cache"
10+
>
11+
<testsuites>
12+
<testsuite name="Fixtures/symfonyextension Test Suite">
13+
<directory>tests</directory>
14+
</testsuite>
15+
</testsuites>
16+
17+
<source ignoreSuppressionOfDeprecations="true">
18+
<include>
19+
<directory>src</directory>
20+
</include>
21+
</source>
22+
</phpunit>

0 commit comments

Comments
 (0)