Hello Folks,
in the last time i started to dive in into unit-testing in general with
the aim to certify modules in the nearer future. One of the first
tutorials i used to get familiar with unittesting in general was
http://netbeans.org/kb/docs/php/phpunit.html, which I found was very
cool doing things in netbeans IDE. After setting some globals in
/etc/profile and switching off all components of ZendServer to get
xdebug working, I finally got my first results.
Encouraged by that I tried to set up the environment for oxid-unit
testing. Folowing the Wiki-entry in
http://wiki.oxidforge.org/Certification/Modules I downloaded the debian
etch image (why such an old distro?) and tried to run it first in
virtual box instead of vmware player which did not work at all, because
the VM did not start up, hanging while mounting root file system. Then I
downloaded the VMware player to try the VM in its home environment. At
least the VM startet up, but had no network interface despite all
settings of VM showed me that there is a plugged network interface. So I
decided to set up my own appliance based on ubuntu-server 10.04 which
seemed to work better. I even have used the same Version of phpunit as
given in the tutorial. Instead of the tests and the shop given in the
oxidVM I used 4.4.8 with unit tests that belong to that version. I
installed the shop under /var/www/oxidce4 and the tests under
/var/www/oxunit4.4.8. So far with my prologue...
Following the oxid tutorial I got things to work with the given
examples, testing a single unit as well as running the AllTestsUnit
without changing anything.
I picked one of our modules and within this module I searched a class
extension ( with pattern class foo extends foo_parent ) which contained
a single method with a parameter and a return value. Perferct for
testing I thought.
Here is what I tried:
The base I wanted to write a test for is an extension of the
oxpayment-class and looked like this:
class MyPaymentExtension extends MyPaymentExtension_parent {
public function GetMode($sType = '') {
$blLiveMode = $this->oxpayments__livemode->value;
if($this->getId() == 'oxidcreditcard' && $sType != '') {
$blLivemode = $this->getConfig()->getConfigParam('blPayExt'.$sType.'Live');
} elseif($this->getId() == 'fcpoonlineueberweisung' && $sType != '') {
$blLivemode = $this->getConfig()->getConfigParam('blPayExt'.$sType.'Live');
}
if($blLiveMode === true) {
return 'live';
} else {
return 'test';
}
}
}
This module is included the standard way in modules which in this case is:
oxpayment => ModuleName/core/MyPaymentExtension
Next I started to write the unit-test for this method, which I put
inside the existing test suite in the unit/modules/ModuleName dir. It
looked like this:
require_once realpath( "." ).'/unit/OxidTestCase.php';
require_once realpath( "." ).'/unit/test_config.inc.php';
class MyPaymentExtensionTest extends OxidTestCase {
public function testGetMode() {
$oPayment = oxNew( 'oxpayment' );
$this->assertEquals('test', $oPayment->GetMode());
$this->assertEquals('test', $oPayment->GetMode('blubb'));
$this->assertEquals('test', $oPayment->GetMode('#++@'));
$this->assertEquals('test', $oPayment->GetMode('♔♕♖♗♘♙♚♛♜♝♞♟'));
$this->assertEquals('test', $oPayment->GetMode(2343523));
}
}
Due I only wanted to test my module and wanted to generate a code
coverage report I changed the bootstrap.php file as suggested:
if (getenv('CODECOVERAGE')) {
// PHPUnit_Util_Filter configuration
PHPUnit_Util_Filter::$addUncoveredFilesFromWhitelist = true;
/*
PHPUnit_Util_Filter::addDirectoryToFilter(oxPATH);
*/
PHPUnit_Util_Filter::addDirectoryToWhitelist(oxPATH.'/modules/ModuleName');
}
And finally I changed the AllTestsUnit.php and removed all TestDirs
despite the modules dir
$aTestDirs = array('modules/ModuleName');
I thought everything is fine now, that I could test this, but I ran into
trouble which you can read here:
www-data@andre-zend2:~/oxunit4.4.8$ oxPATH=/var/www/oxidce4/
CODECOVERAGE=1 oxADMIN_PASSWD='heimat' php -d 'memory_limit=1024M' -d
'include_path=.:/home/oxid/PHPUnit-3.4.9'
/home/oxid/PHPUnit-3.4.9/phpunit.php --verbose --bootstrap bootstrap.php
--coverage-html /var/www/oxidce4/reports/ AllTestsUnit.php
db Dumptime: 0.51274800300598
=========
running php version 5.2.17
============
Adding unit tests from unit/modules/ModuleName/*Test.php
Warning: class not found:
unit_modules_MyPaymentExtension_MyPaymentExtensionTest in
unit/modules/ModuleName/MyPaymentExtensionTest.php
PHPUnit 3.4.9 by Sebastian Bergmann.
PHPUnit
Time: 1 second, Memory: 24.00Mb
OK (0 tests, 0 assertions)
Generating code coverage report, this may take a moment.TSOK
Fatal error: Class 'roles_bemain' not found in
/var/www/oxidce4/core/oxutilsobject.php(331) : eval()'d code on line 1
Call Stack:
0.0003 49836 1. {main}() /home/oxid/PHPUnit-3.4.9/phpunit.php:0
0.0381 4138132 2. PHPUnit_TextUI_Command::main()
/home/oxid/PHPUnit-3.4.9/phpunit.php:54
0.0381 4138648 3. PHPUnit_TextUI_Command->run()
/home/oxid/PHPUnit-3.4.9/PHPUnit/TextUI/Command.php:146
0.5989 8049212 4. PHPUnit_TextUI_TestRunner->doRun()
/home/oxid/PHPUnit-3.4.9/PHPUnit/TextUI/Command.php:213
0.5994 8055268 5. PHPUnit_Util_Report::render()
/home/oxid/PHPUnit-3.4.9/PHPUnit/TextUI/TestRunner.php:478
0.5994 8055908 6.
PHPUnit_Framework_TestResult->getCodeCoverageInformation()
/home/oxid/PHPUnit-3.4.9/PHPUnit/Util/Report.php:97
0.5995 8055988 7. PHPUnit_Util_Filter::getFilteredCodeCoverage()
/home/oxid/PHPUnit-3.4.9/PHPUnit/Framework/TestResult.php:623
0.8635 11172876 8.
include_once('/var/www/oxidce4/modules/ModuleName/admin/MyRolesBeMain.php')
/home/oxid/PHPUnit-3.4.9/PHPUnit/Util/Filter.php:381
0.8635 11173172 9. oxAutoload() /var/www/oxidce4/core/oxfunctions.php:0
0.8644 11175076 10. oxUtilsObject->getClassName()
/var/www/oxidce4/core/oxfunctions.php:103
0.8648 11175564 11. oxUtilsObject->_makeSafeModuleClassParents()
/var/www/oxidce4/core/oxutilsobject.php:264
0.8651 11177856 12. eval(''class MyRolesBeMain_parent extends
roles_bemain {}'') /var/www/oxidce4/core/oxutilsobject.php:331
www-data@andre-zend2:~/oxunit4.4.8$
Because there is not much documentation out there which helps getting
started by using a familiar example and my inexperience with Unit-Tests
I am currently not able to help myself getting into deep with this issue.
I really would be glad if there is someone out there who could confirm
or deny my acting to get my first test of an oxid extension work.
Perhaps based on this example together we could work out a tutorial
which will help developers out there writing tests for their modules and
enhance the quality of 3rd Party Modules.
Thanks for reading and perservering
Regards
André
_______________________________________________
dev-general mailing list
[email protected]
http://dir.gmane.org/gmane.comp.php.oxid.general