Location: PHPKode > projects > PHP on Trax > johnpipi-trax-f599562/trax/test/TraxGeneratorTest.php
<?php
/**
 *  File for the TraxGeneratorTest class
 *
 * (PHP 5)
 *
 * @package PHPonTraxTest
 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
 * @copyright (c) Walter O. Haas 2006
 * @version $Id$
 * @author Walt Haas <hide@address.com>
 */

echo "testing TraxGenerator\n";
require_once 'testenv.php';
require_once 'inflector.php';

// Call TraxGeneratorTest::main() if this source file is executed directly.
if (!defined("PHPUnit2_MAIN_METHOD")) {
    define("PHPUnit2_MAIN_METHOD", "TraxGeneratorTest::main");
}

require_once "PHPUnit2/Framework/TestCase.php";
require_once "PHPUnit2/Framework/TestSuite.php";

// You may remove the following line when all tests have been implemented.
require_once "PHPUnit2/Framework/IncompleteTestError.php";

static $tmpdir;            // temporary directory

//  Create a temporary directory to receive generated files
//  @todo <b>FIXME:</b> Is this platform independent?
do {
    $tmpdir = '/tmp/traxtest' . mt_rand(0, 99999999);
} while(!mkdir($tmpdir,0700));
define('TRAX_ROOT', $tmpdir);

define('TRAX_ENV', 'test');

// Set up information that normally comes from database.ini
Trax::$database_settings['test']
            = array('phptype'    => 'mysql',
                    'database'   => 'test_development',
                    'hostspec'   => 'localhost',
                    'username'   => 'root',
                    'password'   => '',
                    'persistent' => true);

//  Create a DB to test with
@ini_set('include_path','./mockDB:'.ini_get('include_path'));
require_once "DB.php";
$db =&  DB::Connect(Trax::$database_settings[TRAX_ENV],
                    array('persistent' => true));
if (PEAR::isError($db) || is_a($db, 'DB_Error')) {
    PHPUnit2_Framework_Assert::fail("Unable to create database");
 }
$db->setFetchMode(DB_FETCHMODE_ASSOC);
Trax::$active_record_connections[TRAX_ENV] =& $db;

require_once "trax_generator.php";
require_once "action_view/helpers.php";
require_once "action_view/helpers/active_record_helper.php";
require_once "active_record.php";
require_once "trax_exceptions.php";

/**
 *  When a class is referenced, get it from app/models
 */
function __autoload($class_name) {
    $file = Inflector::underscore($class_name).".php";
    $file_org = $class_name.".php";

    if(file_exists(Trax::$models_path."/$file")) {
# Include model classes
        include_once(Trax::$models_path."/$file");
    } elseif(file_exists(TRAX_LIB_ROOT."/$file")) {
# Include extra controller classes
        include_once(TRAX_LIB_ROOT."/$file");
    }
}

/**
 * Test class for TraxGenerator.
 * Generated by PHPUnit2_Util_Skeleton on 2006-03-01 at 15:34:17.
 */
class TraxGeneratorTest extends PHPUnit2_Framework_TestCase {


    /**
     * Runs the test methods of this class.
     *
     * @access public
     * @static
     */
    public static function main() {
        require_once "PHPUnit2/TextUI/TestRunner.php";

        $suite  = new PHPUnit2_Framework_TestSuite("TraxGeneratorTest");
        $result = PHPUnit2_TextUI_TestRunner::run($suite);
    }

    /**
     *  Set up dummy Trax work area
     *
     *  Create a dummy Trax work area in the temporary directory
     *  @access protected
     */
    protected function setUp() {

        //  Create empty directories representing the Trax work area
        mkdir(TRAX_ROOT.'/controllers',0700,true);
        mkdir(TRAX_ROOT.'/helpers',0700,true);
        mkdir(TRAX_ROOT.'/models',0700,true);
        mkdir(TRAX_ROOT.'/views/layouts',0700,true);
    }

