Skip to content

Commit 7505c73

Browse files
authored
test: fix cache test race conditions (#10413)
1 parent b030c75 commit 7505c73

2 files changed

Lines changed: 56 additions & 7 deletions

File tree

‎tests/system/Commands/Cache/ClearCacheTest.php‎

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@
1616
use CodeIgniter\Cache\CacheFactory;
1717
use CodeIgniter\Cache\Handlers\FileHandler;
1818
use CodeIgniter\CLI\CLI;
19+
use CodeIgniter\Config\Factories;
1920
use CodeIgniter\Test\CIUnitTestCase;
2021
use CodeIgniter\Test\StreamFilterTrait;
22+
use Config\Cache;
2123
use Config\Services;
2224
use PHPUnit\Framework\Attributes\Group;
2325

@@ -29,23 +31,45 @@ final class ClearCacheTest extends CIUnitTestCase
2931
{
3032
use StreamFilterTrait;
3133

34+
private Cache $config;
35+
3236
protected function setUp(): void
3337
{
3438
parent::setUp();
3539

3640
CLI::reset();
3741
$this->resetServices();
3842

43+
$this->config = new Cache();
44+
$this->config->file['storePath'] = rtrim($this->config->file['storePath'], DIRECTORY_SEPARATOR)
45+
. DIRECTORY_SEPARATOR . 'FileHandlerCommands';
46+
47+
if (! is_dir($this->config->file['storePath'])) {
48+
mkdir($this->config->file['storePath'], 0777, true);
49+
}
50+
51+
Factories::injectMock('config', Cache::class, $this->config);
52+
3953
// Make sure we are testing with the correct handler (override injections)
40-
Services::injectMock('cache', CacheFactory::getHandler(config('Cache')));
54+
$handler = CacheFactory::getHandler($this->config);
55+
$handler->clean();
56+
Services::injectMock('cache', $handler);
4157
}
4258

4359
protected function tearDown(): void
4460
{
45-
parent::tearDown();
61+
$this->config->handler = 'file';
62+
63+
if (is_dir($this->config->file['storePath'])) {
64+
CacheFactory::getHandler($this->config)->clean();
65+
rmdir($this->config->file['storePath']);
66+
}
4667

4768
CLI::reset();
69+
$this->resetFactories();
4870
$this->resetServices();
71+
72+
parent::tearDown();
4973
}
5074

5175
public function testClearCacheInvalidHandler(): void
@@ -72,7 +96,7 @@ public function testClearCacheWorks(): void
7296
public function testClearCacheFails(): void
7397
{
7498
$cache = $this->getMockBuilder(FileHandler::class)
75-
->setConstructorArgs([config('Cache')])
99+
->setConstructorArgs([$this->config])
76100
->onlyMethods(['clean'])
77101
->getMock();
78102
$cache->expects($this->once())->method('clean')->willReturn(false);

‎tests/system/Commands/Cache/InfoCacheTest.php‎

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@
1414
namespace CodeIgniter\Commands\Cache;
1515

1616
use CodeIgniter\Cache\CacheFactory;
17+
use CodeIgniter\Config\Factories;
1718
use CodeIgniter\Test\CIUnitTestCase;
1819
use CodeIgniter\Test\StreamFilterTrait;
20+
use Config\Cache;
1921
use Config\Services;
2022
use PHPUnit\Framework\Attributes\Group;
2123

@@ -27,18 +29,41 @@ final class InfoCacheTest extends CIUnitTestCase
2729
{
2830
use StreamFilterTrait;
2931

32+
private Cache $config;
33+
3034
protected function setUp(): void
3135
{
3236
parent::setUp();
3337

38+
$this->config = new Cache();
39+
$this->config->file['storePath'] = rtrim($this->config->file['storePath'], DIRECTORY_SEPARATOR)
40+
. DIRECTORY_SEPARATOR . 'FileHandlerCommands';
41+
42+
if (! is_dir($this->config->file['storePath'])) {
43+
mkdir($this->config->file['storePath'], 0777, true);
44+
}
45+
46+
Factories::injectMock('config', Cache::class, $this->config);
47+
3448
// Make sure we are testing with the correct handler (override injections)
35-
Services::injectMock('cache', CacheFactory::getHandler(config('Cache')));
49+
$handler = CacheFactory::getHandler($this->config);
50+
$handler->clean();
51+
Services::injectMock('cache', $handler);
3652
}
3753

3854
protected function tearDown(): void
3955
{
40-
// restore default cache handler
41-
config('Cache')->handler = 'file';
56+
$this->config->handler = 'file';
57+
58+
if (is_dir($this->config->file['storePath'])) {
59+
CacheFactory::getHandler($this->config)->clean();
60+
rmdir($this->config->file['storePath']);
61+
}
62+
63+
$this->resetFactories();
64+
$this->resetServices();
65+
66+
parent::tearDown();
4267
}
4368

4469
protected function getBuffer(): string
@@ -48,7 +73,7 @@ protected function getBuffer(): string
4873

4974
public function testInfoCacheErrorsOnInvalidHandler(): void
5075
{
51-
config('Cache')->handler = 'redis';
76+
$this->config->handler = 'redis';
5277
cache()->save('foo', 'bar');
5378
command('cache:info');
5479

0 commit comments

Comments
 (0)