RunnerTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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\Runner;
  13. use PhpCsFixer\Cache\Directory;
  14. use PhpCsFixer\Cache\NullCacheManager;
  15. use PhpCsFixer\Differ\NullDiffer;
  16. use PhpCsFixer\Error\Error;
  17. use PhpCsFixer\Error\ErrorsManager;
  18. use PhpCsFixer\Fixer;
  19. use PhpCsFixer\Linter\Linter;
  20. use PhpCsFixer\Runner\Runner;
  21. use PhpCsFixer\Tests\Fixtures\FakeDiffer;
  22. use PhpCsFixer\Tests\TestCase;
  23. use Prophecy\Argument;
  24. use Symfony\Component\Finder\Finder;
  25. /**
  26. * @internal
  27. *
  28. * @covers \PhpCsFixer\Runner\Runner
  29. */
  30. final class RunnerTest extends TestCase
  31. {
  32. /**
  33. * @covers \PhpCsFixer\Runner\Runner::fix
  34. * @covers \PhpCsFixer\Runner\Runner::fixFile
  35. */
  36. public function testThatFixSuccessfully(): void
  37. {
  38. $linterProphecy = $this->prophesize(\PhpCsFixer\Linter\LinterInterface::class);
  39. $linterProphecy
  40. ->isAsync()
  41. ->willReturn(false)
  42. ;
  43. $linterProphecy
  44. ->lintFile(Argument::type('string'))
  45. ->willReturn($this->prophesize(\PhpCsFixer\Linter\LintingResultInterface::class)->reveal())
  46. ;
  47. $linterProphecy
  48. ->lintSource(Argument::type('string'))
  49. ->willReturn($this->prophesize(\PhpCsFixer\Linter\LintingResultInterface::class)->reveal())
  50. ;
  51. $fixers = [
  52. new Fixer\ClassNotation\VisibilityRequiredFixer(),
  53. new Fixer\Import\NoUnusedImportsFixer(), // will be ignored cause of test keyword in namespace
  54. ];
  55. $expectedChangedInfo = [
  56. 'appliedFixers' => ['visibility_required'],
  57. 'diff' => '',
  58. ];
  59. $path = __DIR__.\DIRECTORY_SEPARATOR.'..'.\DIRECTORY_SEPARATOR.'Fixtures'.\DIRECTORY_SEPARATOR.'FixerTest'.\DIRECTORY_SEPARATOR.'fix';
  60. $runner = new Runner(
  61. Finder::create()->in($path),
  62. $fixers,
  63. new NullDiffer(),
  64. null,
  65. new ErrorsManager(),
  66. $linterProphecy->reveal(),
  67. true,
  68. new NullCacheManager(),
  69. new Directory($path),
  70. false
  71. );
  72. $changed = $runner->fix();
  73. self::assertCount(2, $changed);
  74. self::assertSame($expectedChangedInfo, array_pop($changed));
  75. self::assertSame($expectedChangedInfo, array_pop($changed));
  76. $path = __DIR__.\DIRECTORY_SEPARATOR.'..'.\DIRECTORY_SEPARATOR.'Fixtures'.\DIRECTORY_SEPARATOR.'FixerTest'.\DIRECTORY_SEPARATOR.'fix';
  77. $runner = new Runner(
  78. Finder::create()->in($path),
  79. $fixers,
  80. new NullDiffer(),
  81. null,
  82. new ErrorsManager(),
  83. $linterProphecy->reveal(),
  84. true,
  85. new NullCacheManager(),
  86. new Directory($path),
  87. true
  88. );
  89. $changed = $runner->fix();
  90. self::assertCount(1, $changed);
  91. self::assertSame($expectedChangedInfo, array_pop($changed));
  92. }
  93. /**
  94. * @covers \PhpCsFixer\Runner\Runner::fix
  95. * @covers \PhpCsFixer\Runner\Runner::fixFile
  96. */
  97. public function testThatFixInvalidFileReportsToErrorManager(): void
  98. {
  99. $errorsManager = new ErrorsManager();
  100. $path = realpath(__DIR__.\DIRECTORY_SEPARATOR.'..').\DIRECTORY_SEPARATOR.'Fixtures'.\DIRECTORY_SEPARATOR.'FixerTest'.\DIRECTORY_SEPARATOR.'invalid';
  101. $runner = new Runner(
  102. Finder::create()->in($path),
  103. [
  104. new Fixer\ClassNotation\VisibilityRequiredFixer(),
  105. new Fixer\Import\NoUnusedImportsFixer(), // will be ignored cause of test keyword in namespace
  106. ],
  107. new NullDiffer(),
  108. null,
  109. $errorsManager,
  110. new Linter(),
  111. true,
  112. new NullCacheManager()
  113. );
  114. $changed = $runner->fix();
  115. $pathToInvalidFile = $path.\DIRECTORY_SEPARATOR.'somefile.php';
  116. self::assertCount(0, $changed);
  117. $errors = $errorsManager->getInvalidErrors();
  118. self::assertCount(1, $errors);
  119. $error = $errors[0];
  120. self::assertInstanceOf(\PhpCsFixer\Error\Error::class, $error);
  121. self::assertSame(Error::TYPE_INVALID, $error->getType());
  122. self::assertSame($pathToInvalidFile, $error->getFilePath());
  123. }
  124. /**
  125. * @covers \PhpCsFixer\Runner\Runner::fix
  126. * @covers \PhpCsFixer\Runner\Runner::fixFile
  127. */
  128. public function testThatDiffedFileIsPassedToDiffer(): void
  129. {
  130. $spy = new FakeDiffer();
  131. $path = __DIR__.\DIRECTORY_SEPARATOR.'..'.\DIRECTORY_SEPARATOR.'Fixtures'.\DIRECTORY_SEPARATOR.'FixerTest'.\DIRECTORY_SEPARATOR.'fix';
  132. $fixers = [
  133. new Fixer\ClassNotation\VisibilityRequiredFixer(),
  134. ];
  135. $runner = new Runner(
  136. Finder::create()->in($path),
  137. $fixers,
  138. $spy,
  139. null,
  140. new ErrorsManager(),
  141. new Linter(),
  142. true,
  143. new NullCacheManager(),
  144. new Directory($path),
  145. true
  146. );
  147. $runner->fix();
  148. self::assertSame($path, $spy->passedFile->getPath());
  149. }
  150. }