RunnerTest.php 4.3 KB

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