FileCacheManagerTest.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773
  1. <?php
  2. /*
  3. * This file is part of PHP CS Fixer.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. * Dariusz Rumiński <dariusz.ruminski@gmail.com>
  7. *
  8. * This source file is subject to the MIT license that is bundled
  9. * with this source code in the file LICENSE.
  10. */
  11. namespace PhpCsFixer\Tests\Cache;
  12. use PhpCsFixer\Cache\CacheInterface;
  13. use PhpCsFixer\Cache\DirectoryInterface;
  14. use PhpCsFixer\Cache\FileCacheManager;
  15. use PhpCsFixer\Cache\FileHandlerInterface;
  16. use PhpCsFixer\Cache\SignatureInterface;
  17. /**
  18. * @author Andreas Möller <am@localheinz.com>
  19. *
  20. * @internal
  21. */
  22. final class FileCacheManagerTest extends \PHPUnit_Framework_TestCase
  23. {
  24. public function testIsFinal()
  25. {
  26. $reflection = new \ReflectionClass('PhpCsFixer\Cache\FileCacheManager');
  27. $this->assertTrue($reflection->isFinal());
  28. }
  29. public function testImplementsCacheManagerInterface()
  30. {
  31. $reflection = new \ReflectionClass('PhpCsFixer\Cache\FileCacheManager');
  32. $this->assertTrue($reflection->implementsInterface('PhpCsFixer\Cache\CacheManagerInterface'));
  33. }
  34. public function testCreatesCacheIfHandlerReturnedNoCache()
  35. {
  36. $signature = $this->getSignatureMock();
  37. $signature
  38. ->expects($this->never())
  39. ->method($this->anything())
  40. ;
  41. $handler = $this->getFileHandlerMock();
  42. $handler
  43. ->expects($this->once())
  44. ->method('read')
  45. ->willReturn(null)
  46. ;
  47. $handler
  48. ->expects($this->once())
  49. ->method('write')
  50. ->with($this->logicalAnd(
  51. $this->isInstanceOf('PhpCsFixer\Cache\CacheInterface'),
  52. $this->callback(function (CacheInterface $cache) use ($signature) {
  53. return $cache->getSignature() === $signature;
  54. })
  55. ))
  56. ;
  57. $handler
  58. ->method('getFile')
  59. ->willReturn($this->getFile())
  60. ;
  61. $manager = new FileCacheManager(
  62. $handler,
  63. $signature
  64. );
  65. unset($manager);
  66. }
  67. public function testCreatesCacheIfCachedSignatureIsDifferent()
  68. {
  69. $cachedSignature = $this->getSignatureMock();
  70. $cache = $this->getCacheMock();
  71. $cache
  72. ->expects($this->once())
  73. ->method('getSignature')
  74. ->willReturn($cachedSignature)
  75. ;
  76. $signature = $this->getSignatureMock();
  77. $signature
  78. ->expects($this->once())
  79. ->method('equals')
  80. ->with($this->identicalTo($cachedSignature))
  81. ->willReturn(false)
  82. ;
  83. $handler = $this->getFileHandlerMock();
  84. $handler
  85. ->expects($this->once())
  86. ->method('read')
  87. ->willReturn($cache)
  88. ;
  89. $handler
  90. ->expects($this->once())
  91. ->method('write')
  92. ->with($this->logicalAnd(
  93. $this->isInstanceOf('PhpCsFixer\Cache\CacheInterface'),
  94. $this->callback(function (CacheInterface $cache) use ($signature) {
  95. return $cache->getSignature() === $signature;
  96. })
  97. ))
  98. ;
  99. $handler
  100. ->method('getFile')
  101. ->willReturn($this->getFile())
  102. ;
  103. $manager = new FileCacheManager(
  104. $handler,
  105. $signature
  106. );
  107. unset($manager);
  108. }
  109. public function testUsesCacheIfCachedSignatureIsEqual()
  110. {
  111. $cachedSignature = $this->getSignatureMock();
  112. $signature = $this->getSignatureMock();
  113. $signature
  114. ->expects($this->once())
  115. ->method('equals')
  116. ->with($this->identicalTo($cachedSignature))
  117. ->willReturn(true)
  118. ;
  119. $cache = $this->getCacheMock();
  120. $cache
  121. ->expects($this->once())
  122. ->method('getSignature')
  123. ->willReturn($cachedSignature)
  124. ;
  125. $handler = $this->getFileHandlerMock();
  126. $handler
  127. ->expects($this->once())
  128. ->method('read')
  129. ->willReturn($cache)
  130. ;
  131. $handler
  132. ->expects($this->once())
  133. ->method('write')
  134. ->with($this->identicalTo($cache))
  135. ;
  136. $handler
  137. ->method('getFile')
  138. ->willReturn($this->getFile())
  139. ;
  140. $manager = new FileCacheManager(
  141. $handler,
  142. $signature
  143. );
  144. unset($manager);
  145. }
  146. public function testNeedFixingReturnsTrueIfCacheHasNoHash()
  147. {
  148. $file = 'hello.php';
  149. $fileContent = '<?php echo "Hello!"';
  150. $cachedSignature = $this->getSignatureMock();
  151. $signature = $this->getSignatureMock();
  152. $signature
  153. ->expects($this->once())
  154. ->method('equals')
  155. ->with($this->identicalTo($cachedSignature))
  156. ->willReturn(true)
  157. ;
  158. $cache = $this->getCacheMock();
  159. $cache
  160. ->expects($this->once())
  161. ->method('getSignature')
  162. ->willReturn($cachedSignature)
  163. ;
  164. $cache
  165. ->expects($this->once())
  166. ->method('has')
  167. ->with($this->identicalTo($file))
  168. ->willReturn(false)
  169. ;
  170. $cache
  171. ->expects($this->never())
  172. ->method('get')
  173. ->with($this->anything())
  174. ;
  175. $handler = $this->getFileHandlerMock();
  176. $handler
  177. ->expects($this->once())
  178. ->method('read')
  179. ->willReturn($cache)
  180. ;
  181. $handler
  182. ->method('getFile')
  183. ->willReturn($this->getFile())
  184. ;
  185. $manager = new FileCacheManager(
  186. $handler,
  187. $signature
  188. );
  189. $this->assertTrue($manager->needFixing($file, $fileContent));
  190. }
  191. public function testNeedFixingReturnsTrueIfCachedHashIsDifferent()
  192. {
  193. $file = 'hello.php';
  194. $fileContent = '<?php echo "Hello!"';
  195. $previousFileContent = '<?php echo "Hello, world!"';
  196. $cachedSignature = $this->getSignatureMock();
  197. $signature = $this->getSignatureMock();
  198. $signature
  199. ->expects($this->once())
  200. ->method('equals')
  201. ->with($this->identicalTo($cachedSignature))
  202. ->willReturn(true)
  203. ;
  204. $cache = $this->getCacheMock();
  205. $cache
  206. ->expects($this->once())
  207. ->method('getSignature')
  208. ->willReturn($cachedSignature)
  209. ;
  210. $cache
  211. ->expects($this->once())
  212. ->method('has')
  213. ->with($this->identicalTo($file))
  214. ->willReturn(true)
  215. ;
  216. $cache
  217. ->expects($this->once())
  218. ->method('get')
  219. ->with($this->identicalTo($file))
  220. ->willReturn(crc32($previousFileContent))
  221. ;
  222. $handler = $this->getFileHandlerMock();
  223. $handler
  224. ->expects($this->once())
  225. ->method('read')
  226. ->willReturn($cache)
  227. ;
  228. $handler
  229. ->method('getFile')
  230. ->willReturn($this->getFile())
  231. ;
  232. $manager = new FileCacheManager(
  233. $handler,
  234. $signature
  235. );
  236. $this->assertTrue($manager->needFixing($file, $fileContent));
  237. }
  238. public function testNeedFixingReturnsFalseIfCachedHashIsIdentical()
  239. {
  240. $file = 'hello.php';
  241. $fileContent = '<?php echo "Hello!"';
  242. $cachedSignature = $this->getSignatureMock();
  243. $signature = $this->getSignatureMock();
  244. $signature
  245. ->expects($this->once())
  246. ->method('equals')
  247. ->with($this->identicalTo($cachedSignature))
  248. ->willReturn(true)
  249. ;
  250. $cache = $this->getCacheMock();
  251. $cache
  252. ->expects($this->once())
  253. ->method('getSignature')
  254. ->willReturn($cachedSignature)
  255. ;
  256. $cache
  257. ->expects($this->once())
  258. ->method('has')
  259. ->with($this->identicalTo($file))
  260. ->willReturn(true)
  261. ;
  262. $cache
  263. ->expects($this->once())
  264. ->method('get')
  265. ->with($this->identicalTo($file))
  266. ->willReturn(crc32($fileContent))
  267. ;
  268. $handler = $this->getFileHandlerMock();
  269. $handler
  270. ->expects($this->once())
  271. ->method('read')
  272. ->willReturn($cache)
  273. ;
  274. $handler
  275. ->method('getFile')
  276. ->willReturn($this->getFile())
  277. ;
  278. $manager = new FileCacheManager(
  279. $handler,
  280. $signature
  281. );
  282. $this->assertFalse($manager->needFixing($file, $fileContent));
  283. }
  284. public function testNeedFixingUsesRelativePathToFile()
  285. {
  286. $cacheFile = $this->getFile();
  287. $file = '/foo/bar/baz/src/hello.php';
  288. $relativePathToFile = 'src/hello.php';
  289. $fileContent = '<?php echo "Hello!"';
  290. $directory = $this->getDirectoryMock();
  291. $directory
  292. ->expects($this->once())
  293. ->method('getRelativePathTo')
  294. ->with($this->identicalTo($file))
  295. ->willReturn($relativePathToFile)
  296. ;
  297. $cachedSignature = $this->getSignatureMock();
  298. $signature = $this->getSignatureMock();
  299. $signature
  300. ->expects($this->once())
  301. ->method('equals')
  302. ->with($this->identicalTo($cachedSignature))
  303. ->willReturn(true)
  304. ;
  305. $cache = $this->getCacheMock();
  306. $cache
  307. ->expects($this->once())
  308. ->method('getSignature')
  309. ->willReturn($cachedSignature)
  310. ;
  311. $cache
  312. ->expects($this->once())
  313. ->method('has')
  314. ->with($this->identicalTo($relativePathToFile))
  315. ->willReturn(true)
  316. ;
  317. $cache
  318. ->expects($this->once())
  319. ->method('get')
  320. ->with($this->identicalTo($relativePathToFile))
  321. ;
  322. $handler = $this->getFileHandlerMock();
  323. $handler
  324. ->expects($this->never())
  325. ->method('getFile')
  326. ->willReturn($cacheFile)
  327. ;
  328. $handler
  329. ->expects($this->once())
  330. ->method('read')
  331. ->willReturn($cache)
  332. ;
  333. $manager = new FileCacheManager(
  334. $handler,
  335. $signature,
  336. false,
  337. $directory
  338. );
  339. $manager->needFixing($file, $fileContent);
  340. }
  341. public function testSetFileSetsHashOfFileContent()
  342. {
  343. $cacheFile = $this->getFile();
  344. $file = 'hello.php';
  345. $fileContent = '<?php echo "Hello!"';
  346. $cachedSignature = $this->getSignatureMock();
  347. $signature = $this->getSignatureMock();
  348. $signature
  349. ->expects($this->once())
  350. ->method('equals')
  351. ->with($this->identicalTo($cachedSignature))
  352. ->willReturn(true)
  353. ;
  354. $cache = $this->getCacheMock();
  355. $cache
  356. ->expects($this->once())
  357. ->method('getSignature')
  358. ->willReturn($cachedSignature)
  359. ;
  360. $cache
  361. ->expects($this->never())
  362. ->method('has')
  363. ->with($this->anything())
  364. ;
  365. $cache
  366. ->expects($this->once())
  367. ->method('set')
  368. ->with(
  369. $this->identicalTo($file),
  370. $this->identicalTo(crc32($fileContent))
  371. )
  372. ;
  373. $handler = $this->getFileHandlerMock();
  374. $handler
  375. ->expects($this->once())
  376. ->method('getFile')
  377. ->willReturn($cacheFile)
  378. ;
  379. $handler
  380. ->expects($this->once())
  381. ->method('read')
  382. ->willReturn($cache)
  383. ;
  384. $handler
  385. ->expects($this->once())
  386. ->method('write')
  387. ->with($this->identicalTo($cache))
  388. ;
  389. $manager = new FileCacheManager(
  390. $handler,
  391. $signature
  392. );
  393. $manager->setFile($file, $fileContent);
  394. }
  395. public function testSetFileSetsHashOfFileContentDuringDryRunIfCacheHasNoHash()
  396. {
  397. $isDryRun = true;
  398. $cacheFile = $this->getFile();
  399. $file = 'hello.php';
  400. $fileContent = '<?php echo "Hello!"';
  401. $cachedSignature = $this->getSignatureMock();
  402. $signature = $this->getSignatureMock();
  403. $signature
  404. ->expects($this->once())
  405. ->method('equals')
  406. ->with($this->identicalTo($cachedSignature))
  407. ->willReturn(true)
  408. ;
  409. $cache = $this->getCacheMock();
  410. $cache
  411. ->expects($this->once())
  412. ->method('getSignature')
  413. ->willReturn($cachedSignature)
  414. ;
  415. $cache
  416. ->expects($this->once())
  417. ->method('has')
  418. ->with($this->identicalTo($file))
  419. ->willReturn(false)
  420. ;
  421. $cache
  422. ->expects($this->never())
  423. ->method('get')
  424. ->with($this->anything())
  425. ;
  426. $cache
  427. ->expects($this->once())
  428. ->method('set')
  429. ->with(
  430. $this->identicalTo($file),
  431. $this->identicalTo(crc32($fileContent))
  432. )
  433. ;
  434. $handler = $this->getFileHandlerMock();
  435. $handler
  436. ->expects($this->once())
  437. ->method('getFile')
  438. ->willReturn($cacheFile)
  439. ;
  440. $handler
  441. ->expects($this->once())
  442. ->method('read')
  443. ->willReturn($cache)
  444. ;
  445. $handler
  446. ->expects($this->once())
  447. ->method('write')
  448. ->with($this->identicalTo($cache))
  449. ;
  450. $manager = new FileCacheManager(
  451. $handler,
  452. $signature,
  453. $isDryRun
  454. );
  455. $manager->setFile($file, $fileContent);
  456. }
  457. public function testSetFileClearsHashDuringDryRunIfCachedHashIsDifferent()
  458. {
  459. $isDryRun = true;
  460. $cacheFile = $this->getFile();
  461. $file = 'hello.php';
  462. $fileContent = '<?php echo "Hello!"';
  463. $previousFileContent = '<?php echo "Hello, world!"';
  464. $cachedSignature = $this->getSignatureMock();
  465. $signature = $this->getSignatureMock();
  466. $signature
  467. ->expects($this->once())
  468. ->method('equals')
  469. ->with($this->identicalTo($cachedSignature))
  470. ->willReturn(true)
  471. ;
  472. $cache = $this->getCacheMock();
  473. $cache
  474. ->expects($this->once())
  475. ->method('getSignature')
  476. ->willReturn($cachedSignature)
  477. ;
  478. $cache
  479. ->expects($this->once())
  480. ->method('has')
  481. ->with($this->identicalTo($file))
  482. ->willReturn(true)
  483. ;
  484. $cache
  485. ->expects($this->once())
  486. ->method('get')
  487. ->with($this->identicalTo($file))
  488. ->willReturn(crc32($previousFileContent))
  489. ;
  490. $cache
  491. ->expects($this->once())
  492. ->method('clear')
  493. ->with($this->identicalTo($file))
  494. ;
  495. $cache
  496. ->expects($this->never())
  497. ->method('set')
  498. ->with($this->anything())
  499. ;
  500. $handler = $this->getFileHandlerMock();
  501. $handler
  502. ->expects($this->once())
  503. ->method('getFile')
  504. ->willReturn($cacheFile)
  505. ;
  506. $handler
  507. ->expects($this->once())
  508. ->method('read')
  509. ->willReturn($cache)
  510. ;
  511. $handler
  512. ->expects($this->once())
  513. ->method('write')
  514. ->with($this->identicalTo($cache))
  515. ;
  516. $manager = new FileCacheManager(
  517. $handler,
  518. $signature,
  519. $isDryRun
  520. );
  521. $manager->setFile($file, $fileContent);
  522. }
  523. public function testSetFileUsesRelativePathToFile()
  524. {
  525. $cacheFile = $this->getFile();
  526. $file = '/foo/bar/baz/src/hello.php';
  527. $relativePathToFile = 'src/hello.php';
  528. $fileContent = '<?php echo "Hello!"';
  529. $directory = $this->getDirectoryMock();
  530. $directory
  531. ->expects($this->once())
  532. ->method('getRelativePathTo')
  533. ->with($this->identicalTo($file))
  534. ->willReturn($relativePathToFile)
  535. ;
  536. $cachedSignature = $this->getSignatureMock();
  537. $signature = $this->getSignatureMock();
  538. $signature
  539. ->expects($this->once())
  540. ->method('equals')
  541. ->with($this->identicalTo($cachedSignature))
  542. ->willReturn(true)
  543. ;
  544. $cache = $this->getCacheMock();
  545. $cache
  546. ->expects($this->once())
  547. ->method('getSignature')
  548. ->willReturn($cachedSignature)
  549. ;
  550. $cache
  551. ->expects($this->once())
  552. ->method('set')
  553. ->with(
  554. $this->identicalTo($relativePathToFile),
  555. $this->identicalTo(crc32($fileContent))
  556. )
  557. ;
  558. $handler = $this->getFileHandlerMock();
  559. $handler
  560. ->expects($this->never())
  561. ->method('getFile')
  562. ->willReturn($cacheFile)
  563. ;
  564. $handler
  565. ->expects($this->once())
  566. ->method('read')
  567. ->willReturn($cache)
  568. ;
  569. $handler
  570. ->expects($this->once())
  571. ->method('write')
  572. ->with($this->identicalTo($cache))
  573. ;
  574. $manager = new FileCacheManager(
  575. $handler,
  576. $signature,
  577. false,
  578. $directory
  579. );
  580. $manager->setFile($file, $fileContent);
  581. }
  582. /**
  583. * @return string
  584. */
  585. private function getFile()
  586. {
  587. return __DIR__.'/../Fixtures/.php_cs.empty-cache';
  588. }
  589. /**
  590. * @return \PHPUnit_Framework_MockObject_MockObject|FileHandlerInterface
  591. */
  592. private function getFileHandlerMock()
  593. {
  594. return $this->getMockBuilder('PhpCsFixer\Cache\FileHandlerInterface')->getMock();
  595. }
  596. /**
  597. * @return \PHPUnit_Framework_MockObject_MockObject|CacheInterface
  598. */
  599. private function getCacheMock()
  600. {
  601. return $this->getMockBuilder('PhpCsFixer\Cache\CacheInterface')->getMock();
  602. }
  603. /**
  604. * @return \PHPUnit_Framework_MockObject_MockObject|SignatureInterface
  605. */
  606. private function getSignatureMock()
  607. {
  608. return $this->getMockBuilder('PhpCsFixer\Cache\SignatureInterface')->getMock();
  609. }
  610. /**
  611. * @return \PHPUnit_Framework_MockObject_MockObject|DirectoryInterface
  612. */
  613. private function getDirectoryMock()
  614. {
  615. return $this->getMockBuilder('PhpCsFixer\Cache\DirectoryInterface')->getMock();
  616. }
  617. }