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:

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

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

public function testSuite() {

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 :)


Wim Pruiksma said...

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

Warning: 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