forked from codeigniter4/CodeIgniter4
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCacheTest.php
More file actions
225 lines (175 loc) · 6.95 KB
/
CacheTest.php
File metadata and controls
225 lines (175 loc) · 6.95 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
<?php
declare(strict_types=1);
/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <[email protected]>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace CodeIgniter\Router\Attributes;
use CodeIgniter\HTTP\IncomingRequest;
use CodeIgniter\HTTP\ResponseInterface;
use CodeIgniter\HTTP\SiteURI;
use CodeIgniter\HTTP\UserAgent;
use CodeIgniter\I18n\Time;
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\Mock\MockAppConfig;
use Config\Services;
use PHPUnit\Framework\Attributes\Group;
/**
* @internal
*/
#[Group('Others')]
final class CacheTest extends CIUnitTestCase
{
protected function setUp(): void
{
parent::setUp();
// Clear cache before each test
cache()->clean();
Time::setTestNow('2026-01-10 12:00:00');
}
protected function tearDown(): void
{
parent::tearDown();
Time::setTestNow();
}
public function testConstructorDefaults(): void
{
$cache = new Cache();
$this->assertSame(3600, $cache->for);
$this->assertNull($cache->key);
}
public function testConstructorCustomValues(): void
{
$cache = new Cache(for: 300, key: 'custom_key');
$this->assertSame(300, $cache->for);
$this->assertSame('custom_key', $cache->key);
}
public function testBeforeReturnsNullForNonGetRequest(): void
{
$cache = new Cache();
$request = $this->createMockRequest('POST', '/test');
$result = $cache->before($request);
$this->assertNull($result);
}
public function testBeforeReturnsCachedResponseWhenFound(): void
{
$cache = new Cache();
$request = $this->createMockRequest('GET', '/test');
// Manually cache a response
$cacheKey = $this->getPrivateMethodInvoker($cache, 'generateCacheKey')($request);
$cachedData = [
'body' => 'Cached content',
'status' => 200,
'headers' => ['Content-Type' => 'text/html'],
'timestamp' => Time::now()->getTimestamp() - 10,
];
cache()->save($cacheKey, $cachedData, 3600);
$result = $cache->before($request);
$this->assertInstanceOf(ResponseInterface::class, $result);
$this->assertSame('Cached content', $result->getBody());
$this->assertSame(200, $result->getStatusCode());
$this->assertSame('text/html', $result->getHeaderLine('Content-Type'));
$this->assertSame('10', $result->getHeaderLine('Age'));
}
public function testBeforeReturnsNullForInvalidCacheData(): void
{
$cache = new Cache();
$request = $this->createMockRequest('GET', '/test');
// Cache invalid data
$cacheKey = $this->getPrivateMethodInvoker($cache, 'generateCacheKey')($request);
cache()->save($cacheKey, 'invalid data', 3600);
$result = $cache->before($request);
$this->assertNull($result);
}
public function testBeforeReturnsNullForIncompleteCacheData(): void
{
$cache = new Cache();
$request = $this->createMockRequest('GET', '/test');
// Cache incomplete data (missing 'headers' key)
$cacheKey = $this->getPrivateMethodInvoker($cache, 'generateCacheKey')($request);
cache()->save($cacheKey, ['body' => 'test', 'status' => 200], 3600);
$result = $cache->before($request);
$this->assertNull($result);
}
public function testBeforeUsesCustomCacheKey(): void
{
$cache = new Cache(key: 'my_custom_key');
$request = $this->createMockRequest('GET', '/test');
// Cache with custom key
$cachedData = [
'body' => 'Custom cached content',
'status' => 200,
'headers' => [],
'timestamp' => Time::now()->getTimestamp(),
];
cache()->save('my_custom_key', $cachedData, 3600);
$result = $cache->before($request);
$this->assertInstanceOf(ResponseInterface::class, $result);
$this->assertSame('Custom cached content', $result->getBody());
}
public function testAfterReturnsNullForNonGetRequest(): void
{
$cache = new Cache();
$request = $this->createMockRequest('POST', '/test');
$response = Services::response();
$result = $cache->after($request, $response);
$this->assertNotInstanceOf(ResponseInterface::class, $result);
}
public function testAfterCachesGetRequestResponse(): void
{
$cache = new Cache(for: 300);
$request = $this->createMockRequest('GET', '/test');
$response = Services::response();
$response->setBody('Test content');
$response->setStatusCode(200);
$response->setHeader('Content-Type', 'text/plain');
$result = $cache->after($request, $response);
$this->assertSame($response, $result);
// Verify cache was saved
$cacheKey = $this->getPrivateMethodInvoker($cache, 'generateCacheKey')($request);
$cached = cache($cacheKey);
$this->assertIsArray($cached);
$this->assertSame('Test content', $cached['body']);
$this->assertSame(200, $cached['status']);
$this->assertArrayHasKey('timestamp', $cached);
}
public function testAfterUsesCustomCacheKey(): void
{
$cache = new Cache(key: 'another_custom_key');
$request = $this->createMockRequest('GET', '/test');
$response = Services::response();
$response->setBody('Custom key content');
$cache->after($request, $response);
// Verify cache was saved with custom key
$cached = cache('another_custom_key');
$this->assertIsArray($cached);
$this->assertSame('Custom key content', $cached['body']);
}
public function testGenerateCacheKeyIncludesMethodPathAndQuery(): void
{
$cache = new Cache();
$request1 = $this->createMockRequest('GET', '/test', 'foo=bar');
$request2 = $this->createMockRequest('GET', '/test', 'foo=baz');
$key1 = $this->getPrivateMethodInvoker($cache, 'generateCacheKey')($request1);
$key2 = $this->getPrivateMethodInvoker($cache, 'generateCacheKey')($request2);
$this->assertNotSame($key1, $key2);
$this->assertStringStartsWith('route_cache_', $key1);
}
private function createMockRequest(string $method, string $path, string $query = ''): IncomingRequest
{
$config = new MockAppConfig();
$uri = new SiteURI($config, 'http://example.com' . $path . ($query !== '' ? '?' . $query : ''));
$userAgent = new UserAgent();
$request = $this->getMockBuilder(IncomingRequest::class)
->setConstructorArgs([$config, $uri, null, $userAgent])
->onlyMethods(['isCLI'])
->getMock();
$request->method('isCLI')->willReturn(false);
$request->setMethod($method);
return $request;
}
}