CacheTest.php 3.6 KB

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