-
Notifications
You must be signed in to change notification settings - Fork 133
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add shield:model
command for make custom UserModel
#491
Closed
Closed
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
6860277
feat: add new CLI command for make custom `UserModel`
datamweb 288f8cb
feat: add tpl for make custom `UserModel`
datamweb 65241d7
fix: rename command name to `shield:model`
datamweb 92ba9d4
tests: add phpunit test
datamweb 469f8b9
docs: add description for command `shield:model`
datamweb 223fad5
docs: correct description
datamweb 8c35a48
docs: use `::$` instead of `->`.
datamweb c142856
feat: use array unpacking
datamweb 49cd606
Update tests/Commands/UserModelGeneratorTest.php
datamweb baf6a26
Update src/Commands/Generators/UserModelGenerator.php
datamweb 105c101
Update src/Commands/Generators/UserModelGenerator.php
datamweb cd15b5c
fix: remove unnecessary method
datamweb 4e6b06d
docs: remove unnecessary details
datamweb de0c36e
fix: remove unnecessary line
datamweb ee29840
updat: use `class` for suport IDE
datamweb 3047309
fix: user `tearDown()` for delete file
datamweb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CodeIgniter\Shield\Commands\Generators; | ||
|
||
use CodeIgniter\CLI\BaseCommand; | ||
use CodeIgniter\CLI\GeneratorTrait; | ||
|
||
/** | ||
* Generates a skeleton command file. | ||
*/ | ||
class UserModelGenerator extends BaseCommand | ||
{ | ||
use GeneratorTrait; | ||
|
||
/** | ||
* The Command's Group | ||
* | ||
* @var string | ||
*/ | ||
protected $group = 'Shield'; | ||
|
||
/** | ||
* The Command's Name | ||
* | ||
* @var string | ||
*/ | ||
protected $name = 'shield:model'; | ||
|
||
/** | ||
* The Command's Description | ||
* | ||
* @var string | ||
*/ | ||
protected $description = 'Generates a new UserModel file.'; | ||
|
||
/** | ||
* The Command's Usage | ||
* | ||
* @var string | ||
*/ | ||
protected $usage = 'shield:model <name> [options]'; | ||
|
||
/** | ||
* The Command's Arguments | ||
* | ||
* @var array<string, string> | ||
*/ | ||
protected $arguments = [ | ||
'name' => 'The model class name.', | ||
]; | ||
|
||
/** | ||
* The Command's Options | ||
* | ||
* @var array<string, string> | ||
*/ | ||
protected $options = [ | ||
'--namespace' => 'Set root namespace. Default: "APP_NAMESPACE".', | ||
'--suffix' => 'Append the component title to the class name (e.g. User => UserModel).', | ||
'--force' => 'Force overwrite existing file.', | ||
]; | ||
|
||
/** | ||
* Actually execute a command. | ||
*/ | ||
public function run(array $params): void | ||
{ | ||
$this->component = 'Model'; | ||
$this->directory = 'Models'; | ||
$this->template = 'usermodel.tpl.php'; | ||
|
||
$this->classNameLang = 'CLI.generator.className.model'; | ||
$this->execute($params); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<@php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace {namespace}; | ||
|
||
use CodeIgniter\Shield\Models\UserModel; | ||
|
||
class {class} extends UserModel | ||
{ | ||
protected function initialize(): void | ||
{ | ||
$this->allowedFields = [ | ||
...$this->allowedFields, | ||
// Add here your custom fields | ||
// 'first_name', | ||
]; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\Commands; | ||
|
||
use CodeIgniter\Test\CIUnitTestCase; | ||
use CodeIgniter\Test\Filters\CITestStreamFilter; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class UserModelGeneratorTest extends CIUnitTestCase | ||
{ | ||
protected $streamFilter; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
CITestStreamFilter::$buffer = ''; | ||
$this->streamFilter = stream_filter_append(STDOUT, 'CITestStreamFilter'); | ||
$this->streamFilter = stream_filter_append(STDERR, 'CITestStreamFilter'); | ||
} | ||
|
||
protected function tearDown(): void | ||
{ | ||
parent::tearDown(); | ||
|
||
stream_filter_remove($this->streamFilter); | ||
$result = str_replace(["\033[0;32m", "\033[0m", "\n"], '', CITestStreamFilter::$buffer); | ||
|
||
$filepath = str_replace('APPPATH' . DIRECTORY_SEPARATOR, APPPATH, trim(substr($result, 14))); | ||
if (is_file($filepath)) { | ||
unlink($filepath); | ||
} | ||
} | ||
|
||
protected function getFileContents(string $filepath): string | ||
{ | ||
if (! file_exists($filepath)) { | ||
return ''; | ||
} | ||
|
||
return file_get_contents($filepath) ?: ''; | ||
} | ||
|
||
public function testGenerateUserModel(): void | ||
{ | ||
command('shield:model MyUserModel'); | ||
$filepath = APPPATH . 'Models/MyUserModel.php'; | ||
|
||
$this->assertStringContainsString('File created: ', CITestStreamFilter::$buffer); | ||
$this->assertFileExists($filepath); | ||
|
||
$this->assertStringContainsString('namespace App\Models;', $this->getFileContents($filepath)); | ||
$this->assertStringContainsString('class MyUserModel extends UserModel', $this->getFileContents($filepath)); | ||
$this->assertStringContainsString('use CodeIgniter\Shield\Models\UserModel;', $this->getFileContents($filepath)); | ||
$this->assertStringContainsString('protected function initialize(): void', $this->getFileContents($filepath)); | ||
} | ||
|
||
public function testGenerateUserModelCustomNamespace(): void | ||
{ | ||
command('shield:model MyUserModel --namespace CodeIgniter\\\\Shield'); | ||
$filepath = HOMEPATH . 'src/Models/MyUserModel.php'; | ||
|
||
$this->assertStringContainsString('File created: ', CITestStreamFilter::$buffer); | ||
$this->assertFileExists($filepath); | ||
|
||
$this->assertStringContainsString('namespace CodeIgniter\Shield\Models;', $this->getFileContents($filepath)); | ||
$this->assertStringContainsString('class MyUserModel extends UserModel', $this->getFileContents($filepath)); | ||
$this->assertStringContainsString('use CodeIgniter\Shield\Models\UserModel;', $this->getFileContents($filepath)); | ||
$this->assertStringContainsString('protected function initialize(): void', $this->getFileContents($filepath)); | ||
|
||
if (is_file($filepath)) { | ||
unlink($filepath); | ||
} | ||
} | ||
|
||
public function testGenerateUserModelWithForce(): void | ||
{ | ||
command('shield:model MyUserModel'); | ||
|
||
command('shield:model MyUserModel --force'); | ||
$this->assertStringContainsString('File overwritten: ', CITestStreamFilter::$buffer); | ||
|
||
$filepath = APPPATH . 'Models/MyUserModel.php'; | ||
$this->assertFileExists($filepath); | ||
} | ||
|
||
public function testGenerateUserModelWithSuffix(): void | ||
{ | ||
command('shield:model MyUser --suffix'); | ||
$filepath = APPPATH . 'Models/MyUserModel.php'; | ||
|
||
$this->assertStringContainsString('File created: ', CITestStreamFilter::$buffer); | ||
$this->assertFileExists($filepath); | ||
$this->assertStringContainsString('class MyUserModel extends UserModel', $this->getFileContents($filepath)); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Change this to a
$this->assertFileExist
test. Deleting the file will be made in tearDown.