<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet"
        integrity="sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD" crossorigin="anonymous">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.3.0/css/all.min.css"
        integrity="sha512-SzlrxWUlpfuzQ+pcUCosxcglQRNAq/DZjVsC0lE40xsADsfeQoEypE+enwcOiGjk/bSuGGKHEyjSoQ1zVisanQ=="
        crossorigin="anonymous" referrerpolicy="no-referrer" />
</head>
</html>
<?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/HEAD/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'                                        => [
                'command'  => '',
                'expected' => '',
            ],
            'Command is simple string'                                       => [
                'command'  => 'csslint',
                'expected' => 'csslint',
            ],
            'Command containing characters which PHP escapes'                => [
                'command'     => '&#;`|*?~<>^()[]{}$\,%!',
                'expected'    => '\&\#\;\`\|\*\?\~\<\>\^\(\)\[\]\{\}\$\\\\,%!',
                'expectedWin' => '^&^#^;^`^|^*^?^~^<^>^^^(^)^[^]^{^}^$^\,^%^!',
            ],
            // @link https://github.com/squizlabs/PHP_CodeSniffer/pull/3214
            'Command containing spaces, which can cause problems on Windows' => [
                'command'     => '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'            => [
                'command'     => '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