    /**
     *  Remove the dummy Trax work area
     *
     *  @access protected
     */
    protected function tearDown() {

        //  Empty the temporary directory
        $this->rmrfdir(TRAX_ROOT);
    }

    /**
     *  Remove all the contents of a directory
     *  @param string $dir Name of directory to empty. Ends with '/'
     */
    private function rmrfdir($dir) {

        //  Open the directory
        $dir_h = opendir($dir);

        if (!$dir_h) {
            return;             // 
        }

        //  Delete everything in the directory
        while (false !== ($item = readdir($dir_h))) {
            if (is_dir($dir.$item)) {

                //  $item is a directory.  Recurse if appropriate
                if ( ($item != '.') && ($item != '..') ) {
                    $this->rmrfdir($dir.$item.'/'); // empty the directory
                    rmdir($dir.$item); // remove the empty directory
                }
            } else {            // if (is_dir($item))

                //  $item is not a directory.
                unlink($dir.$item);
            }                   // if (is_dir($item)) ... else
        }                       // while (false !== ($item = readdir($dir_h)))

        //  Close the directory
        closedir($dir_h);
    }


    /**
     *  Verify that generator_help() outputs a "Usage" message
     */
    public function testGenerator_help() {
        $tg = new TraxGenerator;
        ob_start();
        $tg->generator_help();             // should produce a usage
        $output = ob_get_clean();
        $this->assertContains('Usage', $output);
    }

    /**
     *  Verify that scaffold_help() outputs a "Usage" message
     */
    public function testScaffold_help() {
        $tg = new TraxGenerator;
        ob_start();
        $tg->scaffold_help();             // should produce a usage
        $output = ob_get_clean();
        $this->assertContains('Usage', $output);
    }

    /**
     *  Verify that controller_help() outputs a "Usage" message
     */
    public function testController_help() {
        $tg = new TraxGenerator;
        ob_start();
        $tg->controller_help();             // should produce a usage
        $output = ob_get_clean();
        $this->assertContains('Usage', $output);
    }

    /**
     *  Verify that model_help() outputs a "Usage" message
     */
    public function testModel_help() {
        $tg = new TraxGenerator;
        ob_start();
        $tg->model_help();             // should produce a usage
        $output = ob_get_clean();
        $this->assertContains('Usage', $output);
    }

