Skip to content

Commit dc49aaa

Browse files
committed
AC-661: Create phpcs static check for XmlTest
1 parent a25e2ca commit dc49aaa

File tree

3 files changed

+38
-6
lines changed

3 files changed

+38
-6
lines changed

Magento2/Sniffs/Legacy/WidgetXMLSniff.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,22 @@ public function process(File $phpcsFile, $stackPtr)
3838
return;
3939
}
4040

41-
$xml = simplexml_load_string($this->getFormattedXML($phpcsFile));
41+
try{
42+
$xml = simplexml_load_string($this->getFormattedXML($phpcsFile));
43+
} catch (\Exception $e) {
44+
$phpcsFile->addError(
45+
sprintf(
46+
"Couldn't parse contents of '%s', check that they are in valid XML format",
47+
$phpcsFile->getFilename(),
48+
),
49+
$stackPtr,
50+
self::ERROR_CODE_XML
51+
);
52+
return;
53+
}
4254

4355
$foundElements = $xml->xpath('/widgets/*[@type]');
56+
4457
foreach ($foundElements as $element) {
4558
if (!property_exists($element->attributes(), 'type')) {
4659
continue;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<widgets>
9+
<widget
10+
</widgets>
11+

Magento2/Tests/Legacy/WidgetXMLUnitTest.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,19 @@ class WidgetXMLUnitTest extends AbstractSniffUnitTest
1414
*/
1515
public function getErrorList($testFile = '')
1616
{
17-
return [
18-
9 => 1,
19-
12 => 1,
20-
14 => 1,
21-
];
17+
if ($testFile === 'WidgetXMLUnitTest.1.xml') {
18+
return [
19+
9 => 1,
20+
12 => 1,
21+
14 => 1,
22+
];
23+
}
24+
if ($testFile === 'WidgetXMLUnitTest.2.xml') {
25+
return [
26+
1 => 1
27+
];
28+
}
29+
return [];
2230
}
2331

2432
/**

0 commit comments

Comments
 (0)