CacheTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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\Test\TestCase;
  13. use Composer\Cache;
  14. use Composer\Util\Filesystem;
  15. class CacheTest extends TestCase
  16. {
  17. private $files;
  18. private $root;
  19. private $finder;
  20. private $filesystem;
  21. private $cache;
  22. public function setUp()
  23. {
  24. $this->root = $this->getUniqueTmpDirectory();
  25. $this->files = array();
  26. $zeros = str_repeat('0', 1000);
  27. for ($i = 0; $i < 4; $i++) {
  28. file_put_contents("{$this->root}/cached.file{$i}.zip", $zeros);
  29. $this->files[] = new \SplFileInfo("{$this->root}/cached.file{$i}.zip");
  30. }
  31. $this->finder = $this->getMockBuilder('Symfony\Component\Finder\Finder')->disableOriginalConstructor()->getMock();
  32. $this->filesystem = $this->getMockBuilder('Composer\Util\Filesystem')->getMock();
  33. $io = $this->getMockBuilder('Composer\IO\IOInterface')->getMock();
  34. $this->cache = $this->getMockBuilder('Composer\Cache')
  35. ->setMethods(array('getFinder'))
  36. ->setConstructorArgs(array($io, $this->root))
  37. ->getMock();
  38. $this->cache
  39. ->expects($this->any())
  40. ->method('getFinder')
  41. ->will($this->returnValue($this->finder));
  42. }
  43. protected function tearDown()
  44. {
  45. if (is_dir($this->root)) {
  46. $fs = new Filesystem;
  47. $fs->removeDirectory($this->root);
  48. }
  49. }
  50. public function testRemoveOutdatedFiles()
  51. {
  52. $outdated = array_slice($this->files, 1);
  53. $this->finder
  54. ->expects($this->once())
  55. ->method('getIterator')
  56. ->will($this->returnValue(new \ArrayIterator($outdated)));
  57. $this->finder
  58. ->expects($this->once())
  59. ->method('date')
  60. ->will($this->returnValue($this->finder));
  61. $this->cache->gc(600, 1024 * 1024 * 1024);
  62. for ($i = 1; $i < 4; $i++) {
  63. $this->assertFileNotExists("{$this->root}/cached.file{$i}.zip");
  64. }
  65. $this->assertFileExists("{$this->root}/cached.file0.zip");
  66. }
  67. public function testRemoveFilesWhenCacheIsTooLarge()
  68. {
  69. $emptyFinder = $this->getMockBuilder('Symfony\Component\Finder\Finder')->disableOriginalConstructor()->getMock();
  70. $emptyFinder
  71. ->expects($this->once())
  72. ->method('getIterator')
  73. ->will($this->returnValue(new \EmptyIterator()));
  74. $this->finder
  75. ->expects($this->once())
  76. ->method('date')
  77. ->will($this->returnValue($emptyFinder));
  78. $this->finder
  79. ->expects($this->once())
  80. ->method('getIterator')
  81. ->will($this->returnValue(new \ArrayIterator($this->files)));
  82. $this->finder
  83. ->expects($this->once())
  84. ->method('sortByAccessedTime')
  85. ->will($this->returnValue($this->finder));
  86. $this->cache->gc(600, 1500);
  87. for ($i = 0; $i < 3; $i++) {
  88. $this->assertFileNotExists("{$this->root}/cached.file{$i}.zip");
  89. }
  90. $this->assertFileExists("{$this->root}/cached.file3.zip");
  91. }
  92. public function testClearCache()
  93. {
  94. $io = $this->getMockBuilder('Composer\IO\IOInterface')->getMock();
  95. $this->cache = new Cache($io, $this->root, 'a-z0-9.', $this->filesystem);
  96. $this->assertTrue($this->cache->clear());
  97. }
  98. }