From 5116d18dc1905a3fa4df5b39f2a3b49c52e214ab Mon Sep 17 00:00:00 2001 From: michalsn Date: Thu, 16 Jul 2026 17:45:02 +0200 Subject: [PATCH] test: fix cache test race conditions --- .../system/Commands/Cache/ClearCacheTest.php | 30 +++++++++++++++-- tests/system/Commands/Cache/InfoCacheTest.php | 33 ++++++++++++++++--- 2 files changed, 56 insertions(+), 7 deletions(-) diff --git a/tests/system/Commands/Cache/ClearCacheTest.php b/tests/system/Commands/Cache/ClearCacheTest.php index a5679b00b3d9..e64a24916e15 100644 --- a/tests/system/Commands/Cache/ClearCacheTest.php +++ b/tests/system/Commands/Cache/ClearCacheTest.php @@ -16,8 +16,10 @@ use CodeIgniter\Cache\CacheFactory; use CodeIgniter\Cache\Handlers\FileHandler; use CodeIgniter\CLI\CLI; +use CodeIgniter\Config\Factories; use CodeIgniter\Test\CIUnitTestCase; use CodeIgniter\Test\StreamFilterTrait; +use Config\Cache; use Config\Services; use PHPUnit\Framework\Attributes\Group; @@ -29,6 +31,8 @@ final class ClearCacheTest extends CIUnitTestCase { use StreamFilterTrait; + private Cache $config; + protected function setUp(): void { parent::setUp(); @@ -36,16 +40,36 @@ protected function setUp(): void CLI::reset(); $this->resetServices(); + $this->config = new Cache(); + $this->config->file['storePath'] = rtrim($this->config->file['storePath'], DIRECTORY_SEPARATOR) + . DIRECTORY_SEPARATOR . 'FileHandlerCommands'; + + if (! is_dir($this->config->file['storePath'])) { + mkdir($this->config->file['storePath'], 0777, true); + } + + Factories::injectMock('config', Cache::class, $this->config); + // Make sure we are testing with the correct handler (override injections) - Services::injectMock('cache', CacheFactory::getHandler(config('Cache'))); + $handler = CacheFactory::getHandler($this->config); + $handler->clean(); + Services::injectMock('cache', $handler); } protected function tearDown(): void { - parent::tearDown(); + $this->config->handler = 'file'; + + if (is_dir($this->config->file['storePath'])) { + CacheFactory::getHandler($this->config)->clean(); + rmdir($this->config->file['storePath']); + } CLI::reset(); + $this->resetFactories(); $this->resetServices(); + + parent::tearDown(); } public function testClearCacheInvalidHandler(): void @@ -72,7 +96,7 @@ public function testClearCacheWorks(): void public function testClearCacheFails(): void { $cache = $this->getMockBuilder(FileHandler::class) - ->setConstructorArgs([config('Cache')]) + ->setConstructorArgs([$this->config]) ->onlyMethods(['clean']) ->getMock(); $cache->expects($this->once())->method('clean')->willReturn(false); diff --git a/tests/system/Commands/Cache/InfoCacheTest.php b/tests/system/Commands/Cache/InfoCacheTest.php index 44aa389338cc..0898df6f5ffd 100644 --- a/tests/system/Commands/Cache/InfoCacheTest.php +++ b/tests/system/Commands/Cache/InfoCacheTest.php @@ -14,8 +14,10 @@ namespace CodeIgniter\Commands\Cache; use CodeIgniter\Cache\CacheFactory; +use CodeIgniter\Config\Factories; use CodeIgniter\Test\CIUnitTestCase; use CodeIgniter\Test\StreamFilterTrait; +use Config\Cache; use Config\Services; use PHPUnit\Framework\Attributes\Group; @@ -27,18 +29,41 @@ final class InfoCacheTest extends CIUnitTestCase { use StreamFilterTrait; + private Cache $config; + protected function setUp(): void { parent::setUp(); + $this->config = new Cache(); + $this->config->file['storePath'] = rtrim($this->config->file['storePath'], DIRECTORY_SEPARATOR) + . DIRECTORY_SEPARATOR . 'FileHandlerCommands'; + + if (! is_dir($this->config->file['storePath'])) { + mkdir($this->config->file['storePath'], 0777, true); + } + + Factories::injectMock('config', Cache::class, $this->config); + // Make sure we are testing with the correct handler (override injections) - Services::injectMock('cache', CacheFactory::getHandler(config('Cache'))); + $handler = CacheFactory::getHandler($this->config); + $handler->clean(); + Services::injectMock('cache', $handler); } protected function tearDown(): void { - // restore default cache handler - config('Cache')->handler = 'file'; + $this->config->handler = 'file'; + + if (is_dir($this->config->file['storePath'])) { + CacheFactory::getHandler($this->config)->clean(); + rmdir($this->config->file['storePath']); + } + + $this->resetFactories(); + $this->resetServices(); + + parent::tearDown(); } protected function getBuffer(): string @@ -48,7 +73,7 @@ protected function getBuffer(): string public function testInfoCacheErrorsOnInvalidHandler(): void { - config('Cache')->handler = 'redis'; + $this->config->handler = 'redis'; cache()->save('foo', 'bar'); command('cache:info');