-
Notifications
You must be signed in to change notification settings - Fork 143
feat: add shield:model command
#558
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
Merged
datamweb
merged 7 commits into
codeigniter4:develop
from
paulbalandan:shield-model-spark
Jan 5, 2023
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
9eaf09b
feat: add new CLI command for make custom `UserModel`
datamweb 080299b
Remove unnecessary comments.
paulbalandan f798f25
Fix tests to be compatible with randomized testing
paulbalandan 4afdab0
Fix concepts.md
paulbalandan fbadd42
Use `UserModel` as test class name
paulbalandan 718dd4e
Make the class name input optional with default.
paulbalandan 7e5c4a2
Add conditional check when suffix option is given
paulbalandan 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 hidden or 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 hidden or 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,78 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace CodeIgniter\Shield\Commands\Generators; | ||
|
|
||
| use CodeIgniter\CLI\BaseCommand; | ||
| use CodeIgniter\CLI\CLI; | ||
| use CodeIgniter\CLI\GeneratorTrait; | ||
|
|
||
| /** | ||
| * Generates a custom user model file. | ||
| */ | ||
| class UserModelGenerator extends BaseCommand | ||
| { | ||
| use GeneratorTrait; | ||
|
|
||
| /** | ||
| * @var string | ||
| */ | ||
| protected $group = 'Shield'; | ||
|
|
||
| /** | ||
| * @var string | ||
| */ | ||
| protected $name = 'shield:model'; | ||
|
|
||
| /** | ||
| * @var string | ||
| */ | ||
| protected $description = 'Generate a new UserModel file.'; | ||
|
|
||
| /** | ||
| * @var string | ||
| */ | ||
| protected $usage = 'shield:model [<name>] [options]'; | ||
|
|
||
| /** | ||
| * @var array<string, string> | ||
| */ | ||
| protected $arguments = [ | ||
| 'name' => 'The model class name. If not provided, this will default to `UserModel`.', | ||
| ]; | ||
|
|
||
| /** | ||
| * @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 the 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->setHasClassName(false); | ||
|
|
||
| $class = $params[0] ?? CLI::getSegment(2) ?? 'UserModel'; | ||
|
|
||
| if (in_array(strtolower($class), ['shielduser', 'shieldusermodel'], true)) { | ||
kenjis marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| CLI::error('Cannot use `ShieldUserModel` as class name as this conflicts with the parent class.', 'light_gray', 'red'); | ||
|
|
||
| return; // @TODO when CI4 is at v4.3+, change this to `return 1;` to signify failing exit | ||
| } | ||
|
|
||
| $params[0] = $class; | ||
|
|
||
| $this->execute($params); | ||
| } | ||
| } | ||
This file contains hidden or 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,19 @@ | ||
| <@php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace {namespace}; | ||
|
|
||
| use CodeIgniter\Shield\Models\UserModel as ShieldUserModel; | ||
|
|
||
| class {class} extends ShieldUserModel | ||
| { | ||
| protected function initialize(): void | ||
| { | ||
| $this->allowedFields = [ | ||
| ...$this->allowedFields, | ||
|
|
||
| // 'first_name', | ||
| ]; | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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,142 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Tests\Commands; | ||
|
|
||
| use CodeIgniter\Test\CIUnitTestCase; | ||
| use CodeIgniter\Test\Filters\CITestStreamFilter; | ||
|
|
||
| /** | ||
| * @internal | ||
| */ | ||
| final class UserModelGeneratorTest extends CIUnitTestCase | ||
| { | ||
| private $streamFilter; | ||
|
|
||
| protected function setUp(): void | ||
| { | ||
| parent::setUp(); | ||
|
|
||
| CITestStreamFilter::$buffer = ''; | ||
|
|
||
| $this->streamFilter = stream_filter_append(STDOUT, 'CITestStreamFilter'); | ||
| $this->streamFilter = stream_filter_append(STDERR, 'CITestStreamFilter'); | ||
|
|
||
| if (is_file(HOMEPATH . 'src/Models/UserModel.php')) { | ||
| copy(HOMEPATH . 'src/Models/UserModel.php', HOMEPATH . 'src/Models/UserModel.php.bak'); | ||
| } | ||
|
|
||
| $this->deleteTestFiles(); | ||
| } | ||
|
|
||
| protected function tearDown(): void | ||
| { | ||
| parent::tearDown(); | ||
|
|
||
| stream_filter_remove($this->streamFilter); | ||
| $this->deleteTestFiles(); | ||
|
|
||
| if (is_file(HOMEPATH . 'src/Models/UserModel.php.bak')) { | ||
| copy(HOMEPATH . 'src/Models/UserModel.php.bak', HOMEPATH . 'src/Models/UserModel.php'); | ||
| unlink(HOMEPATH . 'src/Models/UserModel.php.bak'); | ||
| } | ||
| } | ||
|
|
||
| private function deleteTestFiles(): void | ||
| { | ||
| $possibleFiles = [ | ||
| APPPATH . 'Models/UserModel.php', | ||
| HOMEPATH . 'src/Models/UserModel.php', | ||
| ]; | ||
|
|
||
| foreach ($possibleFiles as $file) { | ||
| clearstatcache(true, $file); | ||
|
|
||
| if (is_file($file)) { | ||
| unlink($file); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private function getFileContents(string $filepath): string | ||
| { | ||
| return (string) @file_get_contents($filepath); | ||
| } | ||
|
|
||
| public function testGenerateUserModel(): void | ||
| { | ||
| command('shield:model UserModel'); | ||
|
|
||
| $filepath = APPPATH . 'Models/UserModel.php'; | ||
| $this->assertStringContainsString('File created: ', CITestStreamFilter::$buffer); | ||
| $this->assertFileExists($filepath); | ||
|
|
||
| $contents = $this->getFileContents($filepath); | ||
| $this->assertStringContainsString('namespace App\Models;', $contents); | ||
| $this->assertStringContainsString('class UserModel extends ShieldUserModel', $contents); | ||
| $this->assertStringContainsString('use CodeIgniter\Shield\Models\UserModel as ShieldUserModel;', $contents); | ||
| $this->assertStringContainsString('protected function initialize(): void', $contents); | ||
| } | ||
|
|
||
| public function testGenerateUserModelCustomNamespace(): void | ||
| { | ||
| command('shield:model UserModel --namespace CodeIgniter\\\\Shield'); | ||
|
|
||
| $filepath = HOMEPATH . 'src/Models/UserModel.php'; | ||
| $this->assertStringContainsString('File created: ', CITestStreamFilter::$buffer); | ||
| $this->assertFileExists($filepath); | ||
|
|
||
| $contents = $this->getFileContents($filepath); | ||
| $this->assertStringContainsString('namespace CodeIgniter\Shield\Models;', $contents); | ||
| $this->assertStringContainsString('class UserModel extends ShieldUserModel', $contents); | ||
| $this->assertStringContainsString('use CodeIgniter\Shield\Models\UserModel as ShieldUserModel;', $contents); | ||
| $this->assertStringContainsString('protected function initialize(): void', $contents); | ||
| } | ||
|
|
||
| public function testGenerateUserModelWithForce(): void | ||
| { | ||
| command('shield:model UserModel'); | ||
| command('shield:model UserModel --force'); | ||
|
|
||
| $this->assertStringContainsString('File overwritten: ', CITestStreamFilter::$buffer); | ||
| $this->assertFileExists(APPPATH . 'Models/UserModel.php'); | ||
| } | ||
|
|
||
| public function testGenerateUserModelWithSuffix(): void | ||
| { | ||
| command('shield:model User --suffix'); | ||
|
|
||
| $this->assertStringContainsString('File created: ', CITestStreamFilter::$buffer); | ||
|
|
||
| $filepath = APPPATH . 'Models/UserModel.php'; | ||
| $this->assertFileExists($filepath); | ||
| $this->assertStringContainsString('class UserModel extends ShieldUserModel', $this->getFileContents($filepath)); | ||
| } | ||
|
|
||
| public function testGenerateUserModelWithoutClassNameInput(): void | ||
| { | ||
| command('shield:model'); | ||
|
|
||
| $this->assertStringContainsString('File created: ', CITestStreamFilter::$buffer); | ||
|
|
||
| $filepath = APPPATH . 'Models/UserModel.php'; | ||
| $this->assertFileExists($filepath); | ||
| $this->assertStringContainsString('class UserModel extends ShieldUserModel', $this->getFileContents($filepath)); | ||
| } | ||
|
|
||
| public function testGenerateUserCannotAcceptShieldUserModelAsInput(): void | ||
| { | ||
| command('shield:model ShieldUserModel'); | ||
|
|
||
| $this->assertStringContainsString('Cannot use `ShieldUserModel` as class name as this conflicts with the parent class.', CITestStreamFilter::$buffer); | ||
| $this->assertFileDoesNotExist(APPPATH . 'Models/UserModel.php'); | ||
|
|
||
| CITestStreamFilter::$buffer = ''; | ||
|
|
||
| command('shield:model ShieldUser --suffix'); | ||
|
|
||
| $this->assertStringContainsString('Cannot use `ShieldUserModel` as class name as this conflicts with the parent class.', CITestStreamFilter::$buffer); | ||
| $this->assertFileDoesNotExist(APPPATH . 'Models/UserModel.php'); | ||
| } | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.