Testing: Installation Manager
Introduction
The Installation Manager is a class used to manage the installation of WordPress.
Mantle aims to remove the need to install any external dependencies such as core
testing suites, WordPress VIP MU plugins, or other testing tools. The
Installation Manager should be capable of handling all of that and letting you
focus on writing tests. The only thing you should need to do to test your
plugin/theme/site is run composer phpunit.
Supported Use Cases
The Installation Manager supports two main use cases:
-
Installing WordPress for testing in an existing WordPress installation. The existing installation's code will be used but the Installation Manager will handle setting up the database and other configuration needed for testing.
The Modifying the WordPress Installation section will cover how to customize the WordPress installation for your specific testing needs.
-
Installing WordPress for testing in a temporary directory and rsync-ing your project to live within it for testing.
The Rsync-ing section will cover how to rsync your project for testing purposes and the Modifying the WordPress Installation section will cover how to customize the WordPress installation for your specific testing needs after the rsync process.
Modifying the WordPress Installation
The Installation Manager supports fluent methods for modifying the WordPress installation before/after the installation process. It also has helpers to aid in the setup process for projects to make it easier to get testing.
Registering a Before Callback
Before callbacks are registered using the before() method. The callback will
be executed before the WordPress installation is started.
\Mantle\Testing\manager()
->before( function() {
// Do something before the installation.
} )
->install();
Registering an After Callback
After callbacks are registered using the after() method. The callback will be
executed after the WordPress installation is finished.
\Mantle\Testing\manager()
->after( function() {
// Do something after the installation.
} )
->install();
Registering a Loaded Callback
Loaded callbacks are registered using the loaded() method. The callback will
be executed after the WordPress installation is finished and during the
muplugins_loaded WordPress hook.
\Mantle\Testing\manager()
->loaded( function() {
// Do something after the installation such as loading
// your plugin's main file.
} )
->install();
Enabling Multisite
Multisite can be enabled using the with_multisite() method. This will configure
WordPress to use Multisite during the installation process.
\Mantle\Testing\manager()
->with_multisite()
->install();
Registering a init Callback
Callbacks can be registered to run during the init WordPress hook using the init() method.
\Mantle\Testing\manager()
->init( function() {
// Do something during the init hook.
} )
->install();
Changing the Active Theme
The active theme can be changed using the with_theme() method. The method accepts
the theme name and will switch to the active theme on the muplugins_loaded
hook:
\Mantle\Testing\manager()
->with_theme( 'my-theme-name' )
->install();
Calling with_theme() will also set the WP_DEFAULT_THEME environmental variable
to the theme.
Changing the Active Plugins
The active plugins can be changed using the with_plugins() method. The method
accepts an array of plugin file paths (mirrors the active_plugins option) and
will switch to the active plugins on the muplugins_loaded hook:
\Mantle\Testing\manager()
->with_plugins( [
'my-plugin/my-plugin.php',
'my-other-plugin/my-other-plugin.php',
] )
->install();