Skip to content

Commit

Permalink
Fix entity name generation when bundled in model
Browse files Browse the repository at this point in the history
  • Loading branch information
paulbalandan authored Sep 5, 2021
1 parent 1444142 commit 8207b2f
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 51 deletions.
22 changes: 14 additions & 8 deletions system/Commands/Generators/ModelGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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]);
}
}
6 changes: 1 addition & 5 deletions system/Commands/Generators/Views/entity.tpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
}
46 changes: 23 additions & 23 deletions system/Commands/Generators/Views/model.tpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand All @@ -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 = [];
}
57 changes: 42 additions & 15 deletions tests/system/Commands/ModelGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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 '';
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -76,57 +79,81 @@ 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);
}
}

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));
Expand Down

0 comments on commit 8207b2f

Please sign in to comment.