-
Notifications
You must be signed in to change notification settings - Fork 3
Add model generator tests #70
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
Merged
Changes from 21 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
8c7116b
test: add model generation test.
8dc9b15
Merge branch 'master' into 49-add-model-generator-tests
t0xas 08c0965
Merge branch '49-add-migration-generator-tests' into 49-add-model-gen…
t0xas d2e27c0
chore: add migration generator tests
t0xas ba8a9f0
Merge remote-tracking branch 'origin/49-add-migration-generator-tests…
pirs1337 fe2e1f3
feat: fix tests
pirs1337 45de444
Merge remote-tracking branch 'origin/49-add-migration-generator-tests…
pirs1337 6551491
Merge remote-tracking branch 'origin/49-add-migration-generator-tests…
pirs1337 523ad40
Merge remote-tracking branch 'origin/49-add-migration-generator-tests…
pirs1337 14abfa0
feat: change test
pirs1337 3ae2cdc
refactor: code
pirs1337 d185d9c
refactor: code
pirs1337 f316530
refactor: code
pirs1337 f871c5d
Merge branch 'master' into 49-add-model-generator-tests
DenTray 4b5bd34
fix: model blade
pirs1337 864e2c1
Merge remote-tracking branch 'origin/49-add-model-generator-tests' in…
pirs1337 391eb09
fix: tests
pirs1337 3ce0f96
Merge remote-tracking branch 'origin/master' into 49-add-model-genera…
pirs1337 e35ed3e
feat: add tests
pirs1337 db624fc
refactor: code
pirs1337 8442381
refactor: code
pirs1337 3bde64f
refactor: code
pirs1337 8e61ee5
Update tests/Support/Factory/FactoryMockTrait.php
DenTray 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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -10,22 +10,22 @@ | |||||
|
|
||||||
| class ModelGenerator extends EntityGenerator | ||||||
| { | ||||||
| CONST PLURAL_NUMBER_REQUIRED = [ | ||||||
| protected const array PLURAL_NUMBER_REQUIRED = [ | ||||||
| 'belongsToMany', | ||||||
| 'hasMany' | ||||||
| 'hasMany', | ||||||
| ]; | ||||||
|
|
||||||
| public function generate(): void | ||||||
| { | ||||||
| if ($this->classExists('models', $this->model)) { | ||||||
| $this->throwFailureException( | ||||||
| ClassAlreadyExistsException::class, | ||||||
| "Cannot create {$this->model} Model cause {$this->model} Model already exists.", | ||||||
| "Remove {$this->model} Model." | ||||||
| exceptionClass: ClassAlreadyExistsException::class, | ||||||
| failureMessage: "Cannot create {$this->model} Model cause {$this->model} Model already exists.", | ||||||
| recommendedMessage: "Remove {$this->model} Model.", | ||||||
| ); | ||||||
| } | ||||||
|
|
||||||
| if ($this->isStubExists('model') && ($this->isStubExists('relation') || empty($this->relations))) { | ||||||
| if ($this->isStubExists('model') && (collect($this->relations)->every(fn ($relation) => empty($relation)) || $this->isStubExists('relation', 'model'))) { | ||||||
|
||||||
| if ($this->isStubExists('model') && (collect($this->relations)->every(fn ($relation) => empty($relation)) || $this->isStubExists('relation', 'model'))) { | |
| if ($this->isStubExists('model') && (!$this->hasRelations() || $this->isStubExists('relation', 'model'))) { |
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 |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| public function {{$name}}() | ||
| { | ||
| return $this->{{$type}}({{$entity}}::class); | ||
| } | ||
| } |
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
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,143 @@ | ||
| <?php | ||
|
|
||
| namespace RonasIT\Support\Tests; | ||
|
|
||
| use RonasIT\Support\Events\SuccessCreateMessage; | ||
| use RonasIT\Support\Events\WarningEvent; | ||
| use RonasIT\Support\Exceptions\ClassAlreadyExistsException; | ||
| use RonasIT\Support\Exceptions\ClassNotExistsException; | ||
| use RonasIT\Support\Generators\ModelGenerator; | ||
| use RonasIT\Support\Tests\Support\Model\ModelMockTrait; | ||
|
|
||
| class ModelGeneratorTest extends TestCase | ||
| { | ||
| use ModelMockTrait; | ||
|
|
||
| public function testModelAlreadyExists() | ||
| { | ||
| $this->mockClass(ModelGenerator::class, [ | ||
| $this->classExistsMethodCall(['models', 'Post']), | ||
| ]); | ||
|
|
||
| $this->assertExceptionThrew( | ||
| className: ClassAlreadyExistsException::class, | ||
| message: 'Cannot create Post Model cause Post Model already exists. Remove Post Model.', | ||
| ); | ||
|
|
||
| app(ModelGenerator::class) | ||
| ->setModel('Post') | ||
| ->generate(); | ||
| } | ||
|
|
||
| public function testRelationModelMissing() | ||
| { | ||
| $this->assertExceptionThrew( | ||
| className: ClassNotExistsException::class, | ||
| message: "Cannot create Post Model cause relation model Comment does not exist. " | ||
| . "Create the Comment Model by himself or run command 'php artisan make:entity Comment --only-model'.", | ||
| ); | ||
|
|
||
| app(ModelGenerator::class) | ||
| ->setModel('Post') | ||
| ->setRelations([ | ||
| 'hasOne' => ['Comment'], | ||
| 'hasMany' => [], | ||
| 'belongsTo' => [], | ||
| 'belongsToMany' => [], | ||
| ]) | ||
| ->generate(); | ||
| } | ||
|
|
||
| public function testCreateModel() | ||
| { | ||
| $this->mockFilesystem(); | ||
|
|
||
| app(ModelGenerator::class) | ||
| ->setModel('Post') | ||
| ->setFields([ | ||
| 'integer-required' => ['media_id'], | ||
| 'boolean-required' => ['is_published'], | ||
| ]) | ||
| ->setRelations([ | ||
| 'hasOne' => ['Comment'], | ||
| 'hasMany' => ['User'], | ||
| 'belongsTo' => [], | ||
| 'belongsToMany' => [], | ||
| ]) | ||
| ->generate(); | ||
|
|
||
| $this->assertGeneratedFileEquals('new_model.php', 'app/Models/Post.php'); | ||
| $this->assertGeneratedFileEquals('comment_relation_model.php', 'app/Models/Comment.php'); | ||
| $this->assertGeneratedFileEquals('comment_relation_model.php', 'app/Models/User.php'); | ||
|
|
||
| $this->assertEventPushed( | ||
| className: SuccessCreateMessage::class, | ||
| message: 'Created a new Model: Post', | ||
| ); | ||
| } | ||
|
|
||
| public function testCreateModelStubNotExist() | ||
| { | ||
| $this->mockFilesystem(); | ||
|
|
||
| config(['entity-generator.stubs.model' => 'incorrect_stub']); | ||
|
|
||
| app(ModelGenerator::class) | ||
| ->setModel('Post') | ||
| ->setFields([]) | ||
| ->generate(); | ||
|
|
||
| $this->assertFileDoesNotExist('app/Models/Post.php'); | ||
| $this->assertFileDoesNotExist('app/Models/Comment.php'); | ||
| $this->assertFileDoesNotExist('app/Models/User.php'); | ||
|
|
||
| $this->assertEventPushed( | ||
| className: WarningEvent::class, | ||
| message: 'Generation of model has been skipped cause the view incorrect_stub from the config entity-generator.stubs.model is not exists. Please check that config has the correct view name value.', | ||
| ); | ||
| } | ||
|
|
||
| public function testCreateModelWithoutRelationsRelationStubNotExist() | ||
| { | ||
| $this->mockFilesystem(); | ||
|
|
||
| config(['entity-generator.stubs.relation' => 'incorrect_stub']); | ||
|
|
||
| app(ModelGenerator::class) | ||
| ->setModel('Post') | ||
| ->setFields([]) | ||
| ->generate(); | ||
|
|
||
| $this->assertGeneratedFileEquals('new_model_without_fields_and_relations.php', 'app/Models/Post.php'); | ||
|
|
||
| $this->assertEventPushed( | ||
| className: SuccessCreateMessage::class, | ||
| message: 'Created a new Model: Post', | ||
| ); | ||
| } | ||
|
|
||
| public function testCreateModelWithRelationsRelationStubNotExist() | ||
| { | ||
| $this->mockFilesystem(); | ||
|
|
||
| config(['entity-generator.stubs.relation' => 'incorrect_stub']); | ||
|
|
||
| app(ModelGenerator::class) | ||
| ->setModel('Post') | ||
| ->setFields([]) | ||
| ->setRelations([ | ||
| 'hasOne' => ['Comment'], | ||
| 'hasMany' => ['User'], | ||
| 'belongsTo' => [], | ||
| 'belongsToMany' => [], | ||
| ]) | ||
| ->generate(); | ||
|
|
||
| $this->assertFileDoesNotExist('new_model.php', 'app/Models/Post.php'); | ||
|
|
||
| $this->assertEventPushed( | ||
| className: WarningEvent::class, | ||
| message: 'Generation of model has been skipped cause the view incorrect_stub from the config entity-generator.stubs.relation is not exists. Please check that config has the correct view name value.', | ||
| ); | ||
| } | ||
| } |
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
Oops, something went wrong.
Oops, something went wrong.
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.