123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298 |
- <?php
- declare(strict_types=1);
- /*
- * This file is part of PHP CS Fixer.
- *
- * (c) Fabien Potencier <fabien@symfony.com>
- * Dariusz Rumiński <dariusz.ruminski@gmail.com>
- *
- * This source file is subject to the MIT license that is bundled
- * with this source code in the file LICENSE.
- */
- namespace PhpCsFixer\Tests\Runner;
- use PhpCsFixer\AccessibleObject\AccessibleObject;
- use PhpCsFixer\Cache\Directory;
- use PhpCsFixer\Cache\NullCacheManager;
- use PhpCsFixer\Console\Command\FixCommand;
- use PhpCsFixer\Differ\DifferInterface;
- use PhpCsFixer\Differ\NullDiffer;
- use PhpCsFixer\Error\Error;
- use PhpCsFixer\Error\ErrorsManager;
- use PhpCsFixer\Fixer;
- use PhpCsFixer\Linter\Linter;
- use PhpCsFixer\Linter\LinterInterface;
- use PhpCsFixer\Linter\LintingException;
- use PhpCsFixer\Linter\LintingResultInterface;
- use PhpCsFixer\Runner\Parallel\ParallelConfig;
- use PhpCsFixer\Runner\Runner;
- use PhpCsFixer\Tests\TestCase;
- use PhpCsFixer\ToolInfo;
- use Symfony\Component\Console\Input\ArrayInput;
- use Symfony\Component\Finder\Finder;
- /**
- * @internal
- *
- * @covers \PhpCsFixer\Runner\Runner
- */
- final class RunnerTest extends TestCase
- {
- /**
- * @covers \PhpCsFixer\Runner\Runner::fix
- * @covers \PhpCsFixer\Runner\Runner::fixFile
- */
- public function testThatFixSuccessfully(): void
- {
- $linter = $this->createLinterDouble();
- $fixers = [
- new Fixer\ClassNotation\VisibilityRequiredFixer(),
- new Fixer\Import\NoUnusedImportsFixer(), // will be ignored cause of test keyword in namespace
- ];
- $expectedChangedInfo = [
- 'appliedFixers' => ['visibility_required'],
- 'diff' => '',
- ];
- $path = __DIR__.\DIRECTORY_SEPARATOR.'..'.\DIRECTORY_SEPARATOR.'Fixtures'.\DIRECTORY_SEPARATOR.'FixerTest'.\DIRECTORY_SEPARATOR.'fix';
- $runner = new Runner(
- Finder::create()->in($path),
- $fixers,
- new NullDiffer(),
- null,
- new ErrorsManager(),
- $linter,
- true,
- new NullCacheManager(),
- new Directory($path),
- false
- );
- $changed = $runner->fix();
- self::assertCount(2, $changed);
- self::assertSame($expectedChangedInfo, array_pop($changed));
- self::assertSame($expectedChangedInfo, array_pop($changed));
- $path = __DIR__.\DIRECTORY_SEPARATOR.'..'.\DIRECTORY_SEPARATOR.'Fixtures'.\DIRECTORY_SEPARATOR.'FixerTest'.\DIRECTORY_SEPARATOR.'fix';
- $runner = new Runner(
- Finder::create()->in($path),
- $fixers,
- new NullDiffer(),
- null,
- new ErrorsManager(),
- $linter,
- true,
- new NullCacheManager(),
- new Directory($path),
- true
- );
- $changed = $runner->fix();
- self::assertCount(1, $changed);
- self::assertSame($expectedChangedInfo, array_pop($changed));
- }
- /**
- * @covers \PhpCsFixer\Runner\Runner::fix
- * @covers \PhpCsFixer\Runner\Runner::fixFile
- * @covers \PhpCsFixer\Runner\Runner::fixSequential
- */
- public function testThatSequentialFixOfInvalidFileReportsToErrorManager(): void
- {
- $errorsManager = new ErrorsManager();
- $path = realpath(__DIR__.\DIRECTORY_SEPARATOR.'..').\DIRECTORY_SEPARATOR.'Fixtures'.\DIRECTORY_SEPARATOR.'FixerTest'.\DIRECTORY_SEPARATOR.'invalid';
- $runner = new Runner(
- Finder::create()->in($path),
- [
- new Fixer\ClassNotation\VisibilityRequiredFixer(),
- new Fixer\Import\NoUnusedImportsFixer(), // will be ignored cause of test keyword in namespace
- ],
- new NullDiffer(),
- null,
- $errorsManager,
- new Linter(),
- true,
- new NullCacheManager()
- );
- $changed = $runner->fix();
- $pathToInvalidFile = $path.\DIRECTORY_SEPARATOR.'somefile.php';
- self::assertCount(0, $changed);
- $errors = $errorsManager->getInvalidErrors();
- self::assertCount(1, $errors);
- $error = $errors[0];
- self::assertSame(Error::TYPE_INVALID, $error->getType());
- self::assertSame($pathToInvalidFile, $error->getFilePath());
- }
- /**
- * @covers \PhpCsFixer\Runner\Runner::fix
- * @covers \PhpCsFixer\Runner\Runner::fixFile
- * @covers \PhpCsFixer\Runner\Runner::fixParallel
- */
- public function testThatParallelFixOfInvalidFileReportsToErrorManager(): void
- {
- $errorsManager = new ErrorsManager();
- $path = realpath(__DIR__.\DIRECTORY_SEPARATOR.'..').\DIRECTORY_SEPARATOR.'Fixtures'.\DIRECTORY_SEPARATOR.'FixerTest'.\DIRECTORY_SEPARATOR.'invalid';
- $runner = new Runner(
- Finder::create()->in($path),
- [
- new Fixer\ClassNotation\VisibilityRequiredFixer(),
- new Fixer\Import\NoUnusedImportsFixer(), // will be ignored cause of test keyword in namespace
- ],
- new NullDiffer(),
- null,
- $errorsManager,
- new Linter(),
- true,
- new NullCacheManager(),
- null,
- false,
- new ParallelConfig(2, 1, 50),
- new ArrayInput([], (new FixCommand(new ToolInfo()))->getDefinition())
- );
- $changed = $runner->fix();
- $pathToInvalidFile = $path.\DIRECTORY_SEPARATOR.'somefile.php';
- self::assertCount(0, $changed);
- $errors = $errorsManager->getInvalidErrors();
- self::assertCount(1, $errors);
- $error = $errors[0];
- self::assertInstanceOf(LintingException::class, $error->getSource());
- self::assertSame(Error::TYPE_INVALID, $error->getType());
- self::assertSame($pathToInvalidFile, $error->getFilePath());
- }
- /**
- * @requires OS Darwin|Windows
- *
- * @TODO v4 do not switch on parallel execution by default while this test is not passing on Linux.
- *
- * @covers \PhpCsFixer\Runner\Runner::fix
- * @covers \PhpCsFixer\Runner\Runner::fixFile
- * @covers \PhpCsFixer\Runner\Runner::fixParallel
- *
- * @dataProvider provideParallelFixStopsOnFirstViolationIfSuchOptionIsEnabledCases
- */
- public function testParallelFixStopsOnFirstViolationIfSuchOptionIsEnabled(bool $stopOnViolation, int $expectedChanges): void
- {
- $errorsManager = new ErrorsManager();
- $path = realpath(__DIR__.\DIRECTORY_SEPARATOR.'..').\DIRECTORY_SEPARATOR.'Fixtures'.\DIRECTORY_SEPARATOR.'FixerTest'.\DIRECTORY_SEPARATOR.'fix';
- $runner = new Runner(
- Finder::create()->in($path),
- [
- new Fixer\ClassNotation\VisibilityRequiredFixer(),
- new Fixer\Import\NoUnusedImportsFixer(), // will be ignored cause of test keyword in namespace
- ],
- new NullDiffer(),
- null,
- $errorsManager,
- new Linter(),
- true,
- new NullCacheManager(),
- null,
- $stopOnViolation,
- new ParallelConfig(2, 1, 3),
- new ArrayInput([], (new FixCommand(new ToolInfo()))->getDefinition())
- );
- self::assertCount($expectedChanges, $runner->fix());
- }
- /**
- * @return iterable<array{0: bool, 1: int}>
- */
- public static function provideParallelFixStopsOnFirstViolationIfSuchOptionIsEnabledCases(): iterable
- {
- yield 'do NOT stop on violation' => [false, 2];
- yield 'stop on violation' => [true, 1];
- }
- /**
- * @covers \PhpCsFixer\Runner\Runner::fix
- * @covers \PhpCsFixer\Runner\Runner::fixFile
- */
- public function testThatDiffedFileIsPassedToDiffer(): void
- {
- $differ = $this->createDifferDouble();
- $path = __DIR__.\DIRECTORY_SEPARATOR.'..'.\DIRECTORY_SEPARATOR.'Fixtures'.\DIRECTORY_SEPARATOR.'FixerTest'.\DIRECTORY_SEPARATOR.'fix';
- $fixers = [
- new Fixer\ClassNotation\VisibilityRequiredFixer(),
- ];
- $runner = new Runner(
- Finder::create()->in($path),
- $fixers,
- $differ,
- null,
- new ErrorsManager(),
- new Linter(),
- true,
- new NullCacheManager(),
- new Directory($path),
- true
- );
- $runner->fix();
- self::assertSame($path, AccessibleObject::create($differ)->passedFile->getPath());
- }
- private function createDifferDouble(): DifferInterface
- {
- return new class implements DifferInterface {
- public ?\SplFileInfo $passedFile = null;
- public function diff(string $old, string $new, ?\SplFileInfo $file = null): string
- {
- $this->passedFile = $file;
- return 'some-diff';
- }
- };
- }
- private function createLinterDouble(): LinterInterface
- {
- return new class implements LinterInterface {
- public function isAsync(): bool
- {
- return false;
- }
- public function lintFile(string $path): LintingResultInterface
- {
- return new class implements LintingResultInterface {
- public function check(): void {}
- };
- }
- public function lintSource(string $source): LintingResultInterface
- {
- return new class implements LintingResultInterface {
- public function check(): void {}
- };
- }
- };
- }
- }
|