• Hi, I’m writing unit tests for a plugin which extracts EXIF data from images.

    The plugin works, but the tests are failing.

    Specifically, I’m unable to use wp_read_image_metadata with the [attachment factory](http://develop.svn.wordpress.org/trunk/tests/phpunit/includes/factory/class-wp-unittest-factory-for-attachment.php).

    It seems that the factory uploads an image to a path, which can be returned by get_attached_file(), but doesn’t resolve to a real file. This means that it can’t be inspected by wp_read_image_metadata().

    The root problem seems to be that the ‘file’ fails PHP’s file_exists check. This causes wp_read_image_metadata() to exit with false.

    Inspecting the directory path identified by get_attached_file() shows an empty folder.

    More details on the linked Github issue.

    Many thanks,
    Dan

    The page I need help with: [log in to see the link]

Viewing 1 replies (of 1 total)
  • Joey

    (@leglesslizard)

    Hey,

    To unit test this should be tested completely in isolation. Relying on other functions defeats the points of unit testing. Here you’d want to mock the functions get_attached_file() and wp_read_image_metadata() to return controlled values. With these returning values you are in control of you know that this function is returning the expected result.

    I’m unsure of your exact setup but when unit testing I have always used WPMock https://github.com/10up/wp_mock to mock any WordPress functionality to keep everything isolated.

Viewing 1 replies (of 1 total)

The topic ‘PHP Unit + WP-CLI + wp_read_image_metadata (file_exists)’ is closed to new replies.