PageRenderTime 53ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/protected/vendor/schmunk42/yii2-giiant/src/generators/test/Generator.php

https://gitlab.com/I-NOZex/quiz
PHP | 164 lines | 104 code | 16 blank | 44 comment | 5 complexity | cb9b88b78f84560c6bde1eeb3c45355c MD5 | raw file
  1. <?php
  2. namespace schmunk42\giiant\generators\test;
  3. use Yii;
  4. use yii\gii\CodeFile;
  5. use yii\db\Schema;
  6. /**
  7. * This generator generates unit tests for crud operations
  8. *
  9. * @author Github: gradosevic
  10. */
  11. class Generator extends \schmunk42\giiant\generators\model\Generator
  12. {
  13. /**
  14. * @var string
  15. */
  16. public $ns = 'app\tests\codeception\unit';
  17. /**
  18. * @var string Codeception's root path
  19. *
  20. */
  21. public $codeceptionPath = '/tests/codeception/';
  22. /**
  23. * @var string Controller's class name
  24. */
  25. public $controllerClass = "";
  26. /**
  27. * @var string Model's class name
  28. */
  29. public $modelClass = "";
  30. public $modelNs = "";
  31. /**
  32. * @var string Search model's class name
  33. */
  34. public $searchModelClass = "";
  35. /**
  36. * @inheritdoc
  37. */
  38. public function getName()
  39. {
  40. return 'Giiant Test';
  41. }
  42. /**
  43. * @inheritdoc
  44. */
  45. public function getDescription()
  46. {
  47. return 'This generator generates unit tests for specified model';
  48. }
  49. /**
  50. * @inheritdoc
  51. */
  52. public function requiredTemplates()
  53. {
  54. return ['unit.php'];
  55. }
  56. /**
  57. * @param $table Table schema
  58. * @return array Attributes containing all required model's information for test generator
  59. */
  60. public function generateAttributes($table){
  61. $labels = $this->generateLabels($table);
  62. $attributes = [];
  63. foreach ($table->columns as $column) {
  64. $label = $column->name;
  65. if(isset($labels[$column->name])){
  66. $label = $labels[$column->name];
  67. }
  68. $attribute = [];
  69. $attribute['name'] = $column->name;
  70. $attribute['null'] = $column->allowNull;
  71. $attribute['size'] = $column->size;
  72. $attribute['primary'] = $column->isPrimaryKey;
  73. $attribute['label'] = $label;
  74. if ($column->autoIncrement) {
  75. $attribute['autoincrement'] = 'true';
  76. }
  77. if (!$column->allowNull && $column->defaultValue === null) {
  78. $attribute['required'] = 'true';
  79. }
  80. switch ($column->type) {
  81. case Schema::TYPE_SMALLINT:
  82. case Schema::TYPE_INTEGER:
  83. case Schema::TYPE_BIGINT:
  84. $attribute['type'] = 'integer';
  85. break;
  86. case Schema::TYPE_BOOLEAN:
  87. $attribute['type'] = 'boolean';
  88. break;
  89. case Schema::TYPE_FLOAT:
  90. case 'double': // Schema::TYPE_DOUBLE, which is available since Yii 2.0.3
  91. case Schema::TYPE_DECIMAL:
  92. case Schema::TYPE_MONEY:
  93. $attribute['type'] = 'number';
  94. break;
  95. case Schema::TYPE_DATE:
  96. $attribute['type'] = 'date';
  97. case Schema::TYPE_TIME:
  98. $attribute['type'] = 'time';
  99. case Schema::TYPE_DATETIME:
  100. $attribute['type'] = 'datetime';
  101. case Schema::TYPE_TIMESTAMP:
  102. $attribute['type'] = 'timestamp';
  103. break;
  104. default: // strings
  105. $attribute['type'] = 'string';
  106. }
  107. $attributes[] = $attribute;
  108. }
  109. return $attributes;
  110. }
  111. /**
  112. * @inheritdoc
  113. */
  114. public function generate()
  115. {
  116. $files = [];
  117. // $relations = $this->generateRelations();
  118. $db = $this->getDbConnection();
  119. $class = $this->modelNs.$this->modelClass;
  120. $classTableNameMethod = "tableName";
  121. $this->tableName = $class::$classTableNameMethod();
  122. //TODO: Add unit tests for search model
  123. //if($this->searchModelClass !=="")
  124. //{
  125. //}
  126. foreach ($this->getTableNames() as $tableName) {
  127. $className = $this->generateClassName($tableName);
  128. $tableSchema = $db->getTableSchema($tableName);
  129. $params = [
  130. 'tableName' => $tableName,
  131. 'className' => $className,
  132. 'modelClass' => $this->modelClass,
  133. 'controllerClass'=> $this->controllerClass,
  134. 'labels' => $this->generateLabels($tableSchema),
  135. 'rules' => $this->generateRules($tableSchema),
  136. 'attributes' => $this->generateAttributes($tableSchema),
  137. //TODO: Add unit tests for relations
  138. //'relations' => isset($relations[$tableName]) ? $relations[$tableName] : [],
  139. 'ns' => $this->ns,
  140. ];
  141. $files[] = new CodeFile(
  142. Yii::getAlias('@app/..'. $this->codeceptionPath . str_replace('\\', '/', $this->ns)) . '/' . $className . $this->baseClassSuffix . 'UnitTest.php',
  143. $this->render('unit.php', $params)
  144. );
  145. }
  146. return $files;
  147. }
  148. }