tohokuaikiのチラシの裏

技術的ネタとか。

WordPressのプラグインをUnitTestする

前の続き

WordPress+PHPUnitで検索したところ、
WordPress › Support » Running WordPress tests with PHPUnit
もあったのだけど、自分は
http://stackoverflow.com/questions/9138215/unit-testing-wordpress-plugins
からnb/wordpress-tests · GitHubを使用した。

wp-contents/plugins/pubman/tests/
に、ダウンロードしたwordpress-testsを一式置いて、
unittests-config.php
を作成。この中身は、ほとんどwp-config.phpのDefineだけなんだけど、
$table_prefix = 'wp_';

<?php
// test settings テスト用にbootstrap.phpで必要
define('WP_TESTS_DOMAIN', 'foo.example.jp');
define('WP_PHP_BINARY', '/usr/bin/php');
define('WP_TESTS_TITLE', 'パブマンプラグインのテスト');
define('WP_TESTS_EMAIL', 'webmaster@example.jp');

みたいなんもいるみたい。

そして、おもむろに
phpunit --bootstrap ../../../wp-load.php PubManPluginTest
で雛形を作って、phpunit.xmlを下記のように作成して、

<?xml version="1.0" encoding="UTF-8"?>

<phpunit backupGlobals="false"
         backupStaticAttributes="false"
         colors="true"
         convertErrorsToExceptions="true"
         convertNoticesToExceptions="true"
         convertWarningsToExceptions="true"
         processIsolation="false"
         stopOnFailure="false"
         syntaxCheck="false"
         bootstrap="bootstrap.php">
    <testsuites>
        <testsuite name="PubManPlugin Test Suite">
            <directory>./tests/</directory>
        </testsuite>
    </testsuites>
</phpunit>

$phpunit PubManPluginTest.php
すると、なんかテストを実行してくれる。

最初、マルチサイトのDefineであるdefine('WP_ALLOW_MULTISITE', true);を飛ばしてたら軽快に動いてたのだけど、なんとメイン以外の衛星ブログを消してしまっていた・・・。危険すぎる・・・。