Skip to content

Commit f59b459

Browse files
committed
fix: download minify.exe
1 parent 74ac9ee commit f59b459

File tree

3 files changed

+123
-20
lines changed

3 files changed

+123
-20
lines changed

src/MinifyInstaller.php

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -82,32 +82,29 @@ public function download(string $version): void
8282
$this->filesystem->appendToFile($downloadFilename, $chunk->getContent(), true);
8383
}
8484

85-
if (str_ends_with($downloadFilename, '.zip')) {
86-
$download = function () use ($downloadFilename, $tempDir) {
87-
$archive = new \ZipArchive();
88-
$archive->open($downloadFilename);
89-
$archive->extractTo($tempDir, 'minify');
90-
$archive->close();
91-
};
92-
} else {
93-
$download = function () use ($downloadFilename, $tempDir) {
94-
$archive = new \PharData($downloadFilename);
95-
$archive->extractTo($tempDir, ['minify'], true);
96-
};
97-
}
98-
99-
try {
100-
$download();
101-
} catch (\Throwable $e) {
102-
throw new InstallException(sprintf('Error extracting the binary from archive "%s".', $downloadFilename), 0, $e->getPrevious());
103-
}
104-
10585
$this->filesystem->mkdir(dirname($this->getInstallBinaryPath()));
86+
10687
if (str_ends_with($downloadFilename, '.zip')) {
88+
// Windows archive (minify.exe)
89+
$archive = new \ZipArchive();
90+
if (true !== $archive->open($downloadFilename)) {
91+
throw new InstallException(sprintf('Error opening archive "%s".', $downloadFilename));
92+
}
93+
if (false === $archive->extractTo($tempDir, 'minify.exe')) {
94+
throw new InstallException(sprintf('Error extracting minify.exe from archive "%s".', $downloadFilename));
95+
}
96+
$archive->close();
10797
$this->filesystem->copy(Path::join($tempDir, 'minify.exe'), $this->getInstallBinaryPath());
10898
} else {
99+
$archive = new \PharData($downloadFilename);
100+
try {
101+
$archive->extractTo($tempDir, ['minify'], true);
102+
} catch (\Exception $e) {
103+
throw new InstallException(sprintf('Error extracting the binary from archive "%s".', $downloadFilename), 0, $e);
104+
}
109105
$this->filesystem->copy(Path::join($tempDir, 'minify'), $this->getInstallBinaryPath());
110106
}
107+
111108
$this->filesystem->remove($tempDir);
112109
}
113110

tests/MinifyFactoryTest.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the SensioLabs MinifyBundle package.
7+
*
8+
* (c) Simon André - Sensiolabs
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace Sensiolabs\MinifyBundle\Tests;
15+
16+
use PHPUnit\Framework\Attributes\CoversClass;
17+
use PHPUnit\Framework\TestCase;
18+
use Sensiolabs\MinifyBundle\Exception\BinaryNotFoundException;
19+
use Sensiolabs\MinifyBundle\Exception\LogicException;
20+
use Sensiolabs\MinifyBundle\Minifier\MinifierInstallerInterface;
21+
use Sensiolabs\MinifyBundle\MinifyFactory;
22+
23+
#[CoversClass(MinifyFactory::class)]
24+
final class MinifyFactoryTest extends TestCase
25+
{
26+
public function testExceptionIsThrownWhenBinaryNotFound(): void
27+
{
28+
$this->expectException(BinaryNotFoundException::class);
29+
$factory = new MinifyFactory(false, $this->createMock(MinifierInstallerInterface::class));
30+
$factory->create();
31+
}
32+
33+
public function testExceptionIsThrownWhenInstallerIsNullAndBinaryPathIsFalse(): void
34+
{
35+
$this->expectException(LogicException::class);
36+
$factory = new MinifyFactory(false, null);
37+
$factory->create();
38+
}
39+
40+
public function testInstallerIsCalledWhenBinaryNotFound(): void
41+
{
42+
$installer = $this->createMock(MinifierInstallerInterface::class);
43+
$installer->expects($this->once())->method('install');
44+
$installer->method('isInstalled')->willReturn(false);
45+
$installer->method('getInstallBinaryPath')->willReturn('/usr/local/bin/minify');
46+
47+
$this->expectException(BinaryNotFoundException::class);
48+
$factory = new MinifyFactory(false, $installer);
49+
$factory->create();
50+
}
51+
}

tests/MinifyInstallerTest.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the SensioLabs MinifyBundle package.
7+
*
8+
* (c) Simon André - Sensiolabs
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace Sensiolabs\MinifyBundle\Tests;
15+
16+
use PHPUnit\Framework\Attributes\CoversClass;
17+
use PHPUnit\Framework\TestCase;
18+
use Sensiolabs\MinifyBundle\Exception\InstallException;
19+
use Sensiolabs\MinifyBundle\MinifyInstaller;
20+
use Symfony\Contracts\HttpClient\HttpClientInterface;
21+
use Symfony\Contracts\HttpClient\ResponseInterface;
22+
23+
#[CoversClass(MinifyInstaller::class)]
24+
class MinifyInstallerTest extends TestCase
25+
{
26+
public function testExceptionIsThrownWhenBinaryCannotBeDownloaded(): void
27+
{
28+
$httpClient = $this->createMock(HttpClientInterface::class);
29+
$httpClient->method('request')->willReturn($this->createMockResponse(500));
30+
$installer = new MinifyInstaller('/tmp/minify/'.rand(5, 999), $httpClient);
31+
32+
$this->expectException(InstallException::class);
33+
$installer->install();
34+
}
35+
36+
public function testBinaryIsDownloadedAndInstalledCorrectly(): void
37+
{
38+
$httpClient = $this->createMock(HttpClientInterface::class);
39+
$httpClient->method('request')->willReturn($this->createMockResponse(200, 'binary content'));
40+
$installer = new MinifyInstaller('/tmp/minify', $httpClient);
41+
42+
$installer->install();
43+
$this->assertFileExists('/tmp/minify/minify');
44+
$this->assertTrue(is_executable('/tmp/minify/minify'));
45+
}
46+
47+
private function createMockResponse(int $statusCode, string $content = ''): ResponseInterface
48+
{
49+
$response = $this->createMock(ResponseInterface::class);
50+
$response->method('getStatusCode')->willReturn($statusCode);
51+
$response->method('getContent')->willReturn($content);
52+
53+
return $response;
54+
}
55+
}

0 commit comments

Comments
 (0)