Skip to content

Commit d8ab07c

Browse files
Improve migration failure messages
1 parent 1ca4c54 commit d8ab07c

File tree

3 files changed

+28
-29
lines changed

3 files changed

+28
-29
lines changed

src/TextUI/Command/Commands/MigrateConfigurationCommand.php

+24-14
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
use function copy;
1313
use function file_put_contents;
14+
use function sprintf;
1415
use PHPUnit\TextUI\XmlConfiguration\Migrator;
1516
use Throwable;
1617

@@ -28,24 +29,33 @@ public function __construct(string $filename)
2829

2930
public function execute(): Result
3031
{
31-
copy($this->filename, $this->filename . '.bak');
32+
try {
33+
$migrated = (new Migrator)->migrate($this->filename);
3234

33-
$buffer = 'Created backup: ' . $this->filename . '.bak' . PHP_EOL;
34-
$shellExitCode = Result::SUCCESS;
35+
copy($this->filename, $this->filename . '.bak');
3536

36-
try {
37-
file_put_contents(
38-
$this->filename,
39-
(new Migrator)->migrate($this->filename),
40-
);
37+
file_put_contents($this->filename, $migrated);
4138

42-
$buffer .= 'Migrated configuration: ' . $this->filename . PHP_EOL;
39+
return Result::from(
40+
sprintf(
41+
'Created backup: %s.bak%sMigrated configuration: %s%s',
42+
$this->filename,
43+
PHP_EOL,
44+
$this->filename,
45+
PHP_EOL,
46+
),
47+
);
4348
} catch (Throwable $t) {
44-
$buffer .= 'Migration failed: ' . $t->getMessage() . PHP_EOL;
45-
46-
$shellExitCode = Result::FAILURE;
49+
return Result::from(
50+
sprintf(
51+
'Migration of %s failed:%s%s%s',
52+
$this->filename,
53+
PHP_EOL,
54+
$t->getMessage(),
55+
PHP_EOL,
56+
),
57+
Result::FAILURE,
58+
);
4759
}
48-
49-
return Result::from($buffer, $shellExitCode);
5060
}
5161
}

src/TextUI/Configuration/Xml/Migration/Migrator.php

+2-13
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
*/
1010
namespace PHPUnit\TextUI\XmlConfiguration;
1111

12-
use function sprintf;
1312
use PHPUnit\Runner\Version;
1413
use PHPUnit\Util\Xml\Loader as XmlLoader;
1514
use PHPUnit\Util\Xml\XmlException;
@@ -30,21 +29,11 @@ public function migrate(string $filename): string
3029
$origin = (new SchemaDetector)->detect($filename);
3130

3231
if (!$origin->detected()) {
33-
throw new Exception(
34-
sprintf(
35-
'%s does not validate against any know schema',
36-
$filename,
37-
),
38-
);
32+
throw new Exception('The file does not validate against any know schema');
3933
}
4034

4135
if ($origin->version() === Version::series()) {
42-
throw new Exception(
43-
sprintf(
44-
'%s does not need to be migrated',
45-
$filename,
46-
),
47-
);
36+
throw new Exception('The file does not need to be migrated');
4837
}
4938

5039
$configurationDocument = (new XmlLoader)->loadFile($filename);

tests/end-to-end/migration/unsupported-schema.phpt

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ require_once __DIR__ . '/../../bootstrap.php';
1414
--EXPECTF--
1515
PHPUnit %s by Sebastian Bergmann and contributors.
1616

17-
Created backup: %sphpunit.xml.bak
18-
Migration failed: %s does not validate against any know schema
17+
Migration of %s failed:
18+
The file does not validate against any know schema
1919
--CLEAN--
2020
<?php declare(strict_types=1);
2121
unlink(sys_get_temp_dir() . '/phpunit.xml');

0 commit comments

Comments
 (0)