Skip to content

Commit 3dd81e9

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

File tree

6 files changed

+214
-0
lines changed

6 files changed

+214
-0
lines changed
Lines changed: 34 additions & 0 deletions
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+
}
Lines changed: 34 additions & 0 deletions
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+
}
Lines changed: 34 additions & 0 deletions
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+
}
Lines changed: 34 additions & 0 deletions
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+
}
Lines changed: 34 additions & 0 deletions
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+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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\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\ClockMock;
19+
use Symfony\Bridge\PhpUnit\DnsMock;
20+
21+
class SymfonyExtension implements Extension
22+
{
23+
public function bootstrap(Configuration $configuration, Facade $facade, ParameterCollection $parameters): void
24+
{
25+
if ($parameters->has('clock-mock-namespaces')) {
26+
foreach (explode(',', $parameters->get('clock-mock-namespaces')) as $namespace) {
27+
ClockMock::register($namespace.'\DummyClass');
28+
}
29+
}
30+
31+
$facade->registerSubscriber(new RegisterClockMockSubscriber());
32+
$facade->registerSubscriber(new EnableClockMockSubscriber());
33+
$facade->registerSubscriber(new DisableClockMockSubscriber());
34+
35+
if ($parameters->has('dns-mock-namespaces')) {
36+
foreach (explode(',', $parameters->get('dns-mock-namespaces')) as $namespace) {
37+
DnsMock::register($namespace.'\DummyClass');
38+
}
39+
}
40+
41+
$facade->registerSubscriber(new RegisterDnsMockSubscriber());
42+
$facade->registerSubscriber(new DisableDnsMockSubscriber());
43+
}
44+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy