CacheTest.php 3.1 KB

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