Skip to content

Commit 09f8ddf

Browse files
Change the format of the XML document generated by --list-tests-xml
1 parent ff66572 commit 09f8ddf

File tree

11 files changed

+229
-66
lines changed

11 files changed

+229
-66
lines changed

ChangeLog-11.0.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ All notable changes of the PHPUnit 11.0 release series are documented in this fi
1919
* [#5213](https://github.com/sebastianbergmann/phpunit/issues/5213): Make `TestCase` methods `protected` that should have been `protected` all along
2020
* [#5254](https://github.com/sebastianbergmann/phpunit/issues/5254): Make `TestCase` methods `final` that should have been `final` all along
2121
* [#5619](https://github.com/sebastianbergmann/phpunit/pull/5619): Check and restore error/exception global handlers
22+
* The format of the XML document generated using the `--list-tests-xml` CLI option has been changed
2223
* `small`, `medium`, and `large` can no longer be used as group names with the `#[Group]` attribute
2324

2425
### Deprecated

src/TextUI/Command/Commands/ListTestsAsXmlCommand.php

Lines changed: 47 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
namespace PHPUnit\TextUI\Command;
1111

1212
use function file_put_contents;
13-
use function implode;
13+
use function ksort;
1414
use function sprintf;
1515
use PHPUnit\Framework\TestCase;
1616
use PHPUnit\Framework\TestSuite;
@@ -41,49 +41,82 @@ public function execute(): Result
4141
$writer->openMemory();
4242
$writer->setIndent(true);
4343
$writer->startDocument();
44+
45+
$writer->startElement('testSuite');
46+
$writer->writeAttribute('xmlns', 'https://xml.phpunit.de/testSuite');
47+
4448
$writer->startElement('tests');
4549

46-
$currentTestCase = null;
50+
$currentTestClass = null;
51+
$groups = [];
4752

4853
foreach (new RecursiveIteratorIterator($this->suite) as $test) {
4954
if ($test instanceof TestCase) {
50-
if ($test::class !== $currentTestCase) {
51-
if ($currentTestCase !== null) {
55+
foreach ($test->groups() as $group) {
56+
if (!isset($groups[$group])) {
57+
$groups[$group] = [];
58+
}
59+
60+
$groups[$group][] = $test->valueObjectForEvents()->id();
61+
}
62+
63+
if ($test::class !== $currentTestClass) {
64+
if ($currentTestClass !== null) {
5265
$writer->endElement();
5366
}
5467

55-
$writer->startElement('testCaseClass');
68+
$writer->startElement('testClass');
5669
$writer->writeAttribute('name', $test::class);
70+
$writer->writeAttribute('file', $test->valueObjectForEvents()->file());
5771

58-
$currentTestCase = $test::class;
72+
$currentTestClass = $test::class;
5973
}
6074

61-
$writer->startElement('testCaseMethod');
75+
$writer->startElement('testMethod');
6276
$writer->writeAttribute('id', $test->valueObjectForEvents()->id());
63-
$writer->writeAttribute('name', $test->name());
64-
$writer->writeAttribute('groups', implode(',', $test->groups()));
77+
$writer->writeAttribute('name', $test->valueObjectForEvents()->methodName());
6578
$writer->endElement();
6679

6780
continue;
6881
}
6982

7083
if ($test instanceof PhptTestCase) {
71-
if ($currentTestCase !== null) {
84+
if ($currentTestClass !== null) {
7285
$writer->endElement();
7386

74-
$currentTestCase = null;
87+
$currentTestClass = null;
7588
}
7689

77-
$writer->startElement('phptFile');
78-
$writer->writeAttribute('path', $test->getName());
90+
$writer->startElement('phpt');
91+
$writer->writeAttribute('file', $test->getName());
7992
$writer->endElement();
8093
}
8194
}
8295

83-
if ($currentTestCase !== null) {
96+
if ($currentTestClass !== null) {
97+
$writer->endElement();
98+
}
99+
100+
$writer->endElement();
101+
102+
ksort($groups);
103+
104+
$writer->startElement('groups');
105+
106+
foreach ($groups as $groupName => $testIds) {
107+
$writer->startElement('group');
108+
$writer->writeAttribute('name', $groupName);
109+
110+
foreach ($testIds as $testId) {
111+
$writer->startElement('test');
112+
$writer->writeAttribute('id', $testId);
113+
$writer->endElement();
114+
}
115+
84116
$writer->endElement();
85117
}
86118

119+
$writer->endElement();
87120
$writer->endElement();
88121

89122
file_put_contents($this->filename, $writer->outputMemory());
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* This file is part of PHPUnit.
4+
*
5+
* (c) Sebastian Bergmann <[email protected]>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
namespace PHPUnit\TestFixture\ListTestsXml;
11+
12+
use PHPUnit\Framework\TestCase;
13+
14+
final class AnotherExampleTest extends TestCase
15+
{
16+
public function testOne(): void
17+
{
18+
$this->assertTrue(true);
19+
}
20+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* This file is part of PHPUnit.
4+
*
5+
* (c) Sebastian Bergmann <[email protected]>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
namespace PHPUnit\TestFixture\ListTestsXml;
11+
12+
use PHPUnit\Framework\Attributes\DataProvider;
13+
use PHPUnit\Framework\Attributes\Group;
14+
use PHPUnit\Framework\TestCase;
15+
16+
final class ExampleTest extends TestCase
17+
{
18+
public static function provider(): array
19+
{
20+
return [
21+
[1, 2, 3],
22+
];
23+
}
24+
25+
#[DataProvider('provider')]
26+
#[Group('example')]
27+
public function testOne(int $a, int $b, int $c): void
28+
{
29+
$this->assertTrue(true);
30+
}
31+
32+
#[Group('another-example')]
33+
public function testTwo(): void
34+
{
35+
$this->assertTrue(true);
36+
}
37+
38+
public function testThree(): void
39+
{
40+
$this->assertTrue(true);
41+
}
42+
}

tests/end-to-end/_files/list-tests/example.phpt

Whitespace-only changes.

tests/end-to-end/cli/list-tests/list-tests-dataprovider.phpt

Lines changed: 0 additions & 19 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
--TEST--
2-
phpunit --list-tests --filter testAdd#0 ../../../_files/DataProviderTest.php
2+
phpunit --filter testOne#0 --list-tests ../../_files/list-tests
33
--FILE--
44
<?php declare(strict_types=1);
55
$_SERVER['argv'][] = '--do-not-cache-result';
66
$_SERVER['argv'][] = '--no-configuration';
7-
$_SERVER['argv'][] = '--list-tests';
87
$_SERVER['argv'][] = '--filter';
9-
$_SERVER['argv'][] = 'testAdd#0';
10-
$_SERVER['argv'][] = __DIR__ . '/../../../_files/DataProviderTest.php';
8+
$_SERVER['argv'][] = 'testOne#0';
9+
$_SERVER['argv'][] = '--list-tests';
10+
$_SERVER['argv'][] = __DIR__ . '/../../_files/list-tests';
1111

1212
require_once __DIR__ . '/../../../bootstrap.php';
1313
(new PHPUnit\TextUI\Application)->run($_SERVER['argv']);
@@ -17,7 +17,8 @@ PHPUnit %s by Sebastian Bergmann and contributors.
1717
The --filter and --list-tests options cannot be combined, --filter is ignored
1818

1919
Available test(s):
20-
- PHPUnit\TestFixture\DataProviderTest::testAdd#0
21-
- PHPUnit\TestFixture\DataProviderTest::testAdd#1
22-
- PHPUnit\TestFixture\DataProviderTest::testAdd#2
23-
- PHPUnit\TestFixture\DataProviderTest::testAdd#3
20+
- PHPUnit\TestFixture\ListTestsXml\AnotherExampleTest::testOne
21+
- PHPUnit\TestFixture\ListTestsXml\ExampleTest::testOne#0
22+
- PHPUnit\TestFixture\ListTestsXml\ExampleTest::testTwo
23+
- PHPUnit\TestFixture\ListTestsXml\ExampleTest::testThree
24+
- %sexample.phpt

tests/end-to-end/cli/list-tests/list-tests-xml-dataprovider.phpt

Lines changed: 0 additions & 25 deletions
This file was deleted.
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
--TEST--
2+
phpunit --filter testOne#0 --list-tests-xml php://stdout ../../_files/list-tests
3+
--FILE--
4+
<?php declare(strict_types=1);
5+
$_SERVER['argv'][] = '--do-not-cache-result';
6+
$_SERVER['argv'][] = '--no-configuration';
7+
$_SERVER['argv'][] = '--filter';
8+
$_SERVER['argv'][] = 'testOne#0';
9+
$_SERVER['argv'][] = '--list-tests-xml';
10+
$_SERVER['argv'][] = 'php://stdout';
11+
$_SERVER['argv'][] = __DIR__ . '/../../_files/list-tests';
12+
13+
require_once __DIR__ . '/../../../bootstrap.php';
14+
15+
(new PHPUnit\TextUI\Application)->run($_SERVER['argv']);
16+
--EXPECTF--
17+
PHPUnit %s by Sebastian Bergmann and contributors.
18+
19+
<?xml version="1.0"?>
20+
<testSuite xmlns="https://xml.phpunit.de/testSuite">
21+
<tests>
22+
<testClass name="PHPUnit\TestFixture\ListTestsXml\AnotherExampleTest" file="%sAnotherExampleTest.php">
23+
<testMethod id="PHPUnit\TestFixture\ListTestsXml\AnotherExampleTest::testOne" name="testOne"/>
24+
</testClass>
25+
<testClass name="PHPUnit\TestFixture\ListTestsXml\ExampleTest" file="%sExampleTest.php">
26+
<testMethod id="PHPUnit\TestFixture\ListTestsXml\ExampleTest::testOne#0" name="testOne"/>
27+
<testMethod id="PHPUnit\TestFixture\ListTestsXml\ExampleTest::testTwo" name="testTwo"/>
28+
<testMethod id="PHPUnit\TestFixture\ListTestsXml\ExampleTest::testThree" name="testThree"/>
29+
</testClass>
30+
<phpt file="%sexample.phpt"/>
31+
</tests>
32+
<groups>
33+
<group name="another-example">
34+
<test id="PHPUnit\TestFixture\ListTestsXml\ExampleTest::testTwo"/>
35+
</group>
36+
<group name="default">
37+
<test id="PHPUnit\TestFixture\ListTestsXml\AnotherExampleTest::testOne"/>
38+
<test id="PHPUnit\TestFixture\ListTestsXml\ExampleTest::testThree"/>
39+
</group>
40+
<group name="example">
41+
<test id="PHPUnit\TestFixture\ListTestsXml\ExampleTest::testOne#0"/>
42+
</group>
43+
</groups>
44+
</testSuite>
45+
The --filter and --list-tests-xml options cannot be combined, --filter is ignored
46+
47+
Wrote list of tests that would have been run to php://stdout
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
--TEST--
2+
phpunit --list-tests-xml php://stdout ../../_files/list-tests
3+
--FILE--
4+
<?php declare(strict_types=1);
5+
$_SERVER['argv'][] = '--do-not-cache-result';
6+
$_SERVER['argv'][] = '--no-configuration';
7+
$_SERVER['argv'][] = '--list-tests-xml';
8+
$_SERVER['argv'][] = 'php://stdout';
9+
$_SERVER['argv'][] = __DIR__ . '/../../_files/list-tests';
10+
11+
require_once __DIR__ . '/../../../bootstrap.php';
12+
13+
(new PHPUnit\TextUI\Application)->run($_SERVER['argv']);
14+
--EXPECTF--
15+
PHPUnit %s by Sebastian Bergmann and contributors.
16+
17+
<?xml version="1.0"?>
18+
<testSuite xmlns="https://xml.phpunit.de/testSuite">
19+
<tests>
20+
<testClass name="PHPUnit\TestFixture\ListTestsXml\AnotherExampleTest" file="%sAnotherExampleTest.php">
21+
<testMethod id="PHPUnit\TestFixture\ListTestsXml\AnotherExampleTest::testOne" name="testOne"/>
22+
</testClass>
23+
<testClass name="PHPUnit\TestFixture\ListTestsXml\ExampleTest" file="%sExampleTest.php">
24+
<testMethod id="PHPUnit\TestFixture\ListTestsXml\ExampleTest::testOne#0" name="testOne"/>
25+
<testMethod id="PHPUnit\TestFixture\ListTestsXml\ExampleTest::testTwo" name="testTwo"/>
26+
<testMethod id="PHPUnit\TestFixture\ListTestsXml\ExampleTest::testThree" name="testThree"/>
27+
</testClass>
28+
<phpt file="%sexample.phpt"/>
29+
</tests>
30+
<groups>
31+
<group name="another-example">
32+
<test id="PHPUnit\TestFixture\ListTestsXml\ExampleTest::testTwo"/>
33+
</group>
34+
<group name="default">
35+
<test id="PHPUnit\TestFixture\ListTestsXml\AnotherExampleTest::testOne"/>
36+
<test id="PHPUnit\TestFixture\ListTestsXml\ExampleTest::testThree"/>
37+
</group>
38+
<group name="example">
39+
<test id="PHPUnit\TestFixture\ListTestsXml\ExampleTest::testOne#0"/>
40+
</group>
41+
</groups>
42+
</testSuite>
43+
Wrote list of tests that would have been run to php://stdout

0 commit comments

Comments
 (0)