Skip to content

Commit 57e07ee

Browse files
Do not allow adding the same test to multiple test suites
1 parent edf7f2b commit 57e07ee

File tree

7 files changed

+74
-46
lines changed

7 files changed

+74
-46
lines changed

ChangeLog-11.0.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ All notable changes of the PHPUnit 11.0 release series are documented in this fi
2121
* [#5619](https://github.com/sebastianbergmann/phpunit/pull/5619): Check and restore error/exception global handlers
2222
* The format of the XML document generated using the `--list-tests-xml` CLI option has been changed
2323
* `small`, `medium`, and `large` can no longer be used as group names with the `#[Group]` attribute
24+
* A test can no longer be part of multiple test suites that are configured in the XML configuration file
2425

2526
### Deprecated
2627

src/TextUI/Configuration/Xml/TestSuiteMapper.php

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@
1414
use function in_array;
1515
use function is_dir;
1616
use function is_file;
17+
use function sprintf;
1718
use function str_contains;
1819
use function version_compare;
20+
use PHPUnit\Event\Facade as EventFacade;
1921
use PHPUnit\Framework\Exception as FrameworkException;
2022
use PHPUnit\Framework\TestSuite as TestSuiteObject;
2123
use PHPUnit\TextUI\Configuration\TestSuiteCollection;
@@ -42,6 +44,7 @@ public function map(string $xmlConfigurationFile, TestSuiteCollection $configure
4244
$namesOfIncludedTestSuitesAsArray = $namesOfIncludedTestSuites ? explode(',', $namesOfIncludedTestSuites) : [];
4345
$excludedTestSuitesAsArray = $namesOfExcludedTestSuites ? explode(',', $namesOfExcludedTestSuites) : [];
4446
$result = TestSuiteObject::empty($xmlConfigurationFile);
47+
$processed = [];
4548

4649
foreach ($configuredTestSuites as $configuredTestSuite) {
4750
if (!empty($namesOfIncludedTestSuitesAsArray) && !in_array($configuredTestSuite->name(), $namesOfIncludedTestSuitesAsArray, true)) {
@@ -52,14 +55,15 @@ public function map(string $xmlConfigurationFile, TestSuiteCollection $configure
5255
continue;
5356
}
5457

55-
$exclude = [];
58+
$testSuiteName = $configuredTestSuite->name();
59+
$exclude = [];
5660

5761
foreach ($configuredTestSuite->exclude()->asArray() as $file) {
5862
$exclude[] = $file->path();
5963
}
6064

6165
$testSuite = TestSuiteObject::empty($configuredTestSuite->name());
62-
$processed = [];
66+
$empty = true;
6367

6468
foreach ($configuredTestSuite->directories() as $directory) {
6569
if (!str_contains($directory->path(), '*') && !is_dir($directory->path())) {
@@ -81,10 +85,20 @@ public function map(string $xmlConfigurationFile, TestSuiteCollection $configure
8185

8286
foreach ($files as $file) {
8387
if (isset($processed[$file])) {
88+
EventFacade::emitter()->testRunnerTriggeredWarning(
89+
sprintf(
90+
'Cannot add file %s to test suite "%s" as it was already added to test suite "%s"',
91+
$file,
92+
$testSuiteName,
93+
$processed[$file],
94+
),
95+
);
96+
8497
continue;
8598
}
8699

87-
$processed[$file] = true;
100+
$processed[$file] = $testSuiteName;
101+
$empty = false;
88102

89103
$testSuite->addTestFile($file, $groups);
90104
}
@@ -100,15 +114,25 @@ public function map(string $xmlConfigurationFile, TestSuiteCollection $configure
100114
}
101115

102116
if (isset($processed[$file->path()])) {
117+
EventFacade::emitter()->testRunnerTriggeredWarning(
118+
sprintf(
119+
'Cannot add file %s to test suite "%s" as it was already added to test suite "%s"',
120+
$file->path(),
121+
$testSuiteName,
122+
$processed[$file->path()],
123+
),
124+
);
125+
103126
continue;
104127
}
105128

106-
$processed[$file->path()] = true;
129+
$processed[$file->path()] = $testSuiteName;
130+
$empty = false;
107131

108132
$testSuite->addTestFile($file->path(), $file->groups());
109133
}
110134

111-
if (!empty($processed)) {
135+
if (!$empty) {
112136
$result->addTest($testSuite);
113137
}
114138
}

tests/_files/DataProviderIssue2859/phpunit.xml

Lines changed: 0 additions & 10 deletions
This file was deleted.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="../../../../phpunit.xsd"
4+
>
5+
<testsuites>
6+
<testsuite name="one">
7+
<directory>tests</directory>
8+
</testsuite>
9+
10+
<testsuite name="two">
11+
<directory>tests</directory>
12+
</testsuite>
13+
</testsuites>
14+
</phpunit>

tests/_files/DataProviderIssue2859/tests/another/TestWithDataProviderTest.php renamed to tests/end-to-end/_files/overlapping-testsuite-configuration/tests/ExampleTest.php

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,13 @@
77
* For the full copyright and license information, please view the LICENSE
88
* file that was distributed with this source code.
99
*/
10-
namespace PHPUnit\TestFixture\DataProviderIssue2859;
10+
namespace PHPUnit\TestFixture\OverlappingTestSuiteConfiguration;
1111

12-
use PHPUnit\Framework\Attributes\DataProvider;
1312
use PHPUnit\Framework\TestCase;
1413

15-
class TestWithDataProviderTest extends TestCase
14+
final class ExampleTest extends TestCase
1615
{
17-
public static function provide(): array
18-
{
19-
return [[true]];
20-
}
21-
22-
#[DataProvider('provide')]
23-
public function testFirst($x): void
16+
public function testOne(): void
2417
{
2518
$this->assertTrue(true);
2619
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
--TEST--
2+
A test file must not be in more than one test suite configured in the XML configuration file
3+
--FILE--
4+
<?php declare(strict_types=1);
5+
$_SERVER['argv'][] = '--do-not-cache-result';
6+
$_SERVER['argv'][] = '--configuration';
7+
$_SERVER['argv'][] = __DIR__ . '/../_files/overlapping-testsuite-configuration/phpunit.xml';
8+
9+
require __DIR__ . '/../../bootstrap.php';
10+
11+
(new PHPUnit\TextUI\Application)->run($_SERVER['argv']);
12+
--EXPECTF--
13+
PHPUnit %s by Sebastian Bergmann and contributors.
14+
15+
Runtime: %s
16+
Configuration: %soverlapping-testsuite-configuration%sphpunit.xml
17+
18+
. 1 / 1 (100%)
19+
20+
Time: %s, Memory: %s
21+
22+
There was 1 PHPUnit test runner warning:
23+
24+
1) Cannot add file %sExampleTest.php to test suite "two" as it was already added to test suite "one"
25+
26+
WARNINGS!
27+
Tests: 1, Assertions: 1, Warnings: 1.

tests/end-to-end/generic/dataprovider-issue-2859.phpt

Lines changed: 0 additions & 21 deletions
This file was deleted.

0 commit comments

Comments
 (0)