2012-12-16 19:19:16 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of Composer.
|
|
|
|
*
|
|
|
|
* (c) Nils Adermann <naderman@naderman.de>
|
|
|
|
* Jordi Boggiano <j.boggiano@seld.be>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Composer\Test;
|
|
|
|
|
2018-11-12 14:23:32 +00:00
|
|
|
use Composer\Test\TestCase;
|
2019-02-18 13:12:01 +00:00
|
|
|
use Composer\Cache;
|
2015-12-14 14:35:27 +00:00
|
|
|
use Composer\Util\Filesystem;
|
2012-12-16 19:19:16 +00:00
|
|
|
|
|
|
|
class CacheTest extends TestCase
|
|
|
|
{
|
2017-03-08 14:07:29 +00:00
|
|
|
private $files;
|
|
|
|
private $root;
|
|
|
|
private $finder;
|
2019-02-18 13:03:23 +00:00
|
|
|
private $filesystem;
|
2017-03-08 14:07:29 +00:00
|
|
|
private $cache;
|
2012-12-16 19:19:16 +00:00
|
|
|
|
|
|
|
public function setUp()
|
|
|
|
{
|
2016-01-21 12:01:55 +00:00
|
|
|
$this->root = $this->getUniqueTmpDirectory();
|
2012-12-16 19:19:16 +00:00
|
|
|
$this->files = array();
|
|
|
|
$zeros = str_repeat('0', 1000);
|
2016-01-21 12:01:55 +00:00
|
|
|
|
2012-12-16 19:19:16 +00:00
|
|
|
for ($i = 0; $i < 4; $i++) {
|
|
|
|
file_put_contents("{$this->root}/cached.file{$i}.zip", $zeros);
|
|
|
|
$this->files[] = new \SplFileInfo("{$this->root}/cached.file{$i}.zip");
|
|
|
|
}
|
2016-01-21 12:01:55 +00:00
|
|
|
|
2013-03-02 23:41:12 +00:00
|
|
|
$this->finder = $this->getMockBuilder('Symfony\Component\Finder\Finder')->disableOriginalConstructor()->getMock();
|
2019-02-18 13:03:23 +00:00
|
|
|
$this->filesystem = $this->getMockBuilder('Composer\Util\Filesystem')->getMock();
|
2012-12-16 19:19:16 +00:00
|
|
|
|
2018-04-12 08:24:56 +00:00
|
|
|
$io = $this->getMockBuilder('Composer\IO\IOInterface')->getMock();
|
|
|
|
$this->cache = $this->getMockBuilder('Composer\Cache')
|
|
|
|
->setMethods(array('getFinder'))
|
2019-02-18 13:12:01 +00:00
|
|
|
->setConstructorArgs(array($io, $this->root))
|
2018-04-12 08:24:56 +00:00
|
|
|
->getMock();
|
2012-12-16 19:19:16 +00:00
|
|
|
$this->cache
|
|
|
|
->expects($this->any())
|
|
|
|
->method('getFinder')
|
|
|
|
->will($this->returnValue($this->finder));
|
|
|
|
}
|
|
|
|
|
2015-12-14 14:35:27 +00:00
|
|
|
protected function tearDown()
|
|
|
|
{
|
|
|
|
if (is_dir($this->root)) {
|
2015-12-14 15:52:02 +00:00
|
|
|
$fs = new Filesystem;
|
|
|
|
$fs->removeDirectory($this->root);
|
2015-12-14 14:35:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-16 19:19:16 +00:00
|
|
|
public function testRemoveOutdatedFiles()
|
|
|
|
{
|
|
|
|
$outdated = array_slice($this->files, 1);
|
|
|
|
$this->finder
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getIterator')
|
|
|
|
->will($this->returnValue(new \ArrayIterator($outdated)));
|
|
|
|
$this->finder
|
|
|
|
->expects($this->once())
|
|
|
|
->method('date')
|
|
|
|
->will($this->returnValue($this->finder));
|
|
|
|
|
|
|
|
$this->cache->gc(600, 1024 * 1024 * 1024);
|
|
|
|
|
|
|
|
for ($i = 1; $i < 4; $i++) {
|
2020-09-11 09:27:26 +00:00
|
|
|
$this->assertFileDoesNotExist("{$this->root}/cached.file{$i}.zip");
|
2012-12-16 19:19:16 +00:00
|
|
|
}
|
|
|
|
$this->assertFileExists("{$this->root}/cached.file0.zip");
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testRemoveFilesWhenCacheIsTooLarge()
|
|
|
|
{
|
2013-03-02 23:41:12 +00:00
|
|
|
$emptyFinder = $this->getMockBuilder('Symfony\Component\Finder\Finder')->disableOriginalConstructor()->getMock();
|
2012-12-16 19:19:16 +00:00
|
|
|
$emptyFinder
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getIterator')
|
|
|
|
->will($this->returnValue(new \EmptyIterator()));
|
|
|
|
|
|
|
|
$this->finder
|
|
|
|
->expects($this->once())
|
|
|
|
->method('date')
|
|
|
|
->will($this->returnValue($emptyFinder));
|
|
|
|
$this->finder
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getIterator')
|
|
|
|
->will($this->returnValue(new \ArrayIterator($this->files)));
|
|
|
|
$this->finder
|
|
|
|
->expects($this->once())
|
|
|
|
->method('sortByAccessedTime')
|
|
|
|
->will($this->returnValue($this->finder));
|
|
|
|
|
|
|
|
$this->cache->gc(600, 1500);
|
|
|
|
|
|
|
|
for ($i = 0; $i < 3; $i++) {
|
2020-09-11 09:27:26 +00:00
|
|
|
$this->assertFileDoesNotExist("{$this->root}/cached.file{$i}.zip");
|
2012-12-16 19:19:16 +00:00
|
|
|
}
|
|
|
|
$this->assertFileExists("{$this->root}/cached.file3.zip");
|
|
|
|
}
|
2017-06-01 02:02:15 +00:00
|
|
|
|
|
|
|
public function testClearCache()
|
|
|
|
{
|
2019-02-18 13:12:01 +00:00
|
|
|
$io = $this->getMockBuilder('Composer\IO\IOInterface')->getMock();
|
|
|
|
$this->cache = new Cache($io, $this->root, 'a-z0-9.', $this->filesystem);
|
|
|
|
$this->assertTrue($this->cache->clear());
|
2017-06-01 02:02:15 +00:00
|
|
|
}
|
2012-12-16 19:19:16 +00:00
|
|
|
}
|