-
-
Notifications
You must be signed in to change notification settings - Fork 622
Expand file tree
/
Copy pathCombIndexTest.php
More file actions
126 lines (99 loc) · 3.29 KB
/
CombIndexTest.php
File metadata and controls
126 lines (99 loc) · 3.29 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
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
namespace Tests\Search;
use Illuminate\Contracts\Filesystem\Filesystem;
use Mockery;
use PHPUnit\Framework\Attributes\Test;
use Statamic\Contracts\Search\Searchable;
use Statamic\Search\Comb\Index;
use Tests\TestCase;
class CombIndexTest extends TestCase
{
use IndexTests {
tearDown as protected indexTestsTearDown;
}
private $fs;
private ?string $tmpDir = null;
public function setUp(): void
{
parent::setUp();
$this->fs = Mockery::mock(Filesystem::class);
$this->fs->shouldReceive('disk')->andReturn(Mockery::self());
$this->instance('filesystem', $this->fs);
}
public function tearDown(): void
{
if ($this->tmpDir && is_dir($this->tmpDir)) {
foreach (glob($this->tmpDir.'/*') as $file) {
@unlink($file);
}
@rmdir($this->tmpDir);
}
$this->indexTestsTearDown();
}
protected function beforeSearched()
{
$this->fs
->shouldReceive('exists')
->with('local/storage/search/test.json')
->andReturn(true);
$this->fs
->shouldReceive('get')
->with('local/storage/search/test.json')
->andReturn('[[]]');
}
public function getIndexClass()
{
return Index::class;
}
#[Test]
public function delete_does_not_rewrite_the_index_when_the_reference_is_absent()
{
$path = $this->createIndexFile(['entry::a' => ['title' => 'Foo']]);
$oldMtime = time() - 3600;
touch($path, $oldMtime);
clearstatcache();
$index = $this->getIndex('test', ['path' => $this->tmpDir], null);
$document = Mockery::mock(Searchable::class);
$document->shouldReceive('getSearchReference')->andReturn('entry::missing');
$index->delete($document);
clearstatcache();
$this->assertSame(
$oldMtime,
filemtime($path),
'Comb\Index::delete() rewrote the index file even though the reference was not present.'
);
}
#[Test]
public function delete_rewrites_the_index_when_the_reference_is_present()
{
$path = $this->createIndexFile([
'entry::a' => ['title' => 'Foo'],
'entry::b' => ['title' => 'Bar'],
]);
$oldMtime = time() - 3600;
touch($path, $oldMtime);
clearstatcache();
$index = $this->getIndex('test', ['path' => $this->tmpDir], null);
$document = Mockery::mock(Searchable::class);
$document->shouldReceive('getSearchReference')->andReturn('entry::a');
$index->delete($document);
clearstatcache();
$this->assertGreaterThan(
$oldMtime,
filemtime($path),
'Comb\Index::delete() did not rewrite the index file even though the reference was present.'
);
$this->assertSame(
['entry::b' => ['title' => 'Bar']],
json_decode(file_get_contents($path), true)
);
}
private function createIndexFile(array $data): string
{
$this->tmpDir = sys_get_temp_dir().'/statamic_comb_test_'.uniqid();
mkdir($this->tmpDir, 0777, true);
$path = $this->tmpDir.'/test.json';
file_put_contents($path, json_encode($data));
return $path;
}
}