Skip to content

Fix expectUserDeprecationMessage*() failure when test is run in separate process #6283

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

Open
wants to merge 1 commit into
base: 11.5
Choose a base branch
from
Open
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
17 changes: 15 additions & 2 deletions src/Event/Dispatcher/CollectingDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
*/
namespace PHPUnit\Event;

use PHPUnit\Runner\DeprecationCollector\Facade as DeprecationCollector;
use PHPUnit\Runner\DeprecationCollector\TestTriggeredDeprecationSubscriber;

/**
* @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit
*
Expand All @@ -17,15 +20,25 @@
final class CollectingDispatcher implements Dispatcher
{
private EventCollection $events;
private DirectDispatcher $isolatedDirectDispatcher;

public function __construct()
public function __construct(DirectDispatcher $directDispatcher)
{
$this->events = new EventCollection;
$this->isolatedDirectDispatcher = $directDispatcher;
$this->events = new EventCollection;

$this->isolatedDirectDispatcher->registerSubscriber(new TestTriggeredDeprecationSubscriber(DeprecationCollector::collector()));
}

public function dispatch(Event $event): void
{
$this->events->add($event);

try {
$this->isolatedDirectDispatcher->dispatch($event);
} catch (UnknownEventTypeException) {
// Do nothing.
}
}

public function flush(): EventCollection
Expand Down
7 changes: 6 additions & 1 deletion src/Event/Facade.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use PHPUnit\Event\Telemetry\HRTime;
use PHPUnit\Event\Telemetry\Php81GarbageCollectorStatusProvider;
use PHPUnit\Event\Telemetry\Php83GarbageCollectorStatusProvider;
use PHPUnit\Runner\DeprecationCollector\Facade as DeprecationCollector;

/**
* @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit
Expand Down Expand Up @@ -92,7 +93,11 @@ public function registerTracer(Tracer\Tracer $tracer): void
*/
public function initForIsolation(HRTime $offset): CollectingDispatcher
{
$dispatcher = new CollectingDispatcher;
DeprecationCollector::initForIsolation();

$dispatcher = new CollectingDispatcher(
new DirectDispatcher($this->typeMap()),
);

$this->emitter = new DispatchingEmitter(
$dispatcher,
Expand Down
31 changes: 23 additions & 8 deletions src/Runner/DeprecationCollector/Facade.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
*/
final class Facade
{
private static ?Collector $collector = null;
private static null|Collector|InIsolationCollector $collector = null;
private static bool $inIsolation = false;

/**
* @throws EventFacadeIsSealedException
Expand All @@ -33,6 +34,12 @@ public static function init(): void
self::collector();
}

public static function initForIsolation(): void
{
self::$inIsolation = true;
self::collector();
}

/**
* @throws EventFacadeIsSealedException
* @throws UnknownSubscriberTypeException
Expand All @@ -59,15 +66,23 @@ public static function filteredDeprecations(): array
* @throws EventFacadeIsSealedException
* @throws UnknownSubscriberTypeException
*/
private static function collector(): Collector
public static function collector(): Collector|InIsolationCollector
{
if (self::$collector === null) {
self::$collector = new Collector(
EventFacade::instance(),
new IssueFilter(
ConfigurationRegistry::get()->source(),
),
);
if (self::$inIsolation) {
self::$collector = new InIsolationCollector(
new IssueFilter(
ConfigurationRegistry::get()->source(),
),
);
} else {
self::$collector = new Collector(
EventFacade::instance(),
new IssueFilter(
ConfigurationRegistry::get()->source(),
),
);
}
}

return self::$collector;
Expand Down
65 changes: 65 additions & 0 deletions src/Runner/DeprecationCollector/InIsolationCollector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php declare(strict_types=1);
/*
* This file is part of PHPUnit.
*
* (c) Sebastian Bergmann <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PHPUnit\Runner\DeprecationCollector;

use PHPUnit\Event\Test\DeprecationTriggered;
use PHPUnit\TestRunner\IssueFilter;

/**
* @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit
*
* @internal This class is not covered by the backward compatibility promise for PHPUnit
*/
final class InIsolationCollector
{
private readonly IssueFilter $issueFilter;

/**
* @var list<non-empty-string>
*/
private array $deprecations = [];

/**
* @var list<non-empty-string>
*/
private array $filteredDeprecations = [];

public function __construct(IssueFilter $issueFilter)
{
$this->issueFilter = $issueFilter;
}

/**
* @return list<non-empty-string>
*/
public function deprecations(): array
{
return $this->deprecations;
}

/**
* @return list<non-empty-string>
*/
public function filteredDeprecations(): array
{
return $this->filteredDeprecations;
}

public function testTriggeredDeprecation(DeprecationTriggered $event): void
{
$this->deprecations[] = $event->message();

if (!$this->issueFilter->shouldBeProcessed($event)) {
return;
}

$this->filteredDeprecations[] = $event->message();
}
}
6 changes: 3 additions & 3 deletions src/Runner/DeprecationCollector/Subscriber/Subscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@
*/
abstract class Subscriber
{
private readonly Collector $collector;
private readonly Collector|InIsolationCollector $collector;

public function __construct(Collector $collector)
public function __construct(Collector|InIsolationCollector $collector)
{
$this->collector = $collector;
}

protected function collector(): Collector
protected function collector(): Collector|InIsolationCollector
{
return $this->collector;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php declare(strict_types=1);
/*
* This file is part of PHPUnit.
*
* (c) Sebastian Bergmann <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PHPUnit\TestFixture\Event;

use const E_USER_DEPRECATED;
use function trigger_error;
use PHPUnit\Framework\Attributes\IgnoreDeprecations;
use PHPUnit\Framework\Attributes\RunTestsInSeparateProcesses;
use PHPUnit\Framework\TestCase;

#[RunTestsInSeparateProcesses]
final class TestForDeprecatedFeatureInIsolationTest extends TestCase
{
#[IgnoreDeprecations]
public function testExpectationOnExactDeprecationMessageWorksWhenExpectedDeprecationIsTriggered(): void
{
$this->expectUserDeprecationMessage('message');

@trigger_error('message', E_USER_DEPRECATED);
}

#[IgnoreDeprecations]
public function testExpectationsOnExactDeprecationMessagesWorkWhenExpectedDeprecationsAreTriggered(): void
{
$this->expectUserDeprecationMessage('message');
$this->expectUserDeprecationMessage('another message');

@trigger_error('message', E_USER_DEPRECATED);
@trigger_error('another message', E_USER_DEPRECATED);
}

#[IgnoreDeprecations]
public function testExpectationOnExactDeprecationMessageWorksWhenExpectedDeprecationIsNotTriggered(): void
{
$this->expectUserDeprecationMessage('message');
}

#[IgnoreDeprecations]
public function testExpectationOnExactDeprecationMessageWorksWhenUnexpectedDeprecationIsTriggered(): void
{
$this->expectUserDeprecationMessage('message');

@trigger_error('another message', E_USER_DEPRECATED);
}

#[IgnoreDeprecations]
public function testExpectationOnDeprecationMessageMatchingRegularExpressionWorksWhenExpectedDeprecationIsTriggered(): void
{
$this->expectUserDeprecationMessageMatches('/message/');

@trigger_error('...message...', E_USER_DEPRECATED);
}

#[IgnoreDeprecations]
public function testExpectationsOnDeprecationMessagesMatchingRegularExpressionsWorkWhenExpectedDeprecationsAreTriggered(): void
{
$this->expectUserDeprecationMessageMatches('/foo/');
$this->expectUserDeprecationMessageMatches('/bar/');

@trigger_error('...foo...', E_USER_DEPRECATED);
@trigger_error('...bar...', E_USER_DEPRECATED);
}

#[IgnoreDeprecations]
public function testExpectationOnDeprecationMessageMatchingRegularExpressionWorksWhenExpectedDeprecationIsNotTriggered(): void
{
$this->expectUserDeprecationMessageMatches('/message/');
}

#[IgnoreDeprecations]
public function testExpectationOnDeprecationMessageMatchingRegularExpressionWorksWhenUnepectedDeprecationIsTriggered(): void
{
$this->expectUserDeprecationMessageMatches('/message/');

@trigger_error('something else', E_USER_DEPRECATED);
}
}
38 changes: 38 additions & 0 deletions tests/end-to-end/regression/6102-2.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
--TEST--
Copy link
Contributor

Choose a reason for hiding this comment

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

could we have a better test name then 6102-1 and 6102-2. something which reflects the difference of the 2 tests

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Would 6102-cli-argument and 6102-class-attribute be more relevant?

Copy link
Contributor

Choose a reason for hiding this comment

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

I would go with something like
6102-process-isolated-arg
6102-separate-processes-attribute

Choose a reason for hiding this comment

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

How about 6102-process-isolation-via-attribute.phpt and 6102-process-isolation-via-cli-option.phpt?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

afk at the moment, will look into it when possible if noone beats me at it

https://github.com/sebastianbergmann/phpunit/issues/6102
--XFAIL--
https://github.com/sebastianbergmann/phpunit/issues/6102
--FILE--
<?php declare(strict_types=1);
$_SERVER['argv'][] = '--do-not-cache-result';
$_SERVER['argv'][] = '--no-configuration';
$_SERVER['argv'][] = __DIR__ . '/../../end-to-end/generic/_files/TestForDeprecatedFeatureInIsolationTest.php';

require __DIR__ . '/../../bootstrap.php';

(new PHPUnit\TextUI\Application)->run($_SERVER['argv']);
--EXPECTF--
PHPUnit %s by Sebastian Bergmann and contributors.

Runtime: %s

..FF..FF 8 / 8 (100%)

Time: %s, Memory: %s

There were 4 failures:

1) PHPUnit\TestFixture\Event\TestForDeprecatedFeatureInIsolationTest::testExpectationOnExactDeprecationMessageWorksWhenExpectedDeprecationIsNotTriggered
Expected deprecation with message "message" was not triggered

2) PHPUnit\TestFixture\Event\TestForDeprecatedFeatureInIsolationTest::testExpectationOnExactDeprecationMessageWorksWhenUnexpectedDeprecationIsTriggered
Expected deprecation with message "message" was not triggered

3) PHPUnit\TestFixture\Event\TestForDeprecatedFeatureInIsolationTest::testExpectationOnDeprecationMessageMatchingRegularExpressionWorksWhenExpectedDeprecationIsNotTriggered
Expected deprecation with message matching regular expression "/message/" was not triggered

4) PHPUnit\TestFixture\Event\TestForDeprecatedFeatureInIsolationTest::testExpectationOnDeprecationMessageMatchingRegularExpressionWorksWhenUnepectedDeprecationIsTriggered
Expected deprecation with message matching regular expression "/message/" was not triggered

FAILURES!
Tests: 8, Assertions: 10, Failures: 4.
10 changes: 8 additions & 2 deletions tests/unit/Event/Dispatcher/CollectingDispatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,20 @@ final class CollectingDispatcherTest extends TestCase
{
public function testHasNoCollectedEventsWhenFlushedImmediatelyAfterCreation(): void
{
$dispatcher = new CollectingDispatcher;
$typeMap = new TypeMap;
$typeMap->addMapping(Test\DeprecationTriggeredSubscriber::class, Test\DeprecationTriggered::class);

$dispatcher = new CollectingDispatcher(new DirectDispatcher($typeMap));

$this->assertEmpty($dispatcher->flush());
}

public function testCollectsDispatchedEventsUntilFlushed(): void
{
$dispatcher = new CollectingDispatcher;
$typeMap = new TypeMap;
$typeMap->addMapping(Test\DeprecationTriggeredSubscriber::class, Test\DeprecationTriggered::class);

$dispatcher = new CollectingDispatcher(new DirectDispatcher($typeMap));
$event = $this->createStub(Event::class);

$dispatcher->dispatch($event);
Expand Down