VVV PHP Storm, PHP Unit Testing
NOTE – This information is archival and no longer supported.
Unit Testing Code is a best practice for writing code. This page will focus on how to setup PHP Storm with PHP Unit using VVV.
VVV is an open source local development environment focused on WordPress, powered by Vagrant and is ideal for developing themes and plugins as well as for contributing to WordPress core.
Step 1: Install VVV
Visit varyingvagrantvagrants.org, or their git respository at github.com/Varying-Vagrant-Vagrants/VVV.
VVV instructions will walk you through installing dependencies for VVV including Vagrant and Virtual Box. VVV includes PHP Unit and a WordPress specific set up for using WP Unit Tests.
Step 2: Configure PHP Storm
Make sure VVV, PHP Storm and all their dependencies are up to date before beginning and that your VVV is running with Vagrant Up command before doing the following.
Several pieces of information on how to complete this setup were found on this blog post. However, that post deals with an older version of PHP Storm and VVV, so please extrapolate the details from that post and the information below to your current version of PHP Storm and VVV.
Add PHP CLI interpreter
- Go to Preferences > Language & Frameworks > PHP
- In the dialog, select the ellipsis[
…] in the CLI Interpreter Box. - Select Vagrant.
- Add the path to the root of your Vagrant Instance and click Apply.
The general section should automatically populate.
Tell PHP Storm Where to Load PHP Unit From
Note: This file path is only accessible inside the virtual machine.
- Go to Preferences > Language & Frameworks > PHP > Test Frameworks
- Select “Use Composer Autoloader”
- Select the path to the script
/usr/local/src/composer/vendor/autoload.php - Click Apply and the path mapping will populate automatically.
Setup PHP Test Runner Configuration
This file path is shared between the Virtual Environment and your local computer. Change the Username to your appropriate name. YOU MUST git clone your theme or plugin into this shared file path before being able to select it.
For each project:
- Open the project you are working on, and from the menu bar choose the “Edit Configurations” option.
- Click + and add a new PHP Unit configuration and a name for the Project.
- Select “Defined in Configuration File”
- Select “Use Alternative Configuration File”
- Point to the configuration file within the root of the theme or plugin.
[code]/Users/alanam/vagrant-local/www/wordpress-trunk/public_html/src/wp-content/themes/responsive-framework/phpunit.xml.dist[/code] - From the same screen click the folder icon for “Environment Variables”
- Add a constant that points to where PHP Unit should load for this test.
[code]WP_TESTS_DIR = /srv/www/wordpress-trunk/public_html/tests/phpunit/[/code]
This folder is accessible through the shared files with the virtual machine or via the server path only on the virtual machine. The example shows the server path on the virtual machine and should be selected.
8. Set the password and database name for the test to run in.
Maintenance & Advisory
In the root directory of the WordPress Develop branch instance of VVV, the same place you will typically find the wp-config.php file you will find a wp-tests-config-sample.php file. Copy this to the wp-tests-config.php and update the database name and password for that of the standard VVV password names. For some reason, this is not setup by default inside of VVV. See listing of default credentials.
You can use the PHPMYADMIN screen within VVV to confirm the database name for testing. As of this writing, the default test database with VVV is wordpress_unit_tests. This article in VVV’s docs has further info.
Deep Breath: Now you are ready to run tests! Select the green Play button within the menu bar and you’re off!
Additional Tips
Note: To run as multisite edit the wp-tests-config.php and uncomment the multisite code.
[code]
<span class=”s1″> // Test with multisite enabled.</span>
<span class=”s1″> // Alternatively, use the tests/phpunit/multisite.xml configuration file.</span>
<span class=”s1″>define( ‘WP_TESTS_MULTISITE’, true );</span>
[/code]
Note: There is a conflict in responsive-framework with the code below with Travis test vs local tests on single site install tests. Comment this code out to get successful tests run on local single instances of WordPress.
[code]
/**
* Before WordPress version 4.7, the theme’s functions.php file needs to be included manually.
* This only needs to be done when running as multisite. */
if ( ! function_exists( ‘responsive_init’ ) && ! is_multisite() ) {
global $wp_version;
if ( 1 > version_compare( ‘4.7’, $wp_version ) ) {
require_once( dirname( dirname( __FILE__ ) ) . ‘/functions.php’ );
[/code]