Skip to content

Commit

Permalink
fix: asset command (#15)
Browse files Browse the repository at this point in the history
Fixes GH-14:

- GH-14
  • Loading branch information
alexislefebvre authored Oct 29, 2024
1 parent 655f29e commit c4cbc2d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/Command/MinifyAssetCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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);
Expand Down
14 changes: 14 additions & 0 deletions tests/Command/MinifyAssetCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit c4cbc2d

Please sign in to comment.