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

memo.

Hatamoto vs wp scaffold plugin で作った環境で動かしてみます。

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

Contents

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

$ 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 を実行する

$ 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

wordpress/wp-content/plugins/sandbox-hatamoto/tests/test-sample.php
<?php
/**
 * Class SampleTest
 *
 * @package Sandbox_Hatamoto
 */
/**
 * Sample test case.
 */
class SampleTest extends WP_UnitTestCase {
    /**
     * A single example test.
     */
    function test_sample() {
        // Replace this with some actual testing code.
        $this->assertTrue( true );
    }
}

WP_UnitTestCase

WordPress Codex で情報が見つからず。
世の中的には Core のコード を読んで解決している模様。

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

補遺