Skip to content

Commit 58e1448

Browse files
committed
更新单元测试
1 parent 20d7782 commit 58e1448

File tree

10 files changed

+2568
-2
lines changed

10 files changed

+2568
-2
lines changed

.github/workflows/codecov.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ jobs:
2626
uses: shivammathur/setup-php@v2
2727
with:
2828
php-version: 8.0
29-
extensions: pdo, pdo_mysql, mbstring #optional, setup extensions
29+
extensions: pdo, pdo_mysql, mbstring, mongodb #optional, setup extensions
3030
coverage: xdebug #optional, setup coverage driver
3131

3232
- name: Check Version

.github/workflows/tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ jobs:
3939
uses: shivammathur/setup-php@v2
4040
with:
4141
php-version: ${{ matrix.php }}
42-
extensions: pdo, pdo_mysql, mbstring #optional, setup extensions
42+
extensions: pdo, pdo_mysql, mbstring, mongodb #optional, setup extensions
4343
coverage: none #optional, setup coverage driver
4444

4545
- name: Check Version

tests/orm/CacheTest.php

Lines changed: 272 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,272 @@
1+
<?php
2+
3+
namespace tests\orm;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use think\db\CacheItem;
7+
// use think\db\exception\InvalidArgumentException;
8+
use DateTime;
9+
use DateInterval;
10+
use DateTimeInterface;
11+
12+
class CacheTest extends TestCase
13+
{
14+
public function testCacheItemBasicOperations(): void
15+
{
16+
$cache = new CacheItem('test_key');
17+
18+
// Test key operations
19+
$this->assertEquals('test_key', $cache->getKey());
20+
21+
$cache->setKey('new_key');
22+
$this->assertEquals('new_key', $cache->getKey());
23+
24+
// Test value operations
25+
$this->assertFalse($cache->isHit());
26+
27+
$cache->set('test_value');
28+
$this->assertTrue($cache->isHit());
29+
$this->assertEquals('test_value', $cache->get());
30+
}
31+
32+
public function testCacheItemWithoutKey(): void
33+
{
34+
$cache = new CacheItem();
35+
$this->assertNull($cache->getKey());
36+
37+
$cache->setKey('dynamic_key');
38+
$this->assertEquals('dynamic_key', $cache->getKey());
39+
}
40+
41+
public function testCacheItemTagOperations(): void
42+
{
43+
$cache = new CacheItem('tagged_key');
44+
45+
// Test single tag
46+
$cache->tag('user');
47+
$this->assertEquals('user', $cache->getTag());
48+
49+
// Test array tags
50+
$tags = ['user', 'profile', 'settings'];
51+
$cache->tag($tags);
52+
$this->assertEquals($tags, $cache->getTag());
53+
54+
// Test null tag
55+
$cache->tag(null);
56+
$this->assertNull($cache->getTag());
57+
}
58+
59+
public function testCacheItemExpirationWithInteger(): void
60+
{
61+
$cache = new CacheItem('expiring_key');
62+
63+
// Set expiration with integer (seconds from now)
64+
$expireSeconds = 3600; // 1 hour
65+
$cache->expire($expireSeconds);
66+
67+
// Get expire should return seconds remaining (approximately)
68+
$remaining = $cache->getExpire();
69+
$this->assertIsInt($remaining);
70+
$this->assertLessThanOrEqual($expireSeconds, $remaining);
71+
$this->assertGreaterThan($expireSeconds - 10, $remaining); // Allow some margin for execution time
72+
}
73+
74+
public function testCacheItemExpirationWithDateTime(): void
75+
{
76+
$cache = new CacheItem('datetime_key');
77+
78+
// Set expiration with DateTime
79+
$futureDate = new DateTime('+2 hours');
80+
$cache->expire($futureDate);
81+
82+
$expiration = $cache->getExpire();
83+
$this->assertInstanceOf(DateTimeInterface::class, $expiration);
84+
$this->assertEquals($futureDate, $expiration);
85+
}
86+
87+
public function testCacheItemExpirationWithDateInterval(): void
88+
{
89+
$cache = new CacheItem('interval_key');
90+
91+
// Set expiration with DateInterval
92+
$interval = new DateInterval('PT30M'); // 30 minutes
93+
$beforeTime = time();
94+
$cache->expire($interval);
95+
$afterTime = time();
96+
97+
$remaining = $cache->getExpire();
98+
$this->assertIsInt($remaining);
99+
// Should be approximately 30 minutes (1800 seconds)
100+
$this->assertGreaterThan(1790, $remaining);
101+
$this->assertLessThan(1810, $remaining);
102+
}
103+
104+
public function testCacheItemExpirationWithNull(): void
105+
{
106+
$cache = new CacheItem('null_expire_key');
107+
108+
// Set no expiration
109+
$cache->expire(null);
110+
$this->assertNull($cache->getExpire());
111+
}
112+
113+
public function testCacheItemExpiresAt(): void
114+
{
115+
$cache = new CacheItem('expires_at_key');
116+
117+
$futureDate = new DateTime('+1 day');
118+
$cache->expiresAt($futureDate);
119+
120+
$expiration = $cache->getExpire();
121+
$this->assertInstanceOf(DateTimeInterface::class, $expiration);
122+
$this->assertEquals($futureDate, $expiration);
123+
}
124+
125+
public function testCacheItemExpiresAfterWithInteger(): void
126+
{
127+
$cache = new CacheItem('expires_after_key');
128+
129+
$seconds = 1800; // 30 minutes
130+
$beforeTime = time();
131+
$cache->expiresAfter($seconds);
132+
$afterTime = time();
133+
134+
$remaining = $cache->getExpire();
135+
$this->assertIsInt($remaining);
136+
$this->assertGreaterThanOrEqual($seconds - 2, $remaining);
137+
$this->assertLessThanOrEqual($seconds, $remaining);
138+
}
139+
140+
public function testCacheItemExpiresAfterWithDateInterval(): void
141+
{
142+
$cache = new CacheItem('expires_after_interval_key');
143+
144+
$interval = new DateInterval('PT45M'); // 45 minutes
145+
$cache->expiresAfter($interval);
146+
147+
$remaining = $cache->getExpire();
148+
$this->assertIsInt($remaining);
149+
// Should be approximately 45 minutes (2700 seconds)
150+
$this->assertGreaterThan(2690, $remaining);
151+
$this->assertLessThan(2710, $remaining);
152+
}
153+
154+
public function testCacheItemInvalidExpiration(): void
155+
{
156+
$cache = new CacheItem('invalid_expire_key');
157+
158+
$this->expectException(\Error::class);
159+
$this->expectExceptionMessage('Interface "Psr\SimpleCache\InvalidArgumentException" not found');
160+
161+
$cache->expire('invalid_date_string');
162+
}
163+
164+
public function testCacheItemInvalidExpiresAfter(): void
165+
{
166+
$cache = new CacheItem('invalid_expires_after_key');
167+
168+
$this->expectException(\Error::class);
169+
$this->expectExceptionMessage('Interface "Psr\SimpleCache\InvalidArgumentException" not found');
170+
171+
$cache->expiresAfter('invalid_interval');
172+
}
173+
174+
public function testCacheItemComplexValues(): void
175+
{
176+
$cache = new CacheItem('complex_key');
177+
178+
// Test with array
179+
$arrayValue = ['name' => 'John', 'age' => 30, 'roles' => ['admin', 'user']];
180+
$cache->set($arrayValue);
181+
$this->assertEquals($arrayValue, $cache->get());
182+
$this->assertTrue($cache->isHit());
183+
184+
// Test with object
185+
$objectValue = (object) ['property' => 'value', 'nested' => ['data' => 123]];
186+
$cache->set($objectValue);
187+
$this->assertEquals($objectValue, $cache->get());
188+
}
189+
190+
public function testCacheItemChaining(): void
191+
{
192+
$cache = new CacheItem();
193+
194+
// Test method chaining
195+
$result = $cache->setKey('chained_key')
196+
->set('chained_value')
197+
->tag('chained_tag')
198+
->expire(3600);
199+
200+
$this->assertSame($cache, $result);
201+
$this->assertEquals('chained_key', $cache->getKey());
202+
$this->assertEquals('chained_value', $cache->get());
203+
$this->assertEquals('chained_tag', $cache->getTag());
204+
$this->assertIsInt($cache->getExpire());
205+
}
206+
207+
public function testCacheItemDefaultState(): void
208+
{
209+
$cache = new CacheItem('default_key');
210+
211+
// Test default values
212+
$this->assertNull($cache->get());
213+
$this->assertFalse($cache->isHit());
214+
$this->assertNull($cache->getTag());
215+
$this->assertNull($cache->getExpire());
216+
}
217+
218+
public function testCacheItemOverwriteValues(): void
219+
{
220+
$cache = new CacheItem('overwrite_key');
221+
222+
// Set initial values
223+
$cache->set('initial_value');
224+
$cache->tag('initial_tag');
225+
$cache->expire(1800);
226+
227+
$this->assertEquals('initial_value', $cache->get());
228+
$this->assertEquals('initial_tag', $cache->getTag());
229+
230+
// Overwrite values
231+
$cache->set('new_value');
232+
$cache->tag('new_tag');
233+
$cache->expire(3600);
234+
235+
$this->assertEquals('new_value', $cache->get());
236+
$this->assertEquals('new_tag', $cache->getTag());
237+
238+
// Verify expire changed
239+
$remaining = $cache->getExpire();
240+
$this->assertGreaterThan(3500, $remaining);
241+
}
242+
243+
public function testCacheItemBooleanAndNullValues(): void
244+
{
245+
$cache = new CacheItem('boolean_key');
246+
247+
// Test boolean true
248+
$cache->set(true);
249+
$this->assertTrue($cache->get());
250+
$this->assertTrue($cache->isHit());
251+
252+
// Test boolean false
253+
$cache->set(false);
254+
$this->assertFalse($cache->get());
255+
$this->assertTrue($cache->isHit());
256+
257+
// Test null value
258+
$cache->set(null);
259+
$this->assertNull($cache->get());
260+
$this->assertTrue($cache->isHit());
261+
262+
// Test zero
263+
$cache->set(0);
264+
$this->assertEquals(0, $cache->get());
265+
$this->assertTrue($cache->isHit());
266+
267+
// Test empty string
268+
$cache->set('');
269+
$this->assertEquals('', $cache->get());
270+
$this->assertTrue($cache->isHit());
271+
}
272+
}

0 commit comments

Comments
 (0)