AbstractLinterTestCase.php 3.2 KB

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