    /**
     *  Verify that generate_controller() generates a controller
     *
     *  We would like to test
     *  {@link TraxGenerator::create_controller() create_controller()},
     *  {@link TraxGenerator::create_helper() create_helper()}
     *  and {@link TraxGenerator::create_view() create_view()} in
     *  isolation before we test 
     *  {@link TraxGenerator::generate_controller() generate_controller()}
     *  but that's impossible because they depend on private variables
     *  which are set only in generate_controller().
     */
    public function testGenerate_controller() {

        //  Generate a mumble controller with no views
        $tg = new TraxGenerator;
        ob_start();
        $tg->generate_controller('mumble');
        $output = ob_get_clean();
        $this->assertContains('create', $output);
        $this->assertTrue(is_file(TRAX_ROOT
                                  . '/controllers/mumble_controller.php'));
        $controller = file_get_contents(TRAX_ROOT
                                        . '/controllers/mumble_controller.php');
        $this->assertContains('class MumbleController extends'
                              . ' ApplicationController', $controller);
        $this->assertTrue(is_file(TRAX_ROOT
                                  . '/helpers/mumble_helper.php'));
        $this->assertTrue(is_dir(TRAX_ROOT . '/views/mumble'));

        //  Verify that a second attempt to create the same controller
        //  reports that it exists
        $tg = new TraxGenerator;
        ob_start();
        $tg->generate_controller('mumble');
        $output = ob_get_clean();
        $this->assertContains('exists', $output);
        $this->assertTrue(file_exists(TRAX_ROOT
                                      . '/controllers/mumble_controller.php'));
        
        //  Generate a controller with one view file
        $tg = new TraxGenerator;
        ob_start();
        $tg->generate_controller('store', 'manager');
        $output = ob_get_clean();
        $this->assertContains('create', $output);
        $this->assertTrue(is_file(TRAX_ROOT
                                  . '/controllers/store_controller.php'));
        $controller = file_get_contents(TRAX_ROOT
                                        . '/controllers/store_controller.php');
        $this->assertContains('class StoreController extends'
                              . ' ApplicationController', $controller);
        $this->assertTrue(is_file(TRAX_ROOT . '/helpers/store_helper.php'));
        $this->assertTrue(is_dir(TRAX_ROOT . '/views/store'));
        $this->assertTrue(is_file(TRAX_ROOT . '/views/store/manager.phtml'));
        $view = file_get_contents(TRAX_ROOT . '/views/store/manager.phtml');
        $this->assertContains('Store->manager',$view);
        $this->assertContains('store/manager.phtml',$view);
        
        //  Generate a controller with two view files
        $tg = new TraxGenerator;
        ob_start();
        $tg->generate_controller('account', array('customer','teller'));
        $output = ob_get_clean();
        $this->assertContains('create', $output);
        $this->assertTrue(is_file(TRAX_ROOT
                                  . '/controllers/account_controller.php'));
        $controller = file_get_contents(TRAX_ROOT
                                   . '/controllers/account_controller.php');
        $this->assertContains('class AccountController extends'
                              . ' ApplicationController', $controller);
        $this->assertTrue(is_file(TRAX_ROOT
                                  . '/helpers/account_helper.php'));
        $this->assertTrue(is_dir(TRAX_ROOT . '/views/account'));
        $this->assertTrue(is_file(TRAX_ROOT
                                  . '/views/account/customer.phtml'));
        $view = file_get_contents(TRAX_ROOT . '/views/account/customer.phtml');
        $this->assertContains('Account->customer',$view);
        $this->assertContains('account/customer.phtml',$view);
        $this->assertTrue(is_file(TRAX_ROOT
                                  . '/views/account/teller.phtml'));
        $view = file_get_contents(TRAX_ROOT . '/views/account/teller.phtml');
        $this->assertContains('Account->teller',$view);
        $this->assertContains('account/teller.phtml',$view);

        //  Generate a controller in a subdirectory
        $tg = new TraxGenerator;
        ob_start();
        $tg->generate_controller('forum/admin');
        $output = ob_get_clean();
        $this->assertContains('create', $output);
        $this->assertTrue(is_file(TRAX_ROOT
                                . '/controllers/forum/admin_controller.php'));
        $controller = file_get_contents(TRAX_ROOT
                                 . '/controllers/forum/admin_controller.php');
        $this->assertContains('class AdminController extends'
                              . ' ApplicationController', $controller);
        $this->assertTrue(is_file(TRAX_ROOT
                                  . '/helpers/forum/admin_helper.php'));
        $this->assertTrue(is_dir(TRAX_ROOT . '/views/forum/admin'));
    }

    /**
     *  Verify that generate_model() generates a model
     */
    public function testGenerate_model() {

        //  Generate a CreditCard model
        $tg = new TraxGenerator;
        ob_start();
        $tg->generate_model('CreditCard');
        $output = ob_get_clean();
        $this->assertContains('create', $output);
        $this->assertTrue(is_file(TRAX_ROOT . '/models/credit_card.php'));
        $model = file_get_contents(TRAX_ROOT . '/models/credit_card.php');
        $this->assertContains('class CreditCard extends ActiveRecord',
                              $model);

        //  Generate a soap_opera model
        $tg = new TraxGenerator;
        ob_start();
        $tg->generate_model('soap_opera');
        $output = ob_get_clean();
        $this->assertContains('create', $output);
        $this->assertTrue(is_file(TRAX_ROOT . '/models/soap_opera.php'));
        $model = file_get_contents(TRAX_ROOT . '/models/soap_opera.php');
        $this->assertContains('class SoapOpera extends ActiveRecord',
                              $model);
    }

