# WordPressのマルチサイトでも手元でPHPUnitを動かす。

- リンク: https://torounit.com/blog/wordpress%e3%81%ae%e3%83%9e%e3%83%ab%e3%83%81%e3%82%b5%e3%82%a4%e3%83%88%e3%81%a7%e3%82%82%e6%89%8b%e5%85%83%e3%81%a7phpunit%e3%82%92%e5%8b%95%e3%81%8b%e3%81%99%e3%80%82/
- 公開日: 2015-08-03T22:21:12+09:00
- 作成者: Toro\_Unit

マルチサイト専用のプラグインを作る機会があったのですが、そのときに手元でPHPUnitする
ときのメモです。

とりあえず、wp-cliでプラグインのひな形を作ります。

    ```decode:true
    $wp scaffold plugin hoge
    ```

このままphpunitを実行してもマルチサイトにならないので、以下のようなテストコードを
書くと失敗します。

    ```decode:true
    class SampleMultisiteTest extends WP_UnitTestCase {
        public function test_is_multisite() {
            $this->assertTrue( is_multisite() );
        }
    }
    ```

その場合、phpunit.xmlを以下のように改変します。

    ```decode:true
    <phpunit
        bootstrap="tests/bootstrap.php"
        backupGlobals="false"
        colors="true"
        convertErrorsToExceptions="true"
        convertNoticesToExceptions="true"
        convertWarningsToExceptions="true"
        >
        <php>
            <const name="WP_TESTS_MULTISITE" value="1" />
        </php>
        <testsuites>
            <testsuite>
                <directory prefix="test-" suffix=".php">./tests/</directory>
            </testsuite>
        </testsuites>
    </phpunit>
    ```

WP_TEST_MULTISITEという値を渡してあげるとマルチサイトとしてテスト用のWordPressが
インストールされて動作します。

[https://github.com/torounit/multisite-sample-plugin](https://github.com/torounit/multisite-sample-plugin)

Travis CIだとtravis.yml弄るだけで大丈夫です。Travisすげー。

### 追記

[宮内さん](https://firegoby.jp/)に

    ```decode:true
    $ WP_MULTISITE=1 phpunit
    ```

でいけるだろ、と指摘されました。そりゃそうだ。

シングルサイトでもしっかりテストしなきゃ行けないときはコマンドラインで変数を渡し
て上げる方が良いですね。
