Skip to content

Commit ee143cd

Browse files
committed
test: fix cache tests race conditions
1 parent 8c164d0 commit ee143cd

6 files changed

Lines changed: 24 additions & 79 deletions

File tree

.github/scripts/random-tests-config.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
API
1212
AutoReview
1313
Autoloader
14-
# Cache
14+
Cache
1515
CLI
1616
Commands
1717
Config

tests/system/Cache/Handlers/ApcuHandlerTest.php

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,30 +27,24 @@
2727
#[RequiresPhpExtension('apcu')]
2828
final class ApcuHandlerTest extends AbstractHandlerTestCase
2929
{
30-
/**
31-
* @return list<string>
32-
*/
33-
private static function getKeyArray(): array
34-
{
35-
return [
36-
self::$key1,
37-
self::$key2,
38-
self::$key3,
39-
];
40-
}
41-
4230
protected function setUp(): void
4331
{
4432
parent::setUp();
4533

34+
if (! function_exists('apcu_enabled') || ! apcu_enabled()) {
35+
$this->markTestSkipped('APCu extension is not loaded or not enabled in CLI.');
36+
}
37+
4638
$this->handler = CacheFactory::getHandler(new Cache(), 'apcu');
4739
}
4840

4941
protected function tearDown(): void
5042
{
51-
foreach (self::getKeyArray() as $key) {
52-
$this->handler->delete($key);
43+
if (isset($this->handler)) {
44+
$this->handler->clean();
5345
}
46+
47+
parent::tearDown();
5448
}
5549

5650
public function testNew(): void

tests/system/Cache/Handlers/FileHandlerTest.php

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,6 @@ final class FileHandlerTest extends AbstractHandlerTestCase
3131
private static string $directory = 'FileHandler';
3232
private Cache $config;
3333

34-
/**
35-
* @return list<string>
36-
*/
37-
private static function getKeyArray(): array
38-
{
39-
return [
40-
self::$key1,
41-
self::$key2,
42-
self::$key3,
43-
];
44-
}
45-
4634
protected function setUp(): void
4735
{
4836
parent::setUp();
@@ -69,15 +57,8 @@ protected function tearDown(): void
6957
if (is_dir($this->config->file['storePath'])) {
7058
chmod($this->config->file['storePath'], 0777);
7159

72-
foreach (self::getKeyArray() as $key) {
73-
if (is_file($this->config->file['storePath'] . DIRECTORY_SEPARATOR . $key)) {
74-
chmod($this->config->file['storePath'] . DIRECTORY_SEPARATOR . $key, 0777);
75-
unlink($this->config->file['storePath'] . DIRECTORY_SEPARATOR . $key);
76-
}
77-
if (is_file($this->config->file['storePath'] . DIRECTORY_SEPARATOR . $this->config->prefix . $key)) {
78-
chmod($this->config->file['storePath'] . DIRECTORY_SEPARATOR . $this->config->prefix . $key, 0777);
79-
unlink($this->config->file['storePath'] . DIRECTORY_SEPARATOR . $this->config->prefix . $key);
80-
}
60+
if (isset($this->handler)) {
61+
$this->handler->clean();
8162
}
8263

8364
rmdir($this->config->file['storePath']);
@@ -118,7 +99,7 @@ public function testSetDefaultPath(): void
11899
*/
119100
public function testGet(): void
120101
{
121-
$this->handler->save(self::$key1, 'value', 2);
102+
$this->assertTrue($this->handler->save(self::$key1, 'value', 2));
122103

123104
$this->assertSame('value', $this->handler->get(self::$key1));
124105
$this->assertNull($this->handler->get(self::$dummy));

tests/system/Cache/Handlers/MemcachedHandlerTest.php

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,6 @@
2626
#[Group('CacheLive')]
2727
final class MemcachedHandlerTest extends AbstractHandlerTestCase
2828
{
29-
/**
30-
* @return list<string>
31-
*/
32-
private static function getKeyArray(): array
33-
{
34-
return [
35-
self::$key1,
36-
self::$key2,
37-
self::$key3,
38-
];
39-
}
40-
4129
protected function setUp(): void
4230
{
4331
parent::setUp();
@@ -51,9 +39,11 @@ protected function setUp(): void
5139

5240
protected function tearDown(): void
5341
{
54-
foreach (self::getKeyArray() as $key) {
55-
$this->handler->delete($key);
42+
if (isset($this->handler)) {
43+
$this->handler->clean();
5644
}
45+
46+
parent::tearDown();
5747
}
5848

5949
public function testNew(): void

tests/system/Cache/Handlers/PredisHandlerTest.php

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,6 @@ final class PredisHandlerTest extends AbstractHandlerTestCase
2727
{
2828
private Cache $config;
2929

30-
/**
31-
* @return list<string>
32-
*/
33-
private static function getKeyArray(): array
34-
{
35-
return [
36-
self::$key1,
37-
self::$key2,
38-
self::$key3,
39-
];
40-
}
41-
4230
protected function setUp(): void
4331
{
4432
parent::setUp();
@@ -49,9 +37,11 @@ protected function setUp(): void
4937

5038
protected function tearDown(): void
5139
{
52-
foreach (self::getKeyArray() as $key) {
53-
$this->handler->delete($key);
40+
if (isset($this->handler)) {
41+
$this->handler->clean();
5442
}
43+
44+
parent::tearDown();
5545
}
5646

5747
public function testNew(): void

tests/system/Cache/Handlers/RedisHandlerTest.php

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,6 @@ final class RedisHandlerTest extends AbstractHandlerTestCase
2828
{
2929
private Cache $config;
3030

31-
/**
32-
* @return list<string>
33-
*/
34-
private static function getKeyArray(): array
35-
{
36-
return [
37-
self::$key1,
38-
self::$key2,
39-
self::$key3,
40-
];
41-
}
42-
4331
protected function setUp(): void
4432
{
4533
parent::setUp();
@@ -54,9 +42,11 @@ protected function setUp(): void
5442

5543
protected function tearDown(): void
5644
{
57-
foreach (self::getKeyArray() as $key) {
58-
$this->handler->delete($key);
45+
if (isset($this->handler)) {
46+
$this->handler->clean();
5947
}
48+
49+
parent::tearDown();
6050
}
6151

6252
public function testNew(): void

0 commit comments

Comments
 (0)