ProcessLinterTest.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. /*
  3. * This file is part of PHP CS Fixer.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. * Dariusz Rumiński <dariusz.ruminski@gmail.com>
  7. *
  8. * This source file is subject to the MIT license that is bundled
  9. * with this source code in the file LICENSE.
  10. */
  11. namespace PhpCsFixer\Tests\Linter;
  12. use PhpCsFixer\Linter\ProcessLinter;
  13. use PhpCsFixer\Test\AccessibleObject;
  14. use Symfony\Component\Process\ProcessUtils;
  15. /**
  16. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  17. *
  18. * @internal
  19. *
  20. * @covers PhpCsFixer\Linter\ProcessLinter
  21. * @covers PhpCsFixer\Linter\ProcessLintingResult
  22. */
  23. final class ProcessLinterTest extends AbstractLinterTestCase
  24. {
  25. public function testIsAsync()
  26. {
  27. $this->assertTrue($this->createLinter()->isAsync());
  28. }
  29. public function testPrepareCommandOnPhp()
  30. {
  31. if (defined('HHVM_VERSION')) {
  32. $this->markTestSkipped('Skip tests for PHP compiler when running on HHVM compiler.');
  33. }
  34. $this->assertSame(
  35. $this->fixEscape('"php" -l "foo.php"'),
  36. AccessibleObject::create(new ProcessLinter('php'))->prepareCommand('foo.php')
  37. );
  38. $this->assertSame(
  39. $this->fixEscape('"C:\Program Files\php\php.exe" -l "foo bar\baz.php"'),
  40. AccessibleObject::create(new ProcessLinter('C:\Program Files\php\php.exe'))->prepareCommand('foo bar\baz.php')
  41. );
  42. }
  43. public function testPrepareCommandOnHhvm()
  44. {
  45. if (!defined('HHVM_VERSION')) {
  46. $this->markTestSkipped('Skip tests for HHVM compiler when running on PHP compiler.');
  47. }
  48. $this->assertSame(
  49. $this->fixEscape('"hhvm" --php -l "foo.php"'),
  50. AccessibleObject::create(new ProcessLinter('hhvm'))->prepareCommand('foo.php')
  51. );
  52. }
  53. /**
  54. * {@inheritdoc}
  55. */
  56. protected function createLinter()
  57. {
  58. return new ProcessLinter();
  59. }
  60. /**
  61. * Fix escaping character.
  62. *
  63. * Escape character may be different on various environments.
  64. * This method change used escape character into character that is default
  65. * for environment.
  66. *
  67. * @param string $value value to be fixed
  68. * @param string $usedEscapeChar used escape char, may be only ' or "
  69. *
  70. * @return string
  71. */
  72. private function fixEscape($value, $usedEscapeChar = '"')
  73. {
  74. static $escapeChar = null;
  75. if (null === $escapeChar) {
  76. $escapeChar = substr(ProcessUtils::escapeArgument('x'), 0, 1);
  77. }
  78. if ($usedEscapeChar === $escapeChar) {
  79. return $value;
  80. }
  81. return str_replace($usedEscapeChar, $escapeChar, $value);
  82. }
  83. }