CacheTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. use Composer\TestCase;
  14. class CacheTest extends TestCase
  15. {
  16. private $files, $root, $finder, $cache;
  17. public function setUp()
  18. {
  19. $this->root = sys_get_temp_dir() . '/composer_testdir';
  20. $this->ensureDirectoryExistsAndClear($this->root);
  21. $this->files = array();
  22. $zeros = str_repeat('0', 1000);
  23. for ($i = 0; $i < 4; $i++) {
  24. file_put_contents("{$this->root}/cached.file{$i}.zip", $zeros);
  25. $this->files[] = new \SplFileInfo("{$this->root}/cached.file{$i}.zip");
  26. }
  27. $this->finder = $this->getMockBuilder('Symfony\Component\Finder\Finder')->disableOriginalConstructor()->getMock();
  28. $io = $this->getMock('Composer\IO\IOInterface');
  29. $this->cache = $this->getMock(
  30. 'Composer\Cache',
  31. array('getFinder'),
  32. array($io, $this->root)
  33. );
  34. $this->cache
  35. ->expects($this->any())
  36. ->method('getFinder')
  37. ->will($this->returnValue($this->finder));
  38. }
  39. public function testRemoveOutdatedFiles()
  40. {
  41. if (defined('HHVM_VERSION')) {
  42. $this->markTestSkipped('Test causes intermittent failures with HHVM on Travis');
  43. }
  44. // sleeping a bit to let the filesystem cool down on travis or it has intermittent failures
  45. usleep(50000);
  46. $outdated = array_slice($this->files, 1);
  47. $this->finder
  48. ->expects($this->once())
  49. ->method('getIterator')
  50. ->will($this->returnValue(new \ArrayIterator($outdated)));
  51. $this->finder
  52. ->expects($this->once())
  53. ->method('date')
  54. ->will($this->returnValue($this->finder));
  55. $this->cache->gc(600, 1024 * 1024 * 1024);
  56. for ($i = 1; $i < 4; $i++) {
  57. $this->assertFileNotExists("{$this->root}/cached.file{$i}.zip");
  58. }
  59. $this->assertFileExists("{$this->root}/cached.file0.zip");
  60. }
  61. public function testRemoveFilesWhenCacheIsTooLarge()
  62. {
  63. if (defined('HHVM_VERSION')) {
  64. $this->markTestSkipped('Test causes intermittent failures with HHVM on Travis');
  65. }
  66. // sleeping a bit to let the filesystem cool down on travis or it has intermittent failures
  67. usleep(50000);
  68. $emptyFinder = $this->getMockBuilder('Symfony\Component\Finder\Finder')->disableOriginalConstructor()->getMock();
  69. $emptyFinder
  70. ->expects($this->once())
  71. ->method('getIterator')
  72. ->will($this->returnValue(new \EmptyIterator()));
  73. $this->finder
  74. ->expects($this->once())
  75. ->method('date')
  76. ->will($this->returnValue($emptyFinder));
  77. $this->finder
  78. ->expects($this->once())
  79. ->method('getIterator')
  80. ->will($this->returnValue(new \ArrayIterator($this->files)));
  81. $this->finder
  82. ->expects($this->once())
  83. ->method('sortByAccessedTime')
  84. ->will($this->returnValue($this->finder));
  85. $this->cache->gc(600, 1500);
  86. for ($i = 0; $i < 3; $i++) {
  87. $this->assertFileNotExists("{$this->root}/cached.file{$i}.zip");
  88. }
  89. $this->assertFileExists("{$this->root}/cached.file3.zip");
  90. }
  91. }