数年ぶりにWordPressを触ることになり、色々覚え書きがてら書いていきます。
基本的にWordPressの人ではないです。
テストをするだけでなく、プラグイン内の関数を直接起動して開発したくてPHPUnitを導入することにしました。
フック使えよとか、まあそうなんですがフックの数多すぎでよくわからんのです。
動作させている環境ですが、OSX上のVagrantにCentOSを乗っけて、その中でDockerを使ってUbuntu環境でWPを動かしています。
ですので、ファイルのパーミッションとか実行するユーザーなどは少し変です。
WP-CLIのインストール
WPの便利ツールです。CLI大好き派としては大変ありがたいです。インストールは全くもって簡単です。
http://wp-cli.org に書いてあるようにダウンロードしてパスの通っている場所へ移動させるだけです。
これを使ってphpunitの実行環境をつくります。
curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
chmod +x wp-cli.phar
sudo mv wp-cli.phar /usr/local/bin/wp
これだけ。さて、実行してみます。
wp --info
Error: YIKES! It looks like you're running this as root. You probably meant to run this as the user that your WordPress install exists under.
If you REALLY mean to run this as root, we won't stop you, but just bear in mind that any code on this site will then have full control of your server, making it quite DANGEROUS.
If you'd like to continue as root, please run this again, adding this flag: --allow-root
If you'd like to run it as the user that this site is under, you can run the following to become the respective user:
sudo -u USER -i -- wp
エラーが出てしまいました。どうもrootで実行してはいけないらしいです。ただ、開発環境だし、docker上でrootでなんでもやっているのとファイルはホスト(OSX)とnfsで共有しているので、共有しているUIDを使ってユーザー作ってsudoすることにしました。
useradd -u 501 wp
echo "alias wp='sudo -u wp -i -- wp'" >> ~/.bashrc
wp --info
PHP binary:/usr/bin/php5
PHP version:5.5.9-1ubuntu4.13
php.ini used:/etc/php5/cli/php.ini
WP-CLI root dir:phar://wp-cli.phar
WP-CLI global config:
WP-CLI project config:
WP-CLI version:0.21.0
PHPUnitのインストール
phpのバージョンが5.5なので最新版は使えません。なので一つ前のバージョンを入れます。
curl -O https://phar.phpunit.de/phpunit-old.phar
chmod +x phpunit-old.phar
mv phpunit-old.phar /usr/local/bin/phpunit
phpunit --version
PHPUnit 4.8.16 by Sebastian Bergmann and contributors.
echo "alias phpunit='sudo -u wp phpunit'" >> ~/.bashrc #必要無いならいらない
WP-CLIを使ってPHPUnitを使う準備
テストに必要なファイルの配置
PHPUnitをWordpressで動かすのにどうやら、もう一つWordpressを別の場所にインストールするようです。
テストはテストのたびにDBをリセットしてプラグインを読み込んでその別Wordpress上で行います。
その別Wordpressをインストールしてテスト環境を構築する処理をWP-CLIがやってくれます。
Plugin名をhogePluginとします。
cd /path/to/wordpress/
wp scaffold --path=/path/to/wordpress/ plugin-tests hogePlugin
cd /path/to/wordpress/wp-contents/plugin/hogePlugin
wp scaffoldは自分が実行したところwordpressのインストールプラグインを--pathとして渡さないとエラーが出てしまいました。
これでプラグインディレクトリに
+ .travis.yml #travis ci用今は無視
+ bin/
+ install-wp-tests.sh #次項でつかいます。
+ phpunit.xml #phpunitの設定ファイル
+ test/
+ bootstrap.php #phpunitのbootstrapファイル
+ test-sample.php #テストのサンプル
が出来ているはずです。
テスト環境の構築
bin/install-wp-tests.sh
を使用して新しいWordpressの実行環境とデータベースとテストに必要なファイルをインストールします。
usage: ./bin/install-wp-tests.sh [db-host] [wp-version]
ということですが、データベースを作成するのでDBを作れるユーザーでないとダメです。開発環境だしDocker上だしで自分はroot使ってしまいます。
./bin/install-wp-tests.sh wordpress_test root xxxxxxxx localhost 4.3.1
そうすると指定したバージョンのWordPressをダウンロードして/tmp/wordpress
に展開します。/tmp/wordpress-tests-lib
にも色々入ります。その時にsvnをつかっているのでsubversionもインストールしておきましょう。
最近はgitばかりでsubversion使っていないので入っていないことが多そうです。
ログが流れて最後に
mysqladmin create wordpress_test --user=root --password=xxxxxxxx --host=localhost --protocol=tcp
とでたら完了です。途中で失敗した場合 /tmp/wordpress,/tmp/wordpress-tests-lib
を削除してから再実行して下さい。
テストの実行
WP-cliが作ってくれたサンプルのテストコードがあるのでphpunitと実行するだけで動くはずです。
phpunit
Installing...
Running as single site... To run multisite, use -c tests/phpunit/multisite.xml
Not running ajax tests. To execute these, use --group ajax.
Not running ms-files tests. To execute these, use --group ms-files.
Not running external-http tests. To execute these, use --group external-http.
PHPUnit 4.8.16 by Sebastian Bergmann and contributors.
E
Time: 963 ms, Memory: 33.00Mb
There was 1 error:
1) SampleTest::test_sample
UnexpectedValueException: RecursiveDirectoryIterator::__construct(/tmp/wordpress/wp-content/uploads): failed to open dir: No such file or directory
/tmp/wordpress-tests-lib/includes/testcase.php:523
/tmp/wordpress-tests-lib/includes/testcase.php:541
/tmp/wordpress-tests-lib/includes/testcase.php:26
FAILURES!
Tests: 1, Assertions: 0, Errors: 1.
っと、エラーが出てしまいました。uploadsディレクトリがないようです。
mkdir /tmp/wordpress/wp-content/uploads
chmod 777 /tmp/wordpress/wp-content/uploads
phpunit
Installing...
Running as single site... To run multisite, use -c tests/phpunit/multisite.xml
Not running ajax tests. To execute these, use --group ajax.
Not running ms-files tests. To execute these, use --group ms-files.
Not running external-http tests. To execute these, use --group external-http.
PHPUnit 4.8.16 by Sebastian Bergmann and contributors.
.
Time: 853 ms, Memory: 33.00Mb
OK (1 test, 1 assertion)
サンプルテストが通りました!
これでPHPUnitが使えるようになりました。PHPUnitの使い方はここでは書きません。
おまけ
テスト環境に日本語WordPressを使う
/tmp/wordpress
を同じバージョンの日本語版WordPressに手動で入れ替えてあげればいいだけです。
uploadsディレクトリを作るのをお忘れ無く。
テストでのタイムゾーンを変更する
デフォルトだとテスト環境のタイムゾーンはUTCです。これをAsia/Tokyoにする場合はテスト用DBのwp_options内にあるtimezone_stringをAsia/Tokyoに変えればいいような気がします。
ただ、これをやっても変わりません。実はテストをするたびにDBがリセットされてしまっています。なので、/tmp/wordpress/wp-admin/includes/schema.php
を少々いじります。
$timezone_string = '';
↓
$timezone_string = 'Asia/Tokyo';
これでタイムゾーンが東京になります。