AbstractLinterTestCase.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. /**
  14. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  15. *
  16. * @internal
  17. */
  18. abstract class AbstractLinterTestCase extends \PHPUnit_Framework_TestCase
  19. {
  20. abstract public function testIsAsync();
  21. /**
  22. * @param string $file
  23. * @param null|string $errorRegExp
  24. *
  25. * @dataProvider provideLintFileCases
  26. */
  27. public function testLintFile($file, $errorRegExp = null)
  28. {
  29. if (null !== $errorRegExp) {
  30. $this->setExpectedExceptionRegExp('\PhpCsFixer\Linter\LintingException', $errorRegExp);
  31. }
  32. $linter = $this->createLinter();
  33. $linter->lintFile($file)->check();
  34. }
  35. /**
  36. * @return array
  37. */
  38. public function provideLintFileCases()
  39. {
  40. return array(
  41. array(
  42. __DIR__.'/../Fixtures/Linter/valid.php',
  43. ),
  44. array(
  45. __DIR__.'/../Fixtures/Linter/invalid.php',
  46. '/syntax error, unexpected.*T_ECHO.*line 5/',
  47. ),
  48. );
  49. }
  50. /**
  51. * @param string $source
  52. * @param null|string $errorRegExp
  53. *
  54. * @dataProvider provideLintSourceCases
  55. */
  56. public function testLintSource($source, $errorRegExp = null)
  57. {
  58. if (null !== $errorRegExp) {
  59. $this->setExpectedExceptionRegExp('\PhpCsFixer\Linter\LintingException', $errorRegExp);
  60. }
  61. $linter = $this->createLinter();
  62. $linter->lintSource($source)->check();
  63. }
  64. /**
  65. * @return array
  66. */
  67. public function provideLintSourceCases()
  68. {
  69. return array(
  70. array(
  71. '<?php echo 123;',
  72. ),
  73. array(
  74. '<?php
  75. print "line 2";
  76. print "line 3";
  77. print "line 4";
  78. echo echo;
  79. ',
  80. '/syntax error, unexpected.*T_ECHO.*line 5/',
  81. ),
  82. );
  83. }
  84. /**
  85. * @return LinterInterface
  86. */
  87. abstract protected function createLinter();
  88. }