Skip to content

Commit d0b86f0

Browse files
Merge branch '13.1'
* 13.1: Closes #6590
2 parents d18a2d5 + ec411df commit d0b86f0

3 files changed

Lines changed: 53 additions & 0 deletions

File tree

src/TextUI/Configuration/Xml/Loader.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,14 @@ public function load(string $filename): LoadedFromFileConfiguration
123123

124124
$validationResult = (new Validator)->validate($document, $xsdFilename);
125125

126+
if ($validationResult->hasValidationErrors()) {
127+
$this->ensureConfigurationValidatesAgainstAtLeastOneSchema(
128+
$document,
129+
$configurationFileRealpath,
130+
$validationResult,
131+
);
132+
}
133+
126134
try {
127135
return new LoadedFromFileConfiguration(
128136
$configurationFileRealpath,
@@ -1312,4 +1320,37 @@ private function element(DOMXPath $xpath, string $element): ?DOMElement
13121320

13131321
return null;
13141322
}
1323+
1324+
/**
1325+
* @throws Exception
1326+
*/
1327+
private function ensureConfigurationValidatesAgainstAtLeastOneSchema(DOMDocument $document, string $configurationFile, ValidationResult $validationResult): void
1328+
{
1329+
if ($document->documentElement->localName === 'phpunit') {
1330+
return;
1331+
}
1332+
1333+
$schemaFinder = new SchemaFinder;
1334+
$validator = new Validator;
1335+
1336+
foreach ($schemaFinder->available() as $version) {
1337+
try {
1338+
$xsdFilename = $schemaFinder->find($version);
1339+
} catch (CannotFindSchemaException) {
1340+
continue;
1341+
}
1342+
1343+
if (!$validator->validate($document, $xsdFilename)->hasValidationErrors()) {
1344+
return;
1345+
}
1346+
}
1347+
1348+
throw new Exception(
1349+
sprintf(
1350+
'XML configuration file %s does not validate against any supported PHPUnit schema:' . PHP_EOL . '%s',
1351+
$configurationFile,
1352+
$validationResult->asString(),
1353+
),
1354+
);
1355+
}
13151356
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<invalid>
3+
</invalid>

tests/unit/TextUI/Configuration/Xml/LoaderTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,15 @@ public function testExceptionIsThrownForNotExistingConfigurationFile(): void
7272
$this->configuration('not_existing_file.xml');
7373
}
7474

75+
public function testExceptionIsThrownForConfigurationFileThatDoesNotValidateAgainstAnySupportedSchema(): void
76+
{
77+
$this->expectException(Exception::class);
78+
$this->expectExceptionMessage('does not validate against any supported PHPUnit schema');
79+
80+
/* @noinspection UnusedFunctionResultInspection */
81+
$this->configuration('configuration_invalid_root_element.xml');
82+
}
83+
7584
public function testShouldReadColorsWhenTrueInConfigurationFile(): void
7685
{
7786
$phpunit = $this->configuration('configuration.colors.true.xml')->phpunit();

0 commit comments

Comments
 (0)