Skip to content

Commit 4b76676

Browse files
committed
Add a feedback output to the end of the script execution
1 parent 4920a3d commit 4b76676

File tree

3 files changed

+36
-6
lines changed

3 files changed

+36
-6
lines changed

Scripts/DocCodeExamples/Check.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,8 @@ public function run(): int
133133
{
134134
$this->writer->toStderr($this->config->getVersion());
135135

136-
$exitCode = 0;
136+
$exitCode = 0;
137+
$validationFailedCount = 0;
137138

138139
if (empty($this->xmlFiles)) {
139140
$this->writer->toStderr('ERROR: No XML documentation files found.');
@@ -150,10 +151,26 @@ public function run(): int
150151
$validationSuccessful = $xmlDocValidator->validate();
151152

152153
if ($validationSuccessful === false) {
154+
$validationFailedCount++;
153155
$exitCode = 1;
154156
}
155157
}
156158

159+
$xmlFilesCount = \count($this->xmlFiles);
160+
$feedback = "Checked {$xmlFilesCount} XML documentation files.";
161+
162+
if ($xmlFilesCount === 1) {
163+
$feedback = 'Checked 1 XML documentation file.';
164+
}
165+
166+
if ($exitCode === 0) {
167+
$feedback .= ' All code examples are valid.' . PHP_EOL;
168+
$this->writer->toStdout($feedback);
169+
} else {
170+
$feedback .= " Found incorrect code examples in {$validationFailedCount}." . PHP_EOL;
171+
$this->writer->toStdout($feedback);
172+
}
173+
157174
return $exitCode;
158175
}
159176

