From c4cbc2da9f1d8255a610fcc58951b32df197cae8 Mon Sep 17 00:00:00 2001 From: Alexis Lefebvre Date: Tue, 29 Oct 2024 01:26:43 +0100 Subject: [PATCH] fix: asset command (#15) Fixes GH-14: - GH-14 --- src/Command/MinifyAssetCommand.php | 18 +++++++++++++++--- tests/Command/MinifyAssetCommandTest.php | 14 ++++++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/src/Command/MinifyAssetCommand.php b/src/Command/MinifyAssetCommand.php index ae3c46a..3d593ed 100644 --- a/src/Command/MinifyAssetCommand.php +++ b/src/Command/MinifyAssetCommand.php @@ -82,8 +82,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int return Command::FAILURE; } - /** @var string $inputArg */ - $inputArg = file_get_contents($inputArg); + /** @var string $inputContent */ + $inputContent = file_get_contents($inputArg); /** @var string|null $outputArg */ $outputArg = $input->getArgument('output'); @@ -94,7 +94,19 @@ protected function execute(InputInterface $input, OutputInterface $output): int /** @var 'css'|'js' $typeArg */ $typeArg = $input->getOption('type') ?? pathinfo($inputArg, PATHINFO_EXTENSION); - $output = $this->minifier->minify($inputArg, $typeArg); + if (!in_array($typeArg, [MinifierInterface::TYPE_CSS, MinifierInterface::TYPE_JS], true)) { + $io->error(sprintf( + 'The type of "%s" is "%s", it must be "%s" or "%s".', + $inputArg, + $typeArg, + MinifierInterface::TYPE_CSS, + MinifierInterface::TYPE_JS, + )); + + return Command::FAILURE; + } + + $output = $this->minifier->minify($inputContent, $typeArg); if (null === $outputArg) { $io->text($output); diff --git a/tests/Command/MinifyAssetCommandTest.php b/tests/Command/MinifyAssetCommandTest.php index fef4759..0d54f1b 100644 --- a/tests/Command/MinifyAssetCommandTest.php +++ b/tests/Command/MinifyAssetCommandTest.php @@ -65,6 +65,20 @@ public function testMinifyAssetCommandFailsWhenInputFileDoesNotExist(): void $this->assertStringContainsString('Cannot read file', $tester->getDisplay()); } + public function testMinifyAssetCommandFailsWhenInputFileIsNotCssOrJS(): void + { + $minifier = $this->createMock(MinifierInterface::class); + + $command = new MinifyAssetCommand($minifier, __DIR__.'/../Fixtures'); + $tester = new CommandTester($command); + + $tester->execute(['input' => 'MinifyBundleTestKernel.php']); + $this->assertSame(Command::FAILURE, $tester->getStatusCode()); + $display = $tester->getDisplay(); + $this->assertStringContainsString('The type of', $display); + $this->assertStringContainsString('TestKernel.php" is "php", it must be "css" or "js".', $display); + } + public function testMinifyAssetCommandFailsWhenOutputFileIsNotWritable(): void { $minifier = $this->createMock(MinifierInterface::class);