CacheTest.php 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. /*
  3. * This file is part of Composer.
  4. *
  5. * (c) Nils Adermann <naderman@naderman.de>
  6. * Jordi Boggiano <j.boggiano@seld.be>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Composer\Test;
  12. use Composer\Cache;
  13. class CacheTest extends TestCase
  14. {
  15. private $files, $root, $finder, $cache;
  16. public function setUp()
  17. {
  18. $this->root = sys_get_temp_dir() . '/composer_testdir';
  19. $this->ensureDirectoryExistsAndClear($this->root);
  20. $this->files = array();
  21. $zeros = str_repeat('0', 1000);
  22. for ($i = 0; $i < 4; $i++) {
  23. file_put_contents("{$this->root}/cached.file{$i}.zip", $zeros);
  24. $this->files[] = new \SplFileInfo("{$this->root}/cached.file{$i}.zip");
  25. }
  26. $this->finder = $this->getMockBuilder('Symfony\Component\Finder\Finder')->disableOriginalConstructor()->getMock();
  27. $io = $this->getMock('Composer\IO\IOInterface');
  28. $this->cache = $this->getMock(
  29. 'Composer\Cache',
  30. array('getFinder'),
  31. array($io, $this->root)
  32. );
  33. $this->cache
  34. ->expects($this->any())
  35. ->method('getFinder')
  36. ->will($this->returnValue($this->finder));
  37. }
  38. public function testRemoveOutdatedFiles()
  39. {
  40. $outdated = array_slice($this->files, 1);
  41. $this->finder
  42. ->expects($this->once())
  43. ->method('getIterator')
  44. ->will($this->returnValue(new \ArrayIterator($outdated)));
  45. $this->finder
  46. ->expects($this->once())
  47. ->method('date')
  48. ->will($this->returnValue($this->finder));
  49. $this->cache->gc(600, 1024 * 1024 * 1024);
  50. for ($i = 1; $i < 4; $i++) {
  51. $this->assertFileNotExists("{$this->root}/cached.file{$i}.zip");
  52. }
  53. $this->assertFileExists("{$this->root}/cached.file0.zip");
  54. }
  55. public function testRemoveFilesWhenCacheIsTooLarge()
  56. {
  57. $emptyFinder = $this->getMockBuilder('Symfony\Component\Finder\Finder')->disableOriginalConstructor()->getMock();
  58. $emptyFinder
  59. ->expects($this->once())
  60. ->method('getIterator')
  61. ->will($this->returnValue(new \EmptyIterator()));
  62. $this->finder
  63. ->expects($this->once())
  64. ->method('date')
  65. ->will($this->returnValue($emptyFinder));
  66. $this->finder
  67. ->expects($this->once())
  68. ->method('getIterator')
  69. ->will($this->returnValue(new \ArrayIterator($this->files)));
  70. $this->finder
  71. ->expects($this->once())
  72. ->method('sortByAccessedTime')
  73. ->will($this->returnValue($this->finder));
  74. $this->cache->gc(600, 1500);
  75. for ($i = 0; $i < 3; $i++) {
  76. $this->assertFileNotExists("{$this->root}/cached.file{$i}.zip");
  77. }
  78. $this->assertFileExists("{$this->root}/cached.file3.zip");
  79. }
  80. }