Title: PHP Unit + WP-CLI + wp_read_image_metadata (file_exists)
Last modified: May 14, 2018

---

# PHP Unit + WP-CLI + wp_read_image_metadata (file_exists)

 *  [dotherightthingnz](https://wordpress.org/support/users/dotherightthingnz/)
 * (@dotherightthingnz)
 * [8 years ago](https://wordpress.org/support/topic/php-unit-wp-cli-wp_read_image_metadata-file_exists/)
 * 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](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](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fwordpress.org%2Fsupport%2Ftopic%2Fphp-unit-wp-cli-wp_read_image_metadata-file_exists%2F%3Foutput_format%3Dmd&locale=en_US)
   to see the link]_

Viewing 1 replies (of 1 total)

 *  [Joey](https://wordpress.org/support/users/leglesslizard/)
 * (@leglesslizard)
 * [8 years ago](https://wordpress.org/support/topic/php-unit-wp-cli-wp_read_image_metadata-file_exists/#post-10301453)
 * 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](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.

## Tags

 * [wp-cli](https://wordpress.org/support/topic-tag/wp-cli/)

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 1 reply
 * 2 participants
 * Last reply from: [Joey](https://wordpress.org/support/users/leglesslizard/)
 * Last activity: [8 years ago](https://wordpress.org/support/topic/php-unit-wp-cli-wp_read_image_metadata-file_exists/#post-10301453)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
