| 1 |
<?php |
| 2 |
|
| 3 |
namespace WebPConvert\Tests\Convert\Converters; |
| 4 |
|
| 5 |
use WebPConvert\Convert\Converters\Vips; |
| 6 |
use WebPConvert\Convert\Exceptions\ConversionFailed\ConverterNotOperational\SystemRequirementsNotMetException; |
| 7 |
use WebPConvert\Convert\Exceptions\ConversionFailedException; |
| 8 |
use WebPConvert\Exceptions\InvalidInput\TargetNotFoundException; |
| 9 |
use WebPConvert\Tests\Convert\Exposers\VipsExposer; |
| 10 |
|
| 11 |
use PHPUnit\Framework\TestCase; |
| 12 |
|
| 13 |
/** |
| 14 |
* @coversDefaultClass WebPConvert\Convert\Converters\Vips |
| 15 |
* @covers WebPConvert\Convert\Converters\Vips |
| 16 |
*/ |
| 17 |
class VipsTest extends TestCase |
| 18 |
{ |
| 19 |
|
| 20 |
public function getImageFolder() |
| 21 |
{ |
| 22 |
return realpath(__DIR__ . '/../../images'); |
| 23 |
} |
| 24 |
|
| 25 |
public function getImagePath($image) |
| 26 |
{ |
| 27 |
return $this->getImageFolder() . '/' . $image; |
| 28 |
} |
| 29 |
|
| 30 |
public function testConvert() |
| 31 |
{ |
| 32 |
$options = []; |
| 33 |
ConverterTestHelper::runAllConvertTests($this, 'Vips', $options); |
| 34 |
|
| 35 |
$options = [ |
| 36 |
'smart-subsample' => true, |
| 37 |
'preset' => 'text', |
| 38 |
]; |
| 39 |
ConverterTestHelper::runAllConvertTests($this, 'Vips', $options); |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
private function createVips($src, $options = []) |
| 44 |
{ |
| 45 |
$source = $this->getImagePath($src); |
| 46 |
$this->assertTrue(file_exists($source), 'source does not exist:' . $source); |
| 47 |
|
| 48 |
return new Vips($source, $source . '.webp', $options); |
| 49 |
} |
| 50 |
|
| 51 |
private function createVipsExposer($src, $options = []) |
| 52 |
{ |
| 53 |
return new VipsExposer($this->createVips($src, $options)); |
| 54 |
} |
| 55 |
|
| 56 |
private function isVipsOperational() |
| 57 |
{ |
| 58 |
try { |
| 59 |
$vips = $this->createVips('test.png'); |
| 60 |
$vips->checkOperationality(); |
| 61 |
$vips->checkConvertability(); |
| 62 |
} catch (\Exception $e) { |
| 63 |
return false; |
| 64 |
} |
| 65 |
return true; |
| 66 |
} |
| 67 |
|
| 68 |
public function testCreateParamsForVipsWebPSave1() |
| 69 |
{ |
| 70 |
$options = [ |
| 71 |
'method' => 3, |
| 72 |
'encoding' => 'lossless', |
| 73 |
'smart-subsample' => true, // note: deprecated |
| 74 |
'near-lossless' => 90, |
| 75 |
'preset' => 'picture', // In vips, this has the constant: 1 |
| 76 |
]; |
| 77 |
$vipsExposer = $this->createVipsExposer('test.png', $options); |
| 78 |
|
| 79 |
$vipsParams = $vipsExposer->createParamsForVipsWebPSave(); |
| 80 |
|
| 81 |
// Check some options that are straightforwardly copied |
| 82 |
|
| 83 |
$this->assertSame($options['method'], $vipsParams['reduction_effort']); |
| 84 |
$this->assertSame(true, $vipsParams['lossless']); |
| 85 |
$this->assertSame($options['smart-subsample'], $vipsParams['smart_subsample']); |
| 86 |
$this->assertSame(1, $vipsParams['preset']); |
| 87 |
|
| 88 |
// When near-lossless is set, the value should be copied to Q |
| 89 |
$this->assertSame($options['near-lossless'], $vipsParams['Q']); |
| 90 |
} |
| 91 |
|
| 92 |
public function testCreateParamsForVipsWebPSave2() |
| 93 |
{ |
| 94 |
$options = [ |
| 95 |
'alpha-quality' => 100, |
| 96 |
'sharp-yuv' => true, |
| 97 |
]; |
| 98 |
$vipsExposer = $this->createVipsExposer('test.png', $options); |
| 99 |
|
| 100 |
$vipsParams = $vipsExposer->createParamsForVipsWebPSave(); |
| 101 |
|
| 102 |
$this->assertSame($options['sharp-yuv'], isset($vipsParams['smart_subsample'])); |
| 103 |
|
| 104 |
// Some options are only set if they differ from default |
| 105 |
$this->assertFalse(isset($vipsParams['alpha_q'])); |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
public function testCreateImageResource1() |
| 110 |
{ |
| 111 |
|
| 112 |
$source = $this->getImagePath('non-existing'); |
| 113 |
|
| 114 |
// Next must fail with a TargetNotFoundException |
| 115 |
$this->expectException(TargetNotFoundException::class); |
| 116 |
|
| 117 |
$vips = new Vips($source, $source . '.webp', []); |
| 118 |
|
| 119 |
// Exit if vips is not operational |
| 120 |
if (!$this->isVipsOperational()) { |
| 121 |
return; |
| 122 |
} |
| 123 |
|
| 124 |
/* |
| 125 |
$vipsExposer = new VipsExposer($vips); |
| 126 |
|
| 127 |
// It must fail because it should not be able to create resource when file does not exist |
| 128 |
$this->expectException(ConversionFailedException::class); |
| 129 |
|
| 130 |
$vipsExposer->createImageResource();*/ |
| 131 |
} |
| 132 |
|
| 133 |
public function testNotOperational1() |
| 134 |
{ |
| 135 |
global $pretend; |
| 136 |
|
| 137 |
$vips = $this->createVips('test.png'); |
| 138 |
reset_pretending(); |
| 139 |
|
| 140 |
// pretend vips_image_new_from_file |
| 141 |
$pretend['functionsNotExisting'] = ['vips_image_new_from_file']; |
| 142 |
$this->expectException(SystemRequirementsNotMetException::class); |
| 143 |
$vips->checkOperationality(); |
| 144 |
} |
| 145 |
|
| 146 |
public function testNotOperational2() |
| 147 |
{ |
| 148 |
global $pretend; |
| 149 |
|
| 150 |
$vips = $this->createVips('test.png'); |
| 151 |
reset_pretending(); |
| 152 |
|
| 153 |
// pretend vips_image_new_from_file |
| 154 |
$pretend['extensionsNotExisting'] = ['vips']; |
| 155 |
$this->expectException(SystemRequirementsNotMetException::class); |
| 156 |
$vips->checkOperationality(); |
| 157 |
} |
| 158 |
|
| 159 |
/* |
| 160 |
Commented out because it is no good anymore, after the checkOperationality actually |
| 161 |
itself relies on the function_exists, because it now calls "vips_call" in order to |
| 162 |
detect if webp is supported |
| 163 |
|
| 164 |
public function testOperational1() |
| 165 |
{ |
| 166 |
global $pretend; |
| 167 |
|
| 168 |
$vips = $this->createVips('test.png'); |
| 169 |
reset_pretending(); |
| 170 |
|
| 171 |
// pretend vips_image_new_from_file |
| 172 |
$pretend['functionsExisting'] = ['vips_image_new_from_file', 'vips_call', 'vips_error_buffer']; |
| 173 |
$pretend['extensionsExisting'] = ['vips']; |
| 174 |
$vips->checkOperationality(); |
| 175 |
|
| 176 |
$this->addToAssertionCount(1); |
| 177 |
} |
| 178 |
*/ |
| 179 |
|
| 180 |
/** |
| 181 |
* @covers ::webpsave |
| 182 |
*/ |
| 183 |
public function testWebpsave() |
| 184 |
{ |
| 185 |
reset_pretending(); |
| 186 |
|
| 187 |
$vips = $this->createVips('test.png', []); |
| 188 |
$vipsExposer = new VipsExposer($vips); |
| 189 |
|
| 190 |
// Exit if vips is not operational |
| 191 |
if (!$this->isVipsOperational()) { |
| 192 |
$this->markTestSkipped('vips is not operational'); |
| 193 |
return; |
| 194 |
} |
| 195 |
|
| 196 |
$im = $vipsExposer->createImageResource(); |
| 197 |
$options = $vipsExposer->createParamsForVipsWebPSave(); |
| 198 |
|
| 199 |
// Create non-existing vips option. |
| 200 |
// - The converter must be able to ignore this without failing |
| 201 |
$options['non-existing-vips-option'] = true; |
| 202 |
$vipsExposer->webpsave($im, $options); |
| 203 |
} |
| 204 |
|
| 205 |
/** |
| 206 |
* @covers ::createImageResource |
| 207 |
*/ |
| 208 |
public function testCreateImageResourceWhenFileNotFound() |
| 209 |
{ |
| 210 |
// |
| 211 |
reset_pretending(); |
| 212 |
|
| 213 |
$source = $this->getImagePath('i-do-not-exist.jpg'); |
| 214 |
|
| 215 |
$this->assertFalse(file_exists($source)); |
| 216 |
|
| 217 |
$options = []; |
| 218 |
|
| 219 |
// Next must fail with a TargetNotFoundException |
| 220 |
$this->expectException(TargetNotFoundException::class); |
| 221 |
|
| 222 |
$vips = new Vips($source, $source . '.webp', $options); |
| 223 |
|
| 224 |
// Exit if vips is not operational |
| 225 |
/* |
| 226 |
if (!$this->isVipsOperational()) { |
| 227 |
$this->markTestSkipped('vips is not operational'); |
| 228 |
return; |
| 229 |
}*/ |
| 230 |
|
| 231 |
/* |
| 232 |
$vipsExposer = new VipsExposer($vips); |
| 233 |
|
| 234 |
// this should fail! |
| 235 |
try { |
| 236 |
$im = $vipsExposer->createImageResource(); |
| 237 |
$this->fail('exception was expected'); |
| 238 |
} catch (ConversionFailedException $e) { |
| 239 |
$this->assertRegExp('#not found#', $e->getMessage()); |
| 240 |
}*/ |
| 241 |
|
| 242 |
} |
| 243 |
/* |
| 244 |
public function testDoActualConvert() |
| 245 |
{ |
| 246 |
|
| 247 |
$options = [ |
| 248 |
'alpha-quality' => 100 |
| 249 |
]; |
| 250 |
$vipsExposer = $this->createVipsExposer('test.png', $options); |
| 251 |
|
| 252 |
$vips = $this->createVips('not-existing.png'); |
| 253 |
|
| 254 |
$this->addToAssertionCount(1); |
| 255 |
}*/ |
| 256 |
} |
| 257 |
|
| 258 |
require_once('pretend.inc'); |
| 259 |
|