Wednesday, November 10, 2010

Modx 2 [revo] with PHPUnit

Test driven development is quite important in developing useful code.
As we test each single functionality, we might find some bug which may exists in the framework we are using.

I'm going to show how phpunit could be use with modx for testing.
First, install phpunit through pear module, which the reference can be found here:

http://www.phpunit.de/manual/current/en/installation.html

Once installed, you can now starting coding by putting up these codes above your test class:
----------------------
define('MODX_API_MODE', true);
//define("MODX_HTTP_HOST", ""); //this is optional, set it to the url of your context
global $modx;
require(dirname(dirname(__FILE__)) . "/index.php"); //path to modx root/index.php
$modx->getRequest();
----------------------

Now, we are ready to create the test unit.
Example:
--------------------------
class TestDB extends PHPUnit_Framework_TestCase {

public function testSuite() {
$this->doTestDatabase();
}

public function doTestDatabase() {
global $modx;
$user = $modx->getObject("modUser", 1);
$this->assertEquals(1, $user->get("id"));
}
}
--------------------------
Running the test:
phpunit TestDB.php

Hope it helps ya :)
Cheers~

2 comments:

Wim Pruiksma said...

Hello,
Great post and very helpfull but i get the following error.

Warning: session_name(): session.name cannot be a numeric or empty ''

How can i fix this?

James Tt said...

i havent test this on latest modx,
sorry i havent touch modx for sometime