packages/requirements/tests/Unit/Source/SnapshotCacheTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Source;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Small;
9use PHPUnit\Framework\TestCase;
10use Requirements\Source\SnapshotCache;
11use Tests\Fake\ProjectDirectory;
12
13#[CoversClass(SnapshotCache::class)]
14#[Small]
15final class SnapshotCacheTest extends TestCase
16{
17    public function testWriteCreatesTheSnapshotAndItsDirectories(): void
18    {
19        $project = new ProjectDirectory();
20        (new SnapshotCache())->write($project->path('.requirements-cache/sources/manual.html'), 'Verified text.');
21
22        self::assertSame('Verified text.', $project->read('.requirements-cache/sources/manual.html'));
23    }
24
25    public function testWriteCreatesDirectoriesReadableByOthers(): void
26    {
27        $project = new ProjectDirectory();
28        (new SnapshotCache())->write($project->path('cache/sources/manual.html'), 'Verified text.');
29
30        self::assertSame(0755 & ~umask(), fileperms($project->path('cache/sources')) & 0777);
31    }
32
33    public function testWriteReplacesAnExistingSnapshot(): void
34    {
35        $project = new ProjectDirectory();
36        (new SnapshotCache())->write($project->put('cache/manual.html', 'Old text.'), 'New text.');
37
38        self::assertSame('New text.', $project->read('cache/manual.html'));
39    }
40
41    public function testWriteLeavesNoTemporaryFile(): void
42    {
43        $project = new ProjectDirectory();
44        (new SnapshotCache())->write($project->path('cache/manual.html'), 'Verified text.');
45
46        self::assertSame(['manual.html'], array_values(array_diff((array) scandir($project->path('cache')), ['.', '..'])));
47    }
48}
49