123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388 |
- <?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\Cache;
- use PhpCsFixer\Cache\CacheInterface;
- use PhpCsFixer\Cache\CacheManagerInterface;
- use PhpCsFixer\Cache\FileCacheManager;
- use PhpCsFixer\Cache\FileHandlerInterface;
- use PhpCsFixer\Cache\SignatureInterface;
- use PhpCsFixer\Tests\TestCase;
- use Prophecy\Argument;
- /**
- * @author Andreas Möller <am@localheinz.com>
- *
- * @internal
- *
- * @covers \PhpCsFixer\Cache\FileCacheManager
- */
- final class FileCacheManagerTest extends TestCase
- {
- public function testIsFinal(): void
- {
- $reflection = new \ReflectionClass(FileCacheManager::class);
- self::assertTrue($reflection->isFinal());
- }
- public function testImplementsCacheManagerInterface(): void
- {
- $reflection = new \ReflectionClass(FileCacheManager::class);
- self::assertTrue($reflection->implementsInterface(CacheManagerInterface::class));
- }
- public function testCreatesCacheIfHandlerReturnedNoCache(): void
- {
- $signature = $this->prophesize(SignatureInterface::class)->reveal();
- $handlerProphecy = $this->prophesize(FileHandlerInterface::class);
- $handlerProphecy->read()->shouldBeCalled()->willReturn(null);
- $handlerProphecy->write(Argument::that(static fn (CacheInterface $cache): bool => $cache->getSignature() === $signature))->shouldBeCalled();
- $handler = $handlerProphecy->reveal();
- $manager = new FileCacheManager(
- $handler,
- $signature
- );
- unset($manager);
- }
- public function testCreatesCacheIfCachedSignatureIsDifferent(): void
- {
- $cachedSignature = $this->prophesize(SignatureInterface::class)->reveal();
- $signatureProphecy = $this->prophesize(SignatureInterface::class);
- $signatureProphecy->equals(Argument::is($cachedSignature))->shouldBeCalled()->willReturn(false);
- $signature = $signatureProphecy->reveal();
- $cacheProphecy = $this->prophesize(CacheInterface::class);
- $cacheProphecy->getSignature()->shouldBeCalled()->willReturn($cachedSignature);
- $cache = $cacheProphecy->reveal();
- $handlerProphecy = $this->prophesize(FileHandlerInterface::class);
- $handlerProphecy->read()->shouldBeCalled()->willReturn($cache);
- $handlerProphecy->write(Argument::that(static fn (CacheInterface $cache): bool => $cache->getSignature() === $signature))->shouldBeCalled();
- $handler = $handlerProphecy->reveal();
- $manager = new FileCacheManager(
- $handler,
- $signature
- );
- unset($manager);
- }
- public function testUsesCacheIfCachedSignatureIsEqualAndNoFileWasUpdated(): void
- {
- $cachedSignature = $this->prophesize(SignatureInterface::class)->reveal();
- $signatureProphecy = $this->prophesize(SignatureInterface::class);
- $signatureProphecy->equals(Argument::is($cachedSignature))->willReturn(true);
- $signature = $signatureProphecy->reveal();
- $cacheProphecy = $this->prophesize(CacheInterface::class);
- $cacheProphecy->getSignature()->shouldBeCalled()->willReturn($cachedSignature);
- $cache = $cacheProphecy->reveal();
- $handlerProphecy = $this->prophesize(FileHandlerInterface::class);
- $handlerProphecy->read()->shouldBeCalled()->willReturn($cache);
- $handlerProphecy->write(Argument::is($cache))->shouldNotBeCalled();
- $handler = $handlerProphecy->reveal();
- $manager = new FileCacheManager(
- $handler,
- $signature
- );
- unset($manager);
- }
- public function testNeedFixingReturnsTrueIfCacheHasNoHash(): void
- {
- $file = 'hello.php';
- $fileContent = '<?php echo "Hello!"';
- $cachedSignature = $this->prophesize(SignatureInterface::class)->reveal();
- $signatureProphecy = $this->prophesize(SignatureInterface::class);
- $signatureProphecy->equals(Argument::is($cachedSignature))->willReturn(true);
- $signature = $signatureProphecy->reveal();
- $cacheProphecy = $this->prophesize(CacheInterface::class);
- $cacheProphecy->getSignature()->willReturn($cachedSignature);
- $cacheProphecy->has(Argument::is($file))->willReturn(false);
- $cache = $cacheProphecy->reveal();
- $handlerProphecy = $this->prophesize(FileHandlerInterface::class);
- $handlerProphecy->read()->willReturn($cache);
- $handlerProphecy->getFile()->willReturn($this->getFile());
- $handlerProphecy->write(Argument::is($cache));
- $handler = $handlerProphecy->reveal();
- $manager = new FileCacheManager(
- $handler,
- $signature
- );
- self::assertTrue($manager->needFixing($file, $fileContent));
- }
- public function testNeedFixingReturnsTrueIfCachedHashIsDifferent(): void
- {
- $file = 'hello.php';
- $fileContent = '<?php echo "Hello!"';
- $previousFileContent = '<?php echo "Hello, world!"';
- $cachedSignature = $this->prophesize(SignatureInterface::class)->reveal();
- $signatureProphecy = $this->prophesize(SignatureInterface::class);
- $signatureProphecy->equals(Argument::is($cachedSignature))->willReturn(true);
- $signature = $signatureProphecy->reveal();
- $cacheProphecy = $this->prophesize(CacheInterface::class);
- $cacheProphecy->getSignature()->willReturn($cachedSignature);
- $cacheProphecy->has(Argument::is($file))->willReturn(true);
- $cacheProphecy->get(Argument::is($file))->willReturn(md5($previousFileContent));
- $cache = $cacheProphecy->reveal();
- $handlerProphecy = $this->prophesize(FileHandlerInterface::class);
- $handlerProphecy->read()->willReturn($cache);
- $handlerProphecy->getFile()->willReturn($this->getFile());
- $handlerProphecy->write(Argument::is($cache));
- $handler = $handlerProphecy->reveal();
- $manager = new FileCacheManager(
- $handler,
- $signature
- );
- self::assertTrue($manager->needFixing($file, $fileContent));
- }
- public function testNeedFixingReturnsFalseIfCachedHashIsIdentical(): void
- {
- $file = 'hello.php';
- $fileContent = '<?php echo "Hello!"';
- $cachedSignature = $this->prophesize(SignatureInterface::class)->reveal();
- $signatureProphecy = $this->prophesize(SignatureInterface::class);
- $signatureProphecy->equals(Argument::is($cachedSignature))->willReturn(true);
- $signature = $signatureProphecy->reveal();
- $cacheProphecy = $this->prophesize(CacheInterface::class);
- $cacheProphecy->getSignature()->willReturn($cachedSignature);
- $cacheProphecy->has(Argument::is($file))->willReturn(true);
- $cacheProphecy->get(Argument::is($file))->willReturn(md5($fileContent));
- $cache = $cacheProphecy->reveal();
- $handlerProphecy = $this->prophesize(FileHandlerInterface::class);
- $handlerProphecy->read()->willReturn($cache);
- $handlerProphecy->getFile()->willReturn($this->getFile());
- $handlerProphecy->write(Argument::is($cache));
- $handler = $handlerProphecy->reveal();
- $manager = new FileCacheManager(
- $handler,
- $signature
- );
- self::assertFalse($manager->needFixing($file, $fileContent));
- }
- public function testNeedFixingUsesRelativePathToFile(): void
- {
- $cacheFile = $this->getFile();
- $file = '/foo/bar/baz/src/hello.php';
- $relativePathToFile = 'src/hello.php';
- $fileContent = '<?php echo "Hello!"';
- $directoryProphecy = $this->prophesize(\PhpCsFixer\Cache\DirectoryInterface::class);
- $directoryProphecy->getRelativePathTo(Argument::is($file))->willReturn($relativePathToFile);
- $cachedSignature = $this->prophesize(SignatureInterface::class)->reveal();
- $signatureProphecy = $this->prophesize(SignatureInterface::class);
- $signatureProphecy->equals(Argument::is($cachedSignature))->willReturn(true);
- $signature = $signatureProphecy->reveal();
- $cacheProphecy = $this->prophesize(CacheInterface::class);
- $cacheProphecy->getSignature()->willReturn($cachedSignature);
- $cacheProphecy->has(Argument::is($relativePathToFile))->willReturn(true);
- $cacheProphecy->has(Argument::is($relativePathToFile))->willReturn(0);
- $cache = $cacheProphecy->reveal();
- $handlerProphecy = $this->prophesize(FileHandlerInterface::class);
- $handlerProphecy->read()->willReturn($cache);
- $handlerProphecy->getFile()->willReturn($cacheFile);
- $handlerProphecy->write(Argument::is($cache));
- $handler = $handlerProphecy->reveal();
- $manager = new FileCacheManager(
- $handler,
- $signature,
- false,
- $directoryProphecy->reveal()
- );
- self::assertTrue($manager->needFixing($file, $fileContent));
- }
- public function testSetFileSetsHashOfFileContent(): void
- {
- $cacheFile = $this->getFile();
- $file = 'hello.php';
- $fileContent = '<?php echo "Hello!"';
- $cachedSignature = $this->prophesize(SignatureInterface::class)->reveal();
- $signatureProphecy = $this->prophesize(SignatureInterface::class);
- $signatureProphecy->equals(Argument::is($cachedSignature))->willReturn(true);
- $signature = $signatureProphecy->reveal();
- $cacheProphecy = $this->prophesize(CacheInterface::class);
- $cacheProphecy->getSignature()->willReturn($cachedSignature);
- $cacheProphecy->set(Argument::is($file), Argument::is(md5($fileContent)))->shouldBeCalled();
- $cache = $cacheProphecy->reveal();
- $handlerProphecy = $this->prophesize(FileHandlerInterface::class);
- $handlerProphecy->read()->willReturn($cache);
- $handlerProphecy->getFile()->willReturn($cacheFile);
- $handlerProphecy->write(Argument::is($cache))->shouldBeCalled();
- $handler = $handlerProphecy->reveal();
- $manager = new FileCacheManager(
- $handler,
- $signature
- );
- $manager->setFile($file, $fileContent);
- }
- public function testSetFileSetsHashOfFileContentDuringDryRunIfCacheHasNoHash(): void
- {
- $isDryRun = true;
- $cacheFile = $this->getFile();
- $file = 'hello.php';
- $fileContent = '<?php echo "Hello!"';
- $cachedSignature = $this->prophesize(SignatureInterface::class)->reveal();
- $signatureProphecy = $this->prophesize(SignatureInterface::class);
- $signatureProphecy->equals(Argument::is($cachedSignature))->willReturn(true);
- $signature = $signatureProphecy->reveal();
- $cacheProphecy = $this->prophesize(CacheInterface::class);
- $cacheProphecy->getSignature()->willReturn($cachedSignature);
- $cacheProphecy->has(Argument::is($file))->willReturn(false);
- $cacheProphecy->set(Argument::is($file), Argument::is(md5($fileContent)))->shouldBeCalled();
- $cache = $cacheProphecy->reveal();
- $handlerProphecy = $this->prophesize(FileHandlerInterface::class);
- $handlerProphecy->read()->willReturn($cache);
- $handlerProphecy->getFile()->willReturn($cacheFile);
- $handlerProphecy->write(Argument::is($cache));
- $handler = $handlerProphecy->reveal();
- $manager = new FileCacheManager(
- $handler,
- $signature,
- $isDryRun
- );
- $manager->setFile($file, $fileContent);
- }
- public function testSetFileClearsHashDuringDryRunIfCachedHashIsDifferent(): void
- {
- $isDryRun = true;
- $cacheFile = $this->getFile();
- $file = 'hello.php';
- $fileContent = '<?php echo "Hello!"';
- $previousFileContent = '<?php echo "Hello, world!"';
- $cachedSignature = $this->prophesize(SignatureInterface::class)->reveal();
- $signatureProphecy = $this->prophesize(SignatureInterface::class);
- $signatureProphecy->equals(Argument::is($cachedSignature))->willReturn(true);
- $signature = $signatureProphecy->reveal();
- $cacheProphecy = $this->prophesize(CacheInterface::class);
- $cacheProphecy->getSignature()->willReturn($cachedSignature);
- $cacheProphecy->has(Argument::is($file))->willReturn(true);
- $cacheProphecy->get(Argument::is($file))->willReturn(md5($previousFileContent));
- $cacheProphecy->clear(Argument::is($file))->shouldBeCalled();
- $cache = $cacheProphecy->reveal();
- $handlerProphecy = $this->prophesize(FileHandlerInterface::class);
- $handlerProphecy->read()->willReturn($cache);
- $handlerProphecy->getFile()->willReturn($cacheFile);
- $handlerProphecy->write(Argument::is($cache));
- $handler = $handlerProphecy->reveal();
- $manager = new FileCacheManager(
- $handler,
- $signature,
- $isDryRun
- );
- $manager->setFile($file, $fileContent);
- }
- public function testSetFileUsesRelativePathToFile(): void
- {
- $cacheFile = $this->getFile();
- $file = '/foo/bar/baz/src/hello.php';
- $relativePathToFile = 'src/hello.php';
- $fileContent = '<?php echo "Hello!"';
- $directoryProphecy = $this->prophesize(\PhpCsFixer\Cache\DirectoryInterface::class);
- $directoryProphecy->getRelativePathTo(Argument::is($file))->willReturn($relativePathToFile);
- $cachedSignature = $this->prophesize(SignatureInterface::class)->reveal();
- $signatureProphecy = $this->prophesize(SignatureInterface::class);
- $signatureProphecy->equals(Argument::is($cachedSignature))->willReturn(true);
- $signature = $signatureProphecy->reveal();
- $cacheProphecy = $this->prophesize(CacheInterface::class);
- $cacheProphecy->getSignature()->willReturn($cachedSignature);
- $cacheProphecy->set(Argument::is($relativePathToFile), Argument::is(md5($fileContent)))->shouldBeCalled();
- $cache = $cacheProphecy->reveal();
- $handlerProphecy = $this->prophesize(FileHandlerInterface::class);
- $handlerProphecy->read()->willReturn($cache);
- $handlerProphecy->getFile()->willReturn($cacheFile);
- $handlerProphecy->write(Argument::is($cache));
- $handler = $handlerProphecy->reveal();
- $manager = new FileCacheManager(
- $handler,
- $signature,
- false,
- $directoryProphecy->reveal()
- );
- $manager->setFile($file, $fileContent);
- }
- private function getFile(): string
- {
- return __DIR__.'/../Fixtures/.php_cs.empty-cache';
- }
- }
|