一次產生所有的 testing files

edited 十月 2013 in CakePHP
在網站運行一段時間後才想要做 Unit Test ,但是以前自動產生的東西都刪除了,也不想要一個一個自己打,所以就產生了這個東西,放到 vendors/shells/test.php 後執行 cake test 就可以了,已經存在的檔案會跳過去。
<?php
class TestShell extends Shell {
    var $tasks = array(
        'Model',
        'Controller'
    );

	function main() {
	    /*
	     * 產生所有 Model 的預設 Test Case 與 Fixtur
	     */
	    $modelFolder = new Folder(MODELS);
	    if($files = $modelFolder->find('.*')) {
	        $this->Model->interactive = false;
	        foreach($files AS $file) {
	            $modelName = $this->_modelName(substr($file, 0, strpos($file, '.')));
	            if(file_exists(MODEL_TESTS . Inflector::underscore($modelName) . '.test.php')) {
	                continue;
	            }
	            if(!class_exists($modelName)) {
	                include_once $modelFolder->realpath($file);
	            }
	            $theObject = new $modelName;
	            if(!empty($theObject->belongsTo)) {
	                sort($theObject->belongsTo);
	            } else {
	                $theObject->belongsTo = array();
	            }
	            if(!empty($theObject->hasMany)) {
	                sort($theObject->hasMany);
	            } else {
	                $theObject->hasMany = array();
	            }
	            if(!empty($theObject->hasOne)) {
	                sort($theObject->hasOne);
	            } else {
	                $theObject->hasOne = array();
	            }
	            if(!empty($theObject->hasAndBelongsToMany)) {
	                sort($theObject->hasAndBelongsToMany);
	            } else {
	                $theObject->hasAndBelongsToMany = array();
	            }
	            $relationships = array(
	            	'belongsTo' => $theObject->belongsTo,
	            	'hasMany' => $theObject->hasMany,
	            	'hasOne' => $theObject->hasOne,
	            	'hasAndBelongsToMany' => $theObject->hasAndBelongsToMany,
	            );

	            if($theObject->useTable === false) {
	                unset($theObject);
	                continue;
	            } else if(!empty($theObject->useTable)) {
	                $useTable = $theObject->useTable;
	            } else {
	                $useTable = Inflector::tableize($modelName);
	            }
	            unset($theObject);
	            $this->Model->bakeTest($modelName, $useTable, $relationships);
	        }
	    }

	    /*
	     * 產生所有 Controller 預設 Test Case
	     */
	    $controllerFolder = new Folder(CONTROLLERS);
	    if($files = $controllerFolder->find('.*')) {
	        $this->Controller->interactive = false;
	        foreach($files AS $file) {
	            $controllerName = substr($file, 0, strpos($file, '_controller.'));
	            if(file_exists(CONTROLLER_TESTS . $controllerName . '_controller.test.php')) {
	                continue;
	            }
	            $controllerName = Inflector::camelize($controllerName);
	            $this->Controller->bakeTest($controllerName);
	        }
	    }
	}
}

只是當某一個 Model 有類似這樣的關聯時:
class Hello extends AppModel {
	var $belongsTo = array(
	    'Parent' => array('className' => 'Hello',
	        'foreignKey' => 'parent_id'),
	);
}

程式會進入類似無限迴圈這種事情 ^^||

原始討論: http://twpug.net/x/modules/newbb/viewtopic.php?topic_id=3863
Sign In or Register to comment.