LinterTest.php 2.8 KB

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