Skip to content

Commit

Permalink
Try to validate configuration file against all known schema definitio…
Browse files Browse the repository at this point in the history
…ns before attempting migration
  • Loading branch information
sebastianbergmann committed Jan 18, 2024
1 parent 68bf9de commit 5dbc551
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 2 deletions.
11 changes: 10 additions & 1 deletion src/TextUI/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -840,7 +840,16 @@ private function migrateConfiguration(string $filename): void
{
$this->printVersionString();

if (!(new SchemaDetector)->detect($filename)->detected()) {
$result = (new SchemaDetector)->detect($filename);

if (!$result->detected()) {
print $filename . ' does not validate against any know schema.' . PHP_EOL;

exit(TestRunner::EXCEPTION_EXIT);
}

/** @psalm-suppress MissingThrowsDocblock */
if ($result->version() === Version::series()) {
print $filename . ' does not need to be migrated.' . PHP_EOL;

exit(TestRunner::EXCEPTION_EXIT);
Expand Down
4 changes: 3 additions & 1 deletion src/Util/Xml/SchemaDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ public function detect(string $filename): SchemaDetectionResult
true,
);

foreach (['9.2', '8.5'] as $candidate) {
$schemaFinder = new SchemaFinder;

foreach ($schemaFinder->available() as $candidate) {
$schema = (new SchemaFinder)->find($candidate);

if (!(new Validator)->validate($document, $schema)->hasValidationErrors()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/0.0/phpunit.xsd"
>
<foo>bar</foo>
</phpunit>
21 changes: 21 additions & 0 deletions tests/end-to-end/migration/unsupported-schema.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
--TEST--
Configuration migration is not possible when the configuration file does not validate against any known schema
--FILE--
<?php declare(strict_types=1);
$_SERVER['argv'][] = '--do-not-cache-result';
$_SERVER['argv'][] = '--migrate-configuration';

chdir(sys_get_temp_dir());
copy(__DIR__ . '/_files/unsupported-schema/phpunit.xml', 'phpunit.xml');

require_once __DIR__ . '/../../bootstrap.php';

PHPUnit\TextUI\Command::main();
--EXPECTF--
PHPUnit %s by Sebastian Bergmann and contributors.

%sphpunit.xml does not validate against any know schema.
--CLEAN--
<?php declare(strict_types=1);
unlink(sys_get_temp_dir() . '/phpunit.xml');
unlink(sys_get_temp_dir() . '/phpunit.xml.bak');

0 comments on commit 5dbc551

Please sign in to comment.