    /**
     *  Verify that generate_scaffold() generates a model and controller
     */
    public function testGenerate_scaffold1() {

        //  Generate a PersonName/membership scaffold
        $tg = new TraxGenerator;
        ob_start();
        $tg->generate_scaffold('PersonName', 'membership');

        $output = ob_get_clean();
        $this->assertContains('create', $output);
        $this->assertNotContains('Error:', $output);
        $this->assertNotContains('Notice:', $output);

        //  verify controller
        $this->assertTrue(is_file(TRAX_ROOT
                                  . '/controllers/membership_controller.php'));
        $controller = file_get_contents(TRAX_ROOT
                                  . '/controllers/membership_controller.php');
        $this->assertContains('class MembershipController extends'
                              . ' ApplicationController', $controller);
        $this->assertNotContains('Error:', $controller);
        $this->assertNotContains('Notice:', $controller);

        //  verify model
        $this->assertTrue(is_file(TRAX_ROOT
                                  . '/models/person_name.php'));
        $model = file_get_contents(TRAX_ROOT
                                  . '/models/person_name.php');
        $this->assertContains('class PersonName extends'
                              . ' ActiveRecord', $model);
        $this->assertNotContains('Error:', $model);
        $this->assertNotContains('Notice:', $model);

        //  verify helper
        $this->assertTrue(is_file(TRAX_ROOT
                                  . '/helpers/membership_helper.php'));
        $helper = file_get_contents(TRAX_ROOT
                                  . '/helpers/membership_helper.php');
        $this->assertContains('MembershipController', $helper);
        $this->assertNotContains('Error:', $helper);
        $this->assertNotContains('Notice:', $helper);

        //  verify views
        $this->assertTrue(is_dir(TRAX_ROOT . '/views/membership'));

        $this->assertTrue(is_file(TRAX_ROOT . '/views/membership/add.phtml'));
        $add = file_get_contents(TRAX_ROOT
                                  . '/views/membership/add.phtml');
        $this->assertContains('New PersonName', $add);
        $this->assertNotContains('Error:', $add);
        $this->assertNotContains('Notice:', $add);

        $this->assertTrue(is_file(TRAX_ROOT . '/views/membership/edit.phtml'));
        $edit = file_get_contents(TRAX_ROOT
                                  . '/views/membership/edit.phtml');
        $this->assertContains('Editing PersonName', $edit);
        $this->assertNotContains('Error:', $edit);
        $this->assertNotContains('Notice:', $edit);


        $this->assertTrue(is_file(TRAX_ROOT . '/views/membership/_form.phtml'));
        $_form = file_get_contents(TRAX_ROOT
                                  . '/views/membership/_form.phtml');
        $this->assertContains('PersonName', $_form);
        $this->assertNotContains('Error:', $_form);
        $this->assertNotContains('Notice:', $_form);

        $this->assertTrue(is_file(TRAX_ROOT . '/views/membership/index.phtml'));
        $index = file_get_contents(TRAX_ROOT
                                  . '/views/membership/index.phtml');
        $this->assertContains('Listing PersonNames', $index);
        $this->assertNotContains('Error:', $index);
        $this->assertNotContains('Notice:', $index);

        $this->assertTrue(is_file(TRAX_ROOT . '/views/membership/show.phtml'));
        $show = file_get_contents(TRAX_ROOT
                                  . '/views/membership/show.phtml');
        $this->assertContains('PersonName', $show);
        $this->assertNotContains('Error:', $show);
        $this->assertNotContains('Notice:', $show);

        //  verify layout
        $this->assertTrue(is_file(TRAX_ROOT
                                  . '/views/layouts/membership.phtml'));
        $layout = file_get_contents(TRAX_ROOT
                                  . '/views/layouts/membership.phtml');
        $this->assertContains('membership', $layout);
    }

