AbstractLinterTestCase.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. * @dataProvider provideLintFileCases
  36. */
  37. public function testLintFile(string $file, ?string $errorMessage = null): void
  38. {
  39. if (null !== $errorMessage) {
  40. $this->expectException(LintingException::class);
  41. $this->expectExceptionMessage($errorMessage);
  42. } else {
  43. $this->expectNotToPerformAssertions();
  44. }
  45. $linter = $this->createLinter();
  46. $linter->lintFile($file)->check();
  47. }
  48. /**
  49. * @medium
  50. */
  51. public static function provideLintFileCases(): iterable
  52. {
  53. yield [
  54. __DIR__.'/../Fixtures/Linter/valid.php',
  55. ];
  56. yield [
  57. __DIR__.'/../Fixtures/Linter/invalid.php',
  58. sprintf('Parse error: syntax error, unexpected %s on line 5.', PHP_MAJOR_VERSION >= 8 ? 'token "echo"' : '\'echo\' (T_ECHO)'),
  59. ];
  60. yield [
  61. __DIR__.'/../Fixtures/Linter/multiple.php',
  62. 'Fatal error: Multiple access type modifiers are not allowed on line 4.',
  63. ];
  64. }
  65. /**
  66. * @dataProvider provideLintSourceCases
  67. */
  68. public function testLintSource(string $source, ?string $errorMessage = null): void
  69. {
  70. if (null !== $errorMessage) {
  71. $this->expectException(LintingException::class);
  72. $this->expectExceptionMessage($errorMessage);
  73. } else {
  74. $this->expectNotToPerformAssertions();
  75. }
  76. $linter = $this->createLinter();
  77. $linter->lintSource($source)->check();
  78. }
  79. public static function provideLintSourceCases(): iterable
  80. {
  81. yield [
  82. '<?php echo 123;',
  83. ];
  84. yield [
  85. '<?php
  86. print "line 2";
  87. print "line 3";
  88. print "line 4";
  89. echo echo;
  90. ',
  91. sprintf('Parse error: syntax error, unexpected %s on line 5.', PHP_MAJOR_VERSION >= 8 ? 'token "echo"' : '\'echo\' (T_ECHO)'),
  92. ];
  93. }
  94. abstract protected function createLinter(): LinterInterface;
  95. }