Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 27 additions & 3 deletions tests/system/Commands/Cache/ClearCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -29,23 +31,45 @@ final class ClearCacheTest extends CIUnitTestCase
{
use StreamFilterTrait;

private Cache $config;

protected function setUp(): void
{
parent::setUp();

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
Expand All @@ -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);
Expand Down
33 changes: 29 additions & 4 deletions tests/system/Commands/Cache/InfoCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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
Expand All @@ -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');

Expand Down
Loading