PHPにはユニットテストを行う仕組みとしてPHPUnitがあります。
PHPのパッケージphpunit-watcherを使うことで、プログラムのソースが変更(Ctrl-Sで保存)されるたびに自動でPHPUnitを実行させることができます。
インストール
phpunit-watcherのインストールは、composer requireを実行するだけでOKです。
このパッケージ自体はここのプロジェクトに依存しないモノなので、globalインストールします。
composer global require spatie/phpunit-watcher
phpunit-watcherの実行
phpunit-watcherの実行は、phpのプロジェクトがあるディレクトリに移動してコマンドを1つ実行するだけです。
プロジェクトディレクトリの移動は、cdでディレクトリを移動した後の場所にvendor/bin/phpunitがあればOKです。
$ cd PATH/TO/PROJECT
$ ~/.composer/vendor/bin/phpunit-watcher watch
上記の例ではmacOSでcomposerを使ってグローバルインストールすると~/.composerにコマンドがインストールされるため、~/.composer/vendor/bin/phpunit-watcher
で指定しています。
頻繁に使用する場合は.bash_profileで~/.composer/vendor/binにパスを通しておくと、phpunit-watcher watch
で実行できるので便利です。
# ~/.bash_profile
export PATH=$PATH:~/.composer/vendor/bin
さらに、下記のエイリアスを.bash_profileに登録しておくとt
と打つだけでテストを実行できます。
# ~/.bash_profile
alias t='pushd PATH/TO/PROJECT; phpunit-watcher watch; popd'
コマンドを実行するとすぐにPHPUnitが実行され、結果が表示されます。
$ ~/.composer/vendor/bin/phpunit-watcher watch
PHPUnit Watcher
===============
PHPUnit Watcher 1.12.1 by Spatie and contributors.
No config file detected. Using default options.
Tests will be rerun when *.php files are modified in
* ../app
* ../tests
PHPUnit 8.5.2 by Sebastian Bergmann and contributors.
... 3 / 3 (100%)
Time: 112 ms, Memory: 16.00 MB
OK (3 tests, 3 assertions)
Starting PHPUnit
Press a to run all tests.
Press t to filter by test name.
Press p to filter by file name.
Press g to filter by group name.
Press s to filter by test suite name.
Press r to run tests with a random seed.
Press q to quit the watcher.
Press Enter to trigger a test run.
同時に画面上でも以下のようにテスト結果の通知が表示されます。
その後は、コマンドのの出力にあるようにappとtestsディレクトリの下にある*.phpのファイルを保存すると、自動でPHPUnitが再実行されます。
エラーの原因がプログラムではなくDBや設定などを変更した場合は、コマンドが実行されているウィンドウでEnterを押すことでテストを再実行できます。
phpunit-watcherのオプション
phpunit-watcherコマンドのwatch以外のオプションは、引数なしでコマンドを実行することで確認できます。
$ phpunit-watcher
PHPUnit Watcher 1.12.1 by Spatie
Usage:
command [options] [arguments]
Options:
-h, --help Display this help message
-q, --quiet Do not output any message
-V, --version Display this application version
--ansi Force ANSI output
--no-ansi Disable ANSI output
-n, --no-interaction Do not ask any interactive question
-v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
Available commands:
help Displays help for a command
list Lists commands
watch Rerun PHPUnit tests when source code changes.
上記に加えて--help
オプションを指定しても情報が表示されます
$ phpunit-watcher --help
Description:
Lists commands
Usage:
list [options] [--] [<namespace>]
Arguments:
namespace The namespace name
Options:
--raw To output raw command list
--format=FORMAT The output format (txt, xml, json, or md) [default: "txt"]
Help:
The list command lists all commands:
php ~/.composer/vendor/bin/phpunit-watcher list
You can also display the commands for a specific namespace:
php ~/.composer/vendor/bin/phpunit-watcher list test
You can also output the information in other formats by using the --format option:
php ~/.composer/vendor/bin/phpunit-watcher list --format=xml
It's also possible to get raw list of commands (useful for embedding command runner):
php ~/.composer/vendor/bin/phpunit-watcher list --raw
One thought on “[PHP]phpunit-watcherでファイル保存でPHPUnitを自動実行させる”