CacheTest.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. $outdated = array_slice($this->files, 1);
  42. $this->finder
  43. ->expects($this->once())
  44. ->method('getIterator')
  45. ->will($this->returnValue(new \ArrayIterator($outdated)));
  46. $this->finder
  47. ->expects($this->once())
  48. ->method('date')
  49. ->will($this->returnValue($this->finder));
  50. $this->cache->gc(600, 1024 * 1024 * 1024);
  51. for ($i = 1; $i < 4; $i++) {
  52. $this->assertFileNotExists("{$this->root}/cached.file{$i}.zip");
  53. }
  54. $this->assertFileExists("{$this->root}/cached.file0.zip");
  55. }
  56. public function testRemoveFilesWhenCacheIsTooLarge()
  57. {
  58. $emptyFinder = $this->getMockBuilder('Symfony\Component\Finder\Finder')->disableOriginalConstructor()->getMock();
  59. $emptyFinder
  60. ->expects($this->once())
  61. ->method('getIterator')
  62. ->will($this->returnValue(new \EmptyIterator()));
  63. $this->finder
  64. ->expects($this->once())
  65. ->method('date')
  66. ->will($this->returnValue($emptyFinder));
  67. $this->finder
  68. ->expects($this->once())
  69. ->method('getIterator')
  70. ->will($this->returnValue(new \ArrayIterator($this->files)));
  71. $this->finder
  72. ->expects($this->once())
  73. ->method('sortByAccessedTime')
  74. ->will($this->returnValue($this->finder));
  75. $this->cache->gc(600, 1500);
  76. for ($i = 0; $i < 3; $i++) {
  77. $this->assertFileNotExists("{$this->root}/cached.file{$i}.zip");
  78. }
  79. $this->assertFileExists("{$this->root}/cached.file3.zip");
  80. }
  81. }