Use custom configuration files in WP tests

WP Loader and config files.

The problem

In the majority of the WordPress projects I work on I will have a line like this in the wp-config.php file

$local_config_file = dirname(__FILE__) . '/local-config.php';
if(file_exists($local_config_file)){
    include $local_config_file;
}

in the file itself I’m usually defining some constants relevant to the local version of the project like database access details and constants

<?php

define( 'DB_NAME', 'local' );
define( 'DB_USER', 'root' );
define( 'DB_PASSWORD', 'root' );
define( 'DB_HOST', '127.0.0.1' );

// make sure $_SERVER global is set
if ( ! isset( $_SERVER ) ) {
    $_SERVER = array();
}

// WP-CLI spoof
if ( ! isset( $_SERVER['HTTP_HOST'] ) ) {
    $_SERVER['HTTP_HOST'] = 'wp.dev';
}

if ( ! isset( $_SERVER['SERVER_NAME'] ) ) {
    $_SERVER['SERVER_NAME'] = 'wp.dev';
}

$site_url = 'http://' . $_SERVER['HTTP_HOST'];

// Custom paths
if ( ! defined( 'ABSPATH' ) ) {
    define( 'ABSPATH', dirname( __FILE__ ) . '/wp/' );
}

define( 'WP_HOME', $site_url . '/' );
define( 'WP_SITEURL', WP_HOME . 'wp' );
define( 'WP_CONTENT_DIR', dirname( __FILE__ ) . '/wp-content' );
define( 'WP_CONTENT_URL', $site_url . '/wp-content' );
define( 'WP_PLUGIN_DIR', WP_CONTENT_DIR . '/plugins' );

When running tests using the WP Loader module of wp-browser the constants and variables defined in that file need to be loaded along with the configuration defined in the WP Loader Module configuration.

WP Loader solution

To allow for that I’ve added the config_file parameter to the module configuration, that should be a path relative to wpRootFolder.
As an example this might the WP Loader configuration for a project I’m working on

WPLoader:
    wpRootFolder: /Users/Luca/Sites/wp
    dbName: wordpress-tests
    dbHost: localhost
    dbUser: root
    dbPassword: password
    wpDebug: true
    dbCharset: utf8
    dbCollate: ''
    tablePrefix: wptests_
    domain: wp.dev
    adminEmail: admin@wp.dev
    title: 'WP Tests'
    phpBinary: php
    language: ''
    config_file: ../local-config.php
    plugins:
        - plugin-a/plugin-a.php
        - plugin-b/plugin-b.php

The version current to this post is the 1.6.18 one.