Skip to content

Commit

Permalink
support ClockMock and DnsMock with PHPUnit 10+
Browse files Browse the repository at this point in the history
  • Loading branch information
xabbuh committed Oct 23, 2024
1 parent 2faee18 commit 078398e
Show file tree
Hide file tree
Showing 27 changed files with 853 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ CHANGELOG
7.2
---

* Add a PHPUnit extension that registers the clock mock and DNS mock and the `DebugClassLoader` from the ErrorHandler component if present
* Add `ExpectUserDeprecationMessageTrait` with a polyfill of PHPUnit's `expectUserDeprecationMessage()`
* Use `total` for asserting deprecation count when a group is not defined

Expand Down
39 changes: 39 additions & 0 deletions Extension/DisableClockMockSubscriber.php
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);
}
}
}
}
39 changes: 39 additions & 0 deletions Extension/DisableDnsMockSubscriber.php
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([]);
}
}
}
}
39 changes: 39 additions & 0 deletions Extension/EnableClockMockSubscriber.php
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);
}
}
}
}
39 changes: 39 additions & 0 deletions Extension/RegisterClockMockSubscriber.php
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());
}
}
}
}
}
39 changes: 39 additions & 0 deletions Extension/RegisterDnsMockSubscriber.php
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());
}
}
}
}
}
52 changes: 52 additions & 0 deletions SymfonyExtension.php
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());
}
}
3 changes: 3 additions & 0 deletions Tests/CoverageListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@

use PHPUnit\Framework\TestCase;

/**
* @requires PHPUnit < 10
*/
class CoverageListenerTest extends TestCase
{
public function test()
Expand Down
3 changes: 3 additions & 0 deletions Tests/DeprecationErrorHandler/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,9 @@ public function testExistingBaselineAndGeneration()
$this->assertEquals(json_encode($expected, \JSON_PRETTY_PRINT | \JSON_UNESCAPED_SLASHES), file_get_contents($filename));
}

/**
* @requires PHPUnit < 10
*/
public function testBaselineGenerationWithDeprecationTriggeredByDebugClassLoader()
{
$filename = $this->createFile();
Expand Down
2 changes: 2 additions & 0 deletions Tests/DeprecationErrorHandler/log_file.phpt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
--TEST--
Test DeprecationErrorHandler with log file
--SKIPIF--
<?php if (!getenv('SYMFONY_PHPUNIT_VERSION') || version_compare(getenv('SYMFONY_PHPUNIT_VERSION'), '10.0', '>=')) die('Skipping on PHPUnit 10+');
--FILE--
<?php
$filename = tempnam(sys_get_temp_dir(), 'sf-');
Expand Down
3 changes: 3 additions & 0 deletions Tests/ExpectDeprecationTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
use PHPUnit\Framework\TestCase;
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;

/**
* @requires PHPUnit < 10
*/
final class ExpectDeprecationTraitTest extends TestCase
{
use ExpectDeprecationTrait;
Expand Down
3 changes: 3 additions & 0 deletions Tests/ExpectedDeprecationAnnotationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@

use PHPUnit\Framework\TestCase;

/**
* @requires PHPUnit < 10
*/
final class ExpectedDeprecationAnnotationTest extends TestCase
{
/**
Expand Down
2 changes: 2 additions & 0 deletions Tests/FailTests/ExpectDeprecationTraitTestFail.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
*
* This class is deliberately suffixed with *TestFail.php so that it is ignored
* by PHPUnit. This test is designed to fail. See ../expectdeprecationfail.phpt.
*
* @requires PHPUnit < 10
*/
final class ExpectDeprecationTraitTestFail extends TestCase
{
Expand Down
2 changes: 2 additions & 0 deletions Tests/FailTests/NoAssertionsTestNotRisky.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
/**
* This class is deliberately suffixed with *TestRisky.php so that it is ignored
* by PHPUnit. This test is designed to fail. See ../expectnotrisky.phpt.
*
* @requires PHPUnit < 10
*/
final class NoAssertionsTestNotRisky extends TestCase
{
Expand Down
2 changes: 2 additions & 0 deletions Tests/FailTests/NoAssertionsTestRisky.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
/**
* This class is deliberately suffixed with *TestRisky.php so that it is ignored
* by PHPUnit. This test is designed to fail. See ../expectrisky.phpt.
*
* @requires PHPUnit < 10
*/
final class NoAssertionsTestRisky extends TestCase
{
Expand Down
29 changes: 29 additions & 0 deletions Tests/Fixtures/symfonyextension/phpunit-with-extension.xml.dist
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 Tests/Fixtures/symfonyextension/phpunit-without-extension.xml.dist
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>
Loading

0 comments on commit 078398e

Please sign in to comment.