Skip to content

Commit

Permalink
add test case
Browse files Browse the repository at this point in the history
  • Loading branch information
warcooft committed Jul 29, 2024
1 parent 229f3db commit 9a89389
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 1 deletion.
2 changes: 1 addition & 1 deletion system/Commands/Utilities/Optimize.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
use CodeIgniter\Autoloader\FileLocatorCached;
use CodeIgniter\CLI\BaseCommand;
use CodeIgniter\CLI\CLI;
use CodeIgniter\Publisher\Publisher;
use CodeIgniter\Exceptions\RuntimeException;
use CodeIgniter\Publisher\Publisher;

/**
* Optimize for production.
Expand Down
88 changes: 88 additions & 0 deletions tests/system/Commands/Utilities/OptimizeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

declare(strict_types=1);

/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <[email protected]>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

namespace CodeIgniter\Commands\Utilities;

use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\StreamFilterTrait;
use PHPUnit\Framework\Attributes\Group;

/**
* @internal
*/
#[Group('Others')]
final class OptimizeTest extends CIUnitTestCase
{
use StreamFilterTrait;

protected function setUp(): void
{
$this->resetServices();

parent::setUp();
}

protected function getBuffer(): string
{
return $this->getStreamFilterBuffer();
}

public function testEnableConfigCaching(): void
{
command('optimize -c');

// Check if config caching is enabled
$this->assertFileContains('public bool $configCacheEnabled = true;', APPPATH . 'Config/Optimize.php');
}

public function testEnableLocatorCaching(): void
{
command('optimize -l');

// Check if locator caching is enabled
$this->assertFileContains('public bool $locatorCacheEnabled = true;', APPPATH . 'Config/Optimize.php');
}

public function testDisableCaching(): void
{
command('optimize -d');
// Check if both caches are disabled
$this->assertFileContains('public bool $configCacheEnabled = false;', APPPATH . 'Config/Optimize.php');
$this->assertFileContains('public bool $locatorCacheEnabled = false;', APPPATH . 'Config/Optimize.php');
}

public function testClearCache(): void
{
// Assume the function clearCache() is called within run() and we are testing its effect
command('optimize');

// Check if the cache file was removed
$this->assertFileDoesNotExist(WRITEPATH . 'cache/FactoriesCache_config');
}

public function testRemoveDevPackages(): void
{
// Mock passthru to simulate the removal of dev packages
command('optimize');

// This is more of a behavioral test to ensure the command completes successfully
// In a real test, you'd check the composer status or lock file
$this->assertTrue(true); // Placeholder assertion
}

protected function assertFileContains(string $needle, string $filePath): void
{
$this->assertFileExists($filePath);
$this->assertStringContainsString($needle, file_get_contents($filePath));
}
}

0 comments on commit 9a89389

Please sign in to comment.