AbstractLinterTestCase.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. /*
  3. * This file is part of PHP CS Fixer.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. * Dariusz Rumiński <dariusz.ruminski@gmail.com>
  7. *
  8. * This source file is subject to the MIT license that is bundled
  9. * with this source code in the file LICENSE.
  10. */
  11. namespace PhpCsFixer\Tests\Linter;
  12. use PhpCsFixer\Linter\LinterInterface;
  13. use PhpCsFixer\Tests\TestCase;
  14. use PhpCsFixer\Tokenizer\Token;
  15. use PhpCsFixer\Tokenizer\Tokens;
  16. /**
  17. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  18. *
  19. * @internal
  20. */
  21. abstract class AbstractLinterTestCase extends TestCase
  22. {
  23. abstract public function testIsAsync();
  24. public function testLintingAfterTokenManipulation()
  25. {
  26. $linter = $this->createLinter();
  27. $tokens = Tokens::fromCode("<?php \n#EOF\n");
  28. $tokens->insertAt(1, new Token([T_NS_SEPARATOR, '\\']));
  29. $this->expectException(\PhpCsFixer\Linter\LintingException::class);
  30. $linter->lintSource($tokens->generateCode())->check();
  31. }
  32. /**
  33. * @param string $file
  34. * @param null|string $errorMessage
  35. *
  36. * @dataProvider provideLintFileCases
  37. */
  38. public function testLintFile($file, $errorMessage = null)
  39. {
  40. if (null !== $errorMessage) {
  41. $this->expectException(\PhpCsFixer\Linter\LintingException::class);
  42. $this->expectExceptionMessage($errorMessage);
  43. }
  44. $linter = $this->createLinter();
  45. static::assertNull($linter->lintFile($file)->check());
  46. }
  47. /**
  48. * @return array
  49. *
  50. * @medium
  51. */
  52. public function provideLintFileCases()
  53. {
  54. return [
  55. [
  56. __DIR__.'/../Fixtures/Linter/valid.php',
  57. ],
  58. [
  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. [
  63. __DIR__.'/../Fixtures/Linter/multiple.php',
  64. 'Fatal error: Multiple access type modifiers are not allowed on line 4.',
  65. ],
  66. ];
  67. }
  68. /**
  69. * @param string $source
  70. * @param null|string $errorMessage
  71. *
  72. * @dataProvider provideLintSourceCases
  73. */
  74. public function testLintSource($source, $errorMessage = null)
  75. {
  76. if (null !== $errorMessage) {
  77. $this->expectException(\PhpCsFixer\Linter\LintingException::class);
  78. $this->expectExceptionMessage($errorMessage);
  79. }
  80. $linter = $this->createLinter();
  81. static::assertNull($linter->lintSource($source)->check());
  82. }
  83. /**
  84. * @return array
  85. */
  86. public function provideLintSourceCases()
  87. {
  88. return [
  89. [
  90. '<?php echo 123;',
  91. ],
  92. [
  93. '<?php
  94. print "line 2";
  95. print "line 3";
  96. print "line 4";
  97. echo echo;
  98. ',
  99. sprintf('Parse error: syntax error, unexpected %s on line 5.', PHP_MAJOR_VERSION >= 8 ? 'token "echo"' : '\'echo\' (T_ECHO)'),
  100. ],
  101. ];
  102. }
  103. /**
  104. * @return LinterInterface
  105. */
  106. abstract protected function createLinter();
  107. }