    /**
     *  Verify that generate_scaffold() defaults controller from model
     */
    public function testGenerate_scaffold2() {

        //  Generate a PersonName/person_name scaffold
        $tg = new TraxGenerator;
        ob_start();
        $tg->generate_scaffold('PersonName',null);
        $output = ob_get_clean();
        $this->assertContains('create', $output);
        $this->assertNotContains('Error:', $output);
        $this->assertNotContains('Notice:', $output);

        //  verify controller
        $this->assertTrue(is_file(TRAX_ROOT
                                  . '/controllers/person_name_controller.php'));
        $controller = file_get_contents(TRAX_ROOT
                                  . '/controllers/person_name_controller.php');
        $this->assertContains('class PersonNameController extends'
                              . ' ApplicationController', $controller);
        $this->assertNotContains('Error:', $controller);
        $this->assertNotContains('Notice:', $controller);

        //  verify model
        $this->assertTrue(is_file(TRAX_ROOT
                                  . '/models/person_name.php'));
        $model = file_get_contents(TRAX_ROOT
                                  . '/models/person_name.php');
        $this->assertContains('class PersonName extends'
                              . ' ActiveRecord', $model);
        $this->assertNotContains('Error:', $model);
        $this->assertNotContains('Notice:', $model);

        //  verify helper
        $this->assertTrue(is_file(TRAX_ROOT
                                  . '/helpers/person_name_helper.php'));
        $helper = file_get_contents(TRAX_ROOT
                                  . '/helpers/person_name_helper.php');
        $this->assertContains('PersonNameController', $helper);
        $this->assertNotContains('Error:', $helper);
        $this->assertNotContains('Notice:', $helper);

        //  verify views
        $this->assertTrue(is_dir(TRAX_ROOT . '/views/person_name'));

        $this->assertTrue(is_file(TRAX_ROOT . '/views/person_name/add.phtml'));
        $add = file_get_contents(TRAX_ROOT
                                  . '/views/person_name/add.phtml');
        $this->assertContains('New PersonName', $add);
        $this->assertNotContains('Error:', $add);
        $this->assertNotContains('Notice:', $add);

        $this->assertTrue(is_file(TRAX_ROOT . '/views/person_name/edit.phtml'));
        $edit = file_get_contents(TRAX_ROOT
                                  . '/views/person_name/edit.phtml');
        $this->assertContains('Editing PersonName', $edit);
        $this->assertNotContains('Error:', $edit);
        $this->assertNotContains('Notice:', $edit);


        $this->assertTrue(is_file(TRAX_ROOT
                                  . '/views/person_name/_form.phtml'));
        $_form = file_get_contents(TRAX_ROOT
                                  . '/views/person_name/_form.phtml');
        $this->assertContains('PersonName', $_form);
        $this->assertNotContains('Error:', $_form);
        $this->assertNotContains('Notice:', $_form);

        $this->assertTrue(is_file(TRAX_ROOT
                                  . '/views/person_name/index.phtml'));
        $index = file_get_contents(TRAX_ROOT
                                  . '/views/person_name/index.phtml');
        $this->assertContains('Listing PersonNames', $index);
        $this->assertNotContains('Error:', $index);
        $this->assertNotContains('Notice:', $index);

        $this->assertTrue(is_file(TRAX_ROOT . '/views/person_name/show.phtml'));
        $show = file_get_contents(TRAX_ROOT
                                  . '/views/person_name/show.phtml');
        $this->assertContains('PersonName', $show);
        $this->assertNotContains('Error:', $show);
        $this->assertNotContains('Notice:', $show);

        //  verify layout
        $this->assertTrue(is_file(TRAX_ROOT
                                  . '/views/layouts/person_name.phtml'));
        $layout = file_get_contents(TRAX_ROOT
                                  . '/views/layouts/person_name.phtml');
        $this->assertContains('person_name', $layout);
    }

