PHPUnit: assertFileEquals () not working

I am developing PHP software that creates thumbnails of images.

Now I need to make sure that the thumbnails are created successfully, in other words, that the original image was resized / cropped correctly.

I think there is only one way to do this: I manually create a sketch to compare with the sketch created by the software.

But how to check?

If I use assertFileEquals() to compare the sketch I created and the file created by the software, of course, the test fails, even if both images are identical.

I assume that this happens only because the creation date of the two files is different or for similar reasons.

So how to do this?

+5
source share
2 answers

Storing a pair of source.png and expected_result.png (once generated by the software, verified as good and saved as a reference image) will suffice. The implementation of the comparison function seems overhead.

The main purpose of unit tests is to signal whether the behavior of the system is changing, and what this test will do if the newly created sketch does not match the reference.

Nevertheless, if for some reason the software generates slightly different images each time, then if this is not an error, use the proposed approach for comparing similar images.

What to do if image content is different

In the case of PNG files used in this example, their contents may contain some supporting information, such as EXIF.

Therefore, you may have to try to create a copy of the image without this additional information. Check 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; } 
+3
source

If assertFileEquals does not work, then there is something else between the two files. The internal code calls file_get_contents in both files and claims true if there are null differences (so the creation date is not part of the statement).

Since you are manually creating a thumbnail, there should be slight differences. Instead, you will need to do a “basically the same” comparison; there are two questions related to coding this:

Then determine how much the difference is considered a transition. This way you will be doing “basically the same comparison” and then use the statements to determine if the answer is “basically the same” comparison within the range that you can accept.

Update

I checked a quick test to ensure that assertFileEquals works correctly in a binary:

class FileEqualsTest extends PHPUnit_Framework_TestCase {

 public function test_yes_no_answer() { file_put_contents("a.txt","\e[0m"); file_put_contents("b.txt","\e[0m"); file_put_contents("c.txt","\e[30m"); // straight get contents and comparisons $contentsA = file_get_contents("a.txt"); $contentsB = file_get_contents("b.txt"); $this->assertEquals($contentsA, $contentsB); $contentsC = file_get_contents("c.txt"); $this->assertNotEquals($contentsA, $contentsC); // using file equals has same answer $this->assertFileEquals("a.txt","b.txt"); $this->assertFileNotEquals("a.txt","c.txt"); } 

.. and it worked, as expected, on a very small scale. So there would seem to be some slight difference. You can try the options shown in the other questions above to see if there is a slight difference if this is important for your testing.

+2
source

Source: https://habr.com/ru/post/1257790/


All Articles