-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInMemoryCacheTest.php
More file actions
159 lines (114 loc) · 4.42 KB
/
Copy pathInMemoryCacheTest.php
File metadata and controls
159 lines (114 loc) · 4.42 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php
namespace Beste\Cache\Tests;
use Beste\Cache\InMemoryCache;
use Beste\Clock\FrozenClock;
use PHPUnit\Framework\TestCase;
/**
* @internal
*/
final class InMemoryCacheTest extends TestCase
{
private FrozenClock $clock;
private InMemoryCache $pool;
protected function setUp(): void
{
$this->clock = FrozenClock::fromUTC();
$this->pool = new InMemoryCache($this->clock);
}
public function testItWorksWithoutProvidingAClock(): void
{
$pool = new InMemoryCache();
$item = $pool->getItem('item');
$this->assertFalse($item->isHit());
$item->set('value')->expiresAfter(new \DateInterval('PT5M'));
$pool->save($item);
$item = $pool->getItem('item');
$this->assertTrue($item->isHit());
}
public function testItReturnsANewItem(): void
{
$item = $this->pool->getItem('item');
$this->assertFalse($item->isHit());
$this->assertNull($item->get());
}
public function testItUsesTheProvidedClock(): void
{
$item = $this->pool->getItem('item');
$item->set('value');
$item->expiresAfter(new \DateInterval('PT2H'));
$this->pool->save($item);
$this->clock->setTo($this->clock->now()->add(new \DateInterval('PT1H')));
$this->assertTrue($this->pool->getItem('item')->isHit());
$this->clock->setTo($this->clock->now()->add(new \DateInterval('PT2H')));
$this->assertFalse($this->pool->getItem('item')->isHit());
}
public function testItSavesAnItem(): void
{
$item = $this->pool->getItem('item');
$item->set('value');
$this->pool->save($item);
$this->assertTrue($this->pool->getItem('item')->isHit());
$this->assertSame('value', $this->pool->getItem('item')->get());
}
public function testItHasAnItem(): void
{
$this->assertFalse($this->pool->hasItem('key'));
$item = $this->pool->getItem('key');
$item->set('value');
$this->pool->save($item);
$this->assertTrue($this->pool->hasItem('key'));
}
public function testItCommitsDeferredItems(): void
{
$item = $this->pool->getItem('item');
$item->set('value');
$this->pool->saveDeferred($item);
$this->assertFalse($this->pool->getItem('item')->isHit());
$this->pool->commit();
$this->assertTrue($this->pool->getItem('item')->isHit());
}
public function testItCanBeCleared(): void
{
$this->pool->save($this->pool->getItem('key')->set('value'));
$this->assertTrue($this->pool->getItem('key')->isHit());
$this->pool->clear();
$this->assertFalse($this->pool->getItem('key')->isHit());
}
public function testItReturnsMultipleItems(): void
{
$this->pool->save($this->pool->getItem('first')->set('value'));
$this->pool->save($this->pool->getItem('third')->set('value'));
$items = $this->pool->getItems(['first', 'second', 'third']);
$this->assertCount(3, $items);
$this->assertIsArray($items);
$this->assertArrayHasKey('first', $items);
$this->assertTrue($items['first']->isHit());
$this->assertArrayHasKey('second', $items);
$this->assertFalse($items['second']->isHit());
$this->assertArrayHasKey('third', $items);
$this->assertTrue($items['third']->isHit());
}
public function testItReturnsNoItemsWhenNoKeysAreGiven(): void
{
$this->pool->save($this->pool->getItem('key')->set('value'));
$this->assertEmpty($this->pool->getItems());
}
public function testItDeletesAnItem(): void
{
$this->pool->save($this->pool->getItem('key')->set('value'));
$this->assertTrue($this->pool->hasItem('key'));
$this->pool->deleteItem('key');
$this->assertFalse($this->pool->hasItem('key'));
}
public function testItDeletesMultipleItems(): void
{
$this->pool->save($this->pool->getItem('first')->set('value'));
$this->pool->save($this->pool->getItem('second')->set('value'));
$this->pool->save($this->pool->getItem('third')->set('value'));
$this->pool->deleteItems(['first', 'third', 'fourth']);
$this->assertFalse($this->pool->hasItem('first'));
$this->assertTrue($this->pool->hasItem('second'));
$this->assertFalse($this->pool->hasItem('third'));
$this->assertFalse($this->pool->hasItem('fourth'));
}
}