MoyaSystem

もやしです。

BuddyPressを前提としたプラグインのPHPUnit環境を構築する

PHPUnit使うのも初めてなのに、WordPress環境やらBuddyPress環境やらを整備しないといけなくってだいぶ時間がかかってしまった。備忘録的に手順まとめ。OSはMax OS Xを前提としています。

Wordpress Command line interface をインストールする

以下のリンクを参照。
Command line interface for WordPress | WP-CLI

WordPressテスト環境、およびプラグインテストひな形ファイルを作成

以下のリンクの手順に従って、WordPressユニットテスト用の環境と、プラグインテストのひな形ファイルを作成する。
Plugin Unit Tests · wp-cli/wp-cli Wiki · GitHub
自分の環境では 3)のテスト環境作成の時に「データベース接続エラー」で怒られた。結局rootとは別に管理者ユーザを作成することで解決。なぜ怒られたか理由はわからない。
ここまでやると /tmp/wordpress 以下にテスト用のWordPress環境、/tmp/wordpress-tests-lib 以下にテスト用ライブラリ一式ができあがっているはず。

BuddyPressのユニットテスト用環境を作成

通常のBuddyPressプラグインにはテスト用ソースが含まれていないので、以下のリンクの手順に従ってダウンロードする。ダウンロード先は /tmp/wordpress/wp-content/plugins
Automated Testing · BuddyPress Codex

環境変数 WP_TESTS_DIR は tmp/wordpress-tests-lib に設定しておく。

プラグインユニットテストについてbootstrap.phpを編集

以下を参照しつつbootstrap.phpを書き換える。
Writing automated tests for BuddyPress-dependent plugins · BuddyPress Codex
が、開発環境のbuddypressフォルダ下にテスト用ソース一式を置くような感じになっているので気持ち悪い。ので一部編集。完成品がこちら。

<?php
$_tests_dir = getenv('WP_TESTS_DIR');
if ( !$_tests_dir ) $_tests_dir = '/tmp/wordpress-tests-lib';
require_once $_tests_dir . '/includes/functions.php';

// The BP_TESTS_DIR constant is a helpful shorthand for accessing assets later
// on. By defining in a constant, we allow for setups where the BuddyPress
// tests may be located in a non-standard location.
if ( ! defined( 'BP_TESTS_DIR' ) ) {
    define( 'BP_TESTS_DIR', '/tmp/wordpress/wp-content/plugins/buddypress/tests/phpunit' );
}

// Checking for the existence of tests/bootstrap.php ensures that your version
// of BuddyPress supports this kind of automated testing
if ( file_exists( BP_TESTS_DIR . '/bootstrap.php' ) ) {
    // The functions.php file from the WP test suite needs to be defined early,
    // because it gives us access to the tests_add_filter() function
    require_once $_tests_dir . '/includes/functions.php';
 
    // Hooked to muplugins_loaded, this function is responsible for bootstrapping
    // BuddyPress, as well as your own plugin
    function _bootstrap_plugins() {
        // loader.php will ensure that BP gets installed at the right time, and
        // that BP is initialized before your own plugin
        require BP_TESTS_DIR . '/includes/loader.php';
 
        // Change this path to point to your plugin's loader file
        require __DIR__ . '/../your-plugin.php';

   // Function called on plugin activation should be called here
    }
    tests_add_filter( 'muplugins_loaded', '_bootstrap_plugins' );
 
    // Start up the WP testing environment
    require $_tests_dir . '/includes/bootstrap.php';

    // Requiring this file gives you access to BP_UnitTestCase
    require BP_TESTS_DIR . '/includes/testcase.php';
 
    // Optional: If your plugin needs its own _UnitTestCase class, include it
    // here so that it's available when your testcases are loaded
    //require __DIR__ . '/bp-cli-testcase.php';
}

プラグインのファイルは読み込まれるだけで、内部的にプラグインが有効になるわけではないことに注意。有効化されたときに走るファンクションが定義されている場合は、ファイルがrequireされた後に直接実行してやる必要がある。

おつかれさまでした

あとはプラグインのディレクトリに移動して phpunit コマンドを叩けばテストコードが無事実施されるはずです。