[WP Plugin Dev] WordPress プラグインのユニットテスト環境を作る

memo.

[markdown]
[Hatamoto vs wp scaffold plugin](https://www.d-wood.com/blog/2017/07/27_9125.html) で作った環境で動かしてみます。

サイトの拡張性を飛躍的に高める WordPressプラグイン開発のバイブル
SBクリエイティブ (2014-08-07)
売り上げランキング: 67,805

## PHPUnit テスト用の WordPress を用意する

> * [Plugin Unit Tests – WP-CLI — WordPress](https://make.wordpress.org/cli/handbook/plugin-unit-tests/)

“`prettyprinted
$ cd /var/www/html/wp-content/plugins/sandbox-hatamoto
$ bash bin/install-wp-tests.sh wordpress_test root ‘wordpress’ localhost latest
+ install_wp
+ ‘[‘ -d /tmp/wordpress/ ‘]’
+ mkdir -p /tmp/wordpress/
+ [[ latest == \n\i\g\h\t\l\y ]]
+ [[ latest == \t\r\u\n\k ]]
+ ‘[‘ latest == latest ‘]’
+ local ARCHIVE_NAME=latest
+ download https://wordpress.org/latest.tar.gz /tmp/wordpress.tar.gz
++ which curl
+ ‘[‘ /usr/bin/curl ‘]’
+ curl -s https://wordpress.org/latest.tar.gz
+ tar –strip-components=1 -zxmf /tmp/wordpress.tar.gz -C /tmp/wordpress/
+ download https://raw.github.com/markoheijnen/wp-mysqli/master/db.php /tmp/wordpress//wp-content/db.php
++ which curl
+ ‘[‘ /usr/bin/curl ‘]’
+ curl -s https://raw.github.com/markoheijnen/wp-mysqli/master/db.php
+ install_test_suite
++ uname -s
+ [[ Linux == \D\a\r\w\i\n ]]
+ local ioption=-i
+ ‘[‘ ‘!’ -d /tmp/wordpress-tests-lib ‘]’
+ mkdir -p /tmp/wordpress-tests-lib
+ svn co –quiet https://develop.svn.wordpress.org/tags/4.8/tests/phpunit/includes/ /tmp/wordpress-tests-lib/includes
+ svn co –quiet https://develop.svn.wordpress.org/tags/4.8/tests/phpunit/data/ /tmp/wordpress-tests-lib/data
+ ‘[‘ ‘!’ -f wp-tests-config.php ‘]’
+ download https://develop.svn.wordpress.org/tags/4.8/wp-tests-config-sample.php /tmp/wordpress-tests-lib/wp-tests-config.php
++ which curl
+ ‘[‘ /usr/bin/curl ‘]’
+ curl -s https://develop.svn.wordpress.org/tags/4.8/wp-tests-config-sample.php
++ sed ‘s:/\+$::’
++ echo /tmp/wordpress/
+ WP_CORE_DIR=/tmp/wordpress
+ sed -i ‘s:dirname( __FILE__ ) . ‘\”/src/’\”:’\”/tmp/wordpress/’\”:’ /tmp/wordpress-tests-lib/wp-tests-config.php
+ sed -i s/youremptytestdbnamehere/wordpress_test/ /tmp/wordpress-tests-lib/wp-tests-config.php
+ sed -i s/yourusernamehere/root/ /tmp/wordpress-tests-lib/wp-tests-config.php
+ sed -i s/yourpasswordhere/wordpress/ /tmp/wordpress-tests-lib/wp-tests-config.php
+ sed -i ‘s|localhost|localhost|’ /tmp/wordpress-tests-lib/wp-tests-config.php
+ install_db
+ ‘[‘ false = true ‘]’
+ PARTS=(${DB_HOST//\:/ })
+ local PARTS
+ local DB_HOSTNAME=localhost
+ local DB_SOCK_OR_PORT=
+ local EXTRA=
+ ‘[‘ -z localhost ‘]’
++ grep -e ‘^[0-9]\{1,\}$’
++ echo
+ ‘[‘ ‘]’
+ ‘[‘ -z ‘]’
+ ‘[‘ -z localhost ‘]’
+ EXTRA=’ –host=localhost –protocol=tcp’
+ mysqladmin create wordpress_test –user=root –password=wordpress –host=localhost –protocol=tcp
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
“`

## PHPUnit を実行する

“`prettyprinted
$ 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 5.6.0 by Sebastian Bergmann and contributors.
. 1 / 1 (100%)
Time: 1.24 seconds, Memory: 26.00MB
OK (1 test, 1 assertion)
“`

### 実行された sample code

“`php:wordpress/wp-content/plugins/sandbox-hatamoto/tests/test-sample.php
assertTrue( true );
}
}
“`

## WP_UnitTestCase

[WordPress Codex](https://codex.wordpress.org/) で情報が見つからず。
世の中的には [Core のコード](https://core.trac.wordpress.org/browser/) を読んで解決している模様。

> * [WordPressのコアのテストコードを読んでみようぜー! – Toro_Unit](https://torounit.com/blog/2015/11/17/2139/)
> * [miya0001/wp-unit-docs: Unofficial WP_UnitTestCase Documentation.](https://github.com/miya0001/wp-unit-docs)
> * [tests in trunk – WordPress Trac](https://core.trac.wordpress.org/browser/trunk/tests)

今突っ込んでいくと討ち死にしそうです。

## 補遺

> * [WordPress 魔改造の手引き – Qiita](http://qiita.com/tomita/items/248a377cebb4d1df7bda)
テーマ開発でユニットテストされている。
> * [WordPressの単体テスト : WordPress – FindxFine](http://www.findxfine.com/programming/wp/995561087.html)
> * [案件テーマ開発環境(その2 単体テスト) : WordPress – FindxFine](http://www.findxfine.com/programming/995561106.html)
[/markdown]