Skip to content

[PhpUnitBridge] Enable configuring mock namespaces with attributes #59384

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

Merged
merged 1 commit into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/phpunit-bridge.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@ jobs:
php-version: "7.2"

- name: Lint
run: find ./src/Symfony/Bridge/PhpUnit -name '*.php' | grep -v -e /Tests/ -e ForV7 -e ForV8 -e ForV9 -e ConstraintLogicTrait | parallel -j 4 php -l {}
run: find ./src/Symfony/Bridge/PhpUnit -name '*.php' | grep -v -e /Tests/ -e /Attribute/ -e /Extension/ -e /Metadata/ -e ForV7 -e ForV8 -e ForV9 -e ConstraintLogicTrait | parallel -j 4 php -l {}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PhpUnit 10+ is PHP >=8.1, so it should be ok for the extension classes to use PHP >=8 syntax.

21 changes: 21 additions & 0 deletions src/Symfony/Bridge/PhpUnit/Attribute/DnsSensitive.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?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\Attribute;

#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
final class DnsSensitive
{
public function __construct(
public readonly ?string $class = null,
) {
}
}
21 changes: 21 additions & 0 deletions src/Symfony/Bridge/PhpUnit/Attribute/TimeSensitive.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?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\Attribute;

#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
final class TimeSensitive
{
public function __construct(
public readonly ?string $class = null,
) {
}
}
5 changes: 5 additions & 0 deletions src/Symfony/Bridge/PhpUnit/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

7.3
---

* Enable configuring clock and DNS mock namespaces with attributes

7.2
---

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,20 @@
use PHPUnit\Event\Test\Finished;
use PHPUnit\Event\Test\FinishedSubscriber;
use PHPUnit\Metadata\Group;
use Symfony\Bridge\PhpUnit\Attribute\TimeSensitive;
use Symfony\Bridge\PhpUnit\ClockMock;
use Symfony\Bridge\PhpUnit\Metadata\AttributeReader;

/**
* @internal
*/
class DisableClockMockSubscriber implements FinishedSubscriber
{
public function __construct(
private AttributeReader $reader,
) {
}

public function notify(Finished $event): void
{
$test = $event->test();
Expand All @@ -33,7 +40,12 @@ public function notify(Finished $event): void
foreach ($test->metadata() as $metadata) {
if ($metadata instanceof Group && 'time-sensitive' === $metadata->groupName()) {
ClockMock::withClockMock(false);
break;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor optimization

}
}

if ($this->reader->forClassAndMethod($test->className(), $test->methodName(), TimeSensitive::class)) {
ClockMock::withClockMock(false);
}
}
}
12 changes: 12 additions & 0 deletions src/Symfony/Bridge/PhpUnit/Extension/DisableDnsMockSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,20 @@
use PHPUnit\Event\Test\Finished;
use PHPUnit\Event\Test\FinishedSubscriber;
use PHPUnit\Metadata\Group;
use Symfony\Bridge\PhpUnit\Attribute\DnsSensitive;
use Symfony\Bridge\PhpUnit\DnsMock;
use Symfony\Bridge\PhpUnit\Metadata\AttributeReader;

/**
* @internal
*/
class DisableDnsMockSubscriber implements FinishedSubscriber
{
public function __construct(
private AttributeReader $reader,
) {
}

public function notify(Finished $event): void
{
$test = $event->test();
Expand All @@ -33,7 +40,12 @@ public function notify(Finished $event): void
foreach ($test->metadata() as $metadata) {
if ($metadata instanceof Group && 'dns-sensitive' === $metadata->groupName()) {
DnsMock::withMockedHosts([]);
break;
}
}

if ($this->reader->forClassAndMethod($test->className(), $test->methodName(), DnsSensitive::class)) {
DnsMock::withMockedHosts([]);
}
}
}
12 changes: 12 additions & 0 deletions src/Symfony/Bridge/PhpUnit/Extension/EnableClockMockSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,20 @@
use PHPUnit\Event\Test\PreparationStarted;
use PHPUnit\Event\Test\PreparationStartedSubscriber;
use PHPUnit\Metadata\Group;
use Symfony\Bridge\PhpUnit\Attribute\TimeSensitive;
use Symfony\Bridge\PhpUnit\ClockMock;
use Symfony\Bridge\PhpUnit\Metadata\AttributeReader;

