After find some way to test Controller, this may be guideline for one who want to test their Yii application and want to see Code Coverage information.
Know before
-
Sorry for my bad English
-
if you want to make code coverage report in your unit test, it is not a guideline for that, you can edit in phpunit.xml to generate the report
[color="#0000FF"]In general, you may follow below instructions[/color]
http://www.phpunit.de/manual/current/en/selenium.html
My Developer Tools/Environment
-
Yii Framework 1.1.4
-
Netbeans IDE 6.9.1 with Selenium Plugin
-
Apache 2
-
PHP 5.3
For me, I use virtual host with Apache2, I have edit some code to make it available for all my virtual hosts.
[color="#0000FF"]Now, Let’s start[/color]
File: %[color="#FF0000"]PEARDIRECTORY[/color]%/PHPUnit/Extensions/SeleniumTestCase/prepend.php
// $GLOBALS['PHPUNIT_COVERAGE_DATA_DIRECTORY'] = FALSE;
$GLOBALS['PHPUNIT_COVERAGE_DATA_DIRECTORY'] = $_SERVER["DOCUMENT_ROOT"] ;
File: %[color="#FF0000"]PEARDIRECTORY[/color]%/PHPUnit/Extensions/SeleniumTestCase/phpunit_coverage.php
//$GLOBALS['PHPUNIT_COVERAGE_DATA_DIRECTORY'] = getcwd();
$GLOBALS['PHPUNIT_COVERAGE_DATA_DIRECTORY'] = $_SERVER["DOCUMENT_ROOT"] ;
Now, edit PHP.ini in the following line
; Automatically add files before PHP document.
auto_prepend_file = "D:\Service\var\scripts\PHPUnit\Extensions\SeleniumTestCase\prepend.php"
; Automatically add files after PHP document.
auto_append_file = "D:\Service\var\scripts\PHPUnit\Extensions\SeleniumTestCase\append.php"
Make that directory can access in Apache2
File: httpd.conf (Apache Config)
<Directory "D:/Service/var/scripts">
Options FollowSymLinks Indexes
Allow from all
Order allow,deny
AllowOverride All
</Directory>
[color="#0000FF"]Make it available for all virtual hosts.[/color]
Enable your mod_alias in httpd.conf
# LoadModule alias_module modules/mod_alias.so
LoadModule alias_module modules/mod_alias.so
and add more line in httpd.conf
<IfModule alias_module>
# ScriptAlias /cgi-bin/ "E:/Service/Process/Apache2.2/cgi-bin/"
Alias /selenium-phpunit/ "D:/Service/var/scripts/PHPUnit/Extensions/SeleniumTestCase/"
</IfModule>
@@IMPORTANT STEP@@ - Restart your apache service
next, edit /%YiiFramework%/protected/test/WebTestCase.php
// Add this to class
protected $coverageScriptUrl = 'http://your.localhost./selenium-phpunit/phpunit_coverage.php';
Make sure that you config PHPUnit to generate code coverage report in /%YiiFramework%/protected/test/phpunit.xml
below is my example code
<logging>
<log type="coverage-html" target="%YiiFramework%\protected\tests\coverage" charset="UTF-8"
yui="true" highlight="true" lowUpperBound="35" highLowerBound="70" />
</logging>
Done.
More Resources
- Netbean + PHPUnit http://netbeans.org/kb/docs/php/phpunit.html