Skip to content

Commit defe82e

Browse files
committed
Replace Installer to Creator
1 parent f5cc58c commit defe82e

File tree

8 files changed

+74
-53
lines changed

8 files changed

+74
-53
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
},
2626
"require": {
2727
"php": "^8.3",
28+
"ext-simplexml": "*",
2829
"justinrainbow/json-schema": "^6.0",
2930
"symfony/console": "^5.4 || ^6.4 || ^7.3",
3031
"symfony/yaml": "^5.4 || ^6.4 || ^7.3"
File renamed without changes.
File renamed without changes.

src/PHPUnit/Cobertura/Formatter/Application.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@
1313

1414
use AndreyTech\PHPUnit\Cobertura\Formatter\Config\CommandLine;
1515
use AndreyTech\PHPUnit\Cobertura\Formatter\Config\File as ConfigFile;
16+
use AndreyTech\PHPUnit\Cobertura\Formatter\Config\File\Creator;
1617
use AndreyTech\PHPUnit\Cobertura\Formatter\Parser\File as CoberturaFile;
1718
use AndreyTech\PHPUnit\Cobertura\Formatter\Renderer\Colorizer;
19+
use Exception;
1820
use Symfony\Component\Console\Output\ConsoleOutput;
1921
use Throwable;
2022

@@ -53,8 +55,18 @@ public function run(): int
5355
return $exitCode;
5456
}
5557

58+
/**
59+
* @throws Exception
60+
*/
5661
private function doRun(): int
5762
{
63+
$cmd = new CommandLine();
64+
if ($cmd->optionInit()) {
65+
(new Creator())->create();
66+
67+
return self::EXIT_CODE_OK;
68+
}
69+
5870
(new Renderer(
5971
$this->consoleOutput,
6072
new Colorizer(
@@ -63,7 +75,7 @@ private function doRun(): int
6375
))->render(
6476
(new Parser())->parse(
6577
new CoberturaFile(
66-
(new CommandLine())->coberturaFile()
78+
$cmd->coberturaFile()
6779
)
6880
)
6981
);

src/PHPUnit/Cobertura/Formatter/Config/CommandLine.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@
1515
use Symfony\Component\Console\Input\ArgvInput;
1616
use Symfony\Component\Console\Input\InputArgument;
1717
use Symfony\Component\Console\Input\InputDefinition;
18+
use Symfony\Component\Console\Input\InputOption;
19+
20+
use function copy;
21+
use function file_exists;
22+
use function getcwd;
23+
use function sprintf;
1824

1925
final readonly class CommandLine
2026
{
@@ -29,9 +35,12 @@ public function buildArgvInput(): ArgvInput
2935
{
3036
$definition = new InputDefinition();
3137

32-
$argument = new InputArgument('cobertura-file', InputArgument::REQUIRED);
38+
$argument = new InputArgument('cobertura-file', InputArgument::OPTIONAL);
3339
$definition->addArgument($argument);
3440

41+
$option = new InputOption('init', null, InputOption::VALUE_NONE);
42+
$definition->addOption($option);
43+
3544
return new ArgvInput(null, $definition);
3645
}
3746

@@ -45,4 +54,9 @@ public function coberturaFile(): string
4554

4655
return $coberturaFile;
4756
}
57+
58+
public function optionInit(): bool
59+
{
60+
return (bool) $this->input->getOption('init');
61+
}
4862
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
/**
4+
* @author andrey-tech
5+
* @copyright 2025 andrey-tech
6+
* @link https://github.com/andrey-tech/
7+
* @license MIT
8+
*/
9+
10+
declare(strict_types=1);
11+
12+
namespace AndreyTech\PHPUnit\Cobertura\Formatter\Config\File;
13+
14+
use RuntimeException;
15+
16+
use function copy;
17+
use function file_exists;
18+
use function getcwd;
19+
use function sprintf;
20+
21+
final class Creator
22+
{
23+
private const string CONFIG_DIR = __DIR__ . '/../../../../../../config';
24+
private const string CONFIG_FILE = 'phpunit-cobertura-formatter.yml.dist';
25+
26+
public function create(): void
27+
{
28+
$source = sprintf('%s/%s', self::CONFIG_DIR, self::CONFIG_FILE);
29+
$destination = sprintf('%s/%s', (string) getcwd(), self::CONFIG_FILE);
30+
31+
if (file_exists($destination)) {
32+
throw new RuntimeException(
33+
sprintf('Default config file already exists at "%s".', $destination)
34+
);
35+
}
36+
37+
if (!@copy($source, $destination)) {
38+
throw new RuntimeException(
39+
sprintf('Failed to copy default config file to "%s".', $destination)
40+
);
41+
}
42+
}
43+
}

src/PHPUnit/Cobertura/Formatter/Config/File/Validator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
final class Validator
2828
{
29-
private const string SCHEMA_FILE = 'schema.json';
29+
private const string SCHEMA_FILE = __DIR__ . '/../../../../../../config/phpunit-cobertura-formatter.schema.json';
3030

3131
/**
3232
* @param array<mixed> $config
@@ -56,7 +56,7 @@ public function validate(array $config): void
5656
*/
5757
private function loadSchema(): array
5858
{
59-
$schema = file_get_contents(__DIR__ . '/' . self::SCHEMA_FILE);
59+
$schema = @file_get_contents(self::SCHEMA_FILE);
6060
if (false === $schema) {
6161
throw new RuntimeException(
6262
sprintf('Failed to load JSON schema file: "%s".', self::SCHEMA_FILE)

src/PHPUnit/Cobertura/Formatter/Installer.php

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

0 commit comments

Comments
 (0)