前もつかったけど忘れたので、メモ。
対象にするのは、このWordPressプラグイン「PubMan Wordpress Plugin - MPDLMediaWiki」
インストールに追加して
PEARでpear.phpunit.deをchannel discoverしてインストールした以外に
# su - # pecl install xdebug $ pear install phpunit/PHPUnit_SkeletonGenerator
最後のは、PHPUnit – The PHP Testing Frameworkをする時に必要だったPHPUnitのオプションパッケージ。
スケルトンの作成
意気込んで
$ phpunit-skelgen --test PubManPlugin
したら
PHPUnit Skeleton Generator 1.2.0 by Sebastian Bergmann. PHP Fatal error: Uncaught exception 'RuntimeException' with message 'Neither "PubManPlugin.php" nor "PubManPlugin.php" could be opened.' in /usr/share/php/SebastianBergmann/PHPUnit/SkeletonGenerator/TestGenerator.php:105 Stack trace: #0 [internal function]: SebastianBergmann\PHPUnit\SkeletonGenerator\TestGenerator->__construct('PubManPlugin') #1 /usr/share/php/SebastianBergmann/PHPUnit/SkeletonGenerator/Command.php(171): ReflectionClass->newInstanceArgs(Array) #2 /usr/bin/phpunit-skelgen(45): SebastianBergmann\PHPUnit\SkeletonGenerator\Command::main() #3 {main} thrown in /usr/share/php/SebastianBergmann/PHPUnit/SkeletonGenerator/TestGenerator.php on line 105
ってエラー出た。あー、Class名とファイル名に相関が無いとダメなのね。
pubman_plugin.php にある PubManPluginというクラスのテストを作りたいので
$ phpunit-skelgen --class -- PubManPlugin pubman_plugin.php
とすると
PHPUnit Skeleton Generator 1.2.0 by Sebastian Bergmann. Wrote skeleton for "PubManPl" to "./PubManPl.php".
おぉ、できた!と思ったけど、できたのは
<?php /** * Generated by PHPUnit_SkeletonGenerator 1.2.0 on 2013-01-24 at 11:36:55. */ class PubManPl {}
なんじゃー。意味ない。orz
そっか。--classだったから、--testにすればいいのか!と思って
$ phpunit-skelgen --test -- PubManPlugin pubman_plugin.php
してみたら
PHPUnit Skeleton Generator 1.2.0 by Sebastian Bergmann. PHP Fatal error: Call to undefined function add_action() in ****/pubman_plugin.php on line 146
って。あー。WordPressのプラグインてそうだよね。ということでちょっと本体をClassだけに変更。。。。最初「関数無いよ!」って言われてたところをちまちまと
function add_action(){} function get_option(){} function add_shortcode(){} function update_option(){} function get_categories(){}
って定義してったのだけど、
PHP Fatal error: Call to a member function get_var() on a non-object in ***/pubman_plugin.php on line 869
って言われて断念!!!$wpdbを作らないといけないのか。ということで、オプション見たら--bootstrapって素敵気なもの発見。
$ phpunit-skelgen --bootstrap ../../../wp-load.php --test -- PubManPlugin pubman_plugin.php
ってやったらよさげな雰囲気!と思ってやったらいけた。・・・んだけど
PHPUnit Skeleton Generator 1.2.0 by Sebastian Bergmann. <h1>データベース接続確立エラー</h1>
んんん〜〜〜〜〜〜〜〜〜〜。
#: wp-includes/functions.php:2738 wp-includes/ms-load.php:237
あたりにあるらしい。wp-includes/ms-load.php:237だな。
・・・と思ったのだけど、とりあえず、コード見たらこのpubman_plugin.phpってこのファイル内でClassをnewしてるからコンストラクタが走ってエラーするのか。・・ということで、newしてる部分を削除。したらPubManPluginTest.phpできた。これは結構想定通り。
Testを走らせる
とりあえずできたので、走らせてみようかなと
$ phpunit PubManPluginTest
したら
PHPUnit 3.7.13 by Sebastian Bergmann. PHP Fatal error: Class 'PubManPlugin' not found in ***/PubManPluginTest.php on line 18
そっか。ファイル名とクラス名が(ry・・・
ということで、ド頭でrequireしておく。
<?php require_once dirname(__FILE__).'/pubman_plugin.php';
で、おもむろに
$ phpunit --bootstrap ../../../wp-load.php PubManPluginTest
すると!!
<h1>データベース接続確立エラー</h1>
...orz
誰かWordPressのプラグイン開発でPHPUnit使ってないの!?!?とgoogle:WordPress データベース接続確立エラー PHPUnit]するも、でない・・・ですよねー。じゃあと[google:PHPUnit WordPress "Error establishing a database connection"ってするけど、ダメ。
あっ、と思いWordPress › Support » Running WordPress tests with PHPUnitを発見。
今日はここまで。