CacheTest.php 3.3 KB

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