Skip to content

Commit e66802a

Browse files
committed
Ruleset::processRuleset(): add tests for handling of broken rulesets
1 parent 778f533 commit e66802a

4 files changed

+104
-0
lines changed

tests/Core/Ruleset/ProcessRulesetBrokenRulesetEmptyFileTest.xml

Whitespace-only changes.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0"?>
2+
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="ProcessRulesetBrokenRulesetTest" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/PHPCSStandards/PHP_CodeSniffer/master/phpcs.xsd">
3+
4+
<rule ref="PSR12.Operators">
5+
<properties>
6+
<property name="setforallincategory" value="true" />
7+
<property name="ignoreSpacingBeforeAssignments" value="false">
8+
</rule>
9+
10+
</ruleset>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<?xml version="1.0"?>
2+
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="ProcessRulesetBrokenRulesetTest" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/PHPCSStandards/PHP_CodeSniffer/master/phpcs.xsd">
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
/**
3+
* Test handling of broken ruleset files.
4+
*
5+
* @author Juliette Reinders Folmer <[email protected]>
6+
* @copyright 2024 PHPCSStandards and contributors
7+
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
8+
*/
9+
10+
namespace PHP_CodeSniffer\Tests\Core\Ruleset;
11+
12+
use PHP_CodeSniffer\Ruleset;
13+
use PHP_CodeSniffer\Tests\ConfigDouble;
14+
use PHP_CodeSniffer\Tests\Core\Ruleset\AbstractRulesetTestCase;
15+
16+
/**
17+
* Test handling of broken ruleset files.
18+
*
19+
* Note: these tests need to run in separate processes as otherwise they run into
20+
* some weirdness with the libxml_get_errors()/libxml_clear_errors() functions
21+
* (duplicate error messages).
22+
*
23+
* @runTestsInSeparateProcesses
24+
* @preserveGlobalState disabled
25+
*
26+
* @covers \PHP_CodeSniffer\Ruleset::processRuleset
27+
*/
28+
final class ProcessRulesetBrokenRulesetTest extends AbstractRulesetTestCase
29+
{
30+
31+
32+
/**
33+
* Test displaying an informative error message when an empty XML ruleset file is encountered.
34+
*
35+
* @return void
36+
*/
37+
public function testBrokenRulesetEmptyFile()
38+
{
39+
$standard = __DIR__.'/ProcessRulesetBrokenRulesetEmptyFileTest.xml';
40+
$config = new ConfigDouble(["--standard=$standard"]);
41+
42+
$regex = '`^Ruleset \S+ProcessRulesetBrokenRulesetEmptyFileTest\.xml is not valid\R$`';
43+
$this->expectRuntimeExceptionRegex($regex);
44+
45+
new Ruleset($config);
46+
47+
}//end testBrokenRulesetEmptyFile()
48+
49+
50+
/**
51+
* Test displaying an informative error message for a broken XML ruleset with a single XML error.
52+
*
53+
* @return void
54+
*/
55+
public function testBrokenRulesetSingleError()
56+
{
57+
$standard = __DIR__.'/ProcessRulesetBrokenRulesetSingleErrorTest.xml';
58+
$config = new ConfigDouble(["--standard=$standard"]);
59+
60+
$regex = '`^Ruleset \S+ProcessRulesetBrokenRulesetSingleErrorTest\.xml is not valid\R';
61+
$regex .= '- On line 3, column 1: Premature end of data in tag ruleset line 2\R$`';
62+
63+
$this->expectRuntimeExceptionRegex($regex);
64+
65+
new Ruleset($config);
66+
67+
}//end testBrokenRulesetSingleError()
68+
69+
70+
/**
71+
* Test displaying an informative error message for a broken XML ruleset with multiple XML errors.
72+
*
73+
* @return void
74+
*/
75+
public function testBrokenRulesetMultiError()
76+
{
77+
$standard = __DIR__.'/ProcessRulesetBrokenRulesetMultiErrorTest.xml';
78+
$config = new ConfigDouble(["--standard=$standard"]);
79+
80+
$regex = '`^Ruleset \S+ProcessRulesetBrokenRulesetMultiErrorTest\.xml is not valid\R';
81+
$regex .= '- On line 8, column 12: Opening and ending tag mismatch: property line 7 and rule\R';
82+
$regex .= '- On line 10, column 11: Opening and ending tag mismatch: properties line 5 and ruleset\R';
83+
$regex .= '- On line 11, column 1: Premature end of data in tag rule line 4\R$`';
84+
85+
$this->expectRuntimeExceptionRegex($regex);
86+
87+
new Ruleset($config);
88+
89+
}//end testBrokenRulesetMultiError()
90+
91+
92+
}//end class

0 commit comments

Comments
 (0)