diff --git a/system/Commands/Utilities/Optimize.php b/system/Commands/Utilities/Optimize.php index 1a103a1b9c52..9ba193f77c03 100644 --- a/system/Commands/Utilities/Optimize.php +++ b/system/Commands/Utilities/Optimize.php @@ -15,7 +15,8 @@ use CodeIgniter\Autoloader\FileLocator; use CodeIgniter\Autoloader\FileLocatorCached; -use CodeIgniter\CLI\BaseCommand; +use CodeIgniter\CLI\AbstractCommand; +use CodeIgniter\CLI\Attributes\Command; use CodeIgniter\CLI\CLI; use CodeIgniter\Exceptions\RuntimeException; use CodeIgniter\Publisher\Publisher; @@ -23,41 +24,10 @@ /** * Optimize for production. */ -final class Optimize extends BaseCommand +#[Command(name: 'optimize', description: 'Optimize for production.', group: 'CodeIgniter')] +final class Optimize extends AbstractCommand { - /** - * The group the command is lumped under - * when listing commands. - * - * @var string - */ - protected $group = 'CodeIgniter'; - - /** - * The Command's name - * - * @var string - */ - protected $name = 'optimize'; - - /** - * The Command's short description - * - * @var string - */ - protected $description = 'Optimize for production.'; - - /** - * The Command's usage - * - * @var string - */ - protected $usage = 'optimize'; - - /** - * @return int - */ - public function run(array $params) + protected function execute(array $arguments, array $options): int { try { $this->enableCaching(); @@ -78,25 +48,24 @@ private function clearCache(): void $locator->deleteCache(); CLI::write('Removed FileLocatorCache.', 'green'); - $cache = WRITEPATH . 'cache/FactoriesCache_config'; - $this->removeFile($cache); + $this->removeFile(WRITEPATH . 'cache/FactoriesCache_config'); } - private function removeFile(string $cache): void + private function removeFile(string $file): void { - if (is_file($cache)) { - $result = unlink($cache); + if (! is_file($file)) { + return; + } - if ($result) { - CLI::write('Removed "' . clean_path($cache) . '".', 'green'); + if (unlink($file)) { + CLI::write(sprintf('Removed "%s".', clean_path($file)), 'green'); - return; - } + return; + } - CLI::error('Error in removing file: ' . clean_path($cache)); + CLI::error(sprintf('Error in removing file: %s', clean_path($file))); - throw new RuntimeException(__METHOD__); - } + throw new RuntimeException(__METHOD__); } private function enableCaching(): void @@ -122,7 +91,7 @@ private function enableCaching(): void return; } - CLI::error('Error in updating file: ' . clean_path($config)); + CLI::error(sprintf('Error in updating file: %s', clean_path($config))); throw new RuntimeException(__METHOD__); } diff --git a/tests/system/Commands/Utilities/OptimizeTest.php b/tests/system/Commands/Utilities/OptimizeTest.php new file mode 100644 index 000000000000..b51628fae0c7 --- /dev/null +++ b/tests/system/Commands/Utilities/OptimizeTest.php @@ -0,0 +1,68 @@ + + * + * For the full copyright and license information, please view + * the LICENSE file that was distributed with this source code. + */ + +namespace CodeIgniter\Commands\Utilities; + +use Closure; +use CodeIgniter\Test\CIUnitTestCase; +use CodeIgniter\Test\ReflectionHelper; +use CodeIgniter\Test\StreamFilterTrait; +use PHPUnit\Framework\Attributes\Group; + +/** + * @internal + */ +#[Group('Others')] +final class OptimizeTest extends CIUnitTestCase +{ + use ReflectionHelper; + use StreamFilterTrait; + + private string $file = WRITEPATH . 'cache/OptimizeTest_config'; + + protected function tearDown(): void + { + parent::tearDown(); + + if (is_file($this->file)) { + unlink($this->file); + } + } + + /** + * @return Closure(string): void + */ + private function getRemoveFile(): Closure + { + return self::getPrivateMethodInvoker(new Optimize(service('commands')), 'removeFile'); + } + + public function testRemoveFileDeletesTheFile(): void + { + file_put_contents($this->file, 'getRemoveFile())($this->file); + + $this->assertFileDoesNotExist($this->file); + $this->assertStringContainsString('Removed', $this->getStreamFilterBuffer()); + } + + public function testRemoveFileDoesNothingWhenFileIsAbsent(): void + { + $this->assertFileDoesNotExist($this->file); + + ($this->getRemoveFile())($this->file); + + $this->assertSame('', $this->getStreamFilterBuffer()); + } +}