Shim for the WordPress filesystem API to use a mock filesystem in PHPUnit tests
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src
tests/phpunit
.travis.yml
LICENSE
README.md
composer.json
phpunit.xml.dist

README.md

WP Filesystem Mock Build Status

Provides a class that can be used as a mock filesystem, and also a shim for the WordPress filesystem API that uses it. This is useful in unit tests that include simple filesystem operations.

Requirements

  • PHP: 5.2.17+
  • WordPress: 3.9+

It will probably work with earlier versions of WordPress as well, but that is all that I have tested.

Note that WordPress is actually not required, and you can use the filesystem mocker without it (you just won't be able to use the extension of WordPress's filesystem API).

Installation

You may install with composer:

composer require --dev jdgrimes/wp-filesystem-mock

Usage

If you aren't using auto-loading, the first thing you need to do is load the thing:

		/**
		 * WordPress's base filesystem API class.
		 *
		 * We need to make sure this is loaded before we can load the mock.
		 */
		require_once( ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php' );

		/**
		 * The filesystem API shim that uses mock filesystems.
		 */
		require_once( MY_TESTS_DIR . '/../../vendor/jdgrimes/wp-filesystem-mock/src/wp-filesystem-mock.php' );

		/**
		 * The mock filesystem class.
		 */
		require_once( MY_TESTS_DIR . '/../../vendor/jdgrimes/wp-filesystem-mock/src/wp-mock-filesystem.php' );

To use the mock filesystem in your tests, just add this code (e.g., in your PHPUnit testcase's setUp() method):

		// Creating a new mock filesystem.
		// We assign it to a member property so we can access it later.
		$this->mock_fs = new WP_Mock_Filesystem;
		
		// Create the /wp-content directory.
		// This part is optional, and you'll do more or less setup here depending on
		// what you are testing.
		$this->mock_fs->mkdir_p( WP_CONTENT_DIR );

		// Tell the WordPress filesystem API shim to use this mock filesystem.
		WP_Filesystem_Mock::set_mock( $this->mock_fs );
		
		// Tell the shim to start overriding whatever other filesystem access method
		// is in use.
		WP_Filesystem_Mock::start();
	
		// Set up the $wp_filesystem global, if the code being tested doesn't do this.
		WP_Filesystem();

For a full view of what the it can do, check the source.