CacheTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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\TestCase;
  13. use Composer\Util\Filesystem;
  14. class CacheTest extends TestCase
  15. {
  16. private $files, $root, $finder, $cache;
  17. public function setUp()
  18. {
  19. if (getenv('TRAVIS')) {
  20. $this->markTestSkipped('Test causes intermittent failures on Travis');
  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->getMock('Composer\IO\IOInterface');
  31. $this->cache = $this->getMock(
  32. 'Composer\Cache',
  33. array('getFinder'),
  34. array($io, $this->root)
  35. );
  36. $this->cache
  37. ->expects($this->any())
  38. ->method('getFinder')
  39. ->will($this->returnValue($this->finder));
  40. }
  41. protected function tearDown()
  42. {
  43. if (is_dir($this->root)) {
  44. $fs = new Filesystem;
  45. $fs->removeDirectory($this->root);
  46. }
  47. }
  48. public function testRemoveOutdatedFiles()
  49. {
  50. $outdated = array_slice($this->files, 1);
  51. $this->finder
  52. ->expects($this->once())
  53. ->method('getIterator')
  54. ->will($this->returnValue(new \ArrayIterator($outdated)));
  55. $this->finder
  56. ->expects($this->once())
  57. ->method('date')
  58. ->will($this->returnValue($this->finder));
  59. $this->cache->gc(600, 1024 * 1024 * 1024);
  60. for ($i = 1; $i < 4; $i++) {
  61. $this->assertFileNotExists("{$this->root}/cached.file{$i}.zip");
  62. }
  63. $this->assertFileExists("{$this->root}/cached.file0.zip");
  64. }
  65. public function testRemoveFilesWhenCacheIsTooLarge()
  66. {
  67. $emptyFinder = $this->getMockBuilder('Symfony\Component\Finder\Finder')->disableOriginalConstructor()->getMock();
  68. $emptyFinder
  69. ->expects($this->once())
  70. ->method('getIterator')
  71. ->will($this->returnValue(new \EmptyIterator()));
  72. $this->finder
  73. ->expects($this->once())
  74. ->method('date')
  75. ->will($this->returnValue($emptyFinder));
  76. $this->finder
  77. ->expects($this->once())
  78. ->method('getIterator')
  79. ->will($this->returnValue(new \ArrayIterator($this->files)));
  80. $this->finder
  81. ->expects($this->once())
  82. ->method('sortByAccessedTime')
  83. ->will($this->returnValue($this->finder));
  84. $this->cache->gc(600, 1500);
  85. for ($i = 0; $i < 3; $i++) {
  86. $this->assertFileNotExists("{$this->root}/cached.file{$i}.zip");
  87. }
  88. $this->assertFileExists("{$this->root}/cached.file3.zip");
  89. }
  90. }