Skip to content

Commit 040a7a6

Browse files
committed
Add initial integration tests for the Ruleset class
1 parent fb04e08 commit 040a7a6

8 files changed

+553
-0
lines changed

package.xml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,15 @@ http://pear.php.net/dtd/package-2.0.xsd">
239239
<file baseinstalldir="" name="AcceptTest.php" role="test" />
240240
</dir>
241241
</dir>
242+
<dir name="Ruleset">
243+
<file baseinstalldir="" name="RuleInclusionTest.php" role="test" />
244+
<file baseinstalldir="" name="RuleInclusionTest.xml" role="test" />
245+
<file baseinstalldir="" name="RuleInclusionTest-include.xml" role="test" />
246+
<file baseinstalldir="" name="RuleInclusionAbsoluteLinuxTest.php" role="test" />
247+
<file baseinstalldir="" name="RuleInclusionAbsoluteLinuxTest.xml" role="test" />
248+
<file baseinstalldir="" name="RuleInclusionAbsoluteWindowsTest.php" role="test" />
249+
<file baseinstalldir="" name="RuleInclusionAbsoluteWindowsTest.xml" role="test" />
250+
</dir>
242251
<dir name="Tokenizer">
243252
<file baseinstalldir="" name="AnonClassParenthesisOwnerTest.inc" role="test" />
244253
<file baseinstalldir="" name="AnonClassParenthesisOwnerTest.php" role="test" />
@@ -2063,6 +2072,13 @@ http://pear.php.net/dtd/package-2.0.xsd">
20632072
<install as="CodeSniffer/Core/File/IsReferenceTest.inc" name="tests/Core/File/IsReferenceTest.inc" />
20642073
<install as="CodeSniffer/Core/Filters/Filter/AcceptTest.php" name="tests/Core/Filters/Filter/AcceptTest.php" />
20652074
<install as="CodeSniffer/Core/Filters/Filter/AcceptTest.xml" name="tests/Core/Filters/Filter/AcceptTest.xml" />
2075+
<install as="CodeSniffer/Core/Ruleset/RuleInclusionTest.php" name="tests/Core/Ruleset/RuleInclusionTest.php" />
2076+
<install as="CodeSniffer/Core/Ruleset/RuleInclusionTest.xml" name="tests/Core/Ruleset/RuleInclusionTest.xml" />
2077+
<install as="CodeSniffer/Core/Ruleset/RuleInclusionTest-include.xml" name="tests/Core/Ruleset/RuleInclusionTest-include.xml" />
2078+
<install as="CodeSniffer/Core/Ruleset/RuleInclusionAbsoluteLinuxTest.php" name="tests/Core/Ruleset/RuleInclusionAbsoluteLinuxTest.php" />
2079+
<install as="CodeSniffer/Core/Ruleset/RuleInclusionAbsoluteLinuxTest.xml" name="tests/Core/Ruleset/RuleInclusionAbsoluteLinuxTest.xml" />
2080+
<install as="CodeSniffer/Core/Ruleset/RuleInclusionAbsoluteWindowsTest.php" name="tests/Core/Ruleset/RuleInclusionAbsoluteWindowsTest.php" />
2081+
<install as="CodeSniffer/Core/Ruleset/RuleInclusionAbsoluteWindowsTest.xml" name="tests/Core/Ruleset/RuleInclusionAbsoluteWindowsTest.xml" />
20662082
<install as="CodeSniffer/Core/Tokenizer/AnonClassParenthesisOwnerTest.php" name="tests/Core/Tokenizer/AnonClassParenthesisOwnerTest.php" />
20672083
<install as="CodeSniffer/Core/Tokenizer/AnonClassParenthesisOwnerTest.inc" name="tests/Core/Tokenizer/AnonClassParenthesisOwnerTest.inc" />
20682084
<install as="CodeSniffer/Standards/AllSniffs.php" name="tests/Standards/AllSniffs.php" />
@@ -2102,6 +2118,13 @@ http://pear.php.net/dtd/package-2.0.xsd">
21022118
<install as="CodeSniffer/Core/File/IsReferenceTest.inc" name="tests/Core/File/IsReferenceTest.inc" />
21032119
<install as="CodeSniffer/Core/Filters/Filter/AcceptTest.php" name="tests/Core/Filters/Filter/AcceptTest.php" />
21042120
<install as="CodeSniffer/Core/Filters/Filter/AcceptTest.xml" name="tests/Core/Filters/Filter/AcceptTest.xml" />
2121+
<install as="CodeSniffer/Core/Ruleset/RuleInclusionTest.php" name="tests/Core/Ruleset/RuleInclusionTest.php" />
2122+
<install as="CodeSniffer/Core/Ruleset/RuleInclusionTest.xml" name="tests/Core/Ruleset/RuleInclusionTest.xml" />
2123+
<install as="CodeSniffer/Core/Ruleset/RuleInclusionTest-include.xml" name="tests/Core/Ruleset/RuleInclusionTest-include.xml" />
2124+
<install as="CodeSniffer/Core/Ruleset/RuleInclusionAbsoluteLinuxTest.php" name="tests/Core/Ruleset/RuleInclusionAbsoluteLinuxTest.php" />
2125+
<install as="CodeSniffer/Core/Ruleset/RuleInclusionAbsoluteLinuxTest.xml" name="tests/Core/Ruleset/RuleInclusionAbsoluteLinuxTest.xml" />
2126+
<install as="CodeSniffer/Core/Ruleset/RuleInclusionAbsoluteWindowsTest.php" name="tests/Core/Ruleset/RuleInclusionAbsoluteWindowsTest.php" />
2127+
<install as="CodeSniffer/Core/Ruleset/RuleInclusionAbsoluteWindowsTest.xml" name="tests/Core/Ruleset/RuleInclusionAbsoluteWindowsTest.xml" />
21052128
<install as="CodeSniffer/Core/Tokenizer/AnonClassParenthesisOwnerTest.php" name="tests/Core/Tokenizer/AnonClassParenthesisOwnerTest.php" />
21062129
<install as="CodeSniffer/Core/Tokenizer/AnonClassParenthesisOwnerTest.inc" name="tests/Core/Tokenizer/AnonClassParenthesisOwnerTest.inc" />
21072130
<install as="CodeSniffer/Standards/AllSniffs.php" name="tests/Standards/AllSniffs.php" />
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
<?php
2+
/**
3+
* Tests for the \PHP_CodeSniffer\Ruleset class using a Linux-style absolute path to include a sniff.
4+
*
5+
* @author Juliette Reinders Folmer <phpcs_nospam@adviesenzo.nl>
6+
* @copyright 2019 Juliette Reinders Folmer. All rights reserved.
7+
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
8+
*/
9+
10+
namespace PHP_CodeSniffer\Tests\Core\Ruleset;
11+
12+
use PHP_CodeSniffer\Config;
13+
use PHP_CodeSniffer\Ruleset;
14+
use PHPUnit\Framework\TestCase;
15+
16+
class RuleInclusionAbsoluteLinuxTest extends TestCase
17+
{
18+
19+
/**
20+
* The Ruleset object.
21+
*
22+
* @var \PHP_CodeSniffer\Ruleset
23+
*/
24+
protected $ruleset;
25+
26+
/**
27+
* Path to the ruleset file.
28+
*
29+
* @var string
30+
*/
31+
private $standard = '';
32+
33+
/**
34+
* The original content of the ruleset.
35+
*
36+
* @var string
37+
*/
38+
private $contents = '';
39+
40+
41+
/**
42+
* Initialize the config and ruleset objects.
43+
*
44+
* @return void
45+
*/
46+
public function setUp()
47+
{
48+
$this->standard = __DIR__.'/'.basename(__FILE__, '.php').'.xml';
49+
$repoRootDir = dirname(dirname(dirname(__DIR__)));
50+
51+
// On-the-fly adjust the ruleset test file to be able to test sniffs included with absolute paths.
52+
$contents = file_get_contents($this->standard);
53+
$this->contents = $contents;
54+
55+
$newPath = $repoRootDir;
56+
if (DIRECTORY_SEPARATOR === '\\') {
57+
$newPath = str_replace('\\', '/', $repoRootDir);
58+
}
59+
60+
$adjusted = str_replace('%path_slash_forward%', $newPath, $contents);
61+
62+
if (file_put_contents($this->standard, $adjusted) === false) {
63+
$this->markTestSkipped('On the fly ruleset adjustment failed');
64+
}
65+
66+
// Initialize the config and ruleset objects for the test.
67+
$config = new Config(["--standard={$this->standard}"]);
68+
$this->ruleset = new Ruleset($config);
69+
70+
}//end setUp()
71+
72+
73+
/**
74+
* Reset ruleset file.
75+
*
76+
* @return void
77+
*/
78+
public function tearDown()
79+
{
80+
file_put_contents($this->standard, $this->contents);
81+
82+
}//end tearDown()
83+
84+
85+
/**
86+
* Test that sniffs registed with a Linux absolute path are correctly recognized and that
87+
* properties are correctly set for them.
88+
*
89+
* @return void
90+
*/
91+
public function testLinuxStylePathRuleInclusion()
92+
{
93+
// Test that the sniff is correctly registered.
94+
$this->assertObjectHasAttribute('sniffCodes', $this->ruleset);
95+
$this->assertCount(1, $this->ruleset->sniffCodes);
96+
$this->assertArrayHasKey('Generic.Formatting.SpaceAfterNot', $this->ruleset->sniffCodes);
97+
$this->assertSame(
98+
'PHP_CodeSniffer\Standards\Generic\Sniffs\Formatting\SpaceAfterNotSniff',
99+
$this->ruleset->sniffCodes['Generic.Formatting.SpaceAfterNot']
100+
);
101+
102+
// Test that the sniff properties are correctly set.
103+
$this->assertSame(
104+
'10',
105+
$this->ruleset->sniffs['PHP_CodeSniffer\Standards\Generic\Sniffs\Formatting\SpaceAfterNotSniff']->spacing
106+
);
107+
108+
}//end testLinuxStylePathRuleInclusion()
109+
110+
111+
}//end class
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0"?>
2+
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="RuleInclusionAbsoluteLinuxTest" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/squizlabs/PHP_CodeSniffer/master/phpcs.xsd">
3+
4+
<!-- %path_slash_forward% will be replaced on the fly -->
5+
<rule ref="%path_slash_forward%/src/Standards/Generic/Sniffs/Formatting/SpaceAfterNotSniff.php">
6+
<properties>
7+
<property name="spacing" value="10" />
8+
</properties>
9+
</rule>
10+
11+
</ruleset>
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
<?php
2+
/**
3+
* Tests for the \PHP_CodeSniffer\Ruleset class using a Windows-style absolute path to include a sniff.
4+
*
5+
* @author Juliette Reinders Folmer <phpcs_nospam@adviesenzo.nl>
6+
* @copyright 2019 Juliette Reinders Folmer. All rights reserved.
7+
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
8+
*/
9+
10+
namespace PHP_CodeSniffer\Tests\Core\Ruleset;
11+
12+
use PHP_CodeSniffer\Config;
13+
use PHP_CodeSniffer\Ruleset;
14+
use PHPUnit\Framework\TestCase;
15+
16+
class RuleInclusionAbsoluteWindowsTest extends TestCase
17+
{
18+
19+
/**
20+
* The Ruleset object.
21+
*
22+
* @var \PHP_CodeSniffer\Ruleset
23+
*/
24+
protected $ruleset;
25+
26+
/**
27+
* Path to the ruleset file.
28+
*
29+
* @var string
30+
*/
31+
private $standard = '';
32+
33+
/**
34+
* The original content of the ruleset.
35+
*
36+
* @var string
37+
*/
38+
private $contents = '';
39+
40+
41+
/**
42+
* Initialize the config and ruleset objects.
43+
*
44+
* @return void
45+
*/
46+
public function setUp()
47+
{
48+
if (DIRECTORY_SEPARATOR === '/') {
49+
$this->markTestSkipped('Windows specific test');
50+
}
51+
52+
$this->standard = __DIR__.'/'.basename(__FILE__, '.php').'.xml';
53+
$repoRootDir = dirname(dirname(dirname(__DIR__)));
54+
55+
// On-the-fly adjust the ruleset test file to be able to test sniffs included with absolute paths.
56+
$contents = file_get_contents($this->standard);
57+
$this->contents = $contents;
58+
59+
$adjusted = str_replace('%path_slash_back%', $repoRootDir, $contents);
60+
61+
if (file_put_contents($this->standard, $adjusted) === false) {
62+
$this->markTestSkipped('On the fly ruleset adjustment failed');
63+
}
64+
65+
// Initialize the config and ruleset objects for the test.
66+
$config = new Config(["--standard={$this->standard}"]);
67+
$this->ruleset = new Ruleset($config);
68+
69+
}//end setUp()
70+
71+
72+
/**
73+
* Reset ruleset file.
74+
*
75+
* @return void
76+
*/
77+
public function tearDown()
78+
{
79+
if (DIRECTORY_SEPARATOR !== '/') {
80+
file_put_contents($this->standard, $this->contents);
81+
}
82+
83+
}//end tearDown()
84+
85+
86+
/**
87+
* Test that sniffs registed with a Windows absolute path are correctly recognized and that
88+
* properties are correctly set for them.
89+
*
90+
* @return void
91+
*/
92+
public function testWindowsStylePathRuleInclusion()
93+
{
94+
// Test that the sniff is correctly registered.
95+
$this->assertObjectHasAttribute('sniffCodes', $this->ruleset);
96+
$this->assertCount(1, $this->ruleset->sniffCodes);
97+
$this->assertArrayHasKey('Generic.Formatting.SpaceAfterCast', $this->ruleset->sniffCodes);
98+
$this->assertSame(
99+
'PHP_CodeSniffer\Standards\Generic\Sniffs\Formatting\SpaceAfterCastSniff',
100+
$this->ruleset->sniffCodes['Generic.Formatting.SpaceAfterCast']
101+
);
102+
103+
// Test that the sniff property is correctly set.
104+
$this->assertSame(
105+
'10',
106+
$this->ruleset->sniffs['PHP_CodeSniffer\Standards\Generic\Sniffs\Formatting\SpaceAfterCastSniff']->spacing
107+
);
108+
109+
}//end testWindowsStylePathRuleInclusion()
110+
111+
112+
}//end class
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0"?>
2+
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="RuleInclusionAbsoluteWindowsTest" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/squizlabs/PHP_CodeSniffer/master/phpcs.xsd">
3+
4+
<!-- %path_slash_back% will be replaced on the fly -->
5+
<rule ref="%path_slash_back%\src\Standards\Generic\Sniffs\Formatting\SpaceAfterCastSniff.php">
6+
<properties>
7+
<property name="spacing" value="10" />
8+
</properties>
9+
</rule>
10+
11+
</ruleset>
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="RuleInclusionTest-include" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/squizlabs/PHP_CodeSniffer/master/phpcs.xsd">
3+
4+
<rule ref="Generic.Metrics.NestingLevel">
5+
<properties>
6+
<property name="nestingLevel" value="2" />
7+
</properties>
8+
</rule>
9+
10+
</ruleset>

0 commit comments

Comments
 (0)