schmunk42\giiant\generators\test\Generator::generate PHP Метод

generate() публичный Метод

public generate ( )
    public function generate()
    {
        $files = [];
        // $relations = $this->generateRelations();
        $db = $this->getDbConnection();
        $class = $this->modelNs . $this->modelClass;
        $classTableNameMethod = 'tableName';
        $this->tableName = $class::$classTableNameMethod();
        //TODO: Add unit tests for search model
        //if($this->searchModelClass !=="")
        //{
        //}
        foreach ($this->getTableNames() as $tableName) {
            $className = $this->generateClassName($tableName);
            $tableSchema = $db->getTableSchema($tableName);
            $params = ['tableName' => $tableName, 'className' => $className, 'modelClass' => $this->modelClass, 'controllerClass' => $this->controllerClass, 'labels' => $this->generateLabels($tableSchema), 'rules' => $this->generateRules($tableSchema), 'attributes' => $this->generateAttributes($tableSchema), 'ns' => $this->ns];
            $files[] = new CodeFile(Yii::getAlias('@app/..' . $this->codeceptionPath . str_replace('\\', '/', $this->ns)) . '/' . $className . $this->baseClassSuffix . 'UnitTest.php', $this->render('unit.php', $params));
        }
        return $files;
    }

Usage Example

Пример #1
0
 public function generate()
 {
     if ($this->singularEntities) {
         $this->modelClass = Inflector::singularize($this->modelClass);
         $this->controllerClass = Inflector::singularize(substr($this->controllerClass, 0, strlen($this->controllerClass) - 10)) . "Controller";
         $this->searchModelClass = Inflector::singularize($this->searchModelClass);
     }
     $testFiles = [];
     $baseControllerFile = Yii::getAlias('@' . str_replace('\\', '/', ltrim($this->controllerClass, '\\')) . '.php');
     $baseControllerFile = StringHelper::dirname($baseControllerFile) . '/base/' . StringHelper::basename($baseControllerFile);
     $files[] = new CodeFile($baseControllerFile, $this->render('controller.php'));
     $params['controllerClassName'] = \yii\helpers\StringHelper::basename($this->controllerClass);
     $controllerFile = Yii::getAlias('@' . str_replace('\\', '/', ltrim($this->controllerClass, '\\')) . '.php');
     if ($this->generateControllerClass || !is_file($controllerFile)) {
         $files[] = new CodeFile($controllerFile, $this->render('controller-extended.php', $params));
     }
     $restControllerFile = Yii::getAlias('@' . str_replace('\\', '/', ltrim($this->controllerClass, '\\')) . '.php');
     if ($this->generateControllerClass || !is_file($restControllerFile)) {
         $restControllerFile = StringHelper::dirname($restControllerFile) . '/api/' . StringHelper::basename($baseControllerFile);
         $files[] = new CodeFile($restControllerFile, $this->render('controller-rest.php', $params));
     }
     if (!empty($this->searchModelClass)) {
         $searchModel = Yii::getAlias('@' . str_replace('\\', '/', ltrim($this->searchModelClass, '\\') . '.php'));
         $files[] = new CodeFile($searchModel, $this->render('search.php'));
     }
     $viewPath = $this->getViewPath();
     $templatePath = $this->getTemplatePath() . '/views';
     foreach (scandir($templatePath) as $file) {
         if (empty($this->searchModelClass) && $file === '_search.php') {
             continue;
         }
         if (is_file($templatePath . '/' . $file) && pathinfo($file, PATHINFO_EXTENSION) === 'php') {
             $files[] = new CodeFile("{$viewPath}/{$file}", $this->render("views/{$file}"));
         }
     }
     if ($this->generateTests) {
         $tg = new TestGenerator();
         $tg->template = 'default';
         $tg->modelClass = $this->modelClass;
         $tg->controllerClass = $this->controllerClass;
         $tg->searchModelClass = $this->searchModelClass;
         $tg->ns = $this->testsPath;
         $testFiles = $tg->generate();
     }
     return array_merge($files, $testFiles);
 }