-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathIndexerTest.php
More file actions
113 lines (91 loc) · 3.16 KB
/
Copy pathIndexerTest.php
File metadata and controls
113 lines (91 loc) · 3.16 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
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
<?php
declare(strict_types=1);
namespace Tests\Indexer;
use Override;
use PHPUnit\Framework\Attributes\RunInSeparateProcess;
use PHPUnit\Framework\TestCase;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use ScipPhp\File\Reader;
use ScipPhp\Indexer;
use SplFileInfo;
use function array_keys;
use function exec;
use function file_put_contents;
use function is_file;
use function rename;
use function strlen;
use function substr;
use function sys_get_temp_dir;
use function tempnam;
use function unlink;
use const DIRECTORY_SEPARATOR;
final class IndexerTest extends TestCase
{
private const string TESTDATA_DIR = __DIR__ . DIRECTORY_SEPARATOR . 'testdata' . DIRECTORY_SEPARATOR;
/** @var non-empty-string */
private string $indexFile;
#[Override]
protected function setUp(): void
{
parent::setUp();
$tempDir = sys_get_temp_dir();
$indexFile = tempnam($tempDir, 'scip-php-index-');
if ($indexFile === false || !is_file($indexFile)) {
self::fail('Cannot create temporary file.');
}
if (!rename($indexFile, "{$indexFile}.scip")) {
unlink($indexFile);
self::fail('Cannot rename temporary file.');
}
$this->indexFile = "{$indexFile}.scip";
}
#[Override]
protected function tearDown(): void
{
parent::tearDown();
if (is_file($this->indexFile)) {
unlink($this->indexFile);
}
}
#[RunInSeparateProcess]
public function testIndexer(): void
{
$indexer = new Indexer(self::TESTDATA_DIR . 'scip-php-test', 'test', []);
$index = $indexer->index();
file_put_contents($this->indexFile, $index->serializeToString());
$actualPath = self::TESTDATA_DIR . 'actual';
$output = [];
$result = exec("scip snapshot --strict=false --from={$this->indexFile} --to={$actualPath}", $output);
if ($result === false) {
self::fail('Error executing scip: ' . ($output[0] ?? 'no output') . '.');
}
$goldenFiles = self::files(self::TESTDATA_DIR . 'golden');
$actualFiles = self::files($actualPath);
self::assertEqualsCanonicalizing(array_keys($goldenFiles), array_keys($actualFiles));
foreach ($goldenFiles as $name => $goldenPath) {
$actualPath = $actualFiles[$name];
$golden = Reader::read($goldenPath);
$actual = Reader::read($actualPath);
self::assertSame($golden, $actual);
}
}
/** @return array<non-empty-string, non-empty-string> */
private static function files(string $dir): array
{
$fileIterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($dir),
);
$files = [];
foreach ($fileIterator as $f) {
if ($f instanceof SplFileInfo && $f->isFile() && $f->getExtension() === 'php') {
$path = $f->getRealPath();
$relPath = substr($f->getPathname(), strlen($dir));
if ($path !== false && $path !== '' && $relPath !== '') {
$files[$relPath] = $path;
}
}
}
return $files;
}
}