FileCacheManagerTest.php 12 KB

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