-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[PhpUnitBridge] support ClockMock
and DnsMock
with PHPUnit 10+
#58467
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bridge\PhpUnit\Extension; | ||
|
||
use PHPUnit\Event\Code\TestMethod; | ||
use PHPUnit\Event\Test\Finished; | ||
use PHPUnit\Event\Test\FinishedSubscriber; | ||
use PHPUnit\Metadata\Group; | ||
use Symfony\Bridge\PhpUnit\ClockMock; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
class DisableClockMockSubscriber implements FinishedSubscriber | ||
Check failure on line 23 in src/Symfony/Bridge/PhpUnit/Extension/DisableClockMockSubscriber.php
|
||
{ | ||
public function notify(Finished $event): void | ||
{ | ||
$test = $event->test(); | ||
|
||
if (!$test instanceof TestMethod) { | ||
return; | ||
} | ||
|
||
foreach ($test->metadata() as $metadata) { | ||
if ($metadata instanceof Group && 'time-sensitive' === $metadata->groupName()) { | ||
ClockMock::withClockMock(false); | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bridge\PhpUnit\Extension; | ||
|
||
use PHPUnit\Event\Code\TestMethod; | ||
use PHPUnit\Event\Test\Finished; | ||
use PHPUnit\Event\Test\FinishedSubscriber; | ||
use PHPUnit\Metadata\Group; | ||
use Symfony\Bridge\PhpUnit\DnsMock; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
class DisableDnsMockSubscriber implements FinishedSubscriber | ||
Check failure on line 23 in src/Symfony/Bridge/PhpUnit/Extension/DisableDnsMockSubscriber.php
|
||
{ | ||
public function notify(Finished $event): void | ||
{ | ||
$test = $event->test(); | ||
|
||
if (!$test instanceof TestMethod) { | ||
return; | ||
} | ||
|
||
foreach ($test->metadata() as $metadata) { | ||
if ($metadata instanceof Group && 'dns-sensitive' === $metadata->groupName()) { | ||
DnsMock::withMockedHosts([]); | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bridge\PhpUnit\Extension; | ||
|
||
use PHPUnit\Event\Code\TestMethod; | ||
use PHPUnit\Event\Test\PreparationStarted; | ||
use PHPUnit\Event\Test\PreparationStartedSubscriber; | ||
use PHPUnit\Metadata\Group; | ||
use Symfony\Bridge\PhpUnit\ClockMock; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
class EnableClockMockSubscriber implements PreparationStartedSubscriber | ||
Check failure on line 23 in src/Symfony/Bridge/PhpUnit/Extension/EnableClockMockSubscriber.php
|
||
{ | ||
public function notify(PreparationStarted $event): void | ||
{ | ||
$test = $event->test(); | ||
|
||
if (!$test instanceof TestMethod) { | ||
return; | ||
} | ||
|
||
foreach ($test->metadata() as $metadata) { | ||
if ($metadata instanceof Group && 'time-sensitive' === $metadata->groupName()) { | ||
ClockMock::withClockMock(true); | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bridge\PhpUnit\Extension; | ||
|
||
use PHPUnit\Event\Code\TestMethod; | ||
use PHPUnit\Event\TestSuite\Loaded; | ||
use PHPUnit\Event\TestSuite\LoadedSubscriber; | ||
use PHPUnit\Metadata\Group; | ||
use Symfony\Bridge\PhpUnit\ClockMock; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
class RegisterClockMockSubscriber implements LoadedSubscriber | ||
Check failure on line 23 in src/Symfony/Bridge/PhpUnit/Extension/RegisterClockMockSubscriber.php
|
||
{ | ||
public function notify(Loaded $event): void | ||
{ | ||
foreach ($event->testSuite()->tests() as $test) { | ||
if (!$test instanceof TestMethod) { | ||
continue; | ||
} | ||
|
||
foreach ($test->metadata() as $metadata) { | ||
if ($metadata instanceof Group && 'time-sensitive' === $metadata->groupName()) { | ||
ClockMock::register($test->className()); | ||
} | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bridge\PhpUnit\Extension; | ||
|
||
use PHPUnit\Event\Code\TestMethod; | ||
use PHPUnit\Event\TestSuite\Loaded; | ||
use PHPUnit\Event\TestSuite\LoadedSubscriber; | ||
use PHPUnit\Metadata\Group; | ||
use Symfony\Bridge\PhpUnit\DnsMock; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
class RegisterDnsMockSubscriber implements LoadedSubscriber | ||
Check failure on line 23 in src/Symfony/Bridge/PhpUnit/Extension/RegisterDnsMockSubscriber.php
|
||
{ | ||
public function notify(Loaded $event): void | ||
{ | ||
foreach ($event->testSuite()->tests() as $test) { | ||
if (!$test instanceof TestMethod) { | ||
continue; | ||
} | ||
|
||
foreach ($test->metadata() as $metadata) { | ||
if ($metadata instanceof Group && 'dns-sensitive' === $metadata->groupName()) { | ||
DnsMock::register($test->className()); | ||
} | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bridge\PhpUnit; | ||
|
||
use PHPUnit\Runner\Extension\Extension; | ||
use PHPUnit\Runner\Extension\Facade; | ||
use PHPUnit\Runner\Extension\ParameterCollection; | ||
use PHPUnit\TextUI\Configuration\Configuration; | ||
use Symfony\Bridge\PhpUnit\Extension\DisableClockMockSubscriber; | ||
use Symfony\Bridge\PhpUnit\Extension\DisableDnsMockSubscriber; | ||
use Symfony\Bridge\PhpUnit\Extension\EnableClockMockSubscriber; | ||
use Symfony\Bridge\PhpUnit\Extension\RegisterClockMockSubscriber; | ||
use Symfony\Bridge\PhpUnit\Extension\RegisterDnsMockSubscriber; | ||
use Symfony\Component\ErrorHandler\DebugClassLoader; | ||
|
||
class SymfonyExtension implements Extension | ||
Check failure on line 25 in src/Symfony/Bridge/PhpUnit/SymfonyExtension.php
|
||
{ | ||
public function bootstrap(Configuration $configuration, Facade $facade, ParameterCollection $parameters): void | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this also needs to enable the DebugClassLoader if available, as done in SymfonyTestsListenerTrait. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I did not enable the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But for projects that don't use a custom bootstrap file, I don't see which custom code can run before the loading of the testsuite. So the autoloading of custom classes will still probably happen after that point. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fair enough, this would still fail if a class (e.g. a fixture) is defined in the same file as a test, but it will probably still cover 99% of the other use cases |
||
{ | ||
if (class_exists(DebugClassLoader::class)) { | ||
DebugClassLoader::enable(); | ||
} | ||
|
||
if ($parameters->has('clock-mock-namespaces')) { | ||
foreach (explode(',', $parameters->get('clock-mock-namespaces')) as $namespace) { | ||
ClockMock::register($namespace.'\DummyClass'); | ||
} | ||
} | ||
|
||
$facade->registerSubscriber(new RegisterClockMockSubscriber()); | ||
$facade->registerSubscriber(new EnableClockMockSubscriber()); | ||
$facade->registerSubscriber(new DisableClockMockSubscriber()); | ||
|
||
if ($parameters->has('dns-mock-namespaces')) { | ||
foreach (explode(',', $parameters->get('dns-mock-namespaces')) as $namespace) { | ||
DnsMock::register($namespace.'\DummyClass'); | ||
} | ||
} | ||
|
||
$facade->registerSubscriber(new RegisterDnsMockSubscriber()); | ||
$facade->registerSubscriber(new DisableDnsMockSubscriber()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/11.3/phpunit.xsd" | ||
backupGlobals="false" | ||
colors="true" | ||
bootstrap="tests/bootstrap.php" | ||
failOnRisky="true" | ||
failOnWarning="true" | ||
cacheDirectory=".phpunit.cache" | ||
> | ||
<testsuites> | ||
<testsuite name="Fixtures/symfonyextension Test Suite"> | ||
<directory>tests</directory> | ||
</testsuite> | ||
</testsuites> | ||
|
||
<source ignoreSuppressionOfDeprecations="true"> | ||
<include> | ||
<directory>src</directory> | ||
</include> | ||
</source> | ||
|
||
<extensions> | ||
<bootstrap class="Symfony\Bridge\PhpUnit\SymfonyExtension"> | ||
<parameter name="clock-mock-namespaces" value="App" /> | ||
<parameter name="dns-mock-namespaces" value="App" /> | ||
</bootstrap> | ||
</extensions> | ||
</phpunit> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SymfonyTestsListenerTrait also registers the mock on starting the test in addition to enabling it. Is is safe to drop that part ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this already happens in the
RegisterClockMockSubscriber
which is run on the test suite level, doing it here as well would just redo what already happened before