From 8207b2f3f2bdddbb9e99813053303f210ad68d5c Mon Sep 17 00:00:00 2001 From: "John Paul E. Balandan, CPA" <51850998+paulbalandan@users.noreply.github.com> Date: Sun, 5 Sep 2021 20:05:46 +0800 Subject: [PATCH] Fix entity name generation when bundled in model --- system/Commands/Generators/ModelGenerator.php | 22 ++++--- .../Commands/Generators/Views/entity.tpl.php | 6 +- .../Commands/Generators/Views/model.tpl.php | 46 +++++++-------- tests/system/Commands/ModelGeneratorTest.php | 57 ++++++++++++++----- 4 files changed, 80 insertions(+), 51 deletions(-) diff --git a/system/Commands/Generators/ModelGenerator.php b/system/Commands/Generators/ModelGenerator.php index 1ad8a75e1f8c..b4b7ffcda2ca 100644 --- a/system/Commands/Generators/ModelGenerator.php +++ b/system/Commands/Generators/ModelGenerator.php @@ -92,14 +92,17 @@ public function run(array $params) protected function prepare(string $class): string { $table = $this->getOption('table'); - $DBGroup = $this->getOption('dbgroup'); + $dbGroup = $this->getOption('dbgroup'); $return = $this->getOption('return'); - $baseClass = strtolower(str_replace(trim(implode('\\', array_slice(explode('\\', $class), 0, -1)), '\\') . '\\', '', $class)); - $baseClass = strpos($baseClass, 'model') ? str_replace('model', '', $baseClass) : $baseClass; + $baseClass = class_basename($class); - $table = is_string($table) ? $table : plural($baseClass); - $DBGroup = is_string($DBGroup) ? $DBGroup : 'default'; + if (preg_match('/^(\S+)Model$/i', $baseClass, $match) === 1) { + $baseClass = $match[1]; + } + + $table = is_string($table) ? $table : plural(strtolower($baseClass)); + $dbGroup = is_string($dbGroup) ? $dbGroup : 'default'; $return = is_string($return) ? $return : 'array'; if (! in_array($return, ['array', 'object', 'entity'], true)) { @@ -112,17 +115,20 @@ protected function prepare(string $class): string if ($return === 'entity') { $return = str_replace('Models', 'Entities', $class); - if ($pos = strpos($return, 'Model')) { - $return = substr($return, 0, $pos); + if (preg_match('/^(\S+)Model$/i', $return, $match) === 1) { + $return = $match[1]; if ($this->getOption('suffix')) { $return .= 'Entity'; } } + $return = '\\' . trim($return, '\\') . '::class'; $this->call('make:entity', array_merge([$baseClass], $this->params)); + } else { + $return = "'{$return}'"; } - return $this->parseTemplate($class, ['{table}', '{DBGroup}', '{return}'], [$table, $DBGroup, $return]); + return $this->parseTemplate($class, ['{table}', '{dbGroup}', '{return}'], [$table, $dbGroup, $return]); } } diff --git a/system/Commands/Generators/Views/entity.tpl.php b/system/Commands/Generators/Views/entity.tpl.php index 6623e2f14bbb..c74c776f4ad3 100644 --- a/system/Commands/Generators/Views/entity.tpl.php +++ b/system/Commands/Generators/Views/entity.tpl.php @@ -7,10 +7,6 @@ class {class} extends Entity { protected $datamap = []; - protected $dates = [ - 'created_at', - 'updated_at', - 'deleted_at', - ]; + protected $dates = ['created_at', 'updated_at', 'deleted_at']; protected $casts = []; } diff --git a/system/Commands/Generators/Views/model.tpl.php b/system/Commands/Generators/Views/model.tpl.php index 088ecb7e1161..5fc5ed9ba428 100644 --- a/system/Commands/Generators/Views/model.tpl.php +++ b/system/Commands/Generators/Views/model.tpl.php @@ -6,22 +6,22 @@ class {class} extends Model { - protected $DBGroup = '{DBGroup}'; - protected $table = '{table}'; - protected $primaryKey = 'id'; - protected $useAutoIncrement = true; - protected $insertID = 0; - protected $returnType = '{return}'; - protected $useSoftDeletes = false; - protected $protectFields = true; - protected $allowedFields = []; + protected $DBGroup = '{dbGroup}'; + protected $table = '{table}'; + protected $primaryKey = 'id'; + protected $useAutoIncrement = true; + protected $insertID = 0; + protected $returnType = {return}; + protected $useSoftDeletes = false; + protected $protectFields = true; + protected $allowedFields = []; // Dates - protected $useTimestamps = false; - protected $dateFormat = 'datetime'; - protected $createdField = 'created_at'; - protected $updatedField = 'updated_at'; - protected $deletedField = 'deleted_at'; + protected $useTimestamps = false; + protected $dateFormat = 'datetime'; + protected $createdField = 'created_at'; + protected $updatedField = 'updated_at'; + protected $deletedField = 'deleted_at'; // Validation protected $validationRules = []; @@ -30,13 +30,13 @@ class {class} extends Model protected $cleanValidationRules = true; // Callbacks - protected $allowCallbacks = true; - protected $beforeInsert = []; - protected $afterInsert = []; - protected $beforeUpdate = []; - protected $afterUpdate = []; - protected $beforeFind = []; - protected $afterFind = []; - protected $beforeDelete = []; - protected $afterDelete = []; + protected $allowCallbacks = true; + protected $beforeInsert = []; + protected $afterInsert = []; + protected $beforeUpdate = []; + protected $afterUpdate = []; + protected $beforeFind = []; + protected $afterFind = []; + protected $beforeDelete = []; + protected $afterDelete = []; } diff --git a/tests/system/Commands/ModelGeneratorTest.php b/tests/system/Commands/ModelGeneratorTest.php index 68e001103fb5..2d00460c53ee 100644 --- a/tests/system/Commands/ModelGeneratorTest.php +++ b/tests/system/Commands/ModelGeneratorTest.php @@ -19,10 +19,11 @@ */ final class ModelGeneratorTest extends CIUnitTestCase { - protected $streamFilter; + private $streamFilter; protected function setUp(): void { + parent::setUp(); CITestStreamFilter::$buffer = ''; $this->streamFilter = stream_filter_append(STDOUT, 'CITestStreamFilter'); @@ -31,16 +32,18 @@ protected function setUp(): void protected function tearDown(): void { + parent::tearDown(); stream_filter_remove($this->streamFilter); $result = str_replace(["\033[0;32m", "\033[0m", "\n"], '', CITestStreamFilter::$buffer); $file = str_replace('APPPATH' . DIRECTORY_SEPARATOR, APPPATH, trim(substr($result, 14))); + if (is_file($file)) { unlink($file); } } - protected function getFileContent(string $filepath): string + private function getFileContent(string $filepath): string { if (! is_file($filepath)) { return ''; @@ -51,14 +54,14 @@ protected function getFileContent(string $filepath): string public function testGenerateModel() { - command('make:model user -table users'); + command('make:model user --table users'); $this->assertStringContainsString('File created: ', CITestStreamFilter::$buffer); $file = APPPATH . 'Models/User.php'; $this->assertFileExists($file); $this->assertStringContainsString('extends Model', $this->getFileContent($file)); - $this->assertStringContainsString('protected $table = \'users\';', $this->getFileContent($file)); - $this->assertStringContainsString('protected $DBGroup = \'default\';', $this->getFileContent($file)); - $this->assertStringContainsString('protected $returnType = \'array\';', $this->getFileContent($file)); + $this->assertStringContainsString('protected $table = \'users\';', $this->getFileContent($file)); + $this->assertStringContainsString('protected $DBGroup = \'default\';', $this->getFileContent($file)); + $this->assertStringContainsString('protected $returnType = \'array\';', $this->getFileContent($file)); } public function testGenerateModelWithOptionTable() @@ -67,7 +70,7 @@ public function testGenerateModelWithOptionTable() $this->assertStringContainsString('File created: ', CITestStreamFilter::$buffer); $file = APPPATH . 'Models/Cars.php'; $this->assertFileExists($file); - $this->assertStringContainsString('protected $table = \'utilisateur\';', $this->getFileContent($file)); + $this->assertStringContainsString('protected $table = \'utilisateur\';', $this->getFileContent($file)); } public function testGenerateModelWithOptionDBGroup() @@ -76,43 +79,48 @@ public function testGenerateModelWithOptionDBGroup() $this->assertStringContainsString('File created: ', CITestStreamFilter::$buffer); $file = APPPATH . 'Models/User.php'; $this->assertFileExists($file); - $this->assertStringContainsString('protected $DBGroup = \'testing\';', $this->getFileContent($file)); + $this->assertStringContainsString('protected $DBGroup = \'testing\';', $this->getFileContent($file)); } public function testGenerateModelWithOptionReturnArray() { - command('make:model user -return array'); + command('make:model user --return array'); $this->assertStringContainsString('File created: ', CITestStreamFilter::$buffer); $file = APPPATH . 'Models/User.php'; $this->assertFileExists($file); - $this->assertStringContainsString('protected $returnType = \'array\';', $this->getFileContent($file)); + $this->assertStringContainsString('protected $returnType = \'array\';', $this->getFileContent($file)); } public function testGenerateModelWithOptionReturnObject() { - command('make:model user -return object'); + command('make:model user --return object'); $this->assertStringContainsString('File created: ', CITestStreamFilter::$buffer); $file = APPPATH . 'Models/User.php'; $this->assertFileExists($file); - $this->assertStringContainsString('protected $returnType = \'object\';', $this->getFileContent($file)); + $this->assertStringContainsString('protected $returnType = \'object\';', $this->getFileContent($file)); } public function testGenerateModelWithOptionReturnEntity() { - command('make:model user -return entity'); + command('make:model user --return entity'); $this->assertStringContainsString('File created: ', CITestStreamFilter::$buffer); + $file = APPPATH . 'Models/User.php'; $this->assertFileExists($file); - $this->assertStringContainsString('protected $returnType = \'App\Entities\User\';', $this->getFileContent($file)); + $this->assertStringContainsString('protected $returnType = \App\Entities\User::class;', $this->getFileContent($file)); + if (is_file($file)) { unlink($file); } + $file = APPPATH . 'Entities/User.php'; $this->assertFileExists($file); $dir = dirname($file); + if (is_file($file)) { unlink($file); } + if (is_dir($dir)) { rmdir($dir); } @@ -120,13 +128,32 @@ public function testGenerateModelWithOptionReturnEntity() public function testGenerateModelWithOptionSuffix() { - command('make:model user -suffix -return entity'); + command('make:model user --suffix --return entity'); $model = APPPATH . 'Models/UserModel.php'; $entity = APPPATH . 'Entities/UserEntity.php'; $this->assertFileExists($model); $this->assertFileExists($entity); + + unlink($model); + unlink($entity); + rmdir(dirname($entity)); + } + + /** + * @see https://github.com/codeigniter4/CodeIgniter4/issues/5050 + */ + public function testGenerateModelWithSuffixAndMixedPascalCasedName() + { + command('make:model MyTable --suffix --return entity'); + + $model = APPPATH . 'Models/MyTableModel.php'; + $entity = APPPATH . 'Entities/MyTableEntity.php'; + + $this->assertFileExists($model); + $this->assertFileExists($entity); + unlink($model); unlink($entity); rmdir(dirname($entity));