Repository navigation
Expand file tree
/
Copy pathFileCacheStorageTest.php
More file actions
82 lines (59 loc) · 2.87 KB
/
Copy pathFileCacheStorageTest.php
File metadata and controls
82 lines (59 loc) · 2.87 KB
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
<?php
declare(strict_types=1);
namespace Rector\Tests\Caching\ValueObject\Storage;
use PHPUnit\Framework\Attributes\DoesNotPerformAssertions;
use Rector\Caching\ValueObject\Storage\FileCacheStorage;
use Rector\Testing\PHPUnit\AbstractLazyTestCase;
use Symfony\Component\Filesystem\Filesystem;
final class FileCacheStorageTest extends AbstractLazyTestCase
{
private FileCacheStorage $fileCacheStorage;
protected function setUp(): void
{
parent::setUp();
$this->fileCacheStorage = new FileCacheStorage(__DIR__ . '/Source', new Filesystem());
}
#[DoesNotPerformAssertions]
public function testCleanNonExistingFile(): void
{
$this->fileCacheStorage->clean('inexistant/file');
}
public function testClean(): void
{
$this->fileCacheStorage->save('aaK1STfY', 'TEST', 'file cached');
$file1 = __DIR__ . '/Source/0e/76/0e76658526655756207688271159624026011393.php';
$this->fileCacheStorage->save('aaO8zKZF', 'TEST', 'file cached with the same two first characters');
$file2 = __DIR__ . '/Source/0e/89/0e89257456677279068558073954252716165668.php';
$this->fileCacheStorage->clean('aaK1STfY');
$this->assertFileDoesNotExist($file1);
$this->assertDirectoryDoesNotExist(__DIR__ . '/Source/0e/76');
$this->assertFileExists($file2);
$this->assertDirectoryExists(__DIR__ . '/Source/0e/89');
$this->fileCacheStorage->clean('aaO8zKZF');
$this->assertFileDoesNotExist($file2);
$this->assertDirectoryDoesNotExist(__DIR__ . '/Source/0e/89');
$this->assertDirectoryDoesNotExist(__DIR__ . '/Source/0e');
}
public function testSaveLeavesAConcurrentReaderOnACompleteFile(): void
{
$filePath = __DIR__ . '/Source/0e/76/0e76658526655756207688271159624026011393.php';
$this->fileCacheStorage->save('aaK1STfY', 'TEST', 'first');
$contentsBeforeSave = (string) file_get_contents($filePath);
// every parallel worker require()s this path while booting its container; open it the
// way such a worker would, then have another worker save over it mid-read
$readerHandle = fopen($filePath, 'r');
$this->assertNotFalse($readerHandle);
$this->fileCacheStorage->save('aaK1STfY', 'TEST', 'second');
$contentsSeenByReader = stream_get_contents($readerHandle);
fclose($readerHandle);
// an atomic replace leaves the reader on the whole file it opened; writing through the
// published inode instead exposes the reader to however much of the new file has landed
$this->assertSame($contentsBeforeSave, $contentsSeenByReader);
$this->assertSame('second', $this->fileCacheStorage->load('aaK1STfY', 'TEST'));
$this->fileCacheStorage->clean('aaK1STfY');
}
public function provideConfigFilePath(): string
{
return __DIR__ . '/config.php';
}
}