/**
* @internal
*/
class EnableClockMockSubscriber implements PreparationStartedSubscriber
{
public function __construct(
private AttributeReader $reader,
) {
}

public function notify(PreparationStarted $event): void
{
$test = $event->test();
Expand All @@ -33,7 +40,12 @@ public function notify(PreparationStarted $event): void
foreach ($test->metadata() as $metadata) {
if ($metadata instanceof Group && 'time-sensitive' === $metadata->groupName()) {
ClockMock::withClockMock(true);
break;
}
}

if ($this->reader->forClassAndMethod($test->className(), $test->methodName(), TimeSensitive::class)) {
ClockMock::withClockMock(true);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,20 @@
use PHPUnit\Event\TestSuite\Loaded;
use PHPUnit\Event\TestSuite\LoadedSubscriber;
use PHPUnit\Metadata\Group;
use Symfony\Bridge\PhpUnit\Attribute\TimeSensitive;
use Symfony\Bridge\PhpUnit\ClockMock;
use Symfony\Bridge\PhpUnit\Metadata\AttributeReader;

/**
* @internal
*/
class RegisterClockMockSubscriber implements LoadedSubscriber
{
public function __construct(
private AttributeReader $reader,
) {
}

public function notify(Loaded $event): void
{
foreach ($event->testSuite()->tests() as $test) {
Expand All @@ -34,6 +41,10 @@ public function notify(Loaded $event): void
ClockMock::register($test->className());
}
}

foreach ($this->reader->forClassAndMethod($test->className(), $test->methodName(), TimeSensitive::class) as $attribute) {
ClockMock::register($attribute->class ?? $test->className());
}
}
}
}
11 changes: 11 additions & 0 deletions src/Symfony/Bridge/PhpUnit/Extension/RegisterDnsMockSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,20 @@
use PHPUnit\Event\TestSuite\Loaded;
use PHPUnit\Event\TestSuite\LoadedSubscriber;
use PHPUnit\Metadata\Group;
use Symfony\Bridge\PhpUnit\Attribute\DnsSensitive;
use Symfony\Bridge\PhpUnit\DnsMock;
use Symfony\Bridge\PhpUnit\Metadata\AttributeReader;

/**
* @internal
*/
class RegisterDnsMockSubscriber implements LoadedSubscriber
{
public function __construct(
private AttributeReader $reader,
) {
}

public function notify(Loaded $event): void
{
foreach ($event->testSuite()->tests() as $test) {
Expand All @@ -34,6 +41,10 @@ public function notify(Loaded $event): void
DnsMock::register($test->className());
}
}

foreach ($this->reader->forClassAndMethod($test->className(), $test->methodName(), DnsSensitive::class) as $attribute) {
DnsMock::register($attribute->class ?? $test->className());
}
}
}
}
78 changes: 78 additions & 0 deletions src/Symfony/Bridge/PhpUnit/Metadata/AttributeReader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?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\Metadata;

/**
* @template T of object
*/
final class AttributeReader
{
/**
* @var array<string, array<class-string<T>, list<T>>>
*/
private array $cache = [];

/**
* @param class-string $className
* @param class-string<T> $name
*
* @return list<T>
*/
public function forClass(string $className, string $name): array
{
$attributes = $this->cache[$className] ??= $this->readAttributes(new \ReflectionClass($className));

return $attributes[$name] ?? [];
}

/**
* @param class-string $className
* @param class-string<T> $name
*
* @return list<T>
*/
public function forMethod(string $className, string $methodName, string $name): array
{
$attributes = $this->cache[$className.'::'.$methodName] ??= $this->readAttributes(new \ReflectionMethod($className, $methodName));

return $attributes[$name] ?? [];
}

/**
* @param class-string $className
* @param class-string<T> $name
*
* @return list<T>
*/
public function forClassAndMethod(string $className, string $methodName, string $name): array
{
return [
...$this->forClass($className, $name),
...$this->forMethod($className, $methodName, $name),
];
}

private function readAttributes(\ReflectionClass|\ReflectionMethod $reflection): array
{
$attributeInstances = [];

foreach ($reflection->getAttributes() as $attribute) {
if (!str_starts_with($name = $attribute->getName(), 'Symfony\\Bridge\\PhpUnit\\Attribute\\')) {
continue;
}

$attributeInstances[$name][] = $attribute->newInstance();
}

return $attributeInstances;
}
}
13 changes: 8 additions & 5 deletions src/Symfony/Bridge/PhpUnit/SymfonyExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Symfony\Bridge\PhpUnit\Extension\EnableClockMockSubscriber;
use Symfony\Bridge\PhpUnit\Extension\RegisterClockMockSubscriber;
use Symfony\Bridge\PhpUnit\Extension\RegisterDnsMockSubscriber;
use Symfony\Bridge\PhpUnit\Metadata\AttributeReader;
use Symfony\Component\ErrorHandler\DebugClassLoader;

class SymfonyExtension implements Extension
Expand All @@ -30,23 +31,25 @@ public function bootstrap(Configuration $configuration, Facade $facade, Paramete
DebugClassLoader::enable();
}

$reader = new AttributeReader();

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());
$facade->registerSubscriber(new RegisterClockMockSubscriber($reader));
$facade->registerSubscriber(new EnableClockMockSubscriber($reader));
$facade->registerSubscriber(new DisableClockMockSubscriber($reader));

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());
$facade->registerSubscriber(new RegisterDnsMockSubscriber($reader));
$facade->registerSubscriber(new DisableDnsMockSubscriber($reader));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,14 @@
});

require __DIR__.'/../../../../SymfonyExtension.php';
require __DIR__.'/../../../../Attribute/DnsSensitive.php';
require __DIR__.'/../../../../Attribute/TimeSensitive.php';
require __DIR__.'/../../../../Extension/DisableClockMockSubscriber.php';
require __DIR__.'/../../../../Extension/DisableDnsMockSubscriber.php';
require __DIR__.'/../../../../Extension/EnableClockMockSubscriber.php';
require __DIR__.'/../../../../Extension/RegisterClockMockSubscriber.php';
require __DIR__.'/../../../../Extension/RegisterDnsMockSubscriber.php';
require __DIR__.'/../../../../Metadata/AttributeReader.php';

if (file_exists(__DIR__.'/../../../../vendor/autoload.php')) {
require __DIR__.'/../../../../vendor/autoload.php';
Expand Down
Loading
Loading
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