Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[1.x] Adds --format option #87

Merged
merged 6 commits into from
Jul 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 29 additions & 3 deletions app/Actions/ElaborateSummary.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace App\Actions;

use Illuminate\Console\Command;
use PhpCsFixer\Console\Report\FixReport\ReportSummary;
use PhpCsFixer\Console\Report\FixReport;
use Symfony\Component\Console\Output\OutputInterface;

class ElaborateSummary
Expand Down Expand Up @@ -35,7 +35,7 @@ public function __construct(
*/
public function execute($totalFiles, $changes)
{
$summary = new ReportSummary(
$summary = new FixReport\ReportSummary(
$changes,
0,
0,
Expand All @@ -44,7 +44,11 @@ public function execute($totalFiles, $changes)
$this->output->isDecorated()
);

tap($summary, fn () => $this->summaryOutput->handle($summary, $totalFiles))->getChanged();
if ($this->input->getOption('format')) {
$this->displayUsingFormatter($summary, $totalFiles);
} else {
$this->summaryOutput->handle($summary, $totalFiles);
}

$failure = ($summary->isDryRun() && count($changes) > 0)
|| count($this->errors->getInvalidErrors()) > 0
Expand All @@ -53,4 +57,26 @@ public function execute($totalFiles, $changes)

return $failure ? Command::FAILURE : Command::SUCCESS;
}

/**
* Formats the given summary using the "selected" formatter.
*
* @param \PhpCsFixer\Console\Report\FixReport\ReportSummary $summary
* @param int $totalFiles
* @return void
*/
protected function displayUsingFormatter($summary, $totalFiles)
{
$reporter = match ($format = $this->input->getOption('format')) {
'checkstyle' => new FixReport\CheckstyleReporter(),
'gitlab' => new FixReport\GitlabReporter(),
'json' => new FixReport\JsonReporter(),
'junit' => new FixReport\JunitReporter(),
'txt' => new FixReport\TextReporter(),
'xml' => new FixReport\XmlReporter(),
default => abort(1, sprintf('Format [%s] is not supported.', $format)),
};

$this->output->write($reporter->generate($summary));
}
}
4 changes: 3 additions & 1 deletion app/Actions/FixCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ public function execute()
{
[$resolver, $totalFiles] = ConfigurationResolverFactory::fromIO($this->input, $this->output);

$this->progress->subscribe();
if (is_null($this->input->getOption('format'))) {
$this->progress->subscribe();
}

/** @var array<int, string> $changes */
$changes = with(new Runner(
Expand Down
1 change: 1 addition & 0 deletions app/Commands/DefaultCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ protected function configure()
new InputOption('config', '', InputOption::VALUE_REQUIRED, 'The configuration that should be used'),
new InputOption('preset', '', InputOption::VALUE_REQUIRED, 'The preset that should be used'),
new InputOption('test', '', InputOption::VALUE_NONE, 'Test for code style errors without fixing them'),
new InputOption('format', '', InputOption::VALUE_REQUIRED, 'The output format that should be used'),
]
);
}
Expand Down
81 changes: 81 additions & 0 deletions tests/Feature/FormatTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

it('outputs checkstyle format', function () {
[$statusCode, $output] = run('default', [
'path' => base_path('tests/Fixtures/with-fixable-issues'),
'--preset' => 'psr12',
'--format' => 'checkstyle',
]);

expect($statusCode)->toBe(1)
->and($output)
->toContain('<?xml version="1.0" encoding="UTF-8"?>')
->toContain('<checkstyle>')
->toContain('</checkstyle>')
->not->toContain(sprintf('⨯ %s', implode(DIRECTORY_SEPARATOR, [
'tests', 'Fixtures', 'with-fixable-issues', 'file.php',
])));
});

it('outputs json format', function () {
[$statusCode, $output] = run('default', [
'path' => base_path('tests/Fixtures/with-fixable-issues'),
'--preset' => 'psr12',
'--format' => 'json',
]);

expect($statusCode)->toBe(1)
->and($output)
->toBeJson()
->toContain('appliedFixers')
->not->toContain(sprintf('⨯ %s', implode(DIRECTORY_SEPARATOR, [
'tests', 'Fixtures', 'with-fixable-issues', 'file.php',
])));
});

it('outputs xml format', function () {
[$statusCode, $output] = run('default', [
'path' => base_path('tests/Fixtures/with-fixable-issues'),
'--preset' => 'psr12',
'--format' => 'xml',
]);

expect($statusCode)->toBe(1)
->and($output)
->toContain('<?xml version="1.0" encoding="UTF-8"?>')
->not->toContain(sprintf('⨯ %s', implode(DIRECTORY_SEPARATOR, [
'tests', 'Fixtures', 'with-fixable-issues', 'file.php',
])));
});

it('outputs junit format', function () {
[$statusCode, $output] = run('default', [
'path' => base_path('tests/Fixtures/with-fixable-issues'),
'--preset' => 'psr12',
'--format' => 'junit',
]);

expect($statusCode)->toBe(1)
->and($output)
->toContain('<?xml version="1.0" encoding="UTF-8"?>')
->toContain('CDATA')
->not->toContain(sprintf('⨯ %s', implode(DIRECTORY_SEPARATOR, [
'tests', 'Fixtures', 'with-fixable-issues', 'file.php',
])));
});

it('outputs gitlab format', function () {
[$statusCode, $output] = run('default', [
'path' => base_path('tests/Fixtures/with-fixable-issues'),
'--preset' => 'psr12',
'--format' => 'gitlab',
]);

expect($statusCode)->toBe(1)
->and($output)
->toBeJson()
->toContain('fingerprint')
->not->toContain(sprintf('⨯ %s', implode(DIRECTORY_SEPARATOR, [
'tests', 'Fixtures', 'with-fixable-issues', 'file.php',
])));
});