Skip to content
Everett Griffiths edited this page Apr 10, 2014 · 7 revisions

Repoman (and Composer) were designed to help you product testable code. The most popular tool for writing PHP unit tests is PHPUnit

Sample Test Class

Save the following code as tests/myTest.php

<?php
/**
 *
 * To run these tests, pass the test directory as the 1st argument to phpunit:
 *
 *   phpunit path/to/tests
 *
 * or if you're having any trouble install phpunit, download its .phar file, and 
 * then run the tests like this:
 *
 *  php phpunit.phar path/to/tests
 *
 * To limit the tests to only one set of tests, you can specify a single file:
 *
 *  phpunit tests/myTest.php
 *
 * All classes you create in this directory 
 */
 
class myTest extends PHPUnit_Framework_TestCase {

    // Must be static because we set it up inside a static function
    public static $modx;
    
    /**
     * Load up MODX for our tests.
     */
    public static function setUpBeforeClass() {        
        $docroot = dirname(dirname(dirname(dirname(__FILE__))));
        while (!file_exists($docroot.'/config.core.php')) {
            if ($docroot == '/') {
                die('Failed to locate config.core.php');
            }
            $docroot = dirname($docroot);
        }
        if (!file_exists($docroot.'/config.core.php')) {
            die('Failed to locate config.core.php');
        }
        
        include_once $docroot . '/config.core.php';
        
        if (!defined('MODX_API_MODE')) {
            define('MODX_API_MODE', false);
        }
        
        include_once MODX_CORE_PATH . 'model/modx/modx.class.php';
         
        self::$modx = new modX();
        self::$modx->initialize('mgr');  
        
        // Include this if you are using autoloading:
        if (file_exists(dirname(dirname(__FILE__)).'/vendor/autoload.php')) {
            include_once dirname(dirname(__FILE__)).'/vendor/autoload.php';
        }
    }


    /**
     * Example Test
     */
    public function testMODX() {
        $this->assertTrue(defined('MODX_CORE_PATH'), 'MODX_CORE_PATH not defined.');
        $this->assertTrue(defined('MODX_ASSETS_PATH'), 'MODX_ASSETS_PATH not defined.');
        $this->assertTrue(is_a(self::$modx, 'modX'), 'Invalid modX instance.');
    }

    // Add more tests here, just begin your function names with "test":
}

Clone this wiki locally