Skip to content

Commit fe0d822

Browse files
committed
Add OutputFormat class
1 parent 0d2a028 commit fe0d822

File tree

3 files changed

+230
-3
lines changed

3 files changed

+230
-3
lines changed

src/OutputFormat.php

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace CodeLts\CliTools;
6+
7+
use CodeLts\CliTools\ErrorFormatter\CheckstyleErrorFormatter;
8+
use CodeLts\CliTools\ErrorFormatter\ErrorFormatter;
9+
use CodeLts\CliTools\ErrorFormatter\GithubErrorFormatter;
10+
use CodeLts\CliTools\ErrorFormatter\GitlabErrorFormatter;
11+
use CodeLts\CliTools\ErrorFormatter\JsonErrorFormatter;
12+
use CodeLts\CliTools\ErrorFormatter\JunitErrorFormatter;
13+
use CodeLts\CliTools\ErrorFormatter\RawErrorFormatter;
14+
use CodeLts\CliTools\ErrorFormatter\RawTextErrorFormatter;
15+
use CodeLts\CliTools\ErrorFormatter\TableErrorFormatter;
16+
use CodeLts\CliTools\ErrorFormatter\TeamcityErrorFormatter;
17+
use CodeLts\CliTools\File\FuzzyRelativePathHelper;
18+
use CodeLts\CliTools\File\NullRelativePathHelper;
19+
use CodeLts\CliTools\File\RelativePathHelper;
20+
use CodeLts\CliTools\File\SimpleRelativePathHelper;
21+
use Exception;
22+
23+
class OutputFormat
24+
{
25+
public const OUTPUT_FORMAT_RAW = 'raw';
26+
public const OUTPUT_FORMAT_RAW_TEXT = 'rawtext';
27+
public const OUTPUT_FORMAT_JSON = 'json';
28+
public const OUTPUT_FORMAT_JSON_PRETTY = 'prettyJson';
29+
public const OUTPUT_FORMAT_JUNIT = 'junit';
30+
public const OUTPUT_FORMAT_TABLE = 'table';
31+
public const OUTPUT_FORMAT_CHECKSTYLE = 'checkstyle';
32+
public const OUTPUT_FORMAT_GITLAB = 'gitlab';
33+
public const OUTPUT_FORMAT_GITHUB = 'github';
34+
public const OUTPUT_FORMAT_TEAMCITY = 'teamcity';
35+
36+
public const VALID_OUTPUT_FORMATS = [
37+
OutputFormat::OUTPUT_FORMAT_RAW,
38+
OutputFormat::OUTPUT_FORMAT_RAW_TEXT,
39+
OutputFormat::OUTPUT_FORMAT_TABLE,
40+
OutputFormat::OUTPUT_FORMAT_CHECKSTYLE,
41+
OutputFormat::OUTPUT_FORMAT_JSON,
42+
OutputFormat::OUTPUT_FORMAT_JUNIT,
43+
OutputFormat::OUTPUT_FORMAT_JSON_PRETTY,
44+
OutputFormat::OUTPUT_FORMAT_GITLAB,
45+
OutputFormat::OUTPUT_FORMAT_GITHUB,
46+
OutputFormat::OUTPUT_FORMAT_TEAMCITY,
47+
];
48+
49+
/**
50+
* Checks that the format is valid
51+
*
52+
* @param string $outputFormat The format to check
53+
* @return true
54+
*
55+
* @throws Exception
56+
*/
57+
public static function checkOutputFormatIsValid(string $outputFormat): bool
58+
{
59+
if (in_array($outputFormat, OutputFormat::VALID_OUTPUT_FORMATS)) {
60+
return true;
61+
}
62+
throw new Exception(
63+
sprintf(
64+
'Error formatter "%s" not found. Available error formatters are: %s',
65+
$outputFormat,
66+
implode(', ', OutputFormat::VALID_OUTPUT_FORMATS)
67+
)
68+
);
69+
}
70+
71+
/**
72+
* Display to the output the AnalysisResult for the format
73+
*/
74+
public static function displayUserChoiceFormat(
75+
string $outputFormat,
76+
AnalysisResult $analysisResult,
77+
?string $sourceRootDirectory,
78+
Output $output
79+
): void {
80+
$pathHelper = new NullRelativePathHelper();
81+
if ($sourceRootDirectory !== null) {
82+
$pathHelper = new FuzzyRelativePathHelper(
83+
new SimpleRelativePathHelper(
84+
$sourceRootDirectory
85+
),
86+
$sourceRootDirectory
87+
);
88+
}
89+
90+
$formatter = OutputFormat::getFormatterForChoice($outputFormat, $pathHelper);
91+
$formatter->formatErrors(
92+
$analysisResult,
93+
$output
94+
);
95+
}
96+
97+
/**
98+
* get an ErrorFormatter for the given format
99+
*
100+
* @throws Exception if the format is not implemented
101+
*/
102+
public static function getFormatterForChoice(
103+
string $outputFormat,
104+
RelativePathHelper $pathHelper
105+
): ErrorFormatter {
106+
if ($outputFormat === OutputFormat::OUTPUT_FORMAT_RAW) {
107+
return new RawErrorFormatter();
108+
}
109+
110+
if ($outputFormat === OutputFormat::OUTPUT_FORMAT_RAW_TEXT) {
111+
return new RawTextErrorFormatter();
112+
}
113+
114+
if ($outputFormat === OutputFormat::OUTPUT_FORMAT_JSON) {
115+
return new JsonErrorFormatter(false);
116+
}
117+
118+
if ($outputFormat === OutputFormat::OUTPUT_FORMAT_JSON_PRETTY) {
119+
return new JsonErrorFormatter(true);
120+
}
121+
122+
if ($outputFormat === OutputFormat::OUTPUT_FORMAT_JUNIT) {
123+
return new JunitErrorFormatter($pathHelper);
124+
}
125+
126+
if ($outputFormat === OutputFormat::OUTPUT_FORMAT_TABLE) {
127+
return new TableErrorFormatter($pathHelper);
128+
}
129+
130+
if ($outputFormat === OutputFormat::OUTPUT_FORMAT_CHECKSTYLE) {
131+
return new CheckstyleErrorFormatter($pathHelper);
132+
}
133+
134+
if ($outputFormat === OutputFormat::OUTPUT_FORMAT_GITLAB) {
135+
return new GitlabErrorFormatter($pathHelper);
136+
}
137+
138+
if ($outputFormat === OutputFormat::OUTPUT_FORMAT_GITHUB) {
139+
return new GithubErrorFormatter($pathHelper, new TableErrorFormatter($pathHelper));
140+
}
141+
142+
if ($outputFormat === OutputFormat::OUTPUT_FORMAT_TEAMCITY) {
143+
return new TeamcityErrorFormatter($pathHelper);
144+
}
145+
146+
throw new Exception('Not implemented format');
147+
}
148+
}

