Source for file TraxGeneratorTest.php
Documentation is available at TraxGeneratorTest.php
* File for the TraxGeneratorTest class
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
* @copyright (c) Walter O. Haas 2006
* @version $Id: TraxGeneratorTest.php 208 2006-05-28 17:59:55Z john $
* @author Walt Haas <haas@xmission.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?
$tmpdir = '/tmp/traxtest' . mt_rand(0, 99999999);
} while(!mkdir($tmpdir,0700));
// Set up information that normally comes from database.ini
Trax::$database_settings['test']
= array('phptype' => 'mysql',
'database' => 'test_development',
'hostspec' => 'localhost',
// Create a DB to test with
$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");
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
$file_org = $class_name. ".php";
include_once(Trax::$models_path. "/$file");
# Include extra controller classes
* Test class for TraxGenerator.
* Generated by PHPUnit2_Util_Skeleton on 2006-03-01 at 15:34:17.
* Runs the test methods of this class.
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
protected function setUp() {
// Create empty directories representing the Trax work area
* Remove the dummy Trax work area
// Empty the temporary directory
* Remove all the contents of a directory
* @param string $dir Name of directory to empty. Ends with '/'
// Delete everything in the directory
while (false !== ($item = readdir($dir_h))) {
// $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.
} // if (is_dir($item)) ... else
} // while (false !== ($item = readdir($dir_h)))
* Verify that generator_help() outputs a "Usage" message
$tg->generator_help(); // should produce a usage
$this->assertContains('Usage', $output);
* Verify that scaffold_help() outputs a "Usage" message
$tg->scaffold_help(); // should produce a usage
$this->assertContains('Usage', $output);
* Verify that controller_help() outputs a "Usage" message
$tg->controller_help(); // should produce a usage
$this->assertContains('Usage', $output);
* Verify that model_help() outputs a "Usage" message
$tg->model_help(); // should produce a usage
$this->assertContains('Usage', $output);
* Verify that generate_controller() generates a controller
* {@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().
// Generate a mumble controller with no views
$tg->generate_controller('mumble');
$this->assertContains('create', $output);
. '/controllers/mumble_controller.php'));
. '/controllers/mumble_controller.php');
$this->assertContains('class MumbleController extends'
. ' ApplicationController', $controller);
. '/helpers/mumble_helper.php'));
// Verify that a second attempt to create the same controller
// reports that it exists
$tg->generate_controller('mumble');
$this->assertContains('exists', $output);
. '/controllers/mumble_controller.php'));
// Generate a controller with one view file
$tg->generate_controller('store', 'manager');
$this->assertContains('create', $output);
. '/controllers/store_controller.php'));
. '/controllers/store_controller.php');
$this->assertContains('class StoreController extends'
. ' ApplicationController', $controller);
$this->assertContains('Store->manager',$view);
$this->assertContains('store/manager.phtml',$view);
// Generate a controller with two view files
$tg->generate_controller('account', array('customer','teller'));
$this->assertContains('create', $output);
. '/controllers/account_controller.php'));
. '/controllers/account_controller.php');
$this->assertContains('class AccountController extends'
. ' ApplicationController', $controller);
. '/helpers/account_helper.php'));
. '/views/account/customer.phtml'));
$this->assertContains('Account->customer',$view);
$this->assertContains('account/customer.phtml',$view);
. '/views/account/teller.phtml'));
$this->assertContains('Account->teller',$view);
$this->assertContains('account/teller.phtml',$view);
// Generate a controller in a subdirectory
$tg->generate_controller('forum/admin');
$this->assertContains('create', $output);
. '/controllers/forum/admin_controller.php'));
. '/controllers/forum/admin_controller.php');
$this->assertContains('class AdminController extends'
. ' ApplicationController', $controller);
. '/helpers/forum/admin_helper.php'));
* Verify that generate_model() generates a model
// Generate a CreditCard model
$tg->generate_model('CreditCard');
$this->assertContains('create', $output);
$this->assertContains('class CreditCard extends ActiveRecord',
// Generate a soap_opera model
$tg->generate_model('soap_opera');
$this->assertContains('create', $output);
$this->assertContains('class SoapOpera extends ActiveRecord',
* Verify that generate_scaffold() generates a model and controller
// Generate a PersonName/membership scaffold
$tg->generate_scaffold('PersonName', 'membership');
$this->assertContains('create', $output);
$this->assertNotContains('Error:', $output);
$this->assertNotContains('Notice:', $output);
. '/controllers/membership_controller.php'));
. '/controllers/membership_controller.php');
$this->assertContains('class MembershipController extends'
. ' ApplicationController', $controller);
$this->assertNotContains('Error:', $controller);
$this->assertNotContains('Notice:', $controller);
. '/models/person_name.php'));
. '/models/person_name.php');
$this->assertContains('class PersonName extends'
. ' ActiveRecord', $model);
$this->assertNotContains('Error:', $model);
$this->assertNotContains('Notice:', $model);
. '/helpers/membership_helper.php'));
. '/helpers/membership_helper.php');
$this->assertContains('MembershipController', $helper);
$this->assertNotContains('Error:', $helper);
$this->assertNotContains('Notice:', $helper);
. '/views/membership/add.phtml');
$this->assertContains('New PersonName', $add);
$this->assertNotContains('Error:', $add);
$this->assertNotContains('Notice:', $add);
. '/views/membership/edit.phtml');
$this->assertContains('Editing PersonName', $edit);
$this->assertNotContains('Error:', $edit);
$this->assertNotContains('Notice:', $edit);
. '/views/membership/_form.phtml');
$this->assertContains('PersonName', $_form);
$this->assertNotContains('Error:', $_form);
$this->assertNotContains('Notice:', $_form);
. '/views/membership/index.phtml');
$this->assertContains('Listing PersonNames', $index);
$this->assertNotContains('Error:', $index);
$this->assertNotContains('Notice:', $index);
. '/views/membership/show.phtml');
$this->assertContains('PersonName', $show);
$this->assertNotContains('Error:', $show);
$this->assertNotContains('Notice:', $show);
. '/views/layouts/membership.phtml'));
. '/views/layouts/membership.phtml');
$this->assertContains('membership', $layout);
* Verify that generate_scaffold() defaults controller from model
// Generate a PersonName/person_name scaffold
$tg->generate_scaffold('PersonName',null);
$this->assertContains('create', $output);
$this->assertNotContains('Error:', $output);
$this->assertNotContains('Notice:', $output);
. '/controllers/person_name_controller.php'));
. '/controllers/person_name_controller.php');
$this->assertContains('class PersonNameController extends'
. ' ApplicationController', $controller);
$this->assertNotContains('Error:', $controller);
$this->assertNotContains('Notice:', $controller);
. '/models/person_name.php'));
. '/models/person_name.php');
$this->assertContains('class PersonName extends'
. ' ActiveRecord', $model);
|