tjtjtjのメモ

自分のためのメモです

yiiでPHPUnit 準備編

PHPUnit

Windows XAMPPなのでここを参考にしました。

First off I'm assuming xampp is installed to C:\xampp
1. Open a command prompt and go to C:\xampp\php
2. Type "pear update-channels" (updates channel definitions)
3. Type "pear upgrade" (upgrades all existing packages and pear)
4. Type "pear channel-discover components.ez.no" (this is needed for PHPUnit)
5. Type "pear channel-discover pear.symfony-project.com" (also needed by PHPUnit)
6. Type "pear channel-discover pear.phpunit.de" (This IS phpunit)
7. Type "pear install --alldeps phpunit/PHPUnit" (installs PHPUnit and all dependencies)

xdebugcurlがとかいわれたらphp.iniのコメントを解除すればいいんじゃないでしょうか。
次のようになればOK。

C:\>phpunit --version
PHPUnit 3.5.13 by Sebastian Bergmann.

C:\>

Selenium

SeleniumHQ - downloadからSelenium Serverをダウンロードし、適当なところに置いておく。

WebTestCase.php 修正

WebTestCase.php の定数TEST_BASE_URLをテスト環境にあわせる。

<?php
/**
 * Change the following URL based on your server configuration
 * Make sure the URL ends with a slash so that we can use relative URLs in test cases
 */
define('TEST_BASE_URL','http://localhost/yiiprac1/index-test.php/');
//define('TEST_BASE_URL','http://localhost/testdrive/index-test.php/');

phpunit.xml 修正

テストに使うブラウザをFireFoxにする。Internet Explorer, FireFox の順序になっているが、FireFox, Internet Explorer の順にする。
ここはテスト環境・ユーザー環境に合わせて。

<phpunit bootstrap="bootstrap.php"
:
	<selenium>
		<browser name="Firefox" browser="*firefox" />
		<browser name="Internet Explorer" browser="*iexplore" />
	</selenium>
:
</phpunit>

テスト(個別)

seleniumサーバー起動。ダウンロードしたselenium-server-*.jarを実行。

コマンドプロンプトから、phpunit で UserTest.php を実行する、FireFoxが表示されたり消えたりを繰り返してこんな感じになる。

C:\xampp\htdocs\yiiprac1\protected\tests>phpunit functional\UserTest.php
PHPUnit 3.5.13 by Sebastian Bergmann.

......

Time: 01:04, Memory: 8.25Mb

OK (6 tests, 0 assertions)

C:\xampp\htdocs\yiiprac1\protected\tests>

テスト(functional以下すべて)

seleniumサーバーを起動まま、phpunit --verboseする。

C:\xampp\htdocs\yiiprac1\protected\tests>phpunit --verbose functional
PHPUnit 3.5.13 by Sebastian Bergmann.

.........

Time: 01:37, Memory: 8.50Mb

OK (9 tests, 10 assertions)

C:\xampp\htdocs\yiiprac1\protected\tests>

なんか assertions があるな。気になるがおいといて次に進む。

カバレッジ

カバレッジをとる。

C:\xampp\htdocs\yiiprac1\protected\tests>phpunit --coverage-html ./report unit
PHPUnit 3.5.13 by Sebastian Bergmann.

.

Time: 1 second, Memory: 8.75Mb

OK (1 test, 0 assertions)

Generating code coverage report, this may take a moment.

C:\xampp\htdocs\yiiprac1\protected\tests>

すると、./report が作成され、index.html を開くとこんな感じ。
f:id:tjtjtjofthedead:20110324235102p:image

4ステップて。。。まあいい。自分ではまるでテストコード書いてないんだし。そういえば3/22にリリースされたFireFox4.0を使ってます。