    /**
     *  Verify generate_scaffold() with additional views
     */
    public function testGenerate_scaffold3() {

        //  Generate a PersonName/membership scaffold
        $tg = new TraxGenerator;
        ob_start();
        $tg->generate_scaffold('PersonName', 'membership',
                               array('join', 'renew'));
        $output = ob_get_clean();
        $this->assertContains('create', $output);
        $this->assertNotContains('Error:', $output);
        $this->assertNotContains('Notice:', $output);

        //  verify controller
        $this->assertTrue(is_file(TRAX_ROOT
                                  . '/controllers/membership_controller.php'));
        $controller = file_get_contents(TRAX_ROOT
                                  . '/controllers/membership_controller.php');
        $this->assertContains('class MembershipController extends'
                              . ' ApplicationController', $controller);
        $this->assertNotContains('Error:', $controller);
        $this->assertNotContains('Notice:', $controller);

        //  verify model
        $this->assertTrue(is_file(TRAX_ROOT
                                  . '/models/person_name.php'));
        $model = file_get_contents(TRAX_ROOT
                                  . '/models/person_name.php');
        $this->assertContains('class PersonName extends'
                              . ' ActiveRecord', $model);
        $this->assertNotContains('Error:', $model);
        $this->assertNotContains('Notice:', $model);

        //  verify helper
        $this->assertTrue(is_file(TRAX_ROOT
                                  . '/helpers/membership_helper.php'));
        $helper = file_get_contents(TRAX_ROOT
                                  . '/helpers/membership_helper.php');
        $this->assertContains('MembershipController', $helper);
        $this->assertNotContains('Error:', $helper);
        $this->assertNotContains('Notice:', $helper);

        //  verify views
        $this->assertTrue(is_dir(TRAX_ROOT . '/views/membership'));

        $this->assertTrue(is_file(TRAX_ROOT . '/views/membership/add.phtml'));
        $add = file_get_contents(TRAX_ROOT
                                  . '/views/membership/add.phtml');
        $this->assertContains('New PersonName', $add);
        $this->assertNotContains('Error:', $add);
        $this->assertNotContains('Notice:', $add);

        $this->assertTrue(is_file(TRAX_ROOT . '/views/membership/edit.phtml'));
        $edit = file_get_contents(TRAX_ROOT
                                  . '/views/membership/edit.phtml');
        $this->assertContains('Editing PersonName', $edit);
        $this->assertNotContains('Error:', $edit);
        $this->assertNotContains('Notice:', $edit);


        $this->assertTrue(is_file(TRAX_ROOT . '/views/membership/_form.phtml'));
        $_form = file_get_contents(TRAX_ROOT
                                  . '/views/membership/_form.phtml');
        $this->assertContains('PersonName', $_form);
        $this->assertNotContains('Error:', $_form);
        $this->assertNotContains('Notice:', $_form);

        $this->assertTrue(is_file(TRAX_ROOT . '/views/membership/index.phtml'));
        $index = file_get_contents(TRAX_ROOT
                                  . '/views/membership/index.phtml');
        $this->assertContains('Listing PersonNames', $index);
        $this->assertNotContains('Error:', $index);
        $this->assertNotContains('Notice:', $index);

        $this->assertTrue(is_file(TRAX_ROOT . 'views/membership/show.phtml'));
        $show = file_get_contents(TRAX_ROOT
                                  . '/views/membership/show.phtml');
        $this->assertContains('PersonName', $show);
        $this->assertNotContains('Error:', $show);
        $this->assertNotContains('Notice:', $show);

        $this->assertTrue(is_file(TRAX_ROOT . '/views/membership/join.phtml'));
        $join = file_get_contents(TRAX_ROOT
                                  . 'views/membership/join.phtml');
        $this->assertContains('Membership->join', $join);
        $this->assertNotContains('Error:', $join);
        $this->assertNotContains('Notice:', $join);

        $this->assertTrue(is_file(TRAX_ROOT . '/views/membership/renew.phtml'));
        $renew = file_get_contents(TRAX_ROOT
                                  . '/views/membership/renew.phtml');
        $this->assertContains('Membership->renew', $renew);
        $this->assertNotContains('Error:', $renew);
        $this->assertNotContains('Notice:', $renew);

        //  verify layout
        $this->assertTrue(is_file(TRAX_ROOT
                                  . '/views/layouts/membership.phtml'));
        $layout = file_get_contents(TRAX_ROOT
                                  . '/views/layouts/membership.phtml');
        $this->assertContains('membership', $layout);
    }

