FileCacheManagerTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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\Cache;
  13. use PhpCsFixer\Cache\CacheInterface;
  14. use PhpCsFixer\Cache\CacheManagerInterface;
  15. use PhpCsFixer\Cache\FileCacheManager;
  16. use PhpCsFixer\Cache\FileHandlerInterface;
  17. use PhpCsFixer\Cache\SignatureInterface;
  18. use PhpCsFixer\Tests\TestCase;
  19. use Prophecy\Argument;
  20. /**
  21. * @author Andreas Möller <am@localheinz.com>
  22. *
  23. * @internal
  24. *
  25. * @covers \PhpCsFixer\Cache\FileCacheManager
  26. */
  27. final class FileCacheManagerTest extends TestCase
  28. {
  29. public function testIsFinal(): void
  30. {
  31. $reflection = new \ReflectionClass(FileCacheManager::class);
  32. self::assertTrue($reflection->isFinal());
  33. }
  34. public function testImplementsCacheManagerInterface(): void
  35. {
  36. $reflection = new \ReflectionClass(FileCacheManager::class);
  37. self::assertTrue($reflection->implementsInterface(CacheManagerInterface::class));
  38. }
  39. public function testCreatesCacheIfHandlerReturnedNoCache(): void
  40. {
  41. $signature = $this->prophesize(SignatureInterface::class)->reveal();
  42. $handlerProphecy = $this->prophesize(FileHandlerInterface::class);
  43. $handlerProphecy->read()->shouldBeCalled()->willReturn(null);
  44. $handlerProphecy->write(Argument::that(static fn (CacheInterface $cache): bool => $cache->getSignature() === $signature))->shouldBeCalled();
  45. $handler = $handlerProphecy->reveal();
  46. $manager = new FileCacheManager(
  47. $handler,
  48. $signature
  49. );
  50. unset($manager);
  51. }
  52. public function testCreatesCacheIfCachedSignatureIsDifferent(): void
  53. {
  54. $cachedSignature = $this->prophesize(SignatureInterface::class)->reveal();
  55. $signatureProphecy = $this->prophesize(SignatureInterface::class);
  56. $signatureProphecy->equals(Argument::is($cachedSignature))->shouldBeCalled()->willReturn(false);
  57. $signature = $signatureProphecy->reveal();
  58. $cacheProphecy = $this->prophesize(CacheInterface::class);
  59. $cacheProphecy->getSignature()->shouldBeCalled()->willReturn($cachedSignature);
  60. $cache = $cacheProphecy->reveal();
  61. $handlerProphecy = $this->prophesize(FileHandlerInterface::class);
  62. $handlerProphecy->read()->shouldBeCalled()->willReturn($cache);
  63. $handlerProphecy->write(Argument::that(static fn (CacheInterface $cache): bool => $cache->getSignature() === $signature))->shouldBeCalled();
  64. $handler = $handlerProphecy->reveal();
  65. $manager = new FileCacheManager(
  66. $handler,
  67. $signature
  68. );
  69. unset($manager);
  70. }
  71. public function testUsesCacheIfCachedSignatureIsEqualAndNoFileWasUpdated(): void
  72. {
  73. $cachedSignature = $this->prophesize(SignatureInterface::class)->reveal();
  74. $signatureProphecy = $this->prophesize(SignatureInterface::class);
  75. $signatureProphecy->equals(Argument::is($cachedSignature))->willReturn(true);
  76. $signature = $signatureProphecy->reveal();
  77. $cacheProphecy = $this->prophesize(CacheInterface::class);
  78. $cacheProphecy->getSignature()->shouldBeCalled()->willReturn($cachedSignature);
  79. $cache = $cacheProphecy->reveal();
  80. $handlerProphecy = $this->prophesize(FileHandlerInterface::class);
  81. $handlerProphecy->read()->shouldBeCalled()->willReturn($cache);
  82. $handlerProphecy->write(Argument::is($cache))->shouldNotBeCalled();
  83. $handler = $handlerProphecy->reveal();
  84. $manager = new FileCacheManager(
  85. $handler,
  86. $signature
  87. );
  88. unset($manager);
  89. }
  90. public function testNeedFixingReturnsTrueIfCacheHasNoHash(): void
  91. {
  92. $file = 'hello.php';
  93. $fileContent = '<?php echo "Hello!"';
  94. $cachedSignature = $this->prophesize(SignatureInterface::class)->reveal();
  95. $signatureProphecy = $this->prophesize(SignatureInterface::class);
  96. $signatureProphecy->equals(Argument::is($cachedSignature))->willReturn(true);
  97. $signature = $signatureProphecy->reveal();
  98. $cacheProphecy = $this->prophesize(CacheInterface::class);
  99. $cacheProphecy->getSignature()->willReturn($cachedSignature);
  100. $cacheProphecy->has(Argument::is($file))->willReturn(false);
  101. $cache = $cacheProphecy->reveal();
  102. $handlerProphecy = $this->prophesize(FileHandlerInterface::class);
  103. $handlerProphecy->read()->willReturn($cache);
  104. $handlerProphecy->getFile()->willReturn($this->getFile());
  105. $handlerProphecy->write(Argument::is($cache));
  106. $handler = $handlerProphecy->reveal();
  107. $manager = new FileCacheManager(
  108. $handler,
  109. $signature
  110. );
  111. self::assertTrue($manager->needFixing($file, $fileContent));
  112. }
  113. public function testNeedFixingReturnsTrueIfCachedHashIsDifferent(): void
  114. {
  115. $file = 'hello.php';
  116. $fileContent = '<?php echo "Hello!"';
  117. $previousFileContent = '<?php echo "Hello, world!"';
  118. $cachedSignature = $this->prophesize(SignatureInterface::class)->reveal();
  119. $signatureProphecy = $this->prophesize(SignatureInterface::class);
  120. $signatureProphecy->equals(Argument::is($cachedSignature))->willReturn(true);
  121. $signature = $signatureProphecy->reveal();
  122. $cacheProphecy = $this->prophesize(CacheInterface::class);
  123. $cacheProphecy->getSignature()->willReturn($cachedSignature);
  124. $cacheProphecy->has(Argument::is($file))->willReturn(true);
  125. $cacheProphecy->get(Argument::is($file))->willReturn(md5($previousFileContent));
  126. $cache = $cacheProphecy->reveal();
  127. $handlerProphecy = $this->prophesize(FileHandlerInterface::class);
  128. $handlerProphecy->read()->willReturn($cache);
  129. $handlerProphecy->getFile()->willReturn($this->getFile());
  130. $handlerProphecy->write(Argument::is($cache));
  131. $handler = $handlerProphecy->reveal();
  132. $manager = new FileCacheManager(
  133. $handler,
  134. $signature
  135. );
  136. self::assertTrue($manager->needFixing($file, $fileContent));
  137. }
  138. public function testNeedFixingReturnsFalseIfCachedHashIsIdentical(): void
  139. {
  140. $file = 'hello.php';
  141. $fileContent = '<?php echo "Hello!"';
  142. $cachedSignature = $this->prophesize(SignatureInterface::class)->reveal();
  143. $signatureProphecy = $this->prophesize(SignatureInterface::class);
  144. $signatureProphecy->equals(Argument::is($cachedSignature))->willReturn(true);
  145. $signature = $signatureProphecy->reveal();
  146. $cacheProphecy = $this->prophesize(CacheInterface::class);
  147. $cacheProphecy->getSignature()->willReturn($cachedSignature);
  148. $cacheProphecy->has(Argument::is($file))->willReturn(true);
  149. $cacheProphecy->get(Argument::is($file))->willReturn(md5($fileContent));
  150. $cache = $cacheProphecy->reveal();
  151. $handlerProphecy = $this->prophesize(FileHandlerInterface::class);
  152. $handlerProphecy->read()->willReturn($cache);
  153. $handlerProphecy->getFile()->willReturn($this->getFile());
  154. $handlerProphecy->write(Argument::is($cache));
  155. $handler = $handlerProphecy->reveal();
  156. $manager = new FileCacheManager(
  157. $handler,
  158. $signature
  159. );
  160. self::assertFalse($manager->needFixing($file, $fileContent));
  161. }
  162. public function testNeedFixingUsesRelativePathToFile(): void
  163. {
  164. $cacheFile = $this->getFile();
  165. $file = '/foo/bar/baz/src/hello.php';
  166. $relativePathToFile = 'src/hello.php';
  167. $fileContent = '<?php echo "Hello!"';
  168. $directoryProphecy = $this->prophesize(\PhpCsFixer\Cache\DirectoryInterface::class);
  169. $directoryProphecy->getRelativePathTo(Argument::is($file))->willReturn($relativePathToFile);
  170. $cachedSignature = $this->prophesize(SignatureInterface::class)->reveal();
  171. $signatureProphecy = $this->prophesize(SignatureInterface::class);
  172. $signatureProphecy->equals(Argument::is($cachedSignature))->willReturn(true);
  173. $signature = $signatureProphecy->reveal();
  174. $cacheProphecy = $this->prophesize(CacheInterface::class);
  175. $cacheProphecy->getSignature()->willReturn($cachedSignature);
  176. $cacheProphecy->has(Argument::is($relativePathToFile))->willReturn(true);
  177. $cacheProphecy->has(Argument::is($relativePathToFile))->willReturn(0);
  178. $cache = $cacheProphecy->reveal();
  179. $handlerProphecy = $this->prophesize(FileHandlerInterface::class);
  180. $handlerProphecy->read()->willReturn($cache);
  181. $handlerProphecy->getFile()->willReturn($cacheFile);
  182. $handlerProphecy->write(Argument::is($cache));
  183. $handler = $handlerProphecy->reveal();
  184. $manager = new FileCacheManager(
  185. $handler,
  186. $signature,
  187. false,
  188. $directoryProphecy->reveal()
  189. );
  190. self::assertTrue($manager->needFixing($file, $fileContent));
  191. }
  192. public function testSetFileSetsHashOfFileContent(): void
  193. {
  194. $cacheFile = $this->getFile();
  195. $file = 'hello.php';
  196. $fileContent = '<?php echo "Hello!"';
  197. $cachedSignature = $this->prophesize(SignatureInterface::class)->reveal();
  198. $signatureProphecy = $this->prophesize(SignatureInterface::class);
  199. $signatureProphecy->equals(Argument::is($cachedSignature))->willReturn(true);
  200. $signature = $signatureProphecy->reveal();
  201. $cacheProphecy = $this->prophesize(CacheInterface::class);
  202. $cacheProphecy->getSignature()->willReturn($cachedSignature);
  203. $cacheProphecy->set(Argument::is($file), Argument::is(md5($fileContent)))->shouldBeCalled();
  204. $cache = $cacheProphecy->reveal();
  205. $handlerProphecy = $this->prophesize(FileHandlerInterface::class);
  206. $handlerProphecy->read()->willReturn($cache);
  207. $handlerProphecy->getFile()->willReturn($cacheFile);
  208. $handlerProphecy->write(Argument::is($cache))->shouldBeCalled();
  209. $handler = $handlerProphecy->reveal();
  210. $manager = new FileCacheManager(
  211. $handler,
  212. $signature
  213. );
  214. $manager->setFile($file, $fileContent);
  215. }
  216. public function testSetFileSetsHashOfFileContentDuringDryRunIfCacheHasNoHash(): void
  217. {
  218. $isDryRun = true;
  219. $cacheFile = $this->getFile();
  220. $file = 'hello.php';
  221. $fileContent = '<?php echo "Hello!"';
  222. $cachedSignature = $this->prophesize(SignatureInterface::class)->reveal();
  223. $signatureProphecy = $this->prophesize(SignatureInterface::class);
  224. $signatureProphecy->equals(Argument::is($cachedSignature))->willReturn(true);
  225. $signature = $signatureProphecy->reveal();
  226. $cacheProphecy = $this->prophesize(CacheInterface::class);
  227. $cacheProphecy->getSignature()->willReturn($cachedSignature);
  228. $cacheProphecy->has(Argument::is($file))->willReturn(false);
  229. $cacheProphecy->set(Argument::is($file), Argument::is(md5($fileContent)))->shouldBeCalled();
  230. $cache = $cacheProphecy->reveal();
  231. $handlerProphecy = $this->prophesize(FileHandlerInterface::class);
  232. $handlerProphecy->read()->willReturn($cache);
  233. $handlerProphecy->getFile()->willReturn($cacheFile);
  234. $handlerProphecy->write(Argument::is($cache));
  235. $handler = $handlerProphecy->reveal();
  236. $manager = new FileCacheManager(
  237. $handler,
  238. $signature,
  239. $isDryRun
  240. );
  241. $manager->setFile($file, $fileContent);
  242. }
  243. public function testSetFileClearsHashDuringDryRunIfCachedHashIsDifferent(): void
  244. {
  245. $isDryRun = true;
  246. $cacheFile = $this->getFile();
  247. $file = 'hello.php';
  248. $fileContent = '<?php echo "Hello!"';
  249. $previousFileContent = '<?php echo "Hello, world!"';
  250. $cachedSignature = $this->prophesize(SignatureInterface::class)->reveal();
  251. $signatureProphecy = $this->prophesize(SignatureInterface::class);
  252. $signatureProphecy->equals(Argument::is($cachedSignature))->willReturn(true);
  253. $signature = $signatureProphecy->reveal();
  254. $cacheProphecy = $this->prophesize(CacheInterface::class);
  255. $cacheProphecy->getSignature()->willReturn($cachedSignature);
  256. $cacheProphecy->has(Argument::is($file))->willReturn(true);
  257. $cacheProphecy->get(Argument::is($file))->willReturn(md5($previousFileContent));
  258. $cacheProphecy->clear(Argument::is($file))->shouldBeCalled();
  259. $cache = $cacheProphecy->reveal();
  260. $handlerProphecy = $this->prophesize(FileHandlerInterface::class);
  261. $handlerProphecy->read()->willReturn($cache);
  262. $handlerProphecy->getFile()->willReturn($cacheFile);
  263. $handlerProphecy->write(Argument::is($cache));
  264. $handler = $handlerProphecy->reveal();
  265. $manager = new FileCacheManager(
  266. $handler,
  267. $signature,
  268. $isDryRun
  269. );
  270. $manager->setFile($file, $fileContent);
  271. }
  272. public function testSetFileUsesRelativePathToFile(): void
  273. {
  274. $cacheFile = $this->getFile();
  275. $file = '/foo/bar/baz/src/hello.php';
  276. $relativePathToFile = 'src/hello.php';
  277. $fileContent = '<?php echo "Hello!"';
  278. $directoryProphecy = $this->prophesize(\PhpCsFixer\Cache\DirectoryInterface::class);
  279. $directoryProphecy->getRelativePathTo(Argument::is($file))->willReturn($relativePathToFile);
  280. $cachedSignature = $this->prophesize(SignatureInterface::class)->reveal();
  281. $signatureProphecy = $this->prophesize(SignatureInterface::class);
  282. $signatureProphecy->equals(Argument::is($cachedSignature))->willReturn(true);
  283. $signature = $signatureProphecy->reveal();
  284. $cacheProphecy = $this->prophesize(CacheInterface::class);
  285. $cacheProphecy->getSignature()->willReturn($cachedSignature);
  286. $cacheProphecy->set(Argument::is($relativePathToFile), Argument::is(md5($fileContent)))->shouldBeCalled();
  287. $cache = $cacheProphecy->reveal();
  288. $handlerProphecy = $this->prophesize(FileHandlerInterface::class);
  289. $handlerProphecy->read()->willReturn($cache);
  290. $handlerProphecy->getFile()->willReturn($cacheFile);
  291. $handlerProphecy->write(Argument::is($cache));
  292. $handler = $handlerProphecy->reveal();
  293. $manager = new FileCacheManager(
  294. $handler,
  295. $signature,
  296. false,
  297. $directoryProphecy->reveal()
  298. );
  299. $manager->setFile($file, $fileContent);
  300. }
  301. private function getFile(): string
  302. {
  303. return __DIR__.'/../Fixtures/.php_cs.empty-cache';
  304. }
  305. }