|
| 1 | +<?php |
| 2 | +declare(strict_types=1); |
| 3 | + |
| 4 | +require_once dirname(__DIR__) . '/index.php'; |
| 5 | +require_once __DIR__ . '/helpers/helpers.php'; |
| 6 | + |
| 7 | +use PHPUnit\Framework\TestCase; |
| 8 | +use PhpcsChanged\PhpcsMessages; |
| 9 | +use PhpcsChanged\CheckstyleReporter; |
| 10 | + |
| 11 | +final class CheckstyleReporterTest extends TestCase { |
| 12 | + public function testSingleWarning() { |
| 13 | + $messages = PhpcsMessages::fromArrays([ |
| 14 | + [ |
| 15 | + 'type' => 'WARNING', |
| 16 | + 'severity' => 5, |
| 17 | + 'fixable' => false, |
| 18 | + 'column' => 5, |
| 19 | + 'source' => 'ImportDetection.Imports.RequireImports.Import', |
| 20 | + 'line' => 15, |
| 21 | + 'message' => 'Found unused symbol Foo.', |
| 22 | + ], |
| 23 | + ], 'fileA.php'); |
| 24 | + $expected = <<<EOF |
| 25 | +<?xml version="1.0" encoding="UTF-8"?> |
| 26 | +<checkstyle version="phpcs-changed-2.11.8"> |
| 27 | + <file name="fileA.php"> |
| 28 | + <error line="15" column="5" severity="warning" message="Found unused symbol Foo." source="ImportDetection.Imports.RequireImports.Import"/> |
| 29 | + </file> |
| 30 | +</checkstyle> |
| 31 | +
|
| 32 | +EOF; |
| 33 | + $reporter = new CheckstyleReporter(); |
| 34 | + $result = $reporter->getFormattedMessages($messages, []); |
| 35 | + $this->assertEquals($expected, $result); |
| 36 | + } |
| 37 | + |
| 38 | + public function testSingleError() { |
| 39 | + $messages = PhpcsMessages::fromArrays([ |
| 40 | + [ |
| 41 | + 'type' => 'ERROR', |
| 42 | + 'severity' => 5, |
| 43 | + 'fixable' => false, |
| 44 | + 'column' => 5, |
| 45 | + 'source' => 'ImportDetection.Imports.RequireImports.Import', |
| 46 | + 'line' => 15, |
| 47 | + 'message' => 'Found unused symbol Foo.', |
| 48 | + ], |
| 49 | + ], 'fileA.php'); |
| 50 | + $expected = <<<EOF |
| 51 | +<?xml version="1.0" encoding="UTF-8"?> |
| 52 | +<checkstyle version="phpcs-changed-2.11.8"> |
| 53 | + <file name="fileA.php"> |
| 54 | + <error line="15" column="5" severity="error" message="Found unused symbol Foo." source="ImportDetection.Imports.RequireImports.Import"/> |
| 55 | + </file> |
| 56 | +</checkstyle> |
| 57 | +
|
| 58 | +EOF; |
| 59 | + $reporter = new CheckstyleReporter(); |
| 60 | + $result = $reporter->getFormattedMessages($messages, []); |
| 61 | + $this->assertEquals($expected, $result); |
| 62 | + } |
| 63 | + |
| 64 | + public function testMultipleWarningsWithLongLineNumber() { |
| 65 | + $messages = PhpcsMessages::fromArrays([ |
| 66 | + [ |
| 67 | + 'type' => 'WARNING', |
| 68 | + 'severity' => 5, |
| 69 | + 'fixable' => false, |
| 70 | + 'column' => 5, |
| 71 | + 'source' => 'ImportDetection.Imports.RequireImports.Import', |
| 72 | + 'line' => 133825, |
| 73 | + 'message' => 'Found unused symbol Foo.', |
| 74 | + ], |
| 75 | + [ |
| 76 | + 'type' => 'WARNING', |
| 77 | + 'severity' => 5, |
| 78 | + 'fixable' => false, |
| 79 | + 'column' => 5, |
| 80 | + 'source' => 'ImportDetection.Imports.RequireImports.Import', |
| 81 | + 'line' => 15, |
| 82 | + 'message' => 'Found unused symbol Bar.', |
| 83 | + ], |
| 84 | + ], 'fileA.php'); |
| 85 | + $expected = <<<EOF |
| 86 | +<?xml version="1.0" encoding="UTF-8"?> |
| 87 | +<checkstyle version="phpcs-changed-2.11.8"> |
| 88 | + <file name="fileA.php"> |
| 89 | + <error line="133825" column="5" severity="warning" message="Found unused symbol Foo." source="ImportDetection.Imports.RequireImports.Import"/> |
| 90 | + <error line="15" column="5" severity="warning" message="Found unused symbol Bar." source="ImportDetection.Imports.RequireImports.Import"/> |
| 91 | + </file> |
| 92 | +</checkstyle> |
| 93 | +
|
| 94 | +EOF; |
| 95 | + $reporter = new CheckstyleReporter(); |
| 96 | + $result = $reporter->getFormattedMessages($messages, []); |
| 97 | + $this->assertEquals($expected, $result); |
| 98 | + } |
| 99 | + |
| 100 | + public function testMultipleWarningsErrorsAndFiles() { |
| 101 | + $messagesA = PhpcsMessages::fromArrays([ |
| 102 | + [ |
| 103 | + 'type' => 'ERROR', |
| 104 | + 'severity' => 5, |
| 105 | + 'fixable' => true, |
| 106 | + 'column' => 2, |
| 107 | + 'source' => 'ImportDetection.Imports.RequireImports.Something', |
| 108 | + 'line' => 12, |
| 109 | + 'message' => 'Found unused symbol Faa.', |
| 110 | + ], |
| 111 | + [ |
| 112 | + 'type' => 'ERROR', |
| 113 | + 'severity' => 5, |
| 114 | + 'fixable' => false, |
| 115 | + 'column' => 5, |
| 116 | + 'source' => 'ImportDetection.Imports.RequireImports.Import', |
| 117 | + 'line' => 15, |
| 118 | + 'message' => 'Found unused symbol Foo.', |
| 119 | + ], |
| 120 | + [ |
| 121 | + 'type' => 'WARNING', |
| 122 | + 'severity' => 5, |
| 123 | + 'fixable' => false, |
| 124 | + 'column' => 8, |
| 125 | + 'source' => 'ImportDetection.Imports.RequireImports.Boom', |
| 126 | + 'line' => 18, |
| 127 | + 'message' => 'Found unused symbol Bar.', |
| 128 | + ], |
| 129 | + ], 'fileA.php'); |
| 130 | + $messagesB = PhpcsMessages::fromArrays([ |
| 131 | + [ |
| 132 | + 'type' => 'WARNING', |
| 133 | + 'severity' => 5, |
| 134 | + 'fixable' => false, |
| 135 | + 'column' => 5, |
| 136 | + 'source' => 'ImportDetection.Imports.RequireImports.Zoop', |
| 137 | + 'line' => 30, |
| 138 | + 'message' => 'Found unused symbol Hi.', |
| 139 | + ], |
| 140 | + ], 'fileB.php'); |
| 141 | + $messages = PhpcsMessages::merge([$messagesA, $messagesB]); |
| 142 | + $expected = <<<EOF |
| 143 | +<?xml version="1.0" encoding="UTF-8"?> |
| 144 | +<checkstyle version="phpcs-changed-2.11.8"> |
| 145 | + <file name="fileA.php"> |
| 146 | + <error line="12" column="2" severity="error" message="Found unused symbol Faa." source="ImportDetection.Imports.RequireImports.Something"/> |
| 147 | + <error line="15" column="5" severity="error" message="Found unused symbol Foo." source="ImportDetection.Imports.RequireImports.Import"/> |
| 148 | + <error line="18" column="8" severity="warning" message="Found unused symbol Bar." source="ImportDetection.Imports.RequireImports.Boom"/> |
| 149 | + </file> |
| 150 | + <file name="fileB.php"> |
| 151 | + <error line="30" column="5" severity="warning" message="Found unused symbol Hi." source="ImportDetection.Imports.RequireImports.Zoop"/> |
| 152 | + </file> |
| 153 | +</checkstyle> |
| 154 | +
|
| 155 | +EOF; |
| 156 | + $reporter = new CheckstyleReporter(); |
| 157 | + $result = $reporter->getFormattedMessages($messages, ['s' => 1]); |
| 158 | + $this->assertEquals($expected, $result); |
| 159 | + } |
| 160 | + |
| 161 | + public function testNoWarnings() { |
| 162 | + $messages = PhpcsMessages::fromArrays([]); |
| 163 | + $expected = <<<EOF |
| 164 | +<?xml version="1.0" encoding="UTF-8"?> |
| 165 | +<checkstyle version="phpcs-changed-2.11.8"> |
| 166 | +</checkstyle> |
| 167 | +
|
| 168 | +EOF; |
| 169 | + $reporter = new CheckstyleReporter(); |
| 170 | + $result = $reporter->getFormattedMessages($messages, []); |
| 171 | + $this->assertEquals($expected, $result); |
| 172 | + } |
| 173 | + |
| 174 | + public function testSingleWarningWithNoFilename() { |
| 175 | + $messages = PhpcsMessages::fromArrays([ |
| 176 | + [ |
| 177 | + 'type' => 'WARNING', |
| 178 | + 'severity' => 5, |
| 179 | + 'fixable' => false, |
| 180 | + 'column' => 5, |
| 181 | + 'source' => 'ImportDetection.Imports.RequireImports.Import', |
| 182 | + 'line' => 15, |
| 183 | + 'message' => 'Found unused symbol Foo.', |
| 184 | + ], |
| 185 | + ]); |
| 186 | + $expected = <<<EOF |
| 187 | +<?xml version="1.0" encoding="UTF-8"?> |
| 188 | +<checkstyle version="phpcs-changed-2.11.8"> |
| 189 | + <file name="STDIN"> |
| 190 | + <error line="15" column="5" severity="warning" message="Found unused symbol Foo." source="ImportDetection.Imports.RequireImports.Import"/> |
| 191 | + </file> |
| 192 | +</checkstyle> |
| 193 | +
|
| 194 | +EOF; |
| 195 | + $reporter = new CheckstyleReporter(); |
| 196 | + $result = $reporter->getFormattedMessages($messages, []); |
| 197 | + $this->assertEquals($expected, $result); |
| 198 | + } |
| 199 | + |
| 200 | + public function testXmlEscaping() { |
| 201 | + $messages = PhpcsMessages::fromArrays([ |
| 202 | + [ |
| 203 | + 'type' => 'ERROR', |
| 204 | + 'severity' => 5, |
| 205 | + 'fixable' => false, |
| 206 | + 'column' => 5, |
| 207 | + 'source' => 'Test.Source<>&"', |
| 208 | + 'line' => 15, |
| 209 | + 'message' => 'Message with <xml> & "quotes".', |
| 210 | + ], |
| 211 | + ], 'fileA.php'); |
| 212 | + $expected = <<<EOF |
| 213 | +<?xml version="1.0" encoding="UTF-8"?> |
| 214 | +<checkstyle version="phpcs-changed-2.11.8"> |
| 215 | + <file name="fileA.php"> |
| 216 | + <error line="15" column="5" severity="error" message="Message with <xml> & "quotes"." source="Test.Source<>&""/> |
| 217 | + </file> |
| 218 | +</checkstyle> |
| 219 | +
|
| 220 | +EOF; |
| 221 | + $reporter = new CheckstyleReporter(); |
| 222 | + $result = $reporter->getFormattedMessages($messages, []); |
| 223 | + $this->assertEquals($expected, $result); |
| 224 | + } |
| 225 | + |
| 226 | + public function testGetExitCodeWithMessages() { |
| 227 | + $messages = PhpcsMessages::fromArrays([ |
| 228 | + [ |
| 229 | + 'type' => 'WARNING', |
| 230 | + 'severity' => 5, |
| 231 | + 'fixable' => false, |
| 232 | + 'column' => 5, |
| 233 | + 'source' => 'ImportDetection.Imports.RequireImports.Import', |
| 234 | + 'line' => 15, |
| 235 | + 'message' => 'Found unused symbol Foo.', |
| 236 | + ], |
| 237 | + ], 'fileA.php'); |
| 238 | + $reporter = new CheckstyleReporter(); |
| 239 | + $this->assertEquals(1, $reporter->getExitCode($messages)); |
| 240 | + } |
| 241 | + |
| 242 | + public function testGetExitCodeWithNoMessages() { |
| 243 | + $messages = PhpcsMessages::fromArrays([], 'fileA.php'); |
| 244 | + $reporter = new CheckstyleReporter(); |
| 245 | + $this->assertEquals(0, $reporter->getExitCode($messages)); |
| 246 | + } |
| 247 | +} |
0 commit comments