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..2a7fc57 100644 --- a/tests/Command/MinifyAssetCommandTest.php +++ b/tests/Command/MinifyAssetCommandTest.php @@ -65,6 +65,18 @@ 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' => 'bin/fakify']); + $this->assertSame(Command::FAILURE, $tester->getStatusCode()); + $this->matchesRegularExpression('#[ERROR] The type of "(.*)bin/fakify" is "", it must be "css" or "js".#', $tester->getDisplay()); + } + public function testMinifyAssetCommandFailsWhenOutputFileIsNotWritable(): void { $minifier = $this->createMock(MinifierInterface::class);