Open Source Content Management System

UnitTesting

  1. Writing unittests for Midgard-PHP
  2. Midgard setup.
      1. Writing Midgard tests.
    1. There is more to it than this

Writing unittests for Midgard-PHP

If you find a bug or an error in the Midgard API please write a unittest for it.

To write a simple test you download and install the simpletest suite of unittesting tools. You can get it here: (https://sourceforge.net/project/showfiles.php?group_id=76550&release_id=153280)Simpletest download

For those who haven't heard about simpletest, you can read more about the API here: (http://www.lastcraft.com/simple_test.php)The Simpletest documentation

Midgard setup.

You should probably not run the tests on live server. Some of the tests try to create and delete articles - but it should be possible to comment them out.

In the test directory there is a file named config.php that contains the username and password for the midgard user. You'll need to change those to something that matches your setup.

Also you'll need to set up a midgard.php file. Heres a sample content: Database Type=Mysql Host=localhost Name=midgard Username=midgard Password=midgard Encoding=utf-8 Blobdir=/var/lib/midgard/blobs/midgard

Writing Midgard tests.

First download latest cvs of midgard and enter the midgard/src/apis/php4/test/querybuilder directory.

From here you can try to run the current tests: php all.php

To write a test. Start by using one of the files that are allready in that directory. As an example, try to write a new test for the querybuilders add_constraint() method. Here's an example for a test to se if the bigger than constraint works:

function test_biggerthan() {
    $qb = new MidgardQueryBrowser('midgard_topic');
    $qb->add_constraint('up', '>', 0);
    $result = $this->qb->execute();
    $this->assertTrue(is_array($result));
    foreach ($result as $key => $obj) {
        $this->assertTrue($obj->up > 0);
    }
 }

The conventions are very easy to follow:

  1. Start each functionname with "test".
  2. To check if you got the result you wanted, use assertTrue().

For more advanced options, take a look at the simpletest webpage.

There is more to it than this

Yes, there is a little more - but not much. The function has to be in a testclass. That is a class that inherits from the UnitTestCase class.

A complete example would be this:

 /* this is set up to run in the test/querybuilder directory! You may have to fix the paths. */
  ini_set('include_path','..:'.ini_get('include_path'));
/* load the midgard cli configuration data and start the database connection 
 * Also load the simpletest files.
 */
 require_once 'lib/midgard_setup.php';

 class my_first_test extends UnitTestCase {
     var $qb;
   function test_biggerthan() {
     $this->qb->add_constraint('up', '>', 0);
     $result = $this->qb->execute();
     $this->assertTrue(is_array($result));
     foreach ($result as $key => $obj) {
         $this->assertTrue($obj->up > 0);
     }
   }
  /* use the setUp() funtion to add something you will do before every test */
   function setUp() {
      $this->qb = new MidgardQueryBuilder("midgard_topic");
   }
   /** the tearDown() function is run after each test */
   function tearDown() {}

}
// by testing for realpath, the file can also be included in larger testsets.
if (realpath($_SERVER'PHP_SELF') == __FILE__)
{
    $test = new my_first_test();
    $test->run (new TextReporter());
}

This file should now be able to run on the commandline.

Designed by Nemein, hosted by Anykey