1
0
Fork 0
composer/tests/Composer/Test/CacheTest.php

121 lines
3.9 KiB
PHP
Raw Normal View History

2022-02-23 15:58:18 +00:00
<?php declare(strict_types=1);
/*
* 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;
2019-02-18 13:12:01 +00:00
use Composer\Cache;
use Composer\Util\Filesystem;
class CacheTest extends TestCase
{
2022-02-23 15:57:47 +00:00
/** @var array<\SplFileInfo> */
2017-03-08 14:07:29 +00:00
private $files;
/** @var string */
2017-03-08 14:07:29 +00:00
private $root;
/** @var \Symfony\Component\Finder\Finder&\PHPUnit\Framework\MockObject\MockObject */
2017-03-08 14:07:29 +00:00
private $finder;
/** @var Filesystem&\PHPUnit\Framework\MockObject\MockObject */
2019-02-18 13:03:23 +00:00
private $filesystem;
/** @var Cache&\PHPUnit\Framework\MockObject\MockObject */
2017-03-08 14:07:29 +00:00
private $cache;
2021-12-08 16:03:05 +00:00
public function setUp(): void
{
$this->root = self::getUniqueTmpDirectory();
2022-08-17 12:20:07 +00:00
$this->files = [];
$zeros = str_repeat('0', 1000);
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");
}
$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();
$io = $this->getMockBuilder('Composer\IO\IOInterface')->getMock();
$this->cache = $this->getMockBuilder('Composer\Cache')
2022-08-17 12:20:07 +00:00
->onlyMethods(['getFinder'])
->setConstructorArgs([$io, $this->root])
->getMock();
$this->cache
->expects($this->any())
->method('getFinder')
->will($this->returnValue($this->finder));
}
2021-12-08 16:03:05 +00:00
protected function tearDown(): void
{
parent::tearDown();
if (is_dir($this->root)) {
$fs = new Filesystem;
$fs->removeDirectory($this->root);
}
}
public function testRemoveOutdatedFiles(): void
{
$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");
}
$this->assertFileExists("{$this->root}/cached.file0.zip");
}
public function testRemoveFilesWhenCacheIsTooLarge(): void
{
$emptyFinder = $this->getMockBuilder('Symfony\Component\Finder\Finder')->disableOriginalConstructor()->getMock();
$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");
}
$this->assertFileExists("{$this->root}/cached.file3.zip");
}
public function testClearCache(): void
{
2019-02-18 13:12:01 +00:00
$io = $this->getMockBuilder('Composer\IO\IOInterface')->getMock();
$cache = new Cache($io, $this->root, 'a-z0-9.', $this->filesystem);
$this->assertTrue($cache->clear());
}
}