CacheTest.php 3.4 KB

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