Skip to content

Commit c4cbc2d

Browse files
fix: asset command (#15)
Fixes GH-14: - GH-14
1 parent 655f29e commit c4cbc2d

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

src/Command/MinifyAssetCommand.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
8282

8383
return Command::FAILURE;
8484
}
85-
/** @var string $inputArg */
86-
$inputArg = file_get_contents($inputArg);
85+
/** @var string $inputContent */
86+
$inputContent = file_get_contents($inputArg);
8787

8888
/** @var string|null $outputArg */
8989
$outputArg = $input->getArgument('output');
@@ -94,7 +94,19 @@ protected function execute(InputInterface $input, OutputInterface $output): int
9494
/** @var 'css'|'js' $typeArg */
9595
$typeArg = $input->getOption('type') ?? pathinfo($inputArg, PATHINFO_EXTENSION);
9696

97-
$output = $this->minifier->minify($inputArg, $typeArg);
97+
if (!in_array($typeArg, [MinifierInterface::TYPE_CSS, MinifierInterface::TYPE_JS], true)) {
98+
$io->error(sprintf(
99+
'The type of "%s" is "%s", it must be "%s" or "%s".',
100+
$inputArg,
101+
$typeArg,
102+
MinifierInterface::TYPE_CSS,
103+
MinifierInterface::TYPE_JS,
104+
));
105+
106+
return Command::FAILURE;
107+
}
108+
109+
$output = $this->minifier->minify($inputContent, $typeArg);
98110

99111
if (null === $outputArg) {
100112
$io->text($output);

tests/Command/MinifyAssetCommandTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,20 @@ public function testMinifyAssetCommandFailsWhenInputFileDoesNotExist(): void
6565
$this->assertStringContainsString('Cannot read file', $tester->getDisplay());
6666
}
6767

68+
public function testMinifyAssetCommandFailsWhenInputFileIsNotCssOrJS(): void
69+
{
70+
$minifier = $this->createMock(MinifierInterface::class);
71+
72+
$command = new MinifyAssetCommand($minifier, __DIR__.'/../Fixtures');
73+
$tester = new CommandTester($command);
74+
75+
$tester->execute(['input' => 'MinifyBundleTestKernel.php']);
76+
$this->assertSame(Command::FAILURE, $tester->getStatusCode());
77+
$display = $tester->getDisplay();
78+
$this->assertStringContainsString('The type of', $display);
79+
$this->assertStringContainsString('TestKernel.php" is "php", it must be "css" or "js".', $display);
80+
}
81+
6882
public function testMinifyAssetCommandFailsWhenOutputFileIsNotWritable(): void
6983
{
7084
$minifier = $this->createMock(MinifierInterface::class);

0 commit comments

Comments
 (0)