Skip to content

Commit 6eba9fc

Browse files
authored
Merge af41534 into 9feb4e5
2 parents 9feb4e5 + af41534 commit 6eba9fc

File tree

5 files changed

+238
-103
lines changed

5 files changed

+238
-103
lines changed

tests/Core/Generators/GeneratorTest.php

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
namespace PHP_CodeSniffer\Tests\Core\Generators;
1010

1111
use PHP_CodeSniffer\Ruleset;
12-
use PHP_CodeSniffer\Runner;
1312
use PHP_CodeSniffer\Tests\ConfigDouble;
1413
use PHP_CodeSniffer\Tests\Core\Generators\Fixtures\MockGenerator;
1514
use PHPUnit\Framework\TestCase;
@@ -225,54 +224,4 @@ public function testGetTitleFallbackToFilename()
225224
}//end testGetTitleFallbackToFilename()
226225

227226

228-
/**
229-
* Test that the documentation for each standard passed on the command-line is shown separately.
230-
*
231-
* @covers \PHP_CodeSniffer\Runner::runPHPCS
232-
*
233-
* @return void
234-
*/
235-
public function testGeneratorWillShowEachStandardSeparately()
236-
{
237-
if (PHP_CODESNIFFER_CBF === true) {
238-
$this->markTestSkipped('This test needs CS mode to run');
239-
}
240-
241-
$standard = __DIR__.'/OneDocTest.xml';
242-
$_SERVER['argv'] = [
243-
'phpcs',
244-
'--generator=Text',
245-
"--standard=$standard,PSR1",
246-
'--report-width=80',
247-
];
248-
249-
$regex = '`^
250-
\R* # Optional blank line at the start.
251-
(?:
252-
(?P<delimiter>-++\R) # Line with dashes.
253-
\|[ ]GENERATORTEST[ ]CODING[ ]STANDARD:[ ][^\|]+\|\R # Doc title line with prefix expected for first standard.
254-
(?P>delimiter) # Line with dashes.
255-
\R(?:[^\r\n]+\R)+\R{2} # Standard description.
256-
) # Only expect this group once.
257-
(?:
258-
(?P>delimiter) # Line with dashes.
259-
\|[ ]PSR1[ ]CODING[ ]STANDARD:[ ][^\|]+\|\R # Doc title line with prefix expected for second standard.
260-
(?P>delimiter) # Line with dashes.
261-
\R(?:[^\r\n]+\R)+\R # Standard description.
262-
(?:
263-
-+[ ]CODE[ ]COMPARISON[ ]-+\R # Code Comparison starter line with dashes.
264-
(?:(?:[^\r\n]+\R)+(?P>delimiter)){2} # Arbitrary text followed by a delimiter line.
265-
)* # Code comparison is optional and can exist multiple times.
266-
\R+
267-
){3,} # This complete group should occur at least three times.
268-
`x';
269-
270-
$this->expectOutputRegex($regex);
271-
272-
$runner = new Runner();
273-
$runner->runPHPCS();
274-
275-
}//end testGeneratorWillShowEachStandardSeparately()
276-
277-
278227
}//end class

tests/Core/Ruleset/ExplainTest.php

Lines changed: 0 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
namespace PHP_CodeSniffer\Tests\Core\Ruleset;
1111

1212
use PHP_CodeSniffer\Ruleset;
13-
use PHP_CodeSniffer\Runner;
1413
use PHP_CodeSniffer\Tests\ConfigDouble;
1514
use PHPUnit\Framework\TestCase;
1615

@@ -210,55 +209,4 @@ public function testExplainWithDeprecatedSniffs()
210209
}//end testExplainWithDeprecatedSniffs()
211210

212211

213-
/**
214-
* Test that each standard passed on the command-line is explained separately.
215-
*
216-
* @covers \PHP_CodeSniffer\Runner::runPHPCS
217-
*
218-
* @return void
219-
*/
220-
public function testExplainWillExplainEachStandardSeparately()
221-
{
222-
if (PHP_CODESNIFFER_CBF === true) {
223-
$this->markTestSkipped('This test needs CS mode to run');
224-
}
225-
226-
$standard = __DIR__.'/ExplainSingleSniffTest.xml';
227-
$_SERVER['argv'] = [
228-
'phpcs',
229-
'-e',
230-
"--standard=PSR1,$standard",
231-
'--report-width=80',
232-
];
233-
234-
$expected = PHP_EOL;
235-
$expected .= 'The PSR1 standard contains 8 sniffs'.PHP_EOL.PHP_EOL;
236-
$expected .= 'Generic (4 sniffs)'.PHP_EOL;
237-
$expected .= '------------------'.PHP_EOL;
238-
$expected .= ' Generic.Files.ByteOrderMark'.PHP_EOL;
239-
$expected .= ' Generic.NamingConventions.UpperCaseConstantName'.PHP_EOL;
240-
$expected .= ' Generic.PHP.DisallowAlternativePHPTags'.PHP_EOL;
241-
$expected .= ' Generic.PHP.DisallowShortOpenTag'.PHP_EOL.PHP_EOL;
242-
$expected .= 'PSR1 (3 sniffs)'.PHP_EOL;
243-
$expected .= '---------------'.PHP_EOL;
244-
$expected .= ' PSR1.Classes.ClassDeclaration'.PHP_EOL;
245-
$expected .= ' PSR1.Files.SideEffects'.PHP_EOL;
246-
$expected .= ' PSR1.Methods.CamelCapsMethodName'.PHP_EOL.PHP_EOL;
247-
$expected .= 'Squiz (1 sniff)'.PHP_EOL;
248-
$expected .= '---------------'.PHP_EOL;
249-
$expected .= ' Squiz.Classes.ValidClassName'.PHP_EOL.PHP_EOL;
250-
251-
$expected .= 'The ExplainSingleSniffTest standard contains 1 sniff'.PHP_EOL.PHP_EOL;
252-
$expected .= 'Squiz (1 sniff)'.PHP_EOL;
253-
$expected .= '---------------'.PHP_EOL;
254-
$expected .= ' Squiz.Scope.MethodScope'.PHP_EOL;
255-
256-
$this->expectOutputString($expected);
257-
258-
$runner = new Runner();
259-
$runner->runPHPCS();
260-
261-
}//end testExplainWillExplainEachStandardSeparately()
262-
263-
264212
}//end class
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php
2+
/**
3+
* Base class to use for tests invoking the Runner class.
4+
*
5+
* As those tests will use the _real_ Config class instead of the ConfigDouble, we need to ensure
6+
* this doesn't negatively impact other tests, what with the Config using static properties.
7+
*
8+
* @copyright 2025 PHPCSStandards and contributors
9+
* @license http://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
10+
*/
11+
12+
namespace PHP_CodeSniffer\Tests\Core\Runner;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use ReflectionProperty;
16+
17+
abstract class AbstractRunnerTestCase extends TestCase
18+
{
19+
20+
21+
/**
22+
* Set static properties in the Config class to prevent tests influencing each other.
23+
*
24+
* @before
25+
*
26+
* @return void
27+
*/
28+
public function setConfigStatics()
29+
{
30+
// Set to the property's default value to clear out potentially set values from other tests.
31+
self::setStaticConfigProperty('overriddenDefaults', []);
32+
self::setStaticConfigProperty('executablePaths', []);
33+
34+
// Set to values which prevent the test-runner user's `CodeSniffer.conf` file
35+
// from being read and influencing the tests.
36+
self::setStaticConfigProperty('configData', []);
37+
self::setStaticConfigProperty('configDataFile', '');
38+
39+
}//end setConfigStatics()
40+
41+
42+
/**
43+
* Clean up after each finished test.
44+
*
45+
* @after
46+
*
47+
* @return void
48+
*/
49+
public function clearArgv()
50+
{
51+
$_SERVER['argv'] = [];
52+
53+
}//end clearArgv()
54+
55+
56+
/**
57+
* Reset the static properties in the Config class to their true defaults to prevent this class
58+
* from influencing other tests.
59+
*
60+
* @afterClass
61+
*
62+
* @return void
63+
*/
64+
public static function reset()
65+
{
66+
self::setStaticConfigProperty('overriddenDefaults', []);
67+
self::setStaticConfigProperty('executablePaths', []);
68+
self::setStaticConfigProperty('configData', null);
69+
self::setStaticConfigProperty('configDataFile', null);
70+
$_SERVER['argv'] = [];
71+
72+
}//end reset()
73+
74+
75+
/**
76+
* Helper function to set a static property on the Config class.
77+
*
78+
* @param string $name The name of the property to set.
79+
* @param mixed $value The value to set the property to.
80+
*
81+
* @return void
82+
*/
83+
public static function setStaticConfigProperty($name, $value)
84+
{
85+
$property = new ReflectionProperty('PHP_CodeSniffer\Config', $name);
86+
$property->setAccessible(true);
87+
$property->setValue(null, $value);
88+
$property->setAccessible(false);
89+
90+
}//end setStaticConfigProperty()
91+
92+
93+
}//end class
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
/**
3+
* Tests the wiring in of the explain functionality in the Runner class.
4+
*
5+
* @author Juliette Reinders Folmer <phpcs_nospam@adviesenzo.nl>
6+
* @copyright 2023 Juliette Reinders Folmer. All rights reserved.
7+
* @license http://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
8+
*/
9+
10+
namespace PHP_CodeSniffer\Tests\Core\Runner;
11+
12+
use PHP_CodeSniffer\Runner;
13+
use PHP_CodeSniffer\Tests\Core\Runner\AbstractRunnerTestCase;
14+
15+
/**
16+
* Tests the wiring in of the explain functionality in the Runner class.
17+
*
18+
* @covers \PHP_CodeSniffer\Runner::runPHPCS
19+
*/
20+
final class RunPHPCSExplainTest extends AbstractRunnerTestCase
21+
{
22+
23+
24+
/**
25+
* Test that each standard passed on the command-line is explained separately.
26+
*
27+
* @return void
28+
*/
29+
public function testExplainWillExplainEachStandardSeparately()
30+
{
31+
if (PHP_CODESNIFFER_CBF === true) {
32+
$this->markTestSkipped('This test needs CS mode to run');
33+
}
34+
35+
$standard = dirname(__DIR__).'/Ruleset/ExplainSingleSniffTest.xml';
36+
$_SERVER['argv'] = [
37+
'phpcs',
38+
'-e',
39+
"--standard=PSR1,$standard",
40+
'--report-width=80',
41+
];
42+
43+
$expected = PHP_EOL;
44+
$expected .= 'The PSR1 standard contains 8 sniffs'.PHP_EOL.PHP_EOL;
45+
$expected .= 'Generic (4 sniffs)'.PHP_EOL;
46+
$expected .= '------------------'.PHP_EOL;
47+
$expected .= ' Generic.Files.ByteOrderMark'.PHP_EOL;
48+
$expected .= ' Generic.NamingConventions.UpperCaseConstantName'.PHP_EOL;
49+
$expected .= ' Generic.PHP.DisallowAlternativePHPTags'.PHP_EOL;
50+
$expected .= ' Generic.PHP.DisallowShortOpenTag'.PHP_EOL.PHP_EOL;
51+
$expected .= 'PSR1 (3 sniffs)'.PHP_EOL;
52+
$expected .= '---------------'.PHP_EOL;
53+
$expected .= ' PSR1.Classes.ClassDeclaration'.PHP_EOL;
54+
$expected .= ' PSR1.Files.SideEffects'.PHP_EOL;
55+
$expected .= ' PSR1.Methods.CamelCapsMethodName'.PHP_EOL.PHP_EOL;
56+
$expected .= 'Squiz (1 sniff)'.PHP_EOL;
57+
$expected .= '---------------'.PHP_EOL;
58+
$expected .= ' Squiz.Classes.ValidClassName'.PHP_EOL.PHP_EOL;
59+
60+
$expected .= 'The ExplainSingleSniffTest standard contains 1 sniff'.PHP_EOL.PHP_EOL;
61+
$expected .= 'Squiz (1 sniff)'.PHP_EOL;
62+
$expected .= '---------------'.PHP_EOL;
63+
$expected .= ' Squiz.Scope.MethodScope'.PHP_EOL;
64+
65+
$this->expectOutputString($expected);
66+
67+
$runner = new Runner();
68+
$runner->runPHPCS();
69+
70+
}//end testExplainWillExplainEachStandardSeparately()
71+
72+
73+
}//end class
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
/**
3+
* Tests the wiring in of the Generator functionality in the Runner class.
4+
*
5+
* @copyright 2024 PHPCSStandards and contributors
6+
* @license http://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
7+
*/
8+
9+
namespace PHP_CodeSniffer\Tests\Core\Runner;
10+
11+
use PHP_CodeSniffer\Runner;
12+
use PHP_CodeSniffer\Tests\Core\Runner\AbstractRunnerTestCase;
13+
14+
/**
15+
* Tests the wiring in of the Generator functionality in the Runner class.
16+
*
17+
* @covers \PHP_CodeSniffer\Runner::runPHPCS
18+
* @group Windows
19+
*/
20+
final class RunPHPCSGeneratorTest extends AbstractRunnerTestCase
21+
{
22+
23+
24+
/**
25+
* Test that the documentation for each standard passed on the command-line is shown separately.
26+
*
27+
* @return void
28+
*/
29+
public function testGeneratorWillShowEachStandardSeparately()
30+
{
31+
if (PHP_CODESNIFFER_CBF === true) {
32+
$this->markTestSkipped('This test needs CS mode to run');
33+
}
34+
35+
$standard = dirname(__DIR__).'/Generators/OneDocTest.xml';
36+
$_SERVER['argv'] = [
37+
'phpcs',
38+
'--generator=Text',
39+
"--standard=$standard,PSR1",
40+
'--report-width=80',
41+
];
42+
43+
$regex = '`^
44+
\R* # Optional blank line at the start.
45+
(?:
46+
(?P<delimiter>-++\R) # Line with dashes.
47+
\|[ ]GENERATORTEST[ ]CODING[ ]STANDARD:[ ][^\|]+\|\R # Doc title line with prefix expected for first standard.
48+
(?P>delimiter) # Line with dashes.
49+
\R(?:[^\r\n]+\R)+\R{2} # Standard description.
50+
) # Only expect this group once.
51+
(?:
52+
(?P>delimiter) # Line with dashes.
53+
\|[ ]PSR1[ ]CODING[ ]STANDARD:[ ][^\|]+\|\R # Doc title line with prefix expected for second standard.
54+
(?P>delimiter) # Line with dashes.
55+
\R(?:[^\r\n]+\R)+\R # Standard description.
56+
(?:
57+
-+[ ]CODE[ ]COMPARISON[ ]-+\R # Code Comparison starter line with dashes.
58+
(?:(?:[^\r\n]+\R)+(?P>delimiter)){2} # Arbitrary text followed by a delimiter line.
59+
)* # Code comparison is optional and can exist multiple times.
60+
\R+
61+
){3,} # This complete group should occur at least three times.
62+
`x';
63+
64+
$this->expectOutputRegex($regex);
65+
66+
$runner = new Runner();
67+
$runner->runPHPCS();
68+
69+
}//end testGeneratorWillShowEachStandardSeparately()
70+
71+
72+
}//end class

0 commit comments

Comments
 (0)