| 1 | <?php |
|---|
| 2 | /** |
|---|
| 3 | * File for the TraxGeneratorTest class |
|---|
| 4 | * |
|---|
| 5 | * (PHP 5) |
|---|
| 6 | * |
|---|
| 7 | * @package PHPonTraxTest |
|---|
| 8 | * @license http://opensource.org/licenses/gpl-license.php GNU Public License |
|---|
| 9 | * @copyright (c) Walter O. Haas 2006 |
|---|
| 10 | * @version $Id$ |
|---|
| 11 | * @author Walt Haas <haas@xmission.com> |
|---|
| 12 | */ |
|---|
| 13 | |
|---|
| 14 | echo "testing TraxGenerator\n"; |
|---|
| 15 | require_once 'testenv.php'; |
|---|
| 16 | require_once 'inflector.php'; |
|---|
| 17 | |
|---|
| 18 | // Call TraxGeneratorTest::main() if this source file is executed directly. |
|---|
| 19 | if (!defined("PHPUnit2_MAIN_METHOD")) { |
|---|
| 20 | define("PHPUnit2_MAIN_METHOD", "TraxGeneratorTest::main"); |
|---|
| 21 | } |
|---|
| 22 | |
|---|
| 23 | require_once "PHPUnit2/Framework/TestCase.php"; |
|---|
| 24 | require_once "PHPUnit2/Framework/TestSuite.php"; |
|---|
| 25 | |
|---|
| 26 | // You may remove the following line when all tests have been implemented. |
|---|
| 27 | require_once "PHPUnit2/Framework/IncompleteTestError.php"; |
|---|
| 28 | |
|---|
| 29 | static $tmpdir; // temporary directory |
|---|
| 30 | |
|---|
| 31 | // Create a temporary directory to receive generated files |
|---|
| 32 | // @todo <b>FIXME:</b> Is this platform independent? |
|---|
| 33 | do { |
|---|
| 34 | $tmpdir = '/tmp/traxtest' . mt_rand(0, 99999999); |
|---|
| 35 | } while(!mkdir($tmpdir,0700)); |
|---|
| 36 | define('TRAX_ROOT', $tmpdir); |
|---|
| 37 | |
|---|
| 38 | define('TRAX_ENV', 'test'); |
|---|
| 39 | |
|---|
| 40 | // Set up information that normally comes from database.ini |
|---|
| 41 | Trax::$database_settings['test'] |
|---|
| 42 | = array('phptype' => 'mysql', |
|---|
| 43 | 'database' => 'test_development', |
|---|
| 44 | 'hostspec' => 'localhost', |
|---|
| 45 | 'username' => 'root', |
|---|
| 46 | 'password' => '', |
|---|
| 47 | 'persistent' => true); |
|---|
| 48 | |
|---|
| 49 | // Create a DB to test with |
|---|
| 50 | @ini_set('include_path','./mockDB:'.ini_get('include_path')); |
|---|
| 51 | require_once "DB.php"; |
|---|
| 52 | $db =& DB::Connect(Trax::$database_settings[TRAX_ENV], |
|---|
| 53 | array('persistent' => true)); |
|---|
| 54 | if (PEAR::isError($db) || is_a($db, 'DB_Error')) { |
|---|
| 55 | PHPUnit2_Framework_Assert::fail("Unable to create database"); |
|---|
| 56 | } |
|---|
| 57 | $db->setFetchMode(DB_FETCHMODE_ASSOC); |
|---|
| 58 | Trax::$active_record_connections[TRAX_ENV] =& $db; |
|---|
| 59 | |
|---|
| 60 | require_once "trax_generator.php"; |
|---|
| 61 | require_once "action_view/helpers.php"; |
|---|
| 62 | require_once "action_view/helpers/active_record_helper.php"; |
|---|
| 63 | require_once "active_record.php"; |
|---|
| 64 | require_once "trax_exceptions.php"; |
|---|
| 65 | |
|---|
| 66 | /** |
|---|
| 67 | * When a class is referenced, get it from app/models |
|---|
| 68 | */ |
|---|
| 69 | function __autoload($class_name) { |
|---|
| 70 | $file = Inflector::underscore($class_name).".php"; |
|---|
| 71 | $file_org = $class_name.".php"; |
|---|
| 72 | |
|---|
| 73 | if(file_exists(Trax::$models_path."/$file")) { |
|---|
| 74 | # Include model classes |
|---|
| 75 | include_once(Trax::$models_path."/$file"); |
|---|
| 76 | } elseif(file_exists(TRAX_LIB_ROOT."/$file")) { |
|---|
| 77 | # Include extra controller classes |
|---|
| 78 | include_once(TRAX_LIB_ROOT."/$file"); |
|---|
| 79 | } |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | /** |
|---|
| 83 | * Test class for TraxGenerator. |
|---|
| 84 | * Generated by PHPUnit2_Util_Skeleton on 2006-03-01 at 15:34:17. |
|---|
| 85 | */ |
|---|
| 86 | class TraxGeneratorTest extends PHPUnit2_Framework_TestCase { |
|---|
| 87 | |
|---|
| 88 | |
|---|
| 89 | /** |
|---|
| 90 | * Runs the test methods of this class. |
|---|
| 91 | * |
|---|
| 92 | * @access public |
|---|
| 93 | * @static |
|---|
| 94 | */ |
|---|
| 95 | public static function main() { |
|---|
| 96 | require_once "PHPUnit2/TextUI/TestRunner.php"; |
|---|
| 97 | |
|---|
| 98 | $suite = new PHPUnit2_Framework_TestSuite("TraxGeneratorTest"); |
|---|
| 99 | $result = PHPUnit2_TextUI_TestRunner::run($suite); |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | /** |
|---|
| 103 | * Set up dummy Trax work area |
|---|
| 104 | * |
|---|
| 105 | * Create a dummy Trax work area in the temporary directory |
|---|
| 106 | * @access protected |
|---|
| 107 | */ |
|---|
| 108 | protected function setUp() { |
|---|
| 109 | |
|---|
| 110 | // Create empty directories representing the Trax work area |
|---|
| 111 | mkdir(TRAX_ROOT.'/controllers',0700,true); |
|---|
| 112 | mkdir(TRAX_ROOT.'/helpers',0700,true); |
|---|
| 113 | mkdir(TRAX_ROOT.'/models',0700,true); |
|---|
| 114 | mkdir(TRAX_ROOT.'/views/layouts',0700,true); |
|---|
| 115 | } |
|---|
| 116 | |
|---|
| 117 | /** |
|---|
| 118 | * Remove the dummy Trax work area |
|---|
| 119 | * |
|---|
| 120 | * @access protected |
|---|
| 121 | */ |
|---|
| 122 | protected function tearDown() { |
|---|
| 123 | |
|---|
| 124 | // Empty the temporary directory |
|---|
| 125 | $this->rmrfdir(TRAX_ROOT); |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| 128 | /** |
|---|
| 129 | * Remove all the contents of a directory |
|---|
| 130 | * @param string $dir Name of directory to empty. Ends with '/' |
|---|
| 131 | */ |
|---|
| 132 | private function rmrfdir($dir) { |
|---|
| 133 | |
|---|
| 134 | // Open the directory |
|---|
| 135 | $dir_h = opendir($dir); |
|---|
| 136 | |
|---|
| 137 | if (!$dir_h) { |
|---|
| 138 | return; // |
|---|
| 139 | } |
|---|
| 140 | |
|---|
| 141 | // Delete everything in the directory |
|---|
| 142 | while (false !== ($item = readdir($dir_h))) { |
|---|
| 143 | if (is_dir($dir.$item)) { |
|---|
| 144 | |
|---|
| 145 | // $item is a directory. Recurse if appropriate |
|---|
| 146 | if ( ($item != '.') && ($item != '..') ) { |
|---|
| 147 | $this->rmrfdir($dir.$item.'/'); // empty the directory |
|---|
| 148 | rmdir($dir.$item); // remove the empty directory |
|---|
| 149 | } |
|---|
| 150 | } else { // if (is_dir($item)) |
|---|
| 151 | |
|---|
| 152 | // $item is not a directory. |
|---|
| 153 | unlink($dir.$item); |
|---|
| 154 | } // if (is_dir($item)) ... else |
|---|
| 155 | } // while (false !== ($item = readdir($dir_h))) |
|---|
| 156 | |
|---|
| 157 | // Close the directory |
|---|
| 158 | closedir($dir_h); |
|---|
| 159 | } |
|---|
| 160 | |
|---|
| 161 | |
|---|
| 162 | /** |
|---|
| 163 | * Verify that generator_help() outputs a "Usage" message |
|---|
| 164 | */ |
|---|
| 165 | public function testGenerator_help() { |
|---|
| 166 | $tg = new TraxGenerator; |
|---|
| 167 | ob_start(); |
|---|
| 168 | $tg->generator_help(); // should produce a usage |
|---|
| 169 | $output = ob_get_clean(); |
|---|
| 170 | $this->assertContains('Usage', $output); |
|---|
| 171 | } |
|---|
| 172 | |
|---|
| 173 | /** |
|---|
| 174 | * Verify that scaffold_help() outputs a "Usage" message |
|---|
| 175 | */ |
|---|
| 176 | public function testScaffold_help() { |
|---|
| 177 | $tg = new TraxGenerator; |
|---|
| 178 | ob_start(); |
|---|
| 179 | $tg->scaffold_help(); // should produce a usage |
|---|
| 180 | $output = ob_get_clean(); |
|---|
| 181 | $this->assertContains('Usage', $output); |
|---|
| 182 | } |
|---|
| 183 | |
|---|
| 184 | /** |
|---|
| 185 | * Verify that controller_help() outputs a "Usage" message |
|---|
| 186 | */ |
|---|
| 187 | public function testController_help() { |
|---|
| 188 | $tg = new TraxGenerator; |
|---|
| 189 | ob_start(); |
|---|
| 190 | $tg->controller_help(); // should produce a usage |
|---|
| 191 | $output = ob_get_clean(); |
|---|
| 192 | $this->assertContains('Usage', $output); |
|---|
| 193 | } |
|---|
| 194 | |
|---|
| 195 | /** |
|---|
| 196 | * Verify that model_help() outputs a "Usage" message |
|---|
| 197 | */ |
|---|
| 198 | public function testModel_help() { |
|---|
| 199 | $tg = new TraxGenerator; |
|---|
| 200 | ob_start(); |
|---|
| 201 | $tg->model_help(); // should produce a usage |
|---|
| 202 | $output = ob_get_clean(); |
|---|
| 203 | $this->assertContains('Usage', $output); |
|---|
| 204 | } |
|---|
| 205 | |
|---|
| 206 | /** |
|---|
| 207 | * Verify that generate_controller() generates a controller |
|---|
| 208 | * |
|---|
| 209 | * We would like to test |
|---|
| 210 | * {@link TraxGenerator::create_controller() create_controller()}, |
|---|
| 211 | * {@link TraxGenerator::create_helper() create_helper()} |
|---|
| 212 | * and {@link TraxGenerator::create_view() create_view()} in |
|---|
| 213 | * isolation before we test |
|---|
| 214 | * {@link TraxGenerator::generate_controller() generate_controller()} |
|---|
| 215 | * but that's impossible because they depend on private variables |
|---|
| 216 | * which are set only in generate_controller(). |
|---|
| 217 | */ |
|---|
| 218 | public function testGenerate_controller() { |
|---|
| 219 | |
|---|
| 220 | // Generate a mumble controller with no views |
|---|
| 221 | $tg = new TraxGenerator; |
|---|
| 222 | ob_start(); |
|---|
| 223 | $tg->generate_controller('mumble'); |
|---|
| 224 | $output = ob_get_clean(); |
|---|
| 225 | $this->assertContains('create', $output); |
|---|
| 226 | $this->assertTrue(is_file(TRAX_ROOT |
|---|
| 227 | . '/controllers/mumble_controller.php')); |
|---|
| 228 | $controller = file_get_contents(TRAX_ROOT |
|---|
| 229 | . '/controllers/mumble_controller.php'); |
|---|
| 230 | $this->assertContains('class MumbleController extends' |
|---|
| 231 | . ' ApplicationController', $controller); |
|---|
| 232 | $this->assertTrue(is_file(TRAX_ROOT |
|---|
| 233 | . '/helpers/mumble_helper.php')); |
|---|
| 234 | $this->assertTrue(is_dir(TRAX_ROOT . '/views/mumble')); |
|---|
| 235 | |
|---|
| 236 | // Verify that a second attempt to create the same controller |
|---|
| 237 | // reports that it exists |
|---|
| 238 | $tg = new TraxGenerator; |
|---|
| 239 | ob_start(); |
|---|
| 240 | $tg->generate_controller('mumble'); |
|---|
| 241 | $output = ob_get_clean(); |
|---|
| 242 | $this->assertContains('exists', $output); |
|---|
| 243 | $this->assertTrue(file_exists(TRAX_ROOT |
|---|
| 244 | . '/controllers/mumble_controller.php')); |
|---|
| 245 | |
|---|
| 246 | // Generate a controller with one view file |
|---|
| 247 | $tg = new TraxGenerator; |
|---|
| 248 | ob_start(); |
|---|
| 249 | $tg->generate_controller('store', 'manager'); |
|---|
| 250 | $output = ob_get_clean(); |
|---|
| 251 | $this->assertContains('create', $output); |
|---|
| 252 | $this->assertTrue(is_file(TRAX_ROOT |
|---|
| 253 | . '/controllers/store_controller.php')); |
|---|
| 254 | $controller = file_get_contents(TRAX_ROOT |
|---|
| 255 | . '/controllers/store_controller.php'); |
|---|
| 256 | $this->assertContains('class StoreController extends' |
|---|
| 257 | . ' ApplicationController', $controller); |
|---|
| 258 | $this->assertTrue(is_file(TRAX_ROOT . '/helpers/store_helper.php')); |
|---|
| 259 | $this->assertTrue(is_dir(TRAX_ROOT . '/views/store')); |
|---|
| 260 | $this->assertTrue(is_file(TRAX_ROOT . '/views/store/manager.phtml')); |
|---|
| 261 | $view = file_get_contents(TRAX_ROOT . '/views/store/manager.phtml'); |
|---|
| 262 | $this->assertContains('Store->manager',$view); |
|---|
| 263 | $this->assertContains('store/manager.phtml',$view); |
|---|
| 264 | |
|---|
| 265 | // Generate a controller with two view files |
|---|
| 266 | $tg = new TraxGenerator; |
|---|
| 267 | ob_start(); |
|---|
| 268 | $tg->generate_controller('account', array('customer','teller')); |
|---|
| 269 | $output = ob_get_clean(); |
|---|
| 270 | $this->assertContains('create', $output); |
|---|
| 271 | $this->assertTrue(is_file(TRAX_ROOT |
|---|
| 272 | . '/controllers/account_controller.php')); |
|---|
| 273 | $controller = file_get_contents(TRAX_ROOT |
|---|
| 274 | . '/controllers/account_controller.php'); |
|---|
| 275 | $this->assertContains('class AccountController extends' |
|---|
| 276 | . ' ApplicationController', $controller); |
|---|
| 277 | $this->assertTrue(is_file(TRAX_ROOT |
|---|
| 278 | . '/helpers/account_helper.php')); |
|---|
| 279 | $this->assertTrue(is_dir(TRAX_ROOT . '/views/account')); |
|---|
| 280 | $this->assertTrue(is_file(TRAX_ROOT |
|---|
| 281 | . '/views/account/customer.phtml')); |
|---|
| 282 | $view = file_get_contents(TRAX_ROOT . '/views/account/customer.phtml'); |
|---|
| 283 | $this->assertContains('Account->customer',$view); |
|---|
| 284 | $this->assertContains('account/customer.phtml',$view); |
|---|
| 285 | $this->assertTrue(is_file(TRAX_ROOT |
|---|
| 286 | . '/views/account/teller.phtml')); |
|---|
| 287 | $view = file_get_contents(TRAX_ROOT . '/views/account/teller.phtml'); |
|---|
| 288 | $this->assertContains('Account->teller',$view); |
|---|
| 289 | $this->assertContains('account/teller.phtml',$view); |
|---|
| 290 | |
|---|
| 291 | // Generate a controller in a subdirectory |
|---|
| 292 | $tg = new TraxGenerator; |
|---|
| 293 | ob_start(); |
|---|
| 294 | $tg->generate_controller('forum/admin'); |
|---|
| 295 | $output = ob_get_clean(); |
|---|
| 296 | $this->assertContains('create', $output); |
|---|
| 297 | $this->assertTrue(is_file(TRAX_ROOT |
|---|
| 298 | . '/controllers/forum/admin_controller.php')); |
|---|
| 299 | $controller = file_get_contents(TRAX_ROOT |
|---|
| 300 | . '/controllers/forum/admin_controller.php'); |
|---|
| 301 | $this->assertContains('class AdminController extends' |
|---|
| 302 | . ' ApplicationController', $controller); |
|---|
| 303 | $this->assertTrue(is_file(TRAX_ROOT |
|---|
| 304 | . '/helpers/forum/admin_helper.php')); |
|---|
| 305 | $this->assertTrue(is_dir(TRAX_ROOT . '/views/forum/admin')); |
|---|
| 306 | } |
|---|
| 307 | |
|---|
| 308 | /** |
|---|
| 309 | * Verify that generate_model() generates a model |
|---|
| 310 | */ |
|---|
| 311 | public function testGenerate_model() { |
|---|
| 312 | |
|---|
| 313 | // Generate a CreditCard model |
|---|
| 314 | $tg = new TraxGenerator; |
|---|
| 315 | ob_start(); |
|---|
| 316 | $tg->generate_model('CreditCard'); |
|---|
| 317 | $output = ob_get_clean(); |
|---|
| 318 | $this->assertContains('create', $output); |
|---|
| 319 | $this->assertTrue(is_file(TRAX_ROOT . '/models/credit_card.php')); |
|---|
| 320 | $model = file_get_contents(TRAX_ROOT . '/models/credit_card.php'); |
|---|
| 321 | $this->assertContains('class CreditCard extends ActiveRecord', |
|---|
| 322 | $model); |
|---|
| 323 | |
|---|
| 324 | // Generate a soap_opera model |
|---|
| 325 | $tg = new TraxGenerator; |
|---|
| 326 | ob_start(); |
|---|
| 327 | $tg->generate_model('soap_opera'); |
|---|
| 328 | $output = ob_get_clean(); |
|---|
| 329 | $this->assertContains('create', $output); |
|---|
| 330 | $this->assertTrue(is_file(TRAX_ROOT . '/models/soap_opera.php')); |
|---|
| 331 | $model = file_get_contents(TRAX_ROOT . '/models/soap_opera.php'); |
|---|
| 332 | $this->assertContains('class SoapOpera extends ActiveRecord', |
|---|
| 333 | $model); |
|---|
| 334 | } |
|---|
| 335 | |
|---|
| 336 | /** |
|---|
| 337 | * Verify that generate_scaffold() generates a model and controller |
|---|
| 338 | */ |
|---|
| 339 | public function testGenerate_scaffold1() { |
|---|
| 340 | |
|---|
| 341 | // Generate a PersonName/membership scaffold |
|---|
| 342 | $tg = new TraxGenerator; |
|---|
| 343 | ob_start(); |
|---|
| 344 | $tg->generate_scaffold('PersonName', 'membership'); |
|---|
| 345 | |
|---|
| 346 | $output = ob_get_clean(); |
|---|
| 347 | $this->assertContains('create', $output); |
|---|
| 348 | $this->assertNotContains('Error:', $output); |
|---|
| 349 | $this->assertNotContains('Notice:', $output); |
|---|
| 350 | |
|---|
| 351 | // verify controller |
|---|
| 352 | $this->assertTrue(is_file(TRAX_ROOT |
|---|
| 353 | . '/controllers/membership_controller.php')); |
|---|
| 354 | $controller = file_get_contents(TRAX_ROOT |
|---|
| 355 | . '/controllers/membership_controller.php'); |
|---|
| 356 | $this->assertContains('class MembershipController extends' |
|---|
| 357 | . ' ApplicationController', $controller); |
|---|
| 358 | $this->assertNotContains('Error:', $controller); |
|---|
| 359 | $this->assertNotContains('Notice:', $controller); |
|---|
| 360 | |
|---|
| 361 | // verify model |
|---|
| 362 | $this->assertTrue(is_file(TRAX_ROOT |
|---|
| 363 | . '/models/person_name.php')); |
|---|
| 364 | $model = file_get_contents(TRAX_ROOT |
|---|
| 365 | . '/models/person_name.php'); |
|---|
| 366 | $this->assertContains('class PersonName extends' |
|---|
| 367 | . ' ActiveRecord', $model); |
|---|
| 368 | $this->assertNotContains('Error:', $model); |
|---|
| 369 | $this->assertNotContains('Notice:', $model); |
|---|
| 370 | |
|---|
| 371 | // verify helper |
|---|
| 372 | $this->assertTrue(is_file(TRAX_ROOT |
|---|
| 373 | . '/helpers/membership_helper.php')); |
|---|
| 374 | $helper = file_get_contents(TRAX_ROOT |
|---|
| 375 | . '/helpers/membership_helper.php'); |
|---|
| 376 | $this->assertContains('MembershipController', $helper); |
|---|
| 377 | $this->assertNotContains('Error:', $helper); |
|---|
| 378 | $this->assertNotContains('Notice:', $helper); |
|---|
| 379 | |
|---|
| 380 | // verify views |
|---|
| 381 | $this->assertTrue(is_dir(TRAX_ROOT . '/views/membership')); |
|---|
| 382 | |
|---|
| 383 | $this->assertTrue(is_file(TRAX_ROOT . '/views/membership/add.phtml')); |
|---|
| 384 | $add = file_get_contents(TRAX_ROOT |
|---|
| 385 | . '/views/membership/add.phtml'); |
|---|
| 386 | $this->assertContains('New PersonName', $add); |
|---|
| 387 | $this->assertNotContains('Error:', $add); |
|---|
| 388 | $this->assertNotContains('Notice:', $add); |
|---|
| 389 | |
|---|
| 390 | $this->assertTrue(is_file(TRAX_ROOT . '/views/membership/edit.phtml')); |
|---|
| 391 | $edit = file_get_contents(TRAX_ROOT |
|---|
| 392 | . '/views/membership/edit.phtml'); |
|---|
| 393 | $this->assertContains('Editing PersonName', $edit); |
|---|
| 394 | $this->assertNotContains('Error:', $edit); |
|---|
| 395 | $this->assertNotContains('Notice:', $edit); |
|---|
| 396 | |
|---|
| 397 | |
|---|
| 398 | $this->assertTrue(is_file(TRAX_ROOT . '/views/membership/_form.phtml')); |
|---|
| 399 | $_form = file_get_contents(TRAX_ROOT |
|---|
| 400 | . '/views/membership/_form.phtml'); |
|---|
| 401 | $this->assertContains('PersonName', $_form); |
|---|
| 402 | $this->assertNotContains('Error:', $_form); |
|---|
| 403 | $this->assertNotContains('Notice:', $_form); |
|---|
| 404 | |
|---|
| 405 | $this->assertTrue(is_file(TRAX_ROOT . '/views/membership/index.phtml')); |
|---|
| 406 | $index = file_get_contents(TRAX_ROOT |
|---|
| 407 | . '/views/membership/index.phtml'); |
|---|
| 408 | $this->assertContains('Listing PersonNames', $index); |
|---|
| 409 | $this->assertNotContains('Error:', $index); |
|---|
| 410 | $this->assertNotContains('Notice:', $index); |
|---|
| 411 | |
|---|
| 412 | $this->assertTrue(is_file(TRAX_ROOT . '/views/membership/show.phtml')); |
|---|
| 413 | $show = file_get_contents(TRAX_ROOT |
|---|
| 414 | . '/views/membership/show.phtml'); |
|---|
| 415 | $this->assertContains('PersonName', $show); |
|---|
| 416 | $this->assertNotContains('Error:', $show); |
|---|
| 417 | $this->assertNotContains('Notice:', $show); |
|---|
| 418 | |
|---|
| 419 | // verify layout |
|---|
| 420 | $this->assertTrue(is_file(TRAX_ROOT |
|---|
| 421 | . '/views/layouts/membership.phtml')); |
|---|
| 422 | $layout = file_get_contents(TRAX_ROOT |
|---|
| 423 | . '/views/layouts/membership.phtml'); |
|---|
| 424 | $this->assertContains('membership', $layout); |
|---|
| 425 | } |
|---|
| 426 | |
|---|
| 427 | /** |
|---|
| 428 | * Verify that generate_scaffold() defaults controller from model |
|---|
| 429 | */ |
|---|
| 430 | public function testGenerate_scaffold2() { |
|---|
| 431 | |
|---|
| 432 | // Generate a PersonName/person_name scaffold |
|---|
| 433 | $tg = new TraxGenerator; |
|---|
| 434 | ob_start(); |
|---|
| 435 | $tg->generate_scaffold('PersonName',null); |
|---|
| 436 | $output = ob_get_clean(); |
|---|
| 437 | $this->assertContains('create', $output); |
|---|
| 438 | $this->assertNotContains('Error:', $output); |
|---|
| 439 | $this->assertNotContains('Notice:', $output); |
|---|
| 440 | |
|---|
| 441 | // verify controller |
|---|
| 442 | $this->assertTrue(is_file(TRAX_ROOT |
|---|
| 443 | . '/controllers/person_name_controller.php')); |
|---|
| 444 | $controller = file_get_contents(TRAX_ROOT |
|---|
| 445 | . '/controllers/person_name_controller.php'); |
|---|
| 446 | $this->assertContains('class PersonNameController extends' |
|---|
| 447 | . ' ApplicationController', $controller); |
|---|
| 448 | $this->assertNotContains('Error:', $controller); |
|---|
| 449 | $this->assertNotContains('Notice:', $controller); |
|---|
| 450 | |
|---|
| 451 | // verify model |
|---|
| 452 | $this->assertTrue(is_file(TRAX_ROOT |
|---|
| 453 | . '/models/person_name.php')); |
|---|
| 454 | $model = file_get_contents(TRAX_ROOT |
|---|
| 455 | . '/models/person_name.php'); |
|---|
| 456 | $this->assertContains('class PersonName extends' |
|---|
| 457 | . ' ActiveRecord', $model); |
|---|
| 458 | $this->assertNotContains('Error:', $model); |
|---|
| 459 | $this->assertNotContains('Notice:', $model); |
|---|
| 460 | |
|---|
| 461 | // verify helper |
|---|
| 462 | $this->assertTrue(is_file(TRAX_ROOT |
|---|
| 463 | . '/helpers/person_name_helper.php')); |
|---|
| 464 | $helper = file_get_contents(TRAX_ROOT |
|---|
| 465 | . '/helpers/person_name_helper.php'); |
|---|
| 466 | $this->assertContains('PersonNameController', $helper); |
|---|
| 467 | $this->assertNotContains('Error:', $helper); |
|---|
| 468 | $this->assertNotContains('Notice:', $helper); |
|---|
| 469 | |
|---|
| 470 | // verify views |
|---|
| 471 | $this->assertTrue(is_dir(TRAX_ROOT . '/views/person_name')); |
|---|
| 472 | |
|---|
| 473 | $this->assertTrue(is_file(TRAX_ROOT . '/views/person_name/add.phtml')); |
|---|
| 474 | $add = file_get_contents(TRAX_ROOT |
|---|
| 475 | . '/views/person_name/add.phtml'); |
|---|
| 476 | $this->assertContains('New PersonName', $add); |
|---|
| 477 | $this->assertNotContains('Error:', $add); |
|---|
| 478 | $this->assertNotContains('Notice:', $add); |
|---|
| 479 | |
|---|
| 480 | $this->assertTrue(is_file(TRAX_ROOT . '/views/person_name/edit.phtml')); |
|---|
| 481 | $edit = file_get_contents(TRAX_ROOT |
|---|
| 482 | . '/views/person_name/edit.phtml'); |
|---|
| 483 | $this->assertContains('Editing PersonName', $edit); |
|---|
| 484 | $this->assertNotContains('Error:', $edit); |
|---|
| 485 | $this->assertNotContains('Notice:', $edit); |
|---|
| 486 | |
|---|
| 487 | |
|---|
| 488 | $this->assertTrue(is_file(TRAX_ROOT |
|---|
| 489 | . '/views/person_name/_form.phtml')); |
|---|
| 490 | $_form = file_get_contents(TRAX_ROOT |
|---|
| 491 | . '/views/person_name/_form.phtml'); |
|---|
| 492 | $this->assertContains('PersonName', $_form); |
|---|
| 493 | $this->assertNotContains('Error:', $_form); |
|---|
| 494 | $this->assertNotContains('Notice:', $_form); |
|---|
| 495 | |
|---|
| 496 | $this->assertTrue(is_file(TRAX_ROOT |
|---|
| 497 | . '/views/person_name/index.phtml')); |
|---|
| 498 | $index = file_get_contents(TRAX_ROOT |
|---|
| 499 | . '/views/person_name/index.phtml'); |
|---|
| 500 | $this->assertContains('Listing PersonNames', $index); |
|---|
| 501 | $this->assertNotContains('Error:', $index); |
|---|
| 502 | $this->assertNotContains('Notice:', $index); |
|---|
| 503 | |
|---|
| 504 | $this->assertTrue(is_file(TRAX_ROOT . '/views/person_name/show.phtml')); |
|---|
| 505 | $show = file_get_contents(TRAX_ROOT |
|---|
| 506 | . '/views/person_name/show.phtml'); |
|---|
| 507 | $this->assertContains('PersonName', $show); |
|---|
| 508 | $this->assertNotContains('Error:', $show); |
|---|
| 509 | $this->assertNotContains('Notice:', $show); |
|---|
| 510 | |
|---|
| 511 | // verify layout |
|---|
| 512 | $this->assertTrue(is_file(TRAX_ROOT |
|---|
| 513 | . '/views/layouts/person_name.phtml')); |
|---|
| 514 | $layout = file_get_contents(TRAX_ROOT |
|---|
| 515 | . '/views/layouts/person_name.phtml'); |
|---|
| 516 | $this->assertContains('person_name', $layout); |
|---|
| 517 | } |
|---|
| 518 | |
|---|
| 519 | /** |
|---|
| 520 | * Verify generate_scaffold() with additional views |
|---|
| 521 | */ |
|---|
| 522 | public function testGenerate_scaffold3() { |
|---|
| 523 | |
|---|
| 524 | // Generate a PersonName/membership scaffold |
|---|
| 525 | $tg = new TraxGenerator; |
|---|
| 526 | ob_start(); |
|---|
| 527 | $tg->generate_scaffold('PersonName', 'membership', |
|---|
| 528 | array('join', 'renew')); |
|---|
| 529 | $output = ob_get_clean(); |
|---|
| 530 | $this->assertContains('create', $output); |
|---|
| 531 | $this->assertNotContains('Error:', $output); |
|---|
| 532 | $this->assertNotContains('Notice:', $output); |
|---|
| 533 | |
|---|
| 534 | // verify controller |
|---|
| 535 | $this->assertTrue(is_file(TRAX_ROOT |
|---|
| 536 | . '/controllers/membership_controller.php')); |
|---|
| 537 | $controller = file_get_contents(TRAX_ROOT |
|---|
| 538 | . '/controllers/membership_controller.php'); |
|---|
| 539 | $this->assertContains('class MembershipController extends' |
|---|
| 540 | . ' ApplicationController', $controller); |
|---|
| 541 | $this->assertNotContains('Error:', $controller); |
|---|
| 542 | $this->assertNotContains('Notice:', $controller); |
|---|
| 543 | |
|---|
| 544 | // verify model |
|---|
| 545 | $this->assertTrue(is_file(TRAX_ROOT |
|---|
| 546 | . '/models/person_name.php')); |
|---|
| 547 | $model = file_get_contents(TRAX_ROOT |
|---|
| 548 | . '/models/person_name.php'); |
|---|
| 549 | $this->assertContains('class PersonName extends' |
|---|
| 550 | . ' ActiveRecord', $model); |
|---|
| 551 | $this->assertNotContains('Error:', $model); |
|---|
| 552 | $this->assertNotContains('Notice:', $model); |
|---|
| 553 | |
|---|
| 554 | // verify helper |
|---|
| 555 | $this->assertTrue(is_file(TRAX_ROOT |
|---|
| 556 | . '/helpers/membership_helper.php')); |
|---|
| 557 | $helper = file_get_contents(TRAX_ROOT |
|---|
| 558 | . '/helpers/membership_helper.php'); |
|---|
| 559 | $this->assertContains('MembershipController', $helper); |
|---|
| 560 | $this->assertNotContains('Error:', $helper); |
|---|
| 561 | $this->assertNotContains('Notice:', $helper); |
|---|
| 562 | |
|---|
| 563 | // verify views |
|---|
| 564 | $this->assertTrue(is_dir(TRAX_ROOT . '/views/membership')); |
|---|
| 565 | |
|---|
| 566 | $this->assertTrue(is_file(TRAX_ROOT . '/views/membership/add.phtml')); |
|---|
| 567 | $add = file_get_contents(TRAX_ROOT |
|---|
| 568 | . '/views/membership/add.phtml'); |
|---|
| 569 | $this->assertContains('New PersonName', $add); |
|---|
| 570 | $this->assertNotContains('Error:', $add); |
|---|
| 571 | $this->assertNotContains('Notice:', $add); |
|---|
| 572 | |
|---|
| 573 | $this->assertTrue(is_file(TRAX_ROOT . '/views/membership/edit.phtml')); |
|---|
| 574 | $edit = file_get_contents(TRAX_ROOT |
|---|
| 575 | . '/views/membership/edit.phtml'); |
|---|
| 576 | $this->assertContains('Editing PersonName', $edit); |
|---|
| 577 | $this->assertNotContains('Error:', $edit); |
|---|
| 578 | $this->assertNotContains('Notice:', $edit); |
|---|
| 579 | |
|---|
| 580 | |
|---|
| 581 | $this->assertTrue(is_file(TRAX_ROOT . '/views/membership/_form.phtml')); |
|---|
| 582 | $_form = file_get_contents(TRAX_ROOT |
|---|
| 583 | . '/views/membership/_form.phtml'); |
|---|
| 584 | $this->assertContains('PersonName', $_form); |
|---|
| 585 | $this->assertNotContains('Error:', $_form); |
|---|
| 586 | $this->assertNotContains('Notice:', $_form); |
|---|
| 587 | |
|---|
| 588 | $this->assertTrue(is_file(TRAX_ROOT . '/views/membership/index.phtml')); |
|---|
| 589 | $index = file_get_contents(TRAX_ROOT |
|---|
| 590 | . '/views/membership/index.phtml'); |
|---|
| 591 | $this->assertContains('Listing PersonNames', $index); |
|---|
| 592 | $this->assertNotContains('Error:', $index); |
|---|
| 593 | $this->assertNotContains('Notice:', $index); |
|---|
| 594 | |
|---|
| 595 | $this->assertTrue(is_file(TRAX_ROOT . 'views/membership/show.phtml')); |
|---|
| 596 | $show = file_get_contents(TRAX_ROOT |
|---|
| 597 | . '/views/membership/show.phtml'); |
|---|
| 598 | $this->assertContains('PersonName', $show); |
|---|
| 599 | $this->assertNotContains('Error:', $show); |
|---|
| 600 | $this->assertNotContains('Notice:', $show); |
|---|
| 601 | |
|---|
| 602 | $this->assertTrue(is_file(TRAX_ROOT . '/views/membership/join.phtml')); |
|---|
| 603 | $join = file_get_contents(TRAX_ROOT |
|---|
| 604 | . 'views/membership/join.phtml'); |
|---|
| 605 | $this->assertContains('Membership->join', $join); |
|---|
| 606 | $this->assertNotContains('Error:', $join); |
|---|
| 607 | $this->assertNotContains('Notice:', $join); |
|---|
| 608 | |
|---|
| 609 | $this->assertTrue(is_file(TRAX_ROOT . '/views/membership/renew.phtml')); |
|---|
| 610 | $renew = file_get_contents(TRAX_ROOT |
|---|
| 611 | . '/views/membership/renew.phtml'); |
|---|
| 612 | $this->assertContains('Membership->renew', $renew); |
|---|
| 613 | $this->assertNotContains('Error:', $renew); |
|---|
| 614 | $this->assertNotContains('Notice:', $renew); |
|---|
| 615 | |
|---|
| 616 | // verify layout |
|---|
| 617 | $this->assertTrue(is_file(TRAX_ROOT |
|---|
| 618 | . '/views/layouts/membership.phtml')); |
|---|
| 619 | $layout = file_get_contents(TRAX_ROOT |
|---|
| 620 | . '/views/layouts/membership.phtml'); |
|---|
| 621 | $this->assertContains('membership', $layout); |
|---|
| 622 | } |
|---|
| 623 | |
|---|
| 624 | /** |
|---|
| 625 | * Verify generate_scaffold() with the controller in a subdirectory |
|---|
| 626 | */ |
|---|
| 627 | public function testGenerate_scaffold4() { |
|---|
| 628 | |
|---|
| 629 | // Generate a PersonName/membership scaffold |
|---|
| 630 | $tg = new TraxGenerator; |
|---|
| 631 | ob_start(); |
|---|
| 632 | $tg->generate_scaffold('PersonName', 'admin/membership'); |
|---|
| 633 | $output = ob_get_clean(); |
|---|
| 634 | $this->assertContains('create', $output); |
|---|
| 635 | $this->assertNotContains('Error:', $output); |
|---|
| 636 | $this->assertNotContains('Notice:', $output); |
|---|
| 637 | |
|---|
| 638 | // verify controller |
|---|
| 639 | $this->assertTrue(is_file(TRAX_ROOT |
|---|
| 640 | . '/controllers/admin/membership_controller.php')); |
|---|
| 641 | $controller = file_get_contents(TRAX_ROOT |
|---|
| 642 | . '/controllers/admin/membership_controller.php'); |
|---|
| 643 | $this->assertContains('class MembershipController extends' |
|---|
| 644 | . ' ApplicationController', $controller); |
|---|
| 645 | $this->assertNotContains('Error:', $controller); |
|---|
| 646 | $this->assertNotContains('Notice:', $controller); |
|---|
| 647 | |
|---|
| 648 | // verify model |
|---|
| 649 | $this->assertTrue(is_file(TRAX_ROOT |
|---|
| 650 | . '/models/person_name.php')); |
|---|
| 651 | $model = file_get_contents(TRAX_ROOT |
|---|
| 652 | . '/models/person_name.php'); |
|---|
| 653 | $this->assertContains('class PersonName extends' |
|---|
| 654 | . ' ActiveRecord', $model); |
|---|
| 655 | $this->assertNotContains('Error:', $model); |
|---|
| 656 | $this->assertNotContains('Notice:', $model); |
|---|
| 657 | |
|---|
| 658 | // verify helper |
|---|
| 659 | $this->assertTrue(is_file(TRAX_ROOT |
|---|
| 660 | . '/helpers/admin/membership_helper.php')); |
|---|
| 661 | $helper = file_get_contents(TRAX_ROOT |
|---|
| 662 | . '/helpers/admin/membership_helper.php'); |
|---|
| 663 | $this->assertContains('MembershipController', $helper); |
|---|
| 664 | $this->assertNotContains('Error:', $helper); |
|---|
| 665 | $this->assertNotContains('Notice:', $helper); |
|---|
| 666 | |
|---|
| 667 | // verify views |
|---|
| 668 | $this->assertTrue(is_dir(TRAX_ROOT . '/views/admin/membership')); |
|---|
| 669 | |
|---|
| 670 | $this->assertTrue(is_file(TRAX_ROOT |
|---|
| 671 | . '/views/admin/membership/add.phtml')); |
|---|
| 672 | $add = file_get_contents(TRAX_ROOT |
|---|
| 673 | . '/views/admin/membership/add.phtml'); |
|---|
| 674 | $this->assertContains('New PersonName', $add); |
|---|
| 675 | $this->assertNotContains('Error:', $add); |
|---|
| 676 | $this->assertNotContains('Notice:', $add); |
|---|
| 677 | |
|---|
| 678 | $this->assertTrue(is_file(TRAX_ROOT |
|---|
| 679 | . '/views/admin/membership/edit.phtml')); |
|---|
| 680 | $edit = file_get_contents(TRAX_ROOT |
|---|
| 681 | . '/views/admin/membership/edit.phtml'); |
|---|
| 682 | $this->assertContains('Editing PersonName', $edit); |
|---|
| 683 | $this->assertNotContains('Error:', $edit); |
|---|
| 684 | $this->assertNotContains('Notice:', $edit); |
|---|
| 685 | |
|---|
| 686 | |
|---|
| 687 | $this->assertTrue(is_file(TRAX_ROOT |
|---|
| 688 | . '/views/admin/membership/_form.phtml')); |
|---|
| 689 | $_form = file_get_contents(TRAX_ROOT |
|---|
| 690 | . '/views/admin/membership/_form.phtml'); |
|---|
| 691 | $this->assertContains('PersonName', $_form); |
|---|
| 692 | $this->assertNotContains('Error:', $_form); |
|---|
| 693 | $this->assertNotContains('Notice:', $_form); |
|---|
| 694 | |
|---|
| 695 | $this->assertTrue(is_file(TRAX_ROOT |
|---|
| 696 | . '/views/admin/membership/index.phtml')); |
|---|
| 697 | $index = file_get_contents(TRAX_ROOT |
|---|
| 698 | . '/views/admin/membership/index.phtml'); |
|---|
| 699 | $this->assertContains('Listing PersonNames', $index); |
|---|
| 700 | $this->assertNotContains('Error:', $index); |
|---|
| 701 | $this->assertNotContains('Notice:', $index); |
|---|
| 702 | |
|---|
| 703 | $this->assertTrue(is_file(TRAX_ROOT |
|---|
| 704 | . '/views/admin/membership/show.phtml')); |
|---|
| 705 | $show = file_get_contents(TRAX_ROOT |
|---|
| 706 | . '/views/admin/membership/show.phtml'); |
|---|
| 707 | $this->assertContains('PersonName', $show); |
|---|
| 708 | $this->assertNotContains('Error:', $show); |
|---|
| 709 | $this->assertNotContains('Notice:', $show); |
|---|
| 710 | |
|---|
| 711 | // verify layout |
|---|
| 712 | $this->assertTrue(is_file(TRAX_ROOT |
|---|
| 713 | . '/views/layouts/admin/membership.phtml')); |
|---|
| 714 | $layout = file_get_contents(TRAX_ROOT |
|---|
| 715 | . '/views/layouts/admin/membership.phtml'); |
|---|
| 716 | $this->assertContains('membership', $layout); |
|---|
| 717 | } |
|---|
| 718 | |
|---|
| 719 | /** |
|---|
| 720 | * Test fix_php_brackets() |
|---|
| 721 | */ |
|---|
| 722 | public function testFix_php_brackets() { |
|---|
| 723 | $tg = new TraxGenerator; |
|---|
| 724 | $this->assertEquals($tg->fix_php_brackets('< ?php foo ? >'), |
|---|
| 725 | '<?php foo ?>'); |
|---|
| 726 | } |
|---|
| 727 | |
|---|
| 728 | /** |
|---|
| 729 | * @todo Implement testRun(). |
|---|
| 730 | */ |
|---|
| 731 | public function testRun() { |
|---|
| 732 | |
|---|
| 733 | // Verify that no command produces usage message |
|---|
| 734 | $tg = new TraxGenerator; |
|---|
| 735 | ob_start(); |
|---|
| 736 | $tg->run(); // should produce a usage |
|---|
| 737 | $result = ob_get_clean(); |
|---|
| 738 | $this->assertContains('Usage', $result); |
|---|
| 739 | |
|---|
| 740 | // Verify that unknown command produces usage message |
|---|
| 741 | $_SERVER['argv'][1] = 'foo'; |
|---|
| 742 | $tg = new TraxGenerator; |
|---|
| 743 | ob_start(); |
|---|
| 744 | $tg->run(); // should produce a usage |
|---|
| 745 | $result = ob_get_clean(); |
|---|
| 746 | $this->assertContains('Usage', $result); |
|---|
| 747 | |
|---|
| 748 | // Generate a CreditCard model |
|---|
| 749 | $_SERVER['argv'][1] = 'model'; |
|---|
| 750 | $_SERVER['argv'][2] = 'CreditCard'; |
|---|
| 751 | $tg = new TraxGenerator; |
|---|
| 752 | ob_start(); |
|---|
| 753 | $tg->run(); |
|---|
| 754 | $output = ob_get_clean(); |
|---|
| 755 | $this->assertContains('create', $output); |
|---|
| 756 | $this->assertTrue(is_file(TRAX_ROOT . '/models/credit_card.php')); |
|---|
| 757 | |
|---|
| 758 | // Generate a mumble controller with no views |
|---|
| 759 | $_SERVER['argv'][1] = 'controller'; |
|---|
| 760 | $_SERVER['argv'][2] = 'mumble'; |
|---|
| 761 | $tg = new TraxGenerator; |
|---|
| 762 | ob_start(); |
|---|
| 763 | $tg->run(); |
|---|
| 764 | $output = ob_get_clean(); |
|---|
| 765 | $this->assertContains('create', $output); |
|---|
| 766 | $this->assertTrue(is_file(TRAX_ROOT |
|---|
| 767 | . '/controllers/mumble_controller.php')); |
|---|
| 768 | $this->assertTrue(is_file(TRAX_ROOT |
|---|
| 769 | . '/helpers/mumble_helper.php')); |
|---|
| 770 | $this->assertTrue(is_dir(TRAX_ROOT . '/views/mumble')); |
|---|
| 771 | |
|---|
| 772 | |
|---|
| 773 | // Generate a PersonName/membership scaffold |
|---|
| 774 | $_SERVER['argv'][1] = 'scaffold'; |
|---|
| 775 | $_SERVER['argv'][2] = 'PersonName'; |
|---|
| 776 | $_SERVER['argv'][3] = 'membership'; |
|---|
| 777 | $tg = new TraxGenerator; |
|---|
| 778 | ob_start(); |
|---|
| 779 | $tg->run(); |
|---|
| 780 | $output = ob_get_clean(); |
|---|
| 781 | $this->assertContains('create', $output); |
|---|
| 782 | $this->assertNotContains('Error:', $output); |
|---|
| 783 | $this->assertNotContains('Notice:', $output); |
|---|
| 784 | |
|---|
| 785 | // verify controller |
|---|
| 786 | $this->assertTrue(is_file(TRAX_ROOT |
|---|
| 787 | . '/controllers/membership_controller.php')); |
|---|
| 788 | |
|---|
| 789 | // verify model |
|---|
| 790 | $this->assertTrue(is_file(TRAX_ROOT |
|---|
| 791 | . '/models/person_name.php')); |
|---|
| 792 | |
|---|
| 793 | // verify helper |
|---|
| 794 | $this->assertTrue(is_file(TRAX_ROOT |
|---|
| 795 | . '/helpers/membership_helper.php')); |
|---|
| 796 | |
|---|
| 797 | // verify views |
|---|
| 798 | $this->assertTrue(is_dir(TRAX_ROOT . '/views/membership')); |
|---|
| 799 | $this->assertTrue(is_file(TRAX_ROOT . '/views/membership/add.phtml')); |
|---|
| 800 | $this->assertTrue(is_file(TRAX_ROOT . '/views/membership/edit.phtml')); |
|---|
| 801 | $this->assertTrue(is_file(TRAX_ROOT . '/views/membership/_form.phtml')); |
|---|
| 802 | $this->assertTrue(is_file(TRAX_ROOT . '/views/membership/index.phtml')); |
|---|
| 803 | $this->assertTrue(is_file(TRAX_ROOT . '/views/membership/show.phtml')); |
|---|
| 804 | |
|---|
| 805 | // verify layout |
|---|
| 806 | $this->assertTrue(is_file(TRAX_ROOT |
|---|
| 807 | . '/views/layouts/membership.phtml')); |
|---|
| 808 | } |
|---|
| 809 | } |
|---|
| 810 | |
|---|
| 811 | // Call TraxGeneratorTest::main() if this source file is executed directly. |
|---|
| 812 | if (PHPUnit2_MAIN_METHOD == "TraxGeneratorTest::main") { |
|---|
| 813 | TraxGeneratorTest::main(); |
|---|
| 814 | } |
|---|
| 815 | |
|---|
| 816 | // Clean up temp directory when done |
|---|
| 817 | rmdir(TRAX_ROOT); |
|---|
| 818 | |
|---|
| 819 | // -- set Emacs parameters -- |
|---|
| 820 | // Local variables: |
|---|
| 821 | // tab-width: 4 |
|---|
| 822 | // c-basic-offset: 4 |
|---|
| 823 | // c-hanging-comment-ender-p: nil |
|---|
| 824 | // indent-tabs-mode: nil |
|---|
| 825 | // End: |
|---|
| 826 | ?> |
|---|