Skip to content
Mitsuru Mutaguchi edited this page Apr 4, 2018 · 16 revisions

PHPUnitを利用します。
http://book.cakephp.org/2.0/ja/development/testing.html
https://phpunit.de/manual/current/ja/writing-tests-for-phpunit.html

作成するプログラムは必ずテストコードを付け、コードカバレッジを100%にしてください。
JavaScriptのコードについては、テストコードを付けることを推奨します。

実行例

ブラウザからの実行
http://book.cakephp.org/2.0/ja/development/testing.html#id8

コマンドで実行

$ cd /var/www/app/app
$ ../vendors/bin/cake test プラグイン名 --stderr 

コマンドを実行すると、phpunit.xmlの定義に基づいてログを出力します。 https://phpunit.de/manual/current/ja/appendixes.configuration.html#appendixes.configuration.logging

サンプルコード(phpunit.xml)

<logging>
    <log type="coverage-html" target="build/logs/clover"/>
</logging>

/var/www/app/app/build/logs/clover配下にログが出力されます。

phpunitテスト実装

PHPUnit5.7 マニュアル
https://phpunit.de/manual/5.7/ja/index.html

サンプル

Controllerテストでmodelをmockに変更。mockの戻り値指定できる

https://github.com/NetCommons3/Videos/blob/3.1.5/Test/Case/Controller/VideoFrameSettingsController/EditTest.php#L163 https://github.com/NetCommons3/NetCommons/blob/3.1.6/TestSuite/NetCommonsControllerTestCase.php#L275

Controllerテストでmodelをmockに変更。mockの戻り値false

modelのsaveで例外テストに使える https://github.com/NetCommons3/Videos/blob/3.1.5/Test/Case/Controller/VideoBlocksController/EditTest.php#L249

Modelテストでmodelをmockに変更。mockの戻り値はFalse

https://github.com/NetCommons3/Videos/blob/3.1.5/Test/Case/Model/Video/CountUpTest.php#L91 https://github.com/NetCommons3/NetCommons/blob/3.1.6/TestSuite/NetCommonsModelTestCase.php#L67

ControllerテストでSessionを設定したい

NetCommonsControllerBaseTestCase::generateNc() でSessionをモックに設定している個所があるため
https://github.com/NetCommons3/UserManager/blob/3.1.5/Test/Case/Controller/UserAddController/BasicTest.php#L123-L125 https://github.com/NetCommons3/NetCommons/blob/3.1.6/TestSuite/NetCommonsControllerBaseTestCase.php#L216-L227

Controllerテストでnewしてるクラスをテストクラスに差し替えたい

App::uses()で差し替わらなかったら、App::import()を使う https://github.com/NetCommons3/Videos/blob/3.1.8/Test/Case/Controller/VideosController/DownloadTest.php#L117 https://github.com/NetCommons3/Videos/blob/3.1.8/Test/test_app/Plugin/TestFiles/Utility/ZipDownloader.php

Fixtureを他プラグインからコピーして自プラグインで使いたい

https://github.com/NetCommons3/Videos/blob/3.1.5/Test/Fixture/UploadFileForVideoFixture.php

dataProvider

dataProviderとはphpunitの便利機能の1つ https://github.com/NetCommons3/Videos/blob/3.1.5/Test/Case/Controller/VideosController/IndexTest.php#L138
https://phpunit.de/manual/5.7/ja/writing-tests-for-phpunit.html#writing-tests-for-phpunit.data-providers

トラビステストで利用しているphpunitのバージョン(NetCommons3リポジトリの場合)

phpunit4.x, 5.xどちらも使ってます。

まずphpunitのstable版(phpunit5.x版)がベースです。(php7.0,7.1が対象)
下記phpのバージョンに応じて、phpunitを取得しなおしてます。
php5.4, 5.5は、phpunit4.x版
php5.6は、phpunit5.x版

https://github.com/NetCommons3/NetCommons3/blob/3.1.8/composer.json#L34 https://github.com/NetCommons3/NetCommons3/blob/3.1.8/tools/build/app/cakephp/travis/pre.sh#L11

phpunitのサポート期間

2018/04/02時点

Major Version PHP Compatibility Initial Release Support
PHPUnit 8 PHP 7.2, PHP 7.3, PHP 7.4 February 1, 2019 Support ends on February 5, 2021
PHPUnit 7 PHP 7.1, PHP 7.2, PHP 7.3 February 2, 2018 Support ends on February 7, 2020
PHPUnit 6 PHP 7.0, PHP 7.1, PHP 7.2 February 3, 2017 Support ends on February 1, 2019
PHPUnit 5 PHP 5.6, PHP 7.0, PHP 7.1 October 2, 2015 Support ended on February 2, 2018

情報源
https://phpunit.de/

Clone this wiki locally