AbstractLinterTestCase.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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\Tokenizer\Token;
  14. use PhpCsFixer\Tokenizer\Tokens;
  15. use PHPUnit\Framework\TestCase;
  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(array(T_NS_SEPARATOR, '\\')));
  29. $this->setExpectedException('\PhpCsFixer\Linter\LintingException');
  30. $linter->lintSource($tokens->generateCode())->check();
  31. }
  32. /**
  33. * @param string $file
  34. * @param null|string $errorRegExp
  35. *
  36. * @dataProvider provideLintFileCases
  37. */
  38. public function testLintFile($file, $errorRegExp = null)
  39. {
  40. if (null !== $errorRegExp) {
  41. $this->setExpectedExceptionRegExp('\PhpCsFixer\Linter\LintingException', $errorRegExp);
  42. }
  43. $linter = $this->createLinter();
  44. $this->assertNull($linter->lintFile($file)->check());
  45. }
  46. /**
  47. * @return array
  48. */
  49. public function provideLintFileCases()
  50. {
  51. return array(
  52. array(
  53. __DIR__.'/../Fixtures/Linter/valid.php',
  54. ),
  55. array(
  56. __DIR__.'/../Fixtures/Linter/invalid.php',
  57. '/syntax error, unexpected.*T_ECHO.*line 5/',
  58. ),
  59. );
  60. }
  61. /**
  62. * @param string $source
  63. * @param null|string $errorRegExp
  64. *
  65. * @dataProvider provideLintSourceCases
  66. */
  67. public function testLintSource($source, $errorRegExp = null)
  68. {
  69. if (null !== $errorRegExp) {
  70. $this->setExpectedExceptionRegExp('\PhpCsFixer\Linter\LintingException', $errorRegExp);
  71. }
  72. $linter = $this->createLinter();
  73. $this->assertNull($linter->lintSource($source)->check());
  74. }
  75. /**
  76. * @return array
  77. */
  78. public function provideLintSourceCases()
  79. {
  80. return array(
  81. array(
  82. '<?php echo 123;',
  83. ),
  84. array(
  85. '<?php
  86. print "line 2";
  87. print "line 3";
  88. print "line 4";
  89. echo echo;
  90. ',
  91. '/syntax error, unexpected.*T_ECHO.*line 5/',
  92. ),
  93. );
  94. }
  95. /**
  96. * @return LinterInterface
  97. */
  98. abstract protected function createLinter();
  99. }