BacktickToShellExecFixerTest.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * This file is part of PHP CS Fixer.
  5. *
  6. * (c) Fabien Potencier <fabien@symfony.com>
  7. * Dariusz Rumiński <dariusz.ruminski@gmail.com>
  8. *
  9. * This source file is subject to the MIT license that is bundled
  10. * with this source code in the file LICENSE.
  11. */
  12. namespace PhpCsFixer\Tests\Fixer\Alias;
  13. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  14. /**
  15. * @author Filippo Tessarotto <zoeslam@gmail.com>
  16. *
  17. * @internal
  18. *
  19. * @covers \PhpCsFixer\Fixer\Alias\BacktickToShellExecFixer
  20. *
  21. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Alias\BacktickToShellExecFixer>
  22. */
  23. final class BacktickToShellExecFixerTest extends AbstractFixerTestCase
  24. {
  25. /**
  26. * @dataProvider provideFixCases
  27. */
  28. public function testFix(string $expected, ?string $input = null): void
  29. {
  30. $this->doTest($expected, $input);
  31. }
  32. /**
  33. * @return iterable<string, array{0: string, 1?: string}>
  34. */
  35. public static function provideFixCases(): iterable
  36. {
  37. yield 'plain' => [
  38. '<?php shell_exec("ls -lah");',
  39. '<?php `ls -lah`;',
  40. ];
  41. yield 'with variables' => [
  42. '<?php shell_exec("$var1 ls ${var2} -lah {$var3} file1.txt {$var4[0]} file2.txt {$var5->call()}");',
  43. '<?php `$var1 ls ${var2} -lah {$var3} file1.txt {$var4[0]} file2.txt {$var5->call()}`;',
  44. ];
  45. yield 'with single quote' => [
  46. <<<'EOT'
  47. <?php
  48. `echo a\'b`;
  49. `echo 'ab'`;
  50. EOT,
  51. ];
  52. yield 'with double quote' => [
  53. <<<'EOT'
  54. <?php
  55. `echo a\"b`;
  56. `echo 'a"b'`;
  57. EOT,
  58. ];
  59. yield 'with backtick' => [
  60. <<<'EOT'
  61. <?php
  62. `echo 'a\`b'`;
  63. `echo a\\\`b`;
  64. EOT,
  65. ];
  66. }
  67. }