-
-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support ClockMock and DnsMock with PHPUnit 10+
- Loading branch information
Showing
27 changed files
with
853 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
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); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
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([]); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
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); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
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()); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
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()); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
public function bootstrap(Configuration $configuration, Facade $facade, ParameterCollection $parameters): void | ||
{ | ||
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
Tests/Fixtures/symfonyextension/phpunit-with-extension.xml.dist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
22 changes: 22 additions & 0 deletions
22
Tests/Fixtures/symfonyextension/phpunit-without-extension.xml.dist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?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> | ||
</phpunit> |
Oops, something went wrong.