Skip to content

Commit 7f15535

Browse files
committed
Combine similar deleteMatching test cases
1 parent ce3750c commit 7f15535

1 file changed

Lines changed: 35 additions & 28 deletions

File tree

tests/system/Cache/Handlers/RedisHandlerTest.php

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@
1313

1414
namespace CodeIgniter\Cache\Handlers;
1515

16+
use CodeIgniter\Cache\CacheFactory;
1617
use CodeIgniter\CLI\CLI;
1718
use CodeIgniter\I18n\Time;
1819
use Config\Cache;
20+
use PHPUnit\Framework\Attributes\DataProvider;
1921
use PHPUnit\Framework\Attributes\Group;
2022

2123
/**
@@ -129,44 +131,49 @@ public function testDelete(): void
129131
$this->assertFalse($this->handler->delete(self::$dummy));
130132
}
131133

132-
public function testDeleteMatchingPrefix(): void
134+
#[DataProvider('provideDeleteMatching')]
135+
public function testDeleteMatching(string $pattern, int $expectedDeleteCount, string $prefix = ''): void
133136
{
134-
// Save 101 items to match on
137+
$cache = new Cache();
138+
139+
if ($prefix !== '') {
140+
$cache->prefix = $prefix;
141+
}
142+
143+
/** @var RedisHandler $handler */
144+
$handler = CacheFactory::getHandler($cache, 'redis');
145+
135146
for ($i = 1; $i <= 101; $i++) {
136-
$this->handler->save('key_' . $i, 'value' . $i);
147+
$handler->save('key_' . $i, 'value_' . $i);
137148
}
138149

139-
// check that there are 101 items is cache store
140-
$dbInfo = explode(',', $this->handler->getCacheInfo()['db0']);
141-
$this->assertSame('keys=101', $dbInfo[0]);
150+
$cacheInfo = $handler->getCacheInfo();
151+
$this->assertIsArray($cacheInfo);
152+
$this->assertArrayHasKey('db0', $cacheInfo);
153+
$this->assertIsString($cacheInfo['db0']);
154+
$this->assertSame(1, preg_match('/^keys=(?P<count>\d+)/', $cacheInfo['db0'], $matches));
155+
$this->assertSame(101, (int) $matches['count']);
156+
157+
$this->assertSame($expectedDeleteCount, $handler->deleteMatching($pattern));
142158

143-
// Checking that given the prefix "key_1", deleteMatching deletes 13 keys:
144-
// (key_1, key_10, key_11, key_12, key_13, key_14, key_15, key_16, key_17, key_18, key_19, key_100, key_101)
145-
$this->assertSame(13, $this->handler->deleteMatching('key_1*'));
159+
$cacheInfo = $handler->getCacheInfo();
160+
$this->assertIsArray($cacheInfo);
161+
$this->assertArrayHasKey('db0', $cacheInfo);
162+
$this->assertIsString($cacheInfo['db0']);
163+
$this->assertSame(1, preg_match('/^keys=(?P<count>\d+)/', $cacheInfo['db0'], $matches));
164+
$this->assertSame(101 - $expectedDeleteCount, (int) $matches['count']);
146165

147-
// check that there remains (101 - 13) = 88 items is cache store
148-
$dbInfo = explode(',', $this->handler->getCacheInfo()['db0']);
149-
$this->assertSame('keys=88', $dbInfo[0]);
166+
$handler->deleteMatching('key_*');
150167
}
151168

152-
public function testDeleteMatchingSuffix(): void
169+
/**
170+
* @return iterable<string, array{0: string, 1: int, 2?: string}>
171+
*/
172+
public static function provideDeleteMatching(): iterable
153173
{
154-
// Save 101 items to match on
155-
for ($i = 1; $i <= 101; $i++) {
156-
$this->handler->save('key_' . $i, 'value' . $i);
157-
}
158-
159-
// check that there are 101 items is cache store
160-
$dbInfo = explode(',', $this->handler->getCacheInfo()['db0']);
161-
$this->assertSame('keys=101', $dbInfo[0]);
162-
163-
// Checking that given the suffix "1", deleteMatching deletes 11 keys:
164-
// (key_1, key_11, key_21, key_31, key_41, key_51, key_61, key_71, key_81, key_91, key_101)
165-
$this->assertSame(11, $this->handler->deleteMatching('*1'));
174+
yield 'prefix' => ['key_1*', 13];
166175

167-
// check that there remains (101 - 13) = 88 items is cache store
168-
$dbInfo = explode(',', $this->handler->getCacheInfo()['db0']);
169-
$this->assertSame('keys=90', $dbInfo[0]);
176+
yield 'suffix' => ['*1', 11];
170177
}
171178

172179
public function testIncrementAndDecrement(): void

0 commit comments

Comments
 (0)