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