Chris@17
|
1 <?php
|
Chris@17
|
2 /**
|
Chris@17
|
3 * JUnit report for PHP_CodeSniffer.
|
Chris@17
|
4 *
|
Chris@17
|
5 * @author Oleg Lobach <oleg@lobach.info>
|
Chris@17
|
6 * @author Greg Sherwood <gsherwood@squiz.net>
|
Chris@17
|
7 * @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
|
Chris@17
|
8 * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
|
Chris@17
|
9 */
|
Chris@17
|
10
|
Chris@17
|
11 namespace PHP_CodeSniffer\Reports;
|
Chris@17
|
12
|
Chris@17
|
13 use PHP_CodeSniffer\Config;
|
Chris@17
|
14 use PHP_CodeSniffer\Files\File;
|
Chris@17
|
15
|
Chris@17
|
16 class Junit implements Report
|
Chris@17
|
17 {
|
Chris@17
|
18
|
Chris@17
|
19
|
Chris@17
|
20 /**
|
Chris@17
|
21 * Generate a partial report for a single processed file.
|
Chris@17
|
22 *
|
Chris@17
|
23 * Function should return TRUE if it printed or stored data about the file
|
Chris@17
|
24 * and FALSE if it ignored the file. Returning TRUE indicates that the file and
|
Chris@17
|
25 * its data should be counted in the grand totals.
|
Chris@17
|
26 *
|
Chris@17
|
27 * @param array $report Prepared report data.
|
Chris@17
|
28 * @param \PHP_CodeSniffer\File $phpcsFile The file being reported on.
|
Chris@17
|
29 * @param bool $showSources Show sources?
|
Chris@17
|
30 * @param int $width Maximum allowed line width.
|
Chris@17
|
31 *
|
Chris@17
|
32 * @return bool
|
Chris@17
|
33 */
|
Chris@17
|
34 public function generateFileReport($report, File $phpcsFile, $showSources=false, $width=80)
|
Chris@17
|
35 {
|
Chris@17
|
36 $out = new \XMLWriter;
|
Chris@17
|
37 $out->openMemory();
|
Chris@17
|
38 $out->setIndent(true);
|
Chris@17
|
39
|
Chris@17
|
40 $out->startElement('testsuite');
|
Chris@17
|
41 $out->writeAttribute('name', $report['filename']);
|
Chris@17
|
42 $out->writeAttribute('errors', 0);
|
Chris@17
|
43
|
Chris@17
|
44 if (count($report['messages']) === 0) {
|
Chris@17
|
45 $out->writeAttribute('tests', 1);
|
Chris@17
|
46 $out->writeAttribute('failures', 0);
|
Chris@17
|
47
|
Chris@17
|
48 $out->startElement('testcase');
|
Chris@17
|
49 $out->writeAttribute('name', $report['filename']);
|
Chris@17
|
50 $out->endElement();
|
Chris@17
|
51 } else {
|
Chris@17
|
52 $failures = ($report['errors'] + $report['warnings']);
|
Chris@17
|
53 $out->writeAttribute('tests', $failures);
|
Chris@17
|
54 $out->writeAttribute('failures', $failures);
|
Chris@17
|
55
|
Chris@17
|
56 foreach ($report['messages'] as $line => $lineErrors) {
|
Chris@17
|
57 foreach ($lineErrors as $column => $colErrors) {
|
Chris@17
|
58 foreach ($colErrors as $error) {
|
Chris@17
|
59 $out->startElement('testcase');
|
Chris@17
|
60 $out->writeAttribute('name', $error['source'].' at '.$report['filename']." ($line:$column)");
|
Chris@17
|
61
|
Chris@17
|
62 $error['type'] = strtolower($error['type']);
|
Chris@17
|
63 if ($phpcsFile->config->encoding !== 'utf-8') {
|
Chris@17
|
64 $error['message'] = iconv($phpcsFile->config->encoding, 'utf-8', $error['message']);
|
Chris@17
|
65 }
|
Chris@17
|
66
|
Chris@17
|
67 $out->startElement('failure');
|
Chris@17
|
68 $out->writeAttribute('type', $error['type']);
|
Chris@17
|
69 $out->writeAttribute('message', $error['message']);
|
Chris@17
|
70 $out->endElement();
|
Chris@17
|
71
|
Chris@17
|
72 $out->endElement();
|
Chris@17
|
73 }
|
Chris@17
|
74 }
|
Chris@17
|
75 }
|
Chris@17
|
76 }//end if
|
Chris@17
|
77
|
Chris@17
|
78 $out->endElement();
|
Chris@17
|
79 echo $out->flush();
|
Chris@17
|
80 return true;
|
Chris@17
|
81
|
Chris@17
|
82 }//end generateFileReport()
|
Chris@17
|
83
|
Chris@17
|
84
|
Chris@17
|
85 /**
|
Chris@17
|
86 * Prints all violations for processed files, in a proprietary XML format.
|
Chris@17
|
87 *
|
Chris@17
|
88 * @param string $cachedData Any partial report data that was returned from
|
Chris@17
|
89 * generateFileReport during the run.
|
Chris@17
|
90 * @param int $totalFiles Total number of files processed during the run.
|
Chris@17
|
91 * @param int $totalErrors Total number of errors found during the run.
|
Chris@17
|
92 * @param int $totalWarnings Total number of warnings found during the run.
|
Chris@17
|
93 * @param int $totalFixable Total number of problems that can be fixed.
|
Chris@17
|
94 * @param bool $showSources Show sources?
|
Chris@17
|
95 * @param int $width Maximum allowed line width.
|
Chris@17
|
96 * @param bool $interactive Are we running in interactive mode?
|
Chris@17
|
97 * @param bool $toScreen Is the report being printed to screen?
|
Chris@17
|
98 *
|
Chris@17
|
99 * @return void
|
Chris@17
|
100 */
|
Chris@17
|
101 public function generate(
|
Chris@17
|
102 $cachedData,
|
Chris@17
|
103 $totalFiles,
|
Chris@17
|
104 $totalErrors,
|
Chris@17
|
105 $totalWarnings,
|
Chris@17
|
106 $totalFixable,
|
Chris@17
|
107 $showSources=false,
|
Chris@17
|
108 $width=80,
|
Chris@17
|
109 $interactive=false,
|
Chris@17
|
110 $toScreen=true
|
Chris@17
|
111 ) {
|
Chris@17
|
112 // Figure out the total number of tests.
|
Chris@17
|
113 $tests = 0;
|
Chris@17
|
114 $matches = [];
|
Chris@17
|
115 preg_match_all('/tests="([0-9]+)"/', $cachedData, $matches);
|
Chris@17
|
116 if (isset($matches[1]) === true) {
|
Chris@17
|
117 foreach ($matches[1] as $match) {
|
Chris@17
|
118 $tests += $match;
|
Chris@17
|
119 }
|
Chris@17
|
120 }
|
Chris@17
|
121
|
Chris@17
|
122 $failures = ($totalErrors + $totalWarnings);
|
Chris@17
|
123 echo '<?xml version="1.0" encoding="UTF-8"?>'.PHP_EOL;
|
Chris@17
|
124 echo '<testsuites name="PHP_CodeSniffer '.Config::VERSION.'" errors="0" tests="'.$tests.'" failures="'.$failures.'">'.PHP_EOL;
|
Chris@17
|
125 echo $cachedData;
|
Chris@17
|
126 echo '</testsuites>'.PHP_EOL;
|
Chris@17
|
127
|
Chris@17
|
128 }//end generate()
|
Chris@17
|
129
|
Chris@17
|
130
|
Chris@17
|
131 }//end class
|