Skip to content

Commit db61bae

Browse files
committed
support ClockMock and DnsMock with PHPUnit 10+
1 parent fddd33e commit db61bae

7 files changed

+218
-0
lines changed

src/Symfony/Bridge/PhpUnit/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 to register the clock mock and DNS mock
78
* Add `ExpectUserDeprecationMessageTrait` with a polyfill of PHPUnit's `expectUserDeprecationMessage()`
89

910
6.4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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\Test\Finished;
15+
use PHPUnit\Event\Test\FinishedSubscriber;
16+
use PHPUnit\Metadata\Group;
17+
use Symfony\Bridge\PhpUnit\ClockMock;
18+
19+
/**
20+
* @internal
21+
*/
22+
class DisableClockMockSubscriber implements FinishedSubscriber
23+
{
24+
public function notify(Finished $event): void
25+
{
26+
$test = $event->test();
27+
28+
foreach ($test->metadata() as $metadata) {
29+
if ($metadata instanceof Group && 'time-sensitive' === $metadata->groupName()) {
30+
ClockMock::withClockMock(false);
31+
}
32+
}
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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\Test\Finished;
15+
use PHPUnit\Event\Test\FinishedSubscriber;
16+
use PHPUnit\Metadata\Group;
17+
use Symfony\Bridge\PhpUnit\DnsMock;
18+
19+
/**
20+
* @internal
21+
*/
22+
class DisableDnsMockSubscriber implements FinishedSubscriber
23+
{
24+
public function notify(Finished $event): void
25+
{
26+
$test = $event->test();
27+
28+
foreach ($test->metadata() as $metadata) {
29+
if ($metadata instanceof Group && 'dns-sensitive' === $metadata->groupName()) {
30+
DnsMock::withMockedHosts([]);
31+
}
32+
}
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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\Test\PreparationStarted;
15+
use PHPUnit\Event\Test\PreparationStartedSubscriber;
16+
use PHPUnit\Metadata\Group;
17+
use Symfony\Bridge\PhpUnit\ClockMock;
18+
19+
/**
20+
* @internal
21+
*/
22+
class EnableClockMockSubscriber implements PreparationStartedSubscriber
23+
{
24+
public function notify(PreparationStarted $event): void
25+
{
26+
$test = $event->test();
27+
28+
foreach ($test->metadata() as $metadata) {
29+
if ($metadata instanceof Group && 'time-sensitive' === $metadata->groupName()) {
30+
ClockMock::withClockMock(true);
31+
}
32+
}
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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\TestSuite\Loaded;
15+
use PHPUnit\Event\TestSuite\LoadedSubscriber;
16+
use PHPUnit\Metadata\Group;
17+
use Symfony\Bridge\PhpUnit\ClockMock;
18+
19+
/**
20+
* @internal
21+
*/
22+
class RegisterClockMockSubscriber implements LoadedSubscriber
23+
{
24+
public function notify(Loaded $event): void
25+
{
26+
foreach ($event->testSuite()->tests() as $test) {
27+
foreach ($test->metadata() as $metadata) {
28+
if ($metadata instanceof Group && 'time-sensitive' === $metadata->groupName()) {
29+
ClockMock::register($test->className());
30+
}
31+
}
32+
}
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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\TestSuite\Loaded;
15+
use PHPUnit\Event\TestSuite\LoadedSubscriber;
16+
use PHPUnit\Metadata\Group;
17+
use Symfony\Bridge\PhpUnit\DnsMock;
18+
19+
/**
20+
* @internal
21+
*/
22+
class RegisterDnsMockSubscriber implements LoadedSubscriber
23+
{
24+
public function notify(Loaded $event): void
25+
{
26+
foreach ($event->testSuite()->tests() as $test) {
27+
foreach ($test->metadata() as $metadata) {
28+
if ($metadata instanceof Group && 'dns-sensitive' === $metadata->groupName()) {
29+
DnsMock::register($test->className());
30+
}
31+
}
32+
}
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
24+
class SymfonyExtension implements Extension
25+
{
26+
public function bootstrap(Configuration $configuration, Facade $facade, ParameterCollection $parameters): void
27+
{
28+
if ($parameters->has('clock-mock-namespaces')) {
29+
foreach (explode(',', $parameters->get('clock-mock-namespaces')) as $namespace) {
30+
ClockMock::register($namespace.'\DummyClass');
31+
}
32+
}
33+
34+
$facade->registerSubscriber(new RegisterClockMockSubscriber());
35+
$facade->registerSubscriber(new EnableClockMockSubscriber());
36+
$facade->registerSubscriber(new DisableClockMockSubscriber());
37+
38+
if ($parameters->has('dns-mock-namespaces')) {
39+
foreach (explode(',', $parameters->get('dns-mock-namespaces')) as $namespace) {
40+
DnsMock::register($namespace.'\DummyClass');
41+
}
42+
}
43+
44+
$facade->registerSubscriber(new RegisterDnsMockSubscriber());
45+
$facade->registerSubscriber(new DisableDnsMockSubscriber());
46+
}
47+
}

0 commit comments

Comments
 (0)