* Dariusz RumiƄski * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Tests\Linter; use org\bovigo\vfs\vfsStream; use PhpCsFixer\Linter\CachingLinter; use PhpCsFixer\Linter\LinterInterface; use PhpCsFixer\Linter\LintingResultInterface; use PhpCsFixer\Tests\TestCase; /** * @author ntzm * * @internal * * @covers \PhpCsFixer\Linter\CachingLinter */ final class CachingLinterTest extends TestCase { /** * @dataProvider provideIsAsyncCases */ public function testIsAsync(bool $isAsync): void { $sublinter = $this->createLinterDouble($isAsync, [], []); $linter = new CachingLinter($sublinter); self::assertSame($isAsync, $linter->isAsync()); } /** * @return iterable */ public static function provideIsAsyncCases(): iterable { yield [true]; yield [false]; } public function testLintFileIsCalledOnceOnSameContent(): void { $fs = vfsStream::setup('root', null, [ 'foo.php' => ' ' 'createLintingResultDouble(); $result2 = $this->createLintingResultDouble(); $sublinter = $this->createLinterDouble( null, [ $fs->url().'/foo.php' => $result1, $fs->url().'/baz.php' => $result2, ], [], ); $linter = new CachingLinter($sublinter); self::assertSame($result1, $linter->lintFile($fs->url().'/foo.php')); self::assertSame($result1, $linter->lintFile($fs->url().'/foo.php')); self::assertSame($result1, $linter->lintFile($fs->url().'/bar.php')); self::assertSame($result2, $linter->lintFile($fs->url().'/baz.php')); } public function testLintSourceIsCalledOnceOnSameContent(): void { $result1 = $this->createLintingResultDouble(); $result2 = $this->createLintingResultDouble(); $sublinter = $this->createLinterDouble( null, [], [ ' $result1, ' $result2, ], ); $linter = new CachingLinter($sublinter); self::assertSame($result1, $linter->lintSource('lintSource('lintSource(' $allowedLintFileCalls * @param array $allowedLintSourceCalls */ private function createLinterDouble(?bool $isAsync, array $allowedLintFileCalls, array $allowedLintSourceCalls): LinterInterface { return new class($isAsync, $allowedLintFileCalls, $allowedLintSourceCalls) implements LinterInterface { private ?bool $isAsync; /** @var array */ private array $allowedLintFileCalls; /** @var array */ private array $allowedLintSourceCalls; /** * @param array $allowedLintFileCalls * @param array $allowedLintSourceCalls */ public function __construct(?bool $isAsync, array $allowedLintFileCalls, array $allowedLintSourceCalls) { $this->isAsync = $isAsync; $this->allowedLintFileCalls = $allowedLintFileCalls; $this->allowedLintSourceCalls = $allowedLintSourceCalls; } public function isAsync(): bool { return $this->isAsync; } public function lintFile(string $path): LintingResultInterface { if (!isset($this->allowedLintFileCalls[$path])) { throw new \LogicException(\sprintf('File "%s" should not be linted.', $path)); } $result = $this->allowedLintFileCalls[$path]; unset($this->allowedLintFileCalls[$path]); return $result; } public function lintSource(string $source): LintingResultInterface { if (!isset($this->allowedLintSourceCalls[$source])) { throw new \LogicException(\sprintf('File "%s" should not be linted.', $source)); } $result = $this->allowedLintSourceCalls[$source]; unset($this->allowedLintSourceCalls[$source]); return $result; } }; } private function createLintingResultDouble(): LintingResultInterface { return new class implements LintingResultInterface { public function check(): void { throw new \LogicException('Not implemented.'); } }; } }