    /**
     *  Verify generate_scaffold() with the controller in a subdirectory
     */
    public function testGenerate_scaffold4() {

        //  Generate a PersonName/membership scaffold
        $tg = new TraxGenerator;
        ob_start();
        $tg->generate_scaffold('PersonName', 'admin/membership');
        $output = ob_get_clean();
        $this->assertContains('create', $output);
        $this->assertNotContains('Error:', $output);
        $this->assertNotContains('Notice:', $output);

        //  verify controller
        $this->assertTrue(is_file(TRAX_ROOT
                            . '/controllers/admin/membership_controller.php'));
        $controller = file_get_contents(TRAX_ROOT
                            . '/controllers/admin/membership_controller.php');
        $this->assertContains('class MembershipController extends'
                              . ' ApplicationController', $controller);
        $this->assertNotContains('Error:', $controller);
        $this->assertNotContains('Notice:', $controller);

        //  verify model
        $this->assertTrue(is_file(TRAX_ROOT
                                  . '/models/person_name.php'));
        $model = file_get_contents(TRAX_ROOT
                                  . '/models/person_name.php');
        $this->assertContains('class PersonName extends'
                              . ' ActiveRecord', $model);
        $this->assertNotContains('Error:', $model);
        $this->assertNotContains('Notice:', $model);

        //  verify helper
        $this->assertTrue(is_file(TRAX_ROOT
                                  . '/helpers/admin/membership_helper.php'));
        $helper = file_get_contents(TRAX_ROOT
                                  . '/helpers/admin/membership_helper.php');
        $this->assertContains('MembershipController', $helper);
        $this->assertNotContains('Error:', $helper);
        $this->assertNotContains('Notice:', $helper);

        //  verify views
        $this->assertTrue(is_dir(TRAX_ROOT . '/views/admin/membership'));

        $this->assertTrue(is_file(TRAX_ROOT
                                  . '/views/admin/membership/add.phtml'));
        $add = file_get_contents(TRAX_ROOT
                                  . '/views/admin/membership/add.phtml');
        $this->assertContains('New PersonName', $add);
        $this->assertNotContains('Error:', $add);
        $this->assertNotContains('Notice:', $add);

        $this->assertTrue(is_file(TRAX_ROOT
                                  . '/views/admin/membership/edit.phtml'));
        $edit = file_get_contents(TRAX_ROOT
                                  . '/views/admin/membership/edit.phtml');
        $this->assertContains('Editing PersonName', $edit);
        $this->assertNotContains('Error:', $edit);
        $this->assertNotContains('Notice:', $edit);


        $this->assertTrue(is_file(TRAX_ROOT
                                  . '/views/admin/membership/_form.phtml'));
        $_form = file_get_contents(TRAX_ROOT
                                  . '/views/admin/membership/_form.phtml');
        $this->assertContains('PersonName', $_form);
        $this->assertNotContains('Error:', $_form);
        $this->assertNotContains('Notice:', $_form);

        $this->assertTrue(is_file(TRAX_ROOT
                                  . '/views/admin/membership/index.phtml'));
        $index = file_get_contents(TRAX_ROOT
                                  . '/views/admin/membership/index.phtml');
        $this->assertContains('Listing PersonNames', $index);
        $this->assertNotContains('Error:', $index);
        $this->assertNotContains('Notice:', $index);

        $this->assertTrue(is_file(TRAX_ROOT
                                  . '/views/admin/membership/show.phtml'));
        $show = file_get_contents(TRAX_ROOT
                                  . '/views/admin/membership/show.phtml');
        $this->assertContains('PersonName', $show);
        $this->assertNotContains('Error:', $show);
        $this->assertNotContains('Notice:', $show);

        //  verify layout
        $this->assertTrue(is_file(TRAX_ROOT
                                  . '/views/layouts/admin/membership.phtml'));
        $layout = file_get_contents(TRAX_ROOT
                                  . '/views/layouts/admin/membership.phtml');
        $this->assertContains('membership', $layout);
    }

