Skip to content

Commit d27d3b0

Browse files
authored
Merge pull request #679 from PHPCSStandards/feature/common-escapeshellcmd-add-tests
Common::escapeshellcmd(): add tests
2 parents 76b2aa7 + 8bfc8da commit d27d3b0

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
/**
3+
* Tests for the \PHP_CodeSniffer\Util\Common::escapeshellcmd() method.
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\Util\Common;
11+
12+
use PHP_CodeSniffer\Util\Common;
13+
use PHPUnit\Framework\TestCase;
14+
15+
/**
16+
* Tests for the \PHP_CodeSniffer\Util\Common::escapeshellcmd() method.
17+
*
18+
* @covers \PHP_CodeSniffer\Util\Common::escapeshellcmd
19+
* @group Windows
20+
*/
21+
final class EscapeshellcmdTest extends TestCase
22+
{
23+
24+
25+
/**
26+
* Test escaping shell commands.
27+
*
28+
* @param string $command The command provided.
29+
* @param string $expected Expected function output.
30+
* @param string $expectedWin Optional. Expected function output on Windows.
31+
* Only needs to be passed if the output on Windows would be different.
32+
*
33+
* @dataProvider dataEscapeshellcmd
34+
*
35+
* @return void
36+
*/
37+
public function testEscapeshellcmd($command, $expected, $expectedWin=null)
38+
{
39+
if (stripos(PHP_OS, 'WIN') === 0 && empty($expectedWin) === false) {
40+
$expected = $expectedWin;
41+
}
42+
43+
$this->assertSame($expected, Common::escapeshellcmd($command));
44+
45+
}//end testEscapeshellcmd()
46+
47+
48+
/**
49+
* Data provider.
50+
*
51+
* Note: we're only testing the PHPCS functionality, not the PHP native `escapeshellcmd()`
52+
* function (as that's not our responsibility).
53+
*
54+
* @see testEscapeshellcmd()
55+
*
56+
* @return array<string, array<string, string>>
57+
*/
58+
public static function dataEscapeshellcmd()
59+
{
60+
return [
61+
'Command is empty string' => [
62+
'text' => '',
63+
'expected' => '',
64+
],
65+
'Command is simple string' => [
66+
'text' => 'csslint',
67+
'expected' => 'csslint',
68+
],
69+
'Command containing characters which PHP escapes' => [
70+
'text' => '&#;`|*?~<>^()[]{}$\,%!',
71+
'expected' => '\&\#\;\`\|\*\?\~\<\>\^\(\)\[\]\{\}\$\\\\,%!',
72+
'expectedWin' => '^&^#^;^`^|^*^?^~^<^>^^^(^)^[^]^{^}^$^\,^%^!',
73+
],
74+
// @link https://github.com/squizlabs/PHP_CodeSniffer/pull/3214
75+
'Command containing spaces, which can cause problems on Windows' => [
76+
'text' => 'C:\Program Files\nodejs\csslint.cmd',
77+
'expected' => 'C:\\\\Program Files\\\\nodejs\\\\csslint.cmd',
78+
'expectedWin' => 'C:^\Program^ Files^\nodejs^\csslint.cmd',
79+
],
80+
// @link https://github.com/php/doc-en/pull/511
81+
'Command containing spaces with additional arguments' => [
82+
'text' => 'php -f ./~home/path to/file.php',
83+
'expected' => 'php -f ./\~home/path to/file.php',
84+
'expectedWin' => 'php^ -f^ ./^~home/path^ to/file.php',
85+
],
86+
];
87+
88+
}//end dataEscapeshellcmd()
89+
90+
91+
}//end class

0 commit comments

Comments
 (0)