View source
<?php
namespace Drupal\Tests\imageapi_optimize_binaries\Unit;
use Drupal\imageapi_optimize_binaries\Plugin\ImageAPIOptimizeProcessor\AdvDef;
class AdvDefTest extends BinaryTestCase {
public function testDefaultConfig() {
$config = [];
$loggerMock = $this
->getLoggerMock();
$imageFactoryMock = $this
->getImageFactoryMock();
$fileSystemMock = $this
->getFileSystemMock();
$shellOperationsMock = $this
->getShellOperationsMock();
$advdef = new AdvDef($config, 'advdef', [], $loggerMock, $imageFactoryMock, $fileSystemMock, $shellOperationsMock);
$this
->assertArrayHasKey('recompress', $advdef
->defaultConfiguration());
$this
->assertArrayHasKey('mode', $advdef
->defaultConfiguration());
}
public function testOnlyApplyToPNG() {
$config = [];
$loggerMock = $this
->getLoggerMock();
$imageFactoryMock = $this
->getImageFactoryMock();
$fileSystemMock = $this
->getFileSystemMock();
$shellOperationsMock = $this
->getShellOperationsMock();
$advdef = new AdvDef($config, 'advdef', [], $loggerMock, $imageFactoryMock, $fileSystemMock, $shellOperationsMock);
$imagePNGMock = $this
->createMock('\\Drupal\\Core\\Image\\ImageInterface');
$imagePNGMock
->method('getMimeType')
->willReturn('image/jpeg');
$imageFactoryMock
->method('get')
->willReturn($imagePNGMock);
$shellOperationsMock
->expects($this
->never())
->method('execShellCommand');
$this
->assertFalse($advdef
->applyToImage('public://test_image.jpg'));
}
public function testPNGOptimized($config, $options) {
$loggerMock = $this
->getLoggerMock();
$imageFactoryMock = $this
->getImageFactoryMock();
$fileSystemMock = $this
->getFileSystemMock();
$shellOperationsMock = $this
->getShellOperationsMock();
$advdef = new AdvDef([
'data' => $config,
], 'advdef', [], $loggerMock, $imageFactoryMock, $fileSystemMock, $shellOperationsMock);
$imagePNGMock = $this
->createMock('\\Drupal\\Core\\Image\\ImageInterface');
$imagePNGMock
->method('getMimeType')
->willReturn('image/png');
$imageFactoryMock
->method('get')
->willReturn($imagePNGMock);
$shellOperationsMock
->expects($this
->once())
->method('execShellCommand')
->with($this
->equalTo('advdef'), $this
->identicalTo($options), $this
->identicalTo([
'public://test_image.png',
]));
$advdef
->applyToImage('public://test_image.png');
}
public function advdefProvider() {
$cases = [];
$cases[] = [
[],
[
'--quiet',
'--recompress',
'-3',
],
];
$cases[] = [
[
'recompress' => FALSE,
],
[
'--quiet',
'-3',
],
];
$cases[] = [
[
'recompress' => FALSE,
'mode' => 1,
],
[
'--quiet',
'-1',
],
];
$cases[] = [
[
'mode' => 1,
],
[
'--quiet',
'--recompress',
'-1',
],
];
$cases[] = [
[
'mode' => 9,
],
[
'--quiet',
'--recompress',
'-9',
],
];
return $cases;
}
}