Skip to content

Commit 37a1e99

Browse files
bug #32903 [PHPUnit Bridge] Avoid registering listener twice (alexpott)
This PR was merged into the 3.4 branch. Discussion ---------- [PHPUnit Bridge] Avoid registering listener twice The listener can be registered via configuration by the user. In that case, we do not want to add it again to the list of listeners. Closes #31649 | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | n/a | License | MIT | Doc PR | n/a Commits ------- b190536205 Check phpunit configuration for listeners
2 parents ca9f40f + c8dd56c commit 37a1e99

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

Legacy/CommandForV5.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ class CommandForV5 extends \PHPUnit_TextUI_Command
2323
*/
2424
protected function createRunner()
2525
{
26-
$listener = new SymfonyTestsListenerForV5();
27-
2826
$this->arguments['listeners'] = isset($this->arguments['listeners']) ? $this->arguments['listeners'] : array();
2927

3028
$registeredLocally = false;
@@ -37,8 +35,21 @@ protected function createRunner()
3735
}
3836
}
3937

38+
if (isset($this->arguments['configuration'])) {
39+
$configuration = $this->arguments['configuration'];
40+
if (!$configuration instanceof \PHPUnit_Util_Configuration) {
41+
$configuration = \PHPUnit_Util_Configuration::getInstance($this->arguments['configuration']);
42+
}
43+
foreach ($configuration->getListenerConfiguration() as $registeredListener) {
44+
if ('Symfony\Bridge\PhpUnit\SymfonyTestsListener' === ltrim($registeredListener['class'], '\\')) {
45+
$registeredLocally = true;
46+
break;
47+
}
48+
}
49+
}
50+
4051
if (!$registeredLocally) {
41-
$this->arguments['listeners'][] = $listener;
52+
$this->arguments['listeners'][] = new SymfonyTestsListenerForV5();
4253
}
4354

4455
return parent::createRunner();

Legacy/CommandForV6.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use PHPUnit\TextUI\Command as BaseCommand;
1515
use PHPUnit\TextUI\TestRunner as BaseRunner;
16+
use PHPUnit\Util\Configuration;
1617
use Symfony\Bridge\PhpUnit\SymfonyTestsListener;
1718

1819
/**
@@ -27,8 +28,6 @@ class CommandForV6 extends BaseCommand
2728
*/
2829
protected function createRunner(): BaseRunner
2930
{
30-
$listener = new SymfonyTestsListener();
31-
3231
$this->arguments['listeners'] = isset($this->arguments['listeners']) ? $this->arguments['listeners'] : [];
3332

3433
$registeredLocally = false;
@@ -41,8 +40,21 @@ protected function createRunner(): BaseRunner
4140
}
4241
}
4342

43+
if (isset($this->arguments['configuration'])) {
44+
$configuration = $this->arguments['configuration'];
45+
if (!$configuration instanceof Configuration) {
46+
$configuration = Configuration::getInstance($this->arguments['configuration']);
47+
}
48+
foreach ($configuration->getListenerConfiguration() as $registeredListener) {
49+
if ('Symfony\Bridge\PhpUnit\SymfonyTestsListener' === ltrim($registeredListener['class'], '\\')) {
50+
$registeredLocally = true;
51+
break;
52+
}
53+
}
54+
}
55+
4456
if (!$registeredLocally) {
45-
$this->arguments['listeners'][] = $listener;
57+
$this->arguments['listeners'][] = new SymfonyTestsListener();
4658
}
4759

4860
return parent::createRunner();

0 commit comments

Comments
 (0)