Skip to content

Commit 0e3868b

Browse files
Implement ugly workaround to not emit Test\AssertionSucceeded and Test\AssertionFailed events when they have no subscribers
1 parent f6dc8a5 commit 0e3868b

32 files changed

+175
-36
lines changed

ChangeLog-10.0.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
All notable changes of the PHPUnit 10.0 release series are documented in this file using the [Keep a CHANGELOG](https://keepachangelog.com/) principles.
44

5+
## [10.0.14] - 2023-MM-DD
6+
7+
### Changed
8+
9+
* The `PHPUnit\Event\Test\AssertionSucceeded` and `PHPUnit\Event\Test\AssertionFailed` events are no longer emitted when they have no subscribers
10+
* The `PHPUnit\Event\Test\AssertionSucceeded::value()` method is no longer deprecated and return the exported value again
11+
512
## [10.0.13] - 2023-02-27
613

714
### Fixed
@@ -50,7 +57,7 @@ All notable changes of the PHPUnit 10.0 release series are documented in this fi
5057

5158
### Changed
5259

53-
* The `PHPUnit\Event\Test\AssertionSucceeded::value()` method is now deprecated and always returns `''`
60+
* The `PHPUnit\Event\Test\AssertionSucceeded::value()` method is now deprecated and always returns `''` (reverted in PHPUnit 10.0.14)
5461

5562
### Fixed
5663

@@ -228,6 +235,7 @@ All notable changes of the PHPUnit 10.0 release series are documented in this fi
228235
* PHP 7.3, PHP 7.4, and PHP 8.0 are no longer supported
229236
* `phpunit/php-code-coverage` [no longer supports PHPDBG and Xdebug 2](https://github.com/sebastianbergmann/php-code-coverage/blob/10.0.0/ChangeLog.md#1000---2023-02-03)
230237

238+
[10.0.14]: https://github.com/sebastianbergmann/phpunit/compare/10.0.13...10.0
231239
[10.0.13]: https://github.com/sebastianbergmann/phpunit/compare/10.0.12...10.0.13
232240
[10.0.12]: https://github.com/sebastianbergmann/phpunit/compare/10.0.11...10.0.12
233241
[10.0.11]: https://github.com/sebastianbergmann/phpunit/compare/10.0.10...10.0.11

src/Event/Dispatcher/DeferringDispatcher.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,17 @@ public function registerSubscriber(Subscriber $subscriber): void
3434
$this->dispatcher->registerSubscriber($subscriber);
3535
}
3636

37+
/**
38+
* @todo Remove this method once we found a better way to avoid creating event objects
39+
* that are expensive to create when there are no subscribers registered for them
40+
*
41+
* @see https://github.com/sebastianbergmann/phpunit/issues/5261
42+
*/
43+
public function seal(): void
44+
{
45+
$this->dispatcher->seal();
46+
}
47+
3748
public function dispatch(Event $event): void
3849
{
3950
if ($this->recording) {

src/Event/Dispatcher/DirectDispatcher.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@
1010
namespace PHPUnit\Event;
1111

1212
use function array_key_exists;
13+
use function assert;
1314
use function dirname;
1415
use function sprintf;
1516
use function str_starts_with;
17+
use PHPUnit\Event\Test\AssertionFailedSubscriber;
18+
use PHPUnit\Event\Test\AssertionSucceededSubscriber;
1619
use Throwable;
1720

1821
/**
@@ -37,6 +40,34 @@ public function __construct(TypeMap $map)
3740
$this->typeMap = $map;
3841
}
3942

43+
/**
44+
* @todo Remove this method once we found a better way to avoid creating event objects
45+
* that are expensive to create when there are no subscribers registered for them
46+
*
47+
* @see https://github.com/sebastianbergmann/phpunit/issues/5261
48+
*/
49+
public function seal(): void
50+
{
51+
$emitter = Facade::emitter();
52+
53+
assert($emitter instanceof DispatchingEmitter);
54+
55+
if (!empty($this->tracers)) {
56+
$emitter->emitAssertionSucceededEvents();
57+
$emitter->emitAssertionFailedEvents();
58+
59+
return;
60+
}
61+
62+
if (isset($this->subscribers[AssertionSucceededSubscriber::class])) {
63+
$emitter->emitAssertionSucceededEvents();
64+
}
65+
66+
if (isset($this->subscribers[AssertionFailedSubscriber::class])) {
67+
$emitter->emitAssertionFailedEvents();
68+
}
69+
}
70+
4071
public function registerTracer(Tracer\Tracer $tracer): void
4172
{
4273
$this->tracers[] = $tracer;

src/Event/Dispatcher/SubscribableDispatcher.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,12 @@ interface SubscribableDispatcher extends Dispatcher
2020
public function registerSubscriber(Subscriber $subscriber): void;
2121

2222
public function registerTracer(Tracer\Tracer $tracer): void;
23+
24+
/**
25+
* @todo Remove this method once we found a better way to avoid creating event objects
26+
* that are expensive to create when there are no subscribers registered for them
27+
*
28+
* @see https://github.com/sebastianbergmann/phpunit/issues/5261
29+
*/
30+
public function seal(): void;
2331
}

src/Event/Emitter/DispatchingEmitter.php

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ final class DispatchingEmitter implements Emitter
3131
private readonly Telemetry\System $system;
3232
private readonly Telemetry\Snapshot $startSnapshot;
3333
private Telemetry\Snapshot $previousSnapshot;
34+
private bool $emitAssertionSucceededEvents = false;
35+
private bool $emitAssertionFailedEvents = false;
3436

3537
public function __construct(Dispatcher $dispatcher, Telemetry\System $system)
3638
{
@@ -41,6 +43,50 @@ public function __construct(Dispatcher $dispatcher, Telemetry\System $system)
4143
$this->previousSnapshot = $system->snapshot();
4244
}
4345

46+
/**
47+
* @todo Remove this method once we found a better way to avoid creating event objects
48+
* that are expensive to create when there are no subscribers registered for them
49+
*
50+
* @see https://github.com/sebastianbergmann/phpunit/issues/5261
51+
*/
52+
public function emitAssertionSucceededEvents(): void
53+
{
54+
$this->emitAssertionSucceededEvents = true;
55+
}
56+
57+
/**
58+
* @todo Remove this method once we found a better way to avoid creating event objects
59+
* that are expensive to create when there are no subscribers registered for them
60+
*
61+
* @see https://github.com/sebastianbergmann/phpunit/issues/5261
62+
*/
63+
public function emitsAssertionSucceededEvents(): bool
64+
{
65+
return $this->emitAssertionSucceededEvents;
66+
}
67+
68+
/**
69+
* @todo Remove this method once we found a better way to avoid creating event objects
70+
* that are expensive to create when there are no subscribers registered for them
71+
*
72+
* @see https://github.com/sebastianbergmann/phpunit/issues/5261
73+
*/
74+
public function emitAssertionFailedEvents(): void
75+
{
76+
$this->emitAssertionFailedEvents = true;
77+
}
78+
79+
/**
80+
* @todo Remove this method once we found a better way to avoid creating event objects
81+
* that are expensive to create when there are no subscribers registered for them
82+
*
83+
* @see https://github.com/sebastianbergmann/phpunit/issues/5261
84+
*/
85+
public function emitsAssertionFailedEvents(): bool
86+
{
87+
return $this->emitAssertionFailedEvents;
88+
}
89+
4490
/**
4591
* @throws InvalidArgumentException
4692
* @throws UnknownEventTypeException
@@ -400,10 +446,14 @@ public function testRegisteredComparator(string $className): void
400446
*/
401447
public function testAssertionSucceeded(mixed $value, Constraint\Constraint $constraint, string $message): void
402448
{
449+
if (!$this->emitAssertionSucceededEvents) {
450+
return;
451+
}
452+
403453
$this->dispatcher->dispatch(
404454
new Test\AssertionSucceeded(
405455
$this->telemetryInfo(),
406-
'',
456+
(new Exporter)->export($value),
407457
$constraint->toString(),
408458
$constraint->count(),
409459
$message,
@@ -417,6 +467,10 @@ public function testAssertionSucceeded(mixed $value, Constraint\Constraint $cons
417467
*/
418468
public function testAssertionFailed(mixed $value, Constraint\Constraint $constraint, string $message): void
419469
{
470+
if (!$this->emitAssertionFailedEvents) {
471+
return;
472+
}
473+
420474
$this->dispatcher->dispatch(
421475
new Test\AssertionFailed(
422476
$this->telemetryInfo(),

src/Event/Events/Test/Assertion/AssertionSucceeded.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,15 @@
1919
final class AssertionSucceeded implements Event
2020
{
2121
private readonly Telemetry\Info $telemetryInfo;
22+
private readonly string $value;
2223
private readonly string $constraint;
2324
private readonly int $count;
2425
private readonly string $message;
2526

2627
public function __construct(Telemetry\Info $telemetryInfo, string $value, string $constraint, int $count, string $message)
2728
{
2829
$this->telemetryInfo = $telemetryInfo;
30+
$this->value = $value;
2931
$this->constraint = $constraint;
3032
$this->count = $count;
3133
$this->message = $message;
@@ -36,12 +38,9 @@ public function telemetryInfo(): Telemetry\Info
3638
return $this->telemetryInfo;
3739
}
3840

39-
/**
40-
* @deprecated https://github.com/sebastianbergmann/phpunit/issues/5183
41-
*/
4241
public function value(): string
4342
{
44-
return '';
43+
return $this->value;
4544
}
4645

4746
public function count(): int
@@ -66,8 +65,9 @@ public function asString(): string
6665
}
6766

6867
return sprintf(
69-
'Assertion Succeeded (Constraint: %s%s)',
68+
'Assertion Succeeded (Constraint: %s, Value: %s%s)',
7069
$this->constraint,
70+
$this->value,
7171
$message
7272
);
7373
}

src/Event/Facade.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public static function emitter(): Emitter
6868
}
6969

7070
/** @noinspection PhpUnused */
71-
public static function initForIsolation(HRTime $offset): CollectingDispatcher
71+
public static function initForIsolation(HRTime $offset, bool $emitAssertionSucceededEvents, bool $emitAssertionFailedEvents): CollectingDispatcher
7272
{
7373
$dispatcher = new CollectingDispatcher;
7474

@@ -80,6 +80,14 @@ public static function initForIsolation(HRTime $offset): CollectingDispatcher
8080
)
8181
);
8282

83+
if ($emitAssertionSucceededEvents) {
84+
self::$emitter->emitAssertionSucceededEvents();
85+
}
86+
87+
if ($emitAssertionFailedEvents) {
88+
self::$emitter->emitAssertionFailedEvents();
89+
}
90+
8391
self::$sealed = true;
8492

8593
return $dispatcher;
@@ -100,6 +108,7 @@ public static function forward(EventCollection $events): void
100108

101109
public static function seal(): void
102110
{
111+
self::deferredDispatcher()->seal();
103112
self::deferredDispatcher()->flush();
104113

105114
self::$sealed = true;

src/Framework/TestRunner.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,13 @@ public function runInSeparateProcess(TestCase $test, bool $runEntireClass, bool
272272
$includedFiles = '';
273273
$iniSettings = '';
274274

275+
$emitter = Event\Facade::emitter();
276+
277+
assert($emitter instanceof Event\DispatchingEmitter);
278+
279+
$emitAssertionSucceededEvents = $emitter->emitsAssertionSucceededEvents() ? 'true' : 'false';
280+
$emitAssertionFailedEvents = $emitter->emitsAssertionFailedEvents() ? 'true' : 'false';
281+
275282
if (ConfigurationRegistry::get()->hasBootstrap()) {
276283
$bootstrap = ConfigurationRegistry::get()->bootstrap();
277284
}
@@ -331,6 +338,8 @@ public function runInSeparateProcess(TestCase $test, bool $runEntireClass, bool
331338
'offsetSeconds' => $offset[0],
332339
'offsetNanoseconds' => $offset[1],
333340
'serializedConfiguration' => $serializedConfiguration,
341+
'emitAssertionSucceededEvents' => $emitAssertionSucceededEvents,
342+
'emitAssertionFailedEvents' => $emitAssertionFailedEvents,
334343
];
335344

336345
if (!$runEntireClass) {

src/Util/PHP/Template/TestCaseClass.tpl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ function __phpunit_run_isolated_test()
3737
PHPUnit\Event\Telemetry\HRTime::fromSecondsAndNanoseconds(
3838
{offsetSeconds},
3939
{offsetNanoseconds}
40-
)
40+
),
41+
{emitAssertionSucceededEvents},
42+
{emitAssertionFailedEvents}
4143
);
4244

4345
require_once '{filename}';

src/Util/PHP/Template/TestCaseMethod.tpl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ function __phpunit_run_isolated_test()
3737
PHPUnit\Event\Telemetry\HRTime::fromSecondsAndNanoseconds(
3838
{offsetSeconds},
3939
{offsetNanoseconds}
40-
)
40+
),
41+
{emitAssertionSucceededEvents},
42+
{emitAssertionFailedEvents}
4143
);
4244

4345
require_once '{filename}';

tests/end-to-end/event/invalid-coverage-metadata.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ Test Suite Started (default, 1 test)
4040
Test Suite Started (PHPUnit\TestFixture\Event\InvalidCoverageMetadata\InvalidCoverageMetadataTest, 1 test)
4141
Test Preparation Started (PHPUnit\TestFixture\Event\InvalidCoverageMetadata\InvalidCoverageMetadataTest::testOne)
4242
Test Prepared (PHPUnit\TestFixture\Event\InvalidCoverageMetadata\InvalidCoverageMetadataTest::testOne)
43-
Assertion Succeeded (Constraint: is true)
43+
Assertion Succeeded (Constraint: is true, Value: true)
4444
Test Passed (PHPUnit\TestFixture\Event\InvalidCoverageMetadata\InvalidCoverageMetadataTest::testOne)
4545
Test Triggered PHPUnit Warning (PHPUnit\TestFixture\Event\InvalidCoverageMetadata\InvalidCoverageMetadataTest::testOne)
4646
Class "PHPUnit\TestFixture\Event\InvalidCoverageMetadata\This\Does\Not\Exist" is not a valid target for code coverage

tests/end-to-end/event/phar-extension.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ Test Suite Started (default, 1 test)
3838
Test Suite Started (PHPUnit\TestFixture\Event\MyExtension\Test, 1 test)
3939
Test Preparation Started (PHPUnit\TestFixture\Event\MyExtension\Test::testOne)
4040
Test Prepared (PHPUnit\TestFixture\Event\MyExtension\Test::testOne)
41-
Assertion Succeeded (Constraint: is true)
41+
Assertion Succeeded (Constraint: is true, Value: true)
4242
Test Passed (PHPUnit\TestFixture\Event\MyExtension\Test::testOne)
4343
Test Finished (PHPUnit\TestFixture\Event\MyExtension\Test::testOne)
4444
Test Suite Finished (PHPUnit\TestFixture\Event\MyExtension\Test, 1 test)

tests/end-to-end/event/php-deprecated.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ Test Preparation Started (PHPUnit\TestFixture\Event\DeprecatedPhpFeatureTest::te
4242
Test Prepared (PHPUnit\TestFixture\Event\DeprecatedPhpFeatureTest::testDeprecatedPhpFeature)
4343
Test Triggered PHP Deprecation (PHPUnit\TestFixture\Event\DeprecatedPhpFeatureTest::testDeprecatedPhpFeature)
4444
defined(): Passing null to parameter #1 ($constant_name) of type string is deprecated
45-
Assertion Succeeded (Constraint: is true)
45+
Assertion Succeeded (Constraint: is true, Value: true)
4646
Test Passed (PHPUnit\TestFixture\Event\DeprecatedPhpFeatureTest::testDeprecatedPhpFeature)
4747
Test Finished (PHPUnit\TestFixture\Event\DeprecatedPhpFeatureTest::testDeprecatedPhpFeature)
4848
Test Suite Finished (PHPUnit\TestFixture\Event\DeprecatedPhpFeatureTest, 1 test)

tests/end-to-end/event/php-notice.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ Test Preparation Started (PHPUnit\TestFixture\Event\PhpNoticeTest::testPhpNotice
3636
Test Prepared (PHPUnit\TestFixture\Event\PhpNoticeTest::testPhpNotice)
3737
Test Triggered PHP Notice (PHPUnit\TestFixture\Event\PhpNoticeTest::testPhpNotice)
3838
Only variables should be assigned by reference
39-
Assertion Succeeded (Constraint: is true)
39+
Assertion Succeeded (Constraint: is true, Value: true)
4040
Test Passed (PHPUnit\TestFixture\Event\PhpNoticeTest::testPhpNotice)
4141
Test Finished (PHPUnit\TestFixture\Event\PhpNoticeTest::testPhpNotice)
4242
Test Suite Finished (PHPUnit\TestFixture\Event\PhpNoticeTest, 1 test)

tests/end-to-end/event/php-warning.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ Test Preparation Started (PHPUnit\TestFixture\Event\PhpWarningTest::testPhpWarni
3636
Test Prepared (PHPUnit\TestFixture\Event\PhpWarningTest::testPhpWarning)
3737
Test Triggered PHP Warning (PHPUnit\TestFixture\Event\PhpWarningTest::testPhpWarning)
3838
Undefined variable $b
39-
Assertion Succeeded (Constraint: is true)
39+
Assertion Succeeded (Constraint: is true, Value: true)
4040
Test Passed (PHPUnit\TestFixture\Event\PhpWarningTest::testPhpWarning)
4141
Test Finished (PHPUnit\TestFixture\Event\PhpWarningTest::testPhpWarning)
4242
Test Suite Finished (PHPUnit\TestFixture\Event\PhpWarningTest, 1 test)

tests/end-to-end/event/phpunit-deprecated.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ Test Preparation Started (PHPUnit\TestFixture\Event\DeprecatedPhpunitFeatureTest
3636
Test Prepared (PHPUnit\TestFixture\Event\DeprecatedPhpunitFeatureTest::testDeprecatedPhpunitFeature)
3737
Test Triggered PHPUnit Deprecation (PHPUnit\TestFixture\Event\DeprecatedPhpunitFeatureTest::testDeprecatedPhpunitFeature)
3838
message
39-
Assertion Succeeded (Constraint: is true)
39+
Assertion Succeeded (Constraint: is true, Value: true)
4040
Test Passed (PHPUnit\TestFixture\Event\DeprecatedPhpunitFeatureTest::testDeprecatedPhpunitFeature)
4141
Test Finished (PHPUnit\TestFixture\Event\DeprecatedPhpunitFeatureTest::testDeprecatedPhpunitFeature)
4242
Test Suite Finished (PHPUnit\TestFixture\Event\DeprecatedPhpunitFeatureTest, 1 test)

tests/end-to-end/event/phpunit-warning.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ Test Preparation Started (PHPUnit\TestFixture\Event\PhpunitWarningTest::testPhpu
3636
Test Prepared (PHPUnit\TestFixture\Event\PhpunitWarningTest::testPhpunitWarning)
3737
Test Triggered PHPUnit Warning (PHPUnit\TestFixture\Event\PhpunitWarningTest::testPhpunitWarning)
3838
message
39-
Assertion Succeeded (Constraint: is true)
39+
Assertion Succeeded (Constraint: is true, Value: true)
4040
Test Passed (PHPUnit\TestFixture\Event\PhpunitWarningTest::testPhpunitWarning)
4141
Test Finished (PHPUnit\TestFixture\Event\PhpunitWarningTest::testPhpunitWarning)
4242
Test Suite Finished (PHPUnit\TestFixture\Event\PhpunitWarningTest, 1 test)

tests/end-to-end/event/test-custom-comparator.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ Test Suite Started (PHPUnit\TestFixture\Event\CustomComparatorTest, 1 test)
3535
Test Preparation Started (PHPUnit\TestFixture\Event\CustomComparatorTest::testWithCustomComparator)
3636
Test Prepared (PHPUnit\TestFixture\Event\CustomComparatorTest::testWithCustomComparator)
3737
Comparator Registered (PHPUnit\TestFixture\Event\CustomComparator)
38-
Assertion Succeeded (Constraint: is equal to true)
38+
Assertion Succeeded (Constraint: is equal to true, Value: false)
3939
Test Passed (PHPUnit\TestFixture\Event\CustomComparatorTest::testWithCustomComparator)
4040
Test Finished (PHPUnit\TestFixture\Event\CustomComparatorTest::testWithCustomComparator)
4141
Test Suite Finished (PHPUnit\TestFixture\Event\CustomComparatorTest, 1 test)

0 commit comments

Comments
 (0)