forked from statamic/cms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStoreTest.php
More file actions
105 lines (83 loc) · 2.93 KB
/
StoreTest.php
File metadata and controls
105 lines (83 loc) · 2.93 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
<?php
namespace Tests\Stache;
use Illuminate\Cache\Events\CacheHit;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Request;
use PHPUnit\Framework\Attributes\Test;
use Statamic\Stache\Stache;
use Statamic\Stache\Stores\Store;
use Tests\Fakes\FakeArtisanRequest;
use Tests\TestCase;
class StoreTest extends TestCase
{
private $store;
public function setUp(): void
{
parent::setUp();
$stache = new Stache;
$this->store = new TestStore($stache);
}
#[Test]
public function it_forces_a_trailing_slash_when_setting_the_directory()
{
$this->assertNull($this->store->directory());
$return = $this->store->directory('/path/to/directory');
$this->assertEquals($this->store, $return);
$this->assertEquals('/path/to/directory/', $this->store->directory());
// Check the value of the property to make sure the property was set with
// the slash, and that ->directory() isn't just appending it.
$property = (new \ReflectionClass($this->store))->getProperty('directory');
$this->assertEquals('/path/to/directory/', $property->getValue($this->store));
}
#[Test]
public function it_gets_the_paths_from_the_cache_only_once()
{
$store = $this->store->directory('/path/to/directory');
$cacheKey = "stache::indexes::{$store->key()}::path";
Cache::put($cacheKey, ['foo', 'bar']);
$cacheHits = 0;
Event::listen(CacheHit::class, function ($event) use (&$cacheHits, $cacheKey) {
if ($event->key === $cacheKey) {
$cacheHits++;
}
});
$expected = collect(['foo', 'bar']);
$this->assertEquals($expected, $store->paths());
$this->assertEquals(1, $cacheHits);
$this->assertEquals($expected, $store->paths());
$this->assertEquals(1, $cacheHits);
}
#[Test]
public function it_gets_the_paths_from_the_cache_every_time_if_running_in_a_queue_worker()
{
$store = $this->store->directory('/path/to/directory');
$cacheKey = "stache::indexes::{$store->key()}::path";
Cache::put($cacheKey, ['foo', 'bar']);
$cacheHits = 0;
Event::listen(CacheHit::class, function ($event) use (&$cacheHits, $cacheKey) {
if ($event->key === $cacheKey) {
$cacheHits++;
}
});
Request::swap(new FakeArtisanRequest('queue:listen'));
$expected = collect(['foo', 'bar']);
$this->assertEquals($expected, $store->paths());
$this->assertEquals(1, $cacheHits);
$this->assertEquals($expected, $store->paths());
$this->assertEquals(2, $cacheHits);
}
}
class TestStore extends Store
{
public function getItem($key)
{
}
public function getItemValues($keys, $valueIndex, $keyIndex)
{
}
public function key()
{
return 'test-store';
}
}