tests/ErrorFormatterTestCase.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,10 @@ protected function getOutputContent(): string
8686
return $this->rtrimMultiline($contents);
8787
}
8888

89-
protected function getAnalysisResult(int $numFileErrors, int $numGenericErrors): AnalysisResult
89+
protected function getAnalysisResult(int $numFileErrors, int $numGenericErrors, int $numWarnings = 0): AnalysisResult
9090
{
9191
if ($numFileErrors > 4 || $numFileErrors < 0 || $numGenericErrors > 2 || $numGenericErrors < 0) {
92-
throw new \Exception();
92+
throw new \Exception('Test case error');
9393
}
9494

9595
$fileErrors = array_slice([
@@ -104,11 +104,17 @@ protected function getAnalysisResult(int $numFileErrors, int $numGenericErrors):
104104
'second generic error',
105105
], 0, $numGenericErrors);
106106

107+
$warnings = array_slice([
108+
'first warning',
109+
'second 😃 warning',
110+
'3rd warning !',
111+
], 0, $numWarnings);
112+
107113
return new AnalysisResult(
108114
$fileErrors,
109115
$genericErrors,
110116
[],
111-
[]
117+
$warnings
112118
);
113119
}
114120

tests/OutputFormatTest.php

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace CodeLts\CliTools\Tests;
6+
7+
use CodeLts\CliTools\AnalysisResult;
8+
use CodeLts\CliTools\ErrorFormatter\ErrorFormatter;
9+
use CodeLts\CliTools\File\NullRelativePathHelper;
10+
use CodeLts\CliTools\OutputFormat;
11+
use Exception;
12+
13+
class OutputFormatTest extends ErrorFormatterTestCase
14+
{
15+
16+
/**
17+
* @return array[]
18+
*/
19+
public function dataProviderFormatsNames(): array
20+
{
21+
$formats = [];
22+
foreach (OutputFormat::VALID_OUTPUT_FORMATS as $format) {
23+
$formats[] = [$format];
24+
}
25+
return $formats;
26+
}
27+
28+
/**
29+
* @dataProvider dataProviderFormatsNames
30+
*/
31+
public function testValidFormats(string $formatName): void
32+
{
33+
$this->assertTrue(OutputFormat::checkOutputFormatIsValid($formatName));
34+
}
35+
36+
/**
37+
* @dataProvider dataProviderFormatsNames
38+
*/
39+
public function testInValidFormats(string $formatName): void
40+
{
41+
$formatName = 'foo' . $formatName;
42+
$this->expectException(Exception::class);
43+
$this->expectExceptionMessage(
44+
'Error formatter "' . $formatName . '" not found.'
45+
. ' Available error formatters are: raw, rawtext, table, checkstyle, json, junit, prettyJson, gitlab, github, teamcity'
46+
);
47+
$this->assertTrue(OutputFormat::checkOutputFormatIsValid($formatName));
48+
}
49+
50+
/**
51+
* @dataProvider dataProviderFormatsNames
52+
*/
53+
public function testGetFormatterForChoice(string $formatName): void
54+
{
55+
$this->assertInstanceOf(ErrorFormatter::class, OutputFormat::getFormatterForChoice($formatName, new NullRelativePathHelper()));
56+
}
57+
58+
/**
59+
* @dataProvider dataProviderFormatsNames
60+
*/
61+
public function testFormatterForChoice(string $formatName): void
62+
{
63+
OutputFormat::displayUserChoiceFormat(
64+
$formatName,
65+
$this->getAnalysisResult(3, 2, 2),
66+
null,
67+
$this->getOutput()
68+
);
69+
$outputContent = $this->getOutputContent();
70+
$this->assertNotEmpty($outputContent);
71+
}
72+
73+
}

0 commit comments

Comments
 (0)