Storing a pair of source.png
and expected_result.png
(generated once by the software, verified as good and stored as the reference image) will suffice. Implementing a comparison function seems to be an overhead.
The main purpose of the unit tests is to signalize if system behavior changes, and that's what such test going to do if newly created thumbnail won't match with the reference one.
Yet, if for whichever reason software generates slightly different images every time, then, in case it's not a bug, use the suggested compare similar images approach.
What if image contents differ
In case of PNG files used in this example their contents might contain some auxiliary info such as EXIF.
So you might have to try creating a copy image without this additional info. Please verify if the following code works for you:
public function testThumbnails()
{
$this->assertPngImageContentsEquals(__DIR__ . '/test1.png', __DIR__ . '/test2.png');
}
public static function assertPngImageContentsEquals(
$expected,
$actual,
$message = 'Contents of PNG files differ'
)
{
self::assertFileExists($expected, $message);
self::assertFileExists($actual, $message);
$copy_expected = self::_makePngCopy($expected, __DIR__ . '/expected.png');
$copy_actual = self::_makePngCopy($actual, __DIR__ . '/actual.png');
var_dump($copy_expected);
var_dump($copy_actual);
self::assertFileEquals($copy_expected, $copy_actual, 'Thumbnails differ');
unlink($copy_expected);
unlink($copy_actual);
}
private static function _makePngCopy($sourceFile, $resultFile)
{
$image = imagecreatefrompng($sourceFile);
imagepng($image, $resultFile);
imagedestroy($image);
return $resultFile;
}