File: EscapeshellcmdTest.php

package info (click to toggle)
php-codesniffer 3.11.2-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 13,772 kB
  • sloc: php: 84,771; pascal: 10,061; xml: 6,832; javascript: 2,096; sh: 11; makefile: 4
file content (91 lines) | stat: -rw-r--r-- 3,237 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php
/**
 * Tests for the \PHP_CodeSniffer\Util\Common::escapeshellcmd() method.
 *
 * @author    Juliette Reinders Folmer <phpcs_nospam@adviesenzo.nl>
 * @copyright 2024 PHPCSStandards and contributors
 * @license   https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
 */

namespace PHP_CodeSniffer\Tests\Core\Util\Common;

use PHP_CodeSniffer\Util\Common;
use PHPUnit\Framework\TestCase;

/**
 * Tests for the \PHP_CodeSniffer\Util\Common::escapeshellcmd() method.
 *
 * @covers \PHP_CodeSniffer\Util\Common::escapeshellcmd
 * @group  Windows
 */
final class EscapeshellcmdTest extends TestCase
{


    /**
     * Test escaping shell commands.
     *
     * @param string $command     The command provided.
     * @param string $expected    Expected function output.
     * @param string $expectedWin Optional. Expected function output on Windows.
     *                            Only needs to be passed if the output on Windows would be different.
     *
     * @dataProvider dataEscapeshellcmd
     *
     * @return void
     */
    public function testEscapeshellcmd($command, $expected, $expectedWin=null)
    {
        if (stripos(PHP_OS, 'WIN') === 0 && empty($expectedWin) === false) {
            $expected = $expectedWin;
        }

        $this->assertSame($expected, Common::escapeshellcmd($command));

    }//end testEscapeshellcmd()


    /**
     * Data provider.
     *
     * Note: we're only testing the PHPCS functionality, not the PHP native `escapeshellcmd()`
     * function (as that's not our responsibility).
     *
     * @see testEscapeshellcmd()
     *
     * @return array<string, array<string, string>>
     */
    public static function dataEscapeshellcmd()
    {
        return [
            'Command is empty string'                                        => [
                'text'     => '',
                'expected' => '',
            ],
            'Command is simple string'                                       => [
                'text'     => 'csslint',
                'expected' => 'csslint',
            ],
            'Command containing characters which PHP escapes'                => [
                'text'        => '&#;`|*?~<>^()[]{}$\,%!',
                'expected'    => '\&\#\;\`\|\*\?\~\<\>\^\(\)\[\]\{\}\$\\\\,%!',
                'expectedWin' => '^&^#^;^`^|^*^?^~^<^>^^^(^)^[^]^{^}^$^\,^%^!',
            ],
            // @link https://github.com/squizlabs/PHP_CodeSniffer/pull/3214
            'Command containing spaces, which can cause problems on Windows' => [
                'text'        => 'C:\Program Files\nodejs\csslint.cmd',
                'expected'    => 'C:\\\\Program Files\\\\nodejs\\\\csslint.cmd',
                'expectedWin' => 'C:^\Program^ Files^\nodejs^\csslint.cmd',
            ],
            // @link https://github.com/php/doc-en/pull/511
            'Command containing spaces with additional arguments'            => [
                'text'        => 'php -f ./~home/path to/file.php',
                'expected'    => 'php -f ./\~home/path to/file.php',
                'expectedWin' => 'php^ -f^ ./^~home/path^ to/file.php',
            ],
        ];

    }//end dataEscapeshellcmd()


}//end class