File: CacheTest.php

package info (click to toggle)
composer 2.0.9-2%2Bdeb11u4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 11,048 kB
  • sloc: php: 61,526; xml: 109; makefile: 50
file content (114 lines) | stat: -rw-r--r-- 3,647 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?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;

use Composer\Cache;
use Composer\Util\Filesystem;

class CacheTest extends TestCase
{
    private $files;
    private $root;
    private $finder;
    private $filesystem;
    private $cache;

    public function setUp(): void
    {
        $this->root = $this->getUniqueTmpDirectory();
        $this->files = array();
        $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();
        $this->filesystem = $this->getMockBuilder('Composer\Util\Filesystem')->getMock();

        $io = $this->getMockBuilder('Composer\IO\IOInterface')->getMock();
        $this->cache = $this->getMockBuilder('Composer\Cache')
            ->setMethods(array('getFinder'))
            ->setConstructorArgs(array($io, $this->root))
            ->getMock();
        $this->cache
            ->expects($this->any())
            ->method('getFinder')
            ->will($this->returnValue($this->finder));
    }

    protected function tearDown(): void
    {
        if (is_dir($this->root)) {
            $fs = new Filesystem;
            $fs->removeDirectory($this->root);
        }
    }

    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++) {
            $this->assertFileDoesNotExist("{$this->root}/cached.file{$i}.zip");
        }
        $this->assertFileExists("{$this->root}/cached.file0.zip");
    }

    public function testRemoveFilesWhenCacheIsTooLarge()
    {
        $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++) {
            $this->assertFileDoesNotExist("{$this->root}/cached.file{$i}.zip");
        }
        $this->assertFileExists("{$this->root}/cached.file3.zip");
    }

    public function testClearCache()
    {
        $io = $this->getMockBuilder('Composer\IO\IOInterface')->getMock();
        $this->cache = new Cache($io, $this->root, 'a-z0-9.', $this->filesystem);
        $this->assertTrue($this->cache->clear());
    }
}