GeneratorsTest.php 3.41 KB
Newer Older
1 2 3
<?php
namespace yiiunit\extensions\gii;

4
use yii\gii\CodeFile;
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
use yii\gii\generators\controller\Generator as ControllerGenerator;
use yii\gii\generators\crud\Generator as CRUDGenerator;
use yii\gii\generators\extension\Generator as ExtensionGenerator;
use yii\gii\generators\form\Generator as FormGenerator;
use yii\gii\generators\model\Generator as ModelGenerator;
use yii\gii\generators\module\Generator as ModuleGenerator;

/**
 * GeneratorsTest checks that Gii generators aren't throwing any errors during generation
 */
class GeneratorsTest extends GiiTestCase
{
    public function testControllerGenerator()
    {
        $generator = new ControllerGenerator();
        $generator->template = 'default';
        $generator->controller = 'test';

        if ($generator->validate()) {
            $generator->generate();
        } else {
            print_r($generator->getErrors());
        }
    }

    public function testExtensionGenerator()
    {
        $generator = new ExtensionGenerator();
        $generator->template = 'default';
        $generator->vendorName = 'samdark';
        $generator->namespace = 'samdark\\';
        $generator->license = 'BSD';
        $generator->title = 'Sample extension';
        $generator->description = 'This is sample description.';
        $generator->authorName = 'Alexander Makarov';
        $generator->authorEmail = 'sam@rmcreative.ru';

        if ($generator->validate()) {
            $generator->generate();
        } else {
            print_r($generator->getErrors());
        }
    }

    public function testModelGenerator()
    {
        $generator = new ModelGenerator();
        $generator->template = 'default';
        $generator->tableName = 'profile';
        $generator->modelClass = 'Profile';

        if ($generator->validate()) {
57 58 59 60 61
            $files = $generator->generate();
            $modelCode = $files[0]->content;

            $this->assertTrue(strpos($modelCode, "'id' => 'ID'") !== false, "ID label should be there:\n" . $modelCode);
            $this->assertTrue(strpos($modelCode, "'description' => 'Description',") !== false, "Description label should be there:\n" . $modelCode);
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
        } else {
            print_r($generator->getErrors());
        }
    }

    public function testModuleGenerator()
    {
        $generator = new ModuleGenerator();
        $generator->template = 'default';
        $generator->moduleID = 'test';
        $generator->moduleClass = 'app\modules\test\Module';

        if ($generator->validate()) {
            $generator->generate();
        } else {
            print_r($generator->getErrors());
        }
    }


    public function testFormGenerator()
    {
        $generator = new FormGenerator();
        $generator->template = 'default';
        $generator->modelClass = 'yiiunit\extensions\gii\Profile';
        $generator->viewName = 'profile';
        $generator->viewPath = '@yiiunit/runtime';

        if ($generator->validate()) {
            $generator->generate();
        } else {
            print_r($generator->getErrors());
        }
    }

    public function testCRUDGenerator()
    {
        $generator = new CRUDGenerator();
        $generator->template = 'default';
        $generator->modelClass = 'yiiunit\extensions\gii\Profile';
        $generator->controllerClass = 'app\TestController';

        if ($generator->validate()) {
            $generator->generate();
        } else {
            print_r($generator->getErrors());
        }
    }
}