1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
|
<?php
declare(strict_types=1);
namespace WebThumbnailer;
use PHPUnit\Framework\TestCase;
use WebThumbnailer\Application\ConfigManager;
use WebThumbnailer\Utils\FileUtils;
/**
* Test library front end using a local server launched by PHPUnit.
*/
class WebThumbnailerTest extends TestCase
{
/**
* PHP builtin local server URL.
*/
protected const LOCAL_SERVER = 'http://localhost:8081/';
/**
* @var string relative path.
*/
protected static $cache = 'tests/workdir/cache/';
/**
* @var string relative path.
*/
protected static $tmp = 'tests/workdir/tmp/';
/**
* @var string relative path.
*/
protected static $expected = 'tests/resources/expected-thumbs/';
/**
* @var string relative path were GD will regenerate expected image.
*/
protected static $regenerated = 'tests/workdir/regnerated/';
/**
* Load test config before running tests.
*/
public function setUp(): void
{
$resource = 'tests/resources/';
ConfigManager::clear();
ConfigManager::addFile($resource . 'settings-useful.json');
}
/**
* Remove cache folder after every tests.
*/
public function tearDown(): void
{
FileUtils::rmdir(self::$cache);
FileUtils::rmdir(self::$tmp);
FileUtils::rmdir(self::$regenerated);
}
/**
* Simple image URL.
*/
public function testDirectImage(): void
{
$image = 'default/image.png';
$this->regenerate($image);
$expected = self::$regenerated . $image;
$url = self::LOCAL_SERVER . $image;
$wt = new WebThumbnailer();
$thumb = $wt->thumbnail($url);
$this->assertEquals(base64_encode(file_get_contents($expected)), base64_encode(file_get_contents($thumb)));
$this->assertFileEquals($expected, $thumb);
}
/**
* Simple image URL without extension.
*/
public function testDirectImageWithoutExtension(): void
{
$image = 'default/image';
$this->regenerate($image);
$expected = self::$regenerated . $image;
$url = self::LOCAL_SERVER . $image;
$wt = new WebThumbnailer();
$thumb = $wt->thumbnail($url);
$this->assertFileEquals($expected, $thumb);
}
/**
* URL which contains an opengraph image.
*/
public function testOpenGraphImage(): void
{
$this->markTestSkipped('Test case depends on non-DFSG resource that was dropped.');
$image = 'default/le-monde.jpg';
$this->regenerate($image);
$expected = self::$regenerated . $image;
$url = self::LOCAL_SERVER . 'default/le-monde.html';
$wt = new WebThumbnailer();
$thumb = $wt->thumbnail($url);
$this->assertFileEquals($expected, $thumb);
}
/**
* URL which contains an opengraph image with absolute path explicitly set.
*/
public function testOpenGraphImageAbsolute(): void
{
$this->markTestSkipped('Test case depends on non-DFSG resource that was dropped.');
$image = 'default/le-monde.png';
$this->regenerate($image);
mkdir(self::$tmp);
file_put_contents(
$conf = self::$tmp . 'tmp.json',
json_encode([
'settings' => [
'path' => [
'cache' => self::$cache,
],
],
])
);
ConfigManager::addFile($conf);
$expected = self::$cache
. 'thumb/421aa90e079fa326b6494f812ad13e79/8f72b887d2e3f64c3a1c719d8058823047d3ec031601600.jpg';
$url = self::LOCAL_SERVER . 'default/le-monde.html';
$wt = new WebThumbnailer();
$thumb = $wt->thumbnail($url);
$this->assertEquals($expected, $thumb);
$this->assertFileEquals($expected, $thumb);
}
/**
* Get a file URL which isn't an image.
*/
public function testNotAnImage(): void
{
$oldlog = ini_get('error_log');
ini_set('error_log', '/dev/null');
$image = 'default/not-image.txt';
$url = self::LOCAL_SERVER . $image;
$wt = new WebThumbnailer();
$this->assertFalse($wt->thumbnail($url));
ini_set('error_log', $oldlog);
}
/**
* Simple image URL in download mode, resizing with max width.
*/
public function testDownloadDirectImageResizeWidth(): void
{
$image = 'default/image-width-341.png';
$this->regenerate($image);
$expected = self::$regenerated . $image;
$url = self::LOCAL_SERVER . 'default/image.png';
$wt = new WebThumbnailer();
$wt = $wt->maxWidth(341);
$thumb = $wt->thumbnail($url);
$this->assertFileEquals($expected, $thumb);
}
/**
* Simple image URL in download mode, resizing with max height.
*/
public function testDownloadDirectImageResizeHeight(): void
{
$image = 'default/image-height-341.png';
$this->regenerate($image);
$expected = self::$regenerated . $image;
$url = self::LOCAL_SERVER . 'default/image.png';
$wt = new WebThumbnailer();
$wt = $wt->maxHeight(341);
$thumb = $wt->thumbnail($url);
$this->assertFileEquals($expected, $thumb);
}
/**
* Simple image URL in download mode, resizing with max width and max height.
*/
public function testDownloadDirectImageResizeBothWidth(): void
{
$image = 'default/image-width-341.png';
$this->regenerate($image);
$expected = self::$regenerated . $image;
$url = self::LOCAL_SERVER . 'default/image.png';
$wt = new WebThumbnailer();
$wt = $wt->maxWidth(341)->maxHeight(341);
$thumb = $wt->thumbnail($url);
$this->assertFileEquals($expected, $thumb);
}
/**
* Simple image URL in download mode, resizing with max height and max height, with vertical image.
*/
public function testDownloadDirectImageResizeBothHeight(): void
{
$image = 'default/image-vertical-height-341.png';
$this->regenerate($image);
$expected = self::$regenerated . $image;
$url = self::LOCAL_SERVER . 'default/image-vertical.png';
$wt = new WebThumbnailer();
$wt = $wt->maxHeight(341)->maxWidth(341);
$thumb = $wt->thumbnail($url);
$this->assertFileEquals($expected, $thumb);
}
/**
* Simple image URL in download mode, crop enabled without both dimensions.
*/
public function testDownloadDirectImageResizeWidthCrop(): void
{
$oldlog = ini_get('error_log');
ini_set('error_log', '/dev/null');
$url = self::LOCAL_SERVER . 'default/image.png';
$wt = new WebThumbnailer();
$wt = $wt->maxWidth(341)->crop(true);
$this->assertFalse(@$wt->thumbnail($url));
ini_set('error_log', $oldlog);
}
/**
* Simple image URL in download mode, crop enabled without both dimensions.
*/
public function testDownloadDirectImageResizeHeightCrop(): void
{
$oldlog = ini_get('error_log');
ini_set('error_log', '/dev/null');
$url = self::LOCAL_SERVER . 'default/image.png';
$wt = new WebThumbnailer();
$wt = $wt->maxHeight(341)->crop(true);
$this->assertFalse($wt->thumbnail($url));
ini_set('error_log', $oldlog);
}
/**
* Simple image URL in download mode, resizing with max height/width + crop.
*/
public function testDownloadDirectImageResizeWidthHeightCrop(): void
{
$image = 'default/image-crop-341-341.png';
$this->regenerate($image, true);
$expected = self::$regenerated . $image;
$url = self::LOCAL_SERVER . 'default/image-crop.png';
$wt = new WebThumbnailer();
$wt = $wt->maxHeight(341)->maxWidth(341)->crop(true);
$thumb = $wt->thumbnail($url);
$this->assertEquals(base64_encode(file_get_contents($expected)), base64_encode(file_get_contents($thumb)));
$this->assertFileEquals($expected, $thumb);
}
/**
* Simple image URL in download mode, resizing with max height/width + crop.
* Override max heigth/width using array settings.
*/
public function testDownloadDirectImageResizeWidthHeightCropOverride(): void
{
$image = 'default/image-crop-120-160.png';
$this->regenerate($image, true);
$expected = self::$regenerated . $image;
$url = self::LOCAL_SERVER . 'default/image-crop.png';
$wt = new WebThumbnailer();
$wt = $wt->maxHeight(341)->maxWidth(341)->crop(true);
$thumb = $wt->thumbnail(
$url,
[
WebThumbnailer::MAX_WIDTH => 120,
WebThumbnailer::MAX_HEIGHT => 160,
]
);
$this->assertEquals(base64_encode(file_get_contents($expected)), base64_encode(file_get_contents($thumb)));
$this->assertFileEquals($expected, $thumb);
}
/**
* Simple image URL, in hotlink mode.
*/
public function testHotlinkSimpleImage(): void
{
$url = self::LOCAL_SERVER . 'default/image.png';
$wt = new WebThumbnailer();
$thumb = $wt->modeHotlink()->thumbnail($url);
$this->assertEquals($url, $thumb);
}
/**
* Simple image URL without extension, in hotlink mode.
*/
public function testHotlinkSimpleImageWithoutExtension(): void
{
$url = self::LOCAL_SERVER . 'default/image';
$wt = new WebThumbnailer();
$thumb = $wt->modeHotlink()->thumbnail($url);
$this->assertEquals($url, $thumb);
}
/**
* Simple opengraph URL, in hotlink mode.
*/
public function testHotlinkOpenGraph(): void
{
$this->markTestSkipped('Test case depends on non-DFSG resource that was dropped.');
$expected = 'https://img.lemde.fr/2016/10/21/107/0/1132/566/1440/720/60/0/fe3b107_3522-d2olbw.y93o25u3di.jpg';
$url = self::LOCAL_SERVER . 'default/le-monde.html';
$wt = new WebThumbnailer();
$thumb = $wt->modeHotlink()->thumbnail($url);
$this->assertEquals($expected, $thumb);
}
/**
* Simple opengraph URL, in hotlink mode set by config file.
*/
public function testHotlinkOpenGraphJsonConfig(): void
{
$this->markTestSkipped('Test case depends on non-DFSG resource that was dropped.');
$expected = 'https://img.lemde.fr/2016/10/21/107/0/1132/566/1440/720/60/0/fe3b107_3522-d2olbw.y93o25u3di.jpg';
$url = self::LOCAL_SERVER . 'default/le-monde.html';
$wt = new WebThumbnailer();
ConfigManager::addFile('tests/resources/settings-hotlink.json');
$thumb = $wt->thumbnail($url);
$this->assertEquals($expected, $thumb);
}
/**
* Duplicate expected thumbnails using the current GD version.
*
* Different versions of GD will result in slightly different images,
* which would make the comparaison test fail. By regenerating expected thumbs,
* the expected and actual result should be the same.
*
* @param string $image relative path of the expected thumb inside the expected thumb directory.
* @param bool $crop Set to true to apply the crop function.
* @param int[] $cropParameters Crop parameters: x, y, width and height
*
* @throws \Exception couldn't create the image.
*/
public function regenerate(string $image, bool $crop = false, array $cropParameters = []): void
{
$targetFolder = dirname(self::$regenerated . $image);
if (! is_dir($targetFolder)) {
mkdir($targetFolder, 0755, true);
}
$content = file_get_contents(self::$expected . $image);
$sourceImg = @imagecreatefromstring($content);
$width = imagesx($sourceImg);
$height = imagesy($sourceImg);
$targetImg = imagecreatetruecolor($width, $height);
if (
!imagecopyresized(
$targetImg,
$sourceImg,
0,
0,
0,
0,
$width,
$height,
$width,
$height
)
) {
@imagedestroy($sourceImg);
@imagedestroy($targetImg);
throw new \Exception('Could not generate the thumbnail from source image.');
}
if ($crop) {
if (!empty($cropParameters)) {
[$x, $y, $cropWidth, $croptHeight] = $cropParameters;
}
$targetImg = imagecrop($targetImg, [
'x' => $x ?? 0,
'y' => $y ?? 0,
'width' => $cropWidth ?? $width,
'height' => $croptHeight ?? $height
]);
}
$target = self::$regenerated . $image;
imagedestroy($sourceImg);
imagejpeg($targetImg, $target);
imagedestroy($targetImg);
}
}
|