AbstractLinterTestCase.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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\Linter;
  13. use PhpCsFixer\Linter\LinterInterface;
  14. use PhpCsFixer\Linter\LintingException;
  15. use PhpCsFixer\Tests\TestCase;
  16. use PhpCsFixer\Tokenizer\Token;
  17. use PhpCsFixer\Tokenizer\Tokens;
  18. /**
  19. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  20. *
  21. * @internal
  22. */
  23. abstract class AbstractLinterTestCase extends TestCase
  24. {
  25. abstract public function testIsAsync(): void;
  26. public function testLintingAfterTokenManipulation(): void
  27. {
  28. $linter = $this->createLinter();
  29. $tokens = Tokens::fromCode("<?php \n#EOF\n");
  30. $tokens->insertAt(1, new Token([T_NS_SEPARATOR, '\\']));
  31. $this->expectException(LintingException::class);
  32. $linter->lintSource($tokens->generateCode())->check();
  33. }
  34. /**
  35. * @medium
  36. *
  37. * @dataProvider provideLintFileCases
  38. */
  39. public function testLintFile(string $file, ?string $errorMessage = null): void
  40. {
  41. if (null !== $errorMessage) {
  42. $this->expectException(LintingException::class);
  43. $this->expectExceptionMessage($errorMessage);
  44. } else {
  45. $this->expectNotToPerformAssertions();
  46. }
  47. $linter = $this->createLinter();
  48. $linter->lintFile($file)->check();
  49. }
  50. public static function provideLintFileCases(): iterable
  51. {
  52. yield [
  53. __DIR__.'/../Fixtures/Linter/valid.php',
  54. ];
  55. yield [
  56. __DIR__.'/../Fixtures/Linter/invalid.php',
  57. \sprintf('Parse error: syntax error, unexpected %s on line 5.', PHP_MAJOR_VERSION >= 8 ? 'token "echo"' : '\'echo\' (T_ECHO)'),
  58. ];
  59. yield [
  60. __DIR__.'/../Fixtures/Linter/multiple.php',
  61. 'Fatal error: Multiple access type modifiers are not allowed on line 4.',
  62. ];
  63. }
  64. /**
  65. * @dataProvider provideLintSourceCases
  66. */
  67. public function testLintSource(string $source, ?string $errorMessage = null): void
  68. {
  69. if (null !== $errorMessage) {
  70. $this->expectException(LintingException::class);
  71. $this->expectExceptionMessage($errorMessage);
  72. } else {
  73. $this->expectNotToPerformAssertions();
  74. }
  75. $linter = $this->createLinter();
  76. $linter->lintSource($source)->check();
  77. }
  78. public static function provideLintSourceCases(): iterable
  79. {
  80. yield [
  81. '<?php echo 123;',
  82. ];
  83. yield [
  84. '<?php
  85. print "line 2";
  86. print "line 3";
  87. print "line 4";
  88. echo echo;
  89. ',
  90. \sprintf('Parse error: syntax error, unexpected %s on line 5.', PHP_MAJOR_VERSION >= 8 ? 'token "echo"' : '\'echo\' (T_ECHO)'),
  91. ];
  92. }
  93. abstract protected function createLinter(): LinterInterface;
  94. }