Tests/DocCodeExamples/CheckTest.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -239,15 +239,15 @@ public static function dataRunWithoutMockingCheckClass(): array
239239
\realpath(self::FIXTURE_DIR . 'CheckCodeExamplesStandard/Docs/Examples/CorrectExamplesStandard.xml'),
240240
],
241241
'expectedExitCode' => 0,
242-
'expectedStdout' => '',
242+
'expectedStdout' => 'Checked 1 XML documentation file. All code examples are valid.' . PHP_EOL,
243243
'expectedStderr' => '',
244244
],
245245
'invalid XML file' => [
246246
'xmlDocs' => [
247247
\realpath(self::FIXTURE_DIR . 'CheckCodeExamplesStandard/Docs/Examples/IncorrectInvalidExampleStandard.xml'),
248248
],
249249
'expectedExitCode' => 1,
250-
'expectedStdout' => '',
250+
'expectedStdout' => 'Checked 1 XML documentation file. Found incorrect code examples in 1.' . PHP_EOL,
251251
'expectedStderr' => 'Errors found while processing .*?[/\\\\]CheckCodeExamplesStandard[/\\\\]Docs[/\\\\]Examples[/\\\\]IncorrectInvalidExampleStandard\.xml\R\s*\R\s*ERROR: Code block is invalid and PHPCS should have returned an error message, but instead it returned nothing\.\R\s*Code block title: "Invalid: invalid code examples\."\R\s*Code block content: "function sniffValidationWillPass\(\) \{\}"\R\s*\R',
252252
],
253253
];
@@ -261,10 +261,11 @@ public static function dataRunWithoutMockingCheckClass(): array
261261
* @param array<string> $xmlDocs XML doc files to process.
262262
* @param array<bool> $validationResults Validation results for each doc file.
263263
* @param int $expectedExitCode Expected exit code from the run() method.
264+
* @param string $expectedStdout Expected stdout output.
264265
*
265266
* @return void
266267
*/
267-
public function testRun(array $xmlDocs, array $validationResults, int $expectedExitCode)
268+
public function testRun(array $xmlDocs, array $validationResults, int $expectedExitCode, string $expectedStdout)
268269
{
269270
$check = $this->createCompatibleMockBuilderWithMethods(Check::class, ['getXmlDocValidator'])
270271
->disableOriginalConstructor()
@@ -292,6 +293,7 @@ public function testRun(array $xmlDocs, array $validationResults, int $expectedE
292293

293294
$exitCode = $check->run();
294295
$this->assertSame($expectedExitCode, $exitCode);
296+
$this->assertSame($expectedStdout, $this->writer->getStdout());
295297
}
296298

297299
/**
@@ -306,6 +308,7 @@ public static function dataRun(): array
306308
'xmlDocs' => [],
307309
'validationResults' => [],
308310
'expectedExitCode' => 1,
311+
'expectedStdout' => '',
309312
],
310313
'all valid docs' => [
311314
'xmlDocs' => [
@@ -314,6 +317,7 @@ public static function dataRun(): array
314317
],
315318
'validationResults' => [true, true],
316319
'expectedExitCode' => 0,
320+
'expectedStdout' => 'Checked 2 XML documentation files. All code examples are valid.' . PHP_EOL,
317321
],
318322
'one invalid and one valid' => [
319323
'xmlDocs' => [
@@ -322,6 +326,7 @@ public static function dataRun(): array
322326
],
323327
'validationResults' => [true, false],
324328
'expectedExitCode' => 1,
329+
'expectedStdout' => 'Checked 2 XML documentation files. Found incorrect code examples in 1.' . PHP_EOL,
325330
],
326331
'all invalid docs' => [
327332
'xmlDocs' => [
@@ -330,20 +335,23 @@ public static function dataRun(): array
330335
],
331336
'validationResults' => [false, false],
332337
'expectedExitCode' => 1,
338+
'expectedStdout' => 'Checked 2 XML documentation files. Found incorrect code examples in 2.' . PHP_EOL,
333339
],
334340
'single doc valid' => [
335341
'xmlDocs' => [
336342
'valid1.xml',
337343
],
338344
'validationResults' => [true],
339345
'expectedExitCode' => 0,
346+
'expectedStdout' => 'Checked 1 XML documentation file. All code examples are valid.' . PHP_EOL,
340347
],
341348
'single doc invalid' => [
342349
'xmlDocs' => [
343350
'invalid1.xml',
344351
],
345352
'validationResults' => [false],
346353
'expectedExitCode' => 1,
354+
'expectedStdout' => 'Checked 1 XML documentation file. Found incorrect code examples in 1.' . PHP_EOL,
347355
],
348356
];
349357
}

Tests/DocCodeExamples/EndToEndTest.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,14 @@ public static function tearDownFixturesAfterClass()
8787
*
8888
* @param string $cliArgs The CLI arguments to pass to the script.
8989
* @param int $expectedExitCode The expected exit code of the script.
90+
* @param string $expectedStdout The expected stdout.
9091
* @param string $expectedStderr The expected stderr.
9192
*
9293
* @dataProvider dataScriptBasicExecution
9394
*
9495
* @return void
9596
*/
96-
public function testScriptBasicExecution(string $cliArgs, int $expectedExitCode, string $expectedStderr)
97+
public function testScriptBasicExecution(string $cliArgs, int $expectedExitCode, string $expectedStdout, string $expectedStderr)
9798
{
9899
$scriptPath = self::maybeConvertDirectorySeparators(self::SCRIPT_PATH);
99100
$command = \sprintf(
@@ -106,7 +107,7 @@ public function testScriptBasicExecution(string $cliArgs, int $expectedExitCode,
106107

107108
$this->assertSame($expectedExitCode, $result['exitcode']);
108109

109-
$this->assertEmpty($result['stdout']);
110+
$this->assertSame($expectedStdout, $result['stdout']);
110111

111112
if (empty($expectedStderr)) {
112113
$this->assertStderrContainsOnlyScriptHeader($result['stderr']);
@@ -138,21 +139,25 @@ public static function dataScriptBasicExecution(): array
138139
'Valid XML file' => [
139140
'cliArgs' => 'Tests/Fixtures/DocCodeExamples/CheckCodeExamplesStandard/Docs/Examples/CorrectExamplesStandard.xml',
140141
'expectedExitCode' => 0,
142+
'expectedStdout' => 'Checked 1 XML documentation file. All code examples are valid.' . PHP_EOL,
141143
'expectedStderr' => '',
142144
],
143145
'Directory with valid XML files (invalid files ignored via --ignore-sniffs)' => [
144146
'cliArgs' => 'Tests/Fixtures/DocCodeExamples/CheckCodeExamplesStandard/ --ignore-sniffs=CheckCodeExamplesStandard.Constructor.TestXmlDocValidatorConstructor,CheckCodeExamplesStandard.Examples.IncorrectInvalidExample,CheckCodeExamplesStandard.Examples.IncorrectValidExample,CheckCodeExamplesStandard.Examples.SyntaxErrorExample',
145147
'expectedExitCode' => 0,
148+
'expectedStdout' => 'Checked 6 XML documentation files. All code examples are valid.' . PHP_EOL,
146149
'expectedStderr' => '',
147150
],
148151
'Invalid XML file' => [
149152
'cliArgs' => 'Tests/Fixtures/DocCodeExamples/CheckCodeExamplesStandard/Docs/Examples/IncorrectValidExampleStandard.xml',
150153
'expectedExitCode' => 1,
154+
'expectedStdout' => 'Checked 1 XML documentation file. Found incorrect code examples in 1.' . PHP_EOL,
151155
'expectedStderr' => '`Errors found while processing .*?Tests/Fixtures/DocCodeExamples/CheckCodeExamplesStandard/Docs/Examples/IncorrectValidExampleStandard\.xml`',
152156
],
153157
'Directory with valid and invalid XML files (empty file ignored via --ignore-sniffs)' => [
154158
'cliArgs' => 'Tests/Fixtures/DocCodeExamples/CheckCodeExamplesStandard/ --ignore-sniffs=CheckCodeExamplesStandard.Constructor.TestXmlDocValidatorConstructor',
155159
'expectedExitCode' => 1,
160+
'expectedStdout' => 'Checked 6 XML documentation files. Found incorrect code examples in 3.' . PHP_EOL,
156161
'expectedStderr' => '`Errors found while processing .*?Tests/Fixtures/DocCodeExamples/CheckCodeExamplesStandard/Docs/Examples/IncorrectInvalidExampleStandard\.xml`',
157162
],
158163
];

0 commit comments

Comments
 (0)