LoginSignup
24
24

More than 5 years have passed since last update.

WordPressプラグインのユニットテスト用のWordPress環境をセットアップする

Posted at

WordPressプラグインの開発にPHPUnitを導入するには、WP-CLIで以下のようなコマンドを実行すれば自動的にひな形をつくってくれます。

$ wp scaffold plugin <plugin-slug>

できあがるプラグインはこんな感じ。

├── .travis.yml
├── bin
│   └── install-wp-tests.sh
├── oreore.php
├── phpunit.xml
├── readme.txt
└── tests
    ├── bootstrap.php
    └── test-sample.php

でも意外とめんどくさいのが、これらのテストで使用されるWordPress環境の構築です。

ためしにphpunitといきなり叩いてみましょう。

$ phpunit 
PHP Warning:  require_once(/tmp/wordpress-tests-lib/includes/functions.php): failed to open stream: No such file or directory in /Users/miyauchi/www/dev.local/www/wordpress/wp-content/plugins/oreore/tests/bootstrap.php on line 6

WordPressがねえぞおらおらーという趣旨のエラーメッセージが出ます。

WP-CLIが自動的に生成するPHPUnitは/tmp以下にWordPressがセットアップされていることを前提としていて、セットアップ用のスクリプトがbin/install-wp-tests.shというファイル名で用意されています。

基本的にはこのシェルスクリプトに引数を与えて叩けばいいのですが、頻繁につくったり捨てたりしないといけない環境なので、一度捨ててもっかい作り直すまでをだーって処理するためのシェルスクリプトを用意しました。

動作環境

ローカル(Macを前提)に以下の環境がセットアップされていることが前提です。

  • phpunit
  • wp-cli
  • MySQLサーバー

あと、MySQLサーバーのrootパスワードが設定されていないことが前提です。

(あくまでもローカルのテスト環境です。外部から接続される環境は想定していませんからね!)

使い方

以下のコマンドを実行してください。

$ curl -L https://raw.githubusercontent.com/miya0001/install-wp-test-env/master/install.sh | bash
24
24
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
24
24