-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
138 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the SensioLabs MinifyBundle package. | ||
* | ||
* (c) Simon André - Sensiolabs | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Sensiolabs\MinifyBundle\Tests; | ||
|
||
use PHPUnit\Framework\Attributes\CoversClass; | ||
use PHPUnit\Framework\TestCase; | ||
use Sensiolabs\MinifyBundle\Exception\BinaryNotFoundException; | ||
use Sensiolabs\MinifyBundle\Exception\LogicException; | ||
use Sensiolabs\MinifyBundle\Minifier\MinifierInstallerInterface; | ||
use Sensiolabs\MinifyBundle\Minify; | ||
use Sensiolabs\MinifyBundle\MinifyFactory; | ||
|
||
#[CoversClass(MinifyFactory::class)] | ||
final class MinifyFactoryTest extends TestCase | ||
{ | ||
public function testMminifyBinaryIsCreatedWhenBinaryPathIsAuto(): void | ||
{ | ||
$factory = new MinifyFactory('auto'); | ||
$minify = $factory->create(); | ||
$this->assertInstanceOf(Minify::class, $minify); | ||
} | ||
|
||
public function testExceptionIsThrownWhenBinaryNotFound(): void | ||
{ | ||
$this->expectException(BinaryNotFoundException::class); | ||
$factory = new MinifyFactory(false, $this->createMock(MinifierInstallerInterface::class)); | ||
$factory->create(); | ||
} | ||
|
||
public function testExceptionIsThrownWhenInstallerIsNullAndBinaryPathIsFalse(): void | ||
{ | ||
$this->expectException(LogicException::class); | ||
$factory = new MinifyFactory(false, null); | ||
$factory->create(); | ||
} | ||
|
||
public function testInstallerIsCalledWhenBinaryNotFound(): void | ||
{ | ||
$installer = $this->createMock(MinifierInstallerInterface::class); | ||
$installer->expects($this->once())->method('install'); | ||
$installer->method('isInstalled')->willReturn(false); | ||
$installer->method('getInstallBinaryPath')->willReturn('/usr/local/bin/minify'); | ||
|
||
$this->expectException(BinaryNotFoundException::class); | ||
$factory = new MinifyFactory(false, $installer); | ||
$factory->create(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the SensioLabs MinifyBundle package. | ||
* | ||
* (c) Simon André - Sensiolabs | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Sensiolabs\MinifyBundle\Tests; | ||
|
||
use PHPUnit\Framework\Attributes\CoversClass; | ||
use PHPUnit\Framework\TestCase; | ||
use Sensiolabs\MinifyBundle\Exception\InstallException; | ||
use Sensiolabs\MinifyBundle\MinifyInstaller; | ||
use Symfony\Contracts\HttpClient\HttpClientInterface; | ||
use Symfony\Contracts\HttpClient\ResponseInterface; | ||
|
||
#[CoversClass(MinifyInstaller::class)] | ||
class MinifyInstallerTest extends TestCase | ||
{ | ||
public function testMinifyBinaryIsInstalledWhenNotAlreadyInstalled(): void | ||
{ | ||
$installer = new MinifyInstaller('/tmp/minify'); | ||
$installer->install(); | ||
$this->assertTrue($installer->isInstalled()); | ||
} | ||
|
||
public function testExceptionIsThrownWhenBinaryCannotBeDownloaded(): void | ||
{ | ||
$httpClient = $this->createMock(HttpClientInterface::class); | ||
$httpClient->method('request')->willReturn($this->createMockResponse(500)); | ||
$installer = new MinifyInstaller('/tmp/minify/'.rand(5, 999), $httpClient); | ||
|
||
$this->expectException(InstallException::class); | ||
$installer->install(); | ||
} | ||
|
||
public function testBinaryIsDownloadedAndInstalledCorrectly(): void | ||
{ | ||
$httpClient = $this->createMock(HttpClientInterface::class); | ||
$httpClient->method('request')->willReturn($this->createMockResponse(200, 'binary content')); | ||
$installer = new MinifyInstaller('/tmp/minify', $httpClient); | ||
|
||
$installer->install(); | ||
$this->assertFileExists('/tmp/minify/minify'); | ||
$this->assertTrue(is_executable('/tmp/minify/minify')); | ||
} | ||
|
||
private function createMockResponse(int $statusCode, string $content = ''): ResponseInterface | ||
{ | ||
$response = $this->createMock(ResponseInterface::class); | ||
$response->method('getStatusCode')->willReturn($statusCode); | ||
$response->method('getContent')->willReturn($content); | ||
|
||
return $response; | ||
} | ||
} |