【PHP】PHPUnitとGuardを使ってファイルを変更時に自動でテストし、結果をMacに通知させる
ファイルシステムに変更が起きたイベントを処理するGuradとPHPUnitを利用して、ファイルを編集した後自動でテストが走るようにする。
gemをインストールする
gem install guard gem install guard-phpunit gem install terminal-notifier-guard
もし上記コマンドを実行している時に追加パッケージのインストールを求められたらインストールする。(rb-fsevent等)
Guardの起動と設定
テストファイルが含まれているディレクトリまで移動し、以下を実行する。
guard init
以下のように新しいGuardfileが作成されて、PHPUnitに追加されたらOK
17:29:04 - INFO - Writing new Guardfile to /Users/ikemonn/Guardfile 17:29:04 - INFO - phpunit guard added to Guardfile, feel free to edit it
もし、実行して以下のエラーが出た場合はこちらを参考に修正する。
17:29:04 - INFO - Writing new Guardfile to /Users/ikemonn/Guardfile 17:29:04 - ERROR - Could not load 'guard/phpunit' or '~/.guard/templates/phpunit' or find class Guard::Phpunit
Guardfileの編集
testsディレクトリ以下のXXXTest.phpが編集されたら自動でテストが走るようにする。 先ほど作成されたGuardfile(上記だと/Users/ikemonn/Guardfile)を以下のように編集する。
# A sample Guardfile # More info at https://github.com/guard/guard#readme guard 'phpunit', :cli => '--colors', :tests_path => 'tests' do watch(%r{^.+Test.php$}) end
Guardの実行
$ guard
以下のようなメッセージが表示されるとOK
18:25:36 - INFO - Guard is using TerminalNotifier to send notifications. 18:25:36 - INFO - Guard is using TerminalTitle to send notifications. 18:25:39 - INFO - Running all tests 18:25:41 - INFO - ...... > [#8E89569690D4] > [#8E89569690D4] Finished in 1.54 seconds > [#8E89569690D4] 6 tests, 6 assertions
Macの通知にもでる。
もし、以下のようなエラーが出た場合はこちらを参考に修正する。
17:41:41 - ERROR - Could not load 'guard/phpunit' or find class Guard::Phpunit 17:41:41 - ERROR - /Users/ikemonn/.rvm/rubies/ruby-2.1.0/lib/ruby/2.1.0/rubygems/specification.rb:2064:in `raise_if_conflicts'