    /**
     *   Test fix_php_brackets()
     */
    public function testFix_php_brackets() {
        $tg = new TraxGenerator;
        $this->assertEquals($tg->fix_php_brackets('< ?php foo ? >'),
                            '<?php foo ?>');
    }

    /**
     * @todo Implement testRun().
     */
    public function testRun() {

        //  Verify that no command produces usage message
        $tg = new TraxGenerator;
        ob_start();
        $tg->run();             // should produce a usage
        $result = ob_get_clean();
        $this->assertContains('Usage', $result);

        //  Verify that unknown command produces usage message
        $_SERVER['argv'][1] = 'foo';
        $tg = new TraxGenerator;
        ob_start();
        $tg->run();             // should produce a usage
        $result = ob_get_clean();
        $this->assertContains('Usage', $result);

        //  Generate a CreditCard model
        $_SERVER['argv'][1] = 'model';
        $_SERVER['argv'][2] = 'CreditCard';
        $tg = new TraxGenerator;
        ob_start();
        $tg->run();
        $output = ob_get_clean();
        $this->assertContains('create', $output);
        $this->assertTrue(is_file(TRAX_ROOT . '/models/credit_card.php'));

        //  Generate a mumble controller with no views
        $_SERVER['argv'][1] = 'controller';
        $_SERVER['argv'][2] = 'mumble';
        $tg = new TraxGenerator;
        ob_start();
        $tg->run();
        $output = ob_get_clean();
        $this->assertContains('create', $output);
        $this->assertTrue(is_file(TRAX_ROOT
                                  . '/controllers/mumble_controller.php'));
        $this->assertTrue(is_file(TRAX_ROOT
                                  . '/helpers/mumble_helper.php'));
        $this->assertTrue(is_dir(TRAX_ROOT . '/views/mumble'));


        //  Generate a PersonName/membership scaffold
        $_SERVER['argv'][1] = 'scaffold';
        $_SERVER['argv'][2] = 'PersonName';
        $_SERVER['argv'][3] = 'membership';
        $tg = new TraxGenerator;
        ob_start();
        $tg->run();
        $output = ob_get_clean();
        $this->assertContains('create', $output);
        $this->assertNotContains('Error:', $output);
        $this->assertNotContains('Notice:', $output);

        //  verify controller
        $this->assertTrue(is_file(TRAX_ROOT
                                  . '/controllers/membership_controller.php'));

        //  verify model
        $this->assertTrue(is_file(TRAX_ROOT
                                  . '/models/person_name.php'));

        //  verify helper
        $this->assertTrue(is_file(TRAX_ROOT
                                  . '/helpers/membership_helper.php'));

        //  verify views
        $this->assertTrue(is_dir(TRAX_ROOT  . '/views/membership'));
        $this->assertTrue(is_file(TRAX_ROOT . '/views/membership/add.phtml'));
        $this->assertTrue(is_file(TRAX_ROOT . '/views/membership/edit.phtml'));
        $this->assertTrue(is_file(TRAX_ROOT . '/views/membership/_form.phtml'));
        $this->assertTrue(is_file(TRAX_ROOT . '/views/membership/index.phtml'));
        $this->assertTrue(is_file(TRAX_ROOT . '/views/membership/show.phtml'));

        //  verify layout
        $this->assertTrue(is_file(TRAX_ROOT
                                  . '/views/layouts/membership.phtml'));
    }
}

// Call TraxGeneratorTest::main() if this source file is executed directly.
if (PHPUnit2_MAIN_METHOD == "TraxGeneratorTest::main") {
    TraxGeneratorTest::main();
}

//  Clean up temp directory when done
rmdir(TRAX_ROOT);

// -- set Emacs parameters --
// Local variables:
// tab-width: 4
// c-basic-offset: 4
// c-hanging-comment-ender-p: nil
// indent-tabs-mode: nil
// End:
?>
Return current item: PHP on Trax