-
Notifications
You must be signed in to change notification settings - Fork 3
refactor: use resource in generate only nova tests #163
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
Closed
RGO230
wants to merge
19
commits into
master
from
140-use-NovaResource-class-in-NovaTestGenerator-instead-of-Model
Closed
Changes from 9 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
b431fb6
refactor: use resource in generate only nova tests
RGO230 563f2c2
fix: tests
RGO230 8a19c3a
fix:tests
RGO230 33ca7bc
fix:Tests
RGO230 516473b
fix:tests
RGO230 bc9b17e
Merge branch 'master' into 140-use-NovaResource-class-in-NovaTestGene…
RGO230 b5d870d
refactor: change set resource from argument ot option
RGO230 1f250a2
style: fix
RGO230 5c49cec
refactor: remove useless
RGO230 9a92f95
Merge branch 'master' into 140-use-NovaResource-class-in-NovaTestGene…
RGO230 a66d9ce
fix:tests
RGO230 c45d415
refactor: check only nova resources
RGO230 0fa88db
refactor: change isNovaResourceExists method
RGO230 47aed4f
fix: remove useless
RGO230 bf4bfc9
style: import generator
RGO230 fd12be4
chore:update readme
RGO230 a6a2747
refactor:use common resources
RGO230 f3fa391
style:fix
RGO230 d0cc813
refactor: remove useless from generator
RGO230 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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -8,27 +8,39 @@ | |||||
| use RonasIT\Support\Events\SuccessCreateMessage; | ||||||
| use RonasIT\Support\Exceptions\ClassAlreadyExistsException; | ||||||
| use RonasIT\Support\Exceptions\ClassNotExistsException; | ||||||
| use RecursiveIteratorIterator; | ||||||
| use RecursiveDirectoryIterator; | ||||||
|
|
||||||
| class NovaTestGenerator extends AbstractTestsGenerator | ||||||
| { | ||||||
| protected $novaModelName; | ||||||
| protected string $novaResourceName; | ||||||
RGO230 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
|
|
||||||
| protected ?string $fullNovaResourcePath = null; | ||||||
|
|
||||||
| public function generate(): void | ||||||
| { | ||||||
| if (class_exists(NovaServiceProvider::class)) { | ||||||
| if (!$this->doesNovaResourceExists()) { | ||||||
| $this->throwFailureException( | ||||||
| ClassNotExistsException::class, | ||||||
| "Cannot create Nova{$this->model}Test cause {$this->model} Nova resource does not exist.", | ||||||
| "Create {$this->model} Nova resource." | ||||||
| "Cannot create Nova{$this->novaResourceName}Test cause {$this->novaResourceName} Nova resource does not exist.", | ||||||
| "Create {$this->novaResourceName} Nova resource." | ||||||
| ); | ||||||
| } | ||||||
|
|
||||||
| if ($this->classExists('nova', "Nova{$this->model}Test")) { | ||||||
| if ($this->classExists('nova', "Nova{$this->novaResourceName}Test")) { | ||||||
| $this->throwFailureException( | ||||||
| ClassAlreadyExistsException::class, | ||||||
| "Cannot create Nova{$this->model}Test cause it's already exist.", | ||||||
| "Remove Nova{$this->model}Test." | ||||||
| "Cannot create Nova{$this->novaResourceName}Test cause it's already exist.", | ||||||
| "Remove Nova{$this->novaResourceName}Test." | ||||||
| ); | ||||||
| } | ||||||
|
|
||||||
| if (!$this->classExists('models', $this->model)) { | ||||||
RGO230 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
| $this->throwFailureException( | ||||||
| ClassNotExistsException::class, | ||||||
| "Cannot create Nova{$this->novaResourceName}Test cause {$this->model} does not exist.", | ||||||
| "Create a {$this->model} Model by himself or run command 'php artisan make:entity {$this->model} --only-model'." | ||||||
| ); | ||||||
| } | ||||||
|
|
||||||
|
|
@@ -38,6 +50,13 @@ public function generate(): void | |||||
| } | ||||||
| } | ||||||
|
|
||||||
| public function setResource($novaResourceName) | ||||||
RGO230 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
| { | ||||||
| $this->novaResourceName = Str::studly($novaResourceName); | ||||||
|
|
||||||
| return $this; | ||||||
| } | ||||||
|
|
||||||
| public function generateTests(): void | ||||||
| { | ||||||
| if (!$this->isStubExists('nova_test')) { | ||||||
|
|
@@ -58,9 +77,9 @@ public function generateTests(): void | |||||
| 'filters' => $filters, | ||||||
| ]); | ||||||
|
|
||||||
| $this->saveClass('tests', "Nova{$this->model}Test", $fileContent); | ||||||
| $this->saveClass('tests', "Nova{$this->novaResourceName}Test", $fileContent); | ||||||
|
|
||||||
| event(new SuccessCreateMessage("Created a new Nova test: Nova{$this->model}Test")); | ||||||
| event(new SuccessCreateMessage("Created a new Nova test: Nova{$this->novaResourceName}Test")); | ||||||
| } | ||||||
|
|
||||||
| protected function getActions(): array | ||||||
|
|
@@ -83,22 +102,22 @@ protected function getActions(): array | |||||
|
|
||||||
| protected function loadNovaActions() | ||||||
| { | ||||||
| return app("\\App\\Nova\\{$this->novaModelName}")->actions(new NovaRequest()); | ||||||
| return app("{$this->fullNovaResourcePath}")->actions(new NovaRequest()); | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| } | ||||||
|
|
||||||
| protected function loadNovaFields() | ||||||
| { | ||||||
| return app("\\App\\Nova\\{$this->novaModelName}")->fields(new NovaRequest()); | ||||||
| return app("{$this->fullNovaResourcePath}")->fields(new NovaRequest()); | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| } | ||||||
|
|
||||||
| protected function loadNovaFilters() | ||||||
| { | ||||||
| return app("\\App\\Nova\\{$this->novaModelName}")->filters(new NovaRequest()); | ||||||
| return app("{$this->fullNovaResourcePath}")->filters(new NovaRequest()); | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| } | ||||||
|
|
||||||
| public function getTestClassName(): string | ||||||
| { | ||||||
| return "Nova{$this->model}Test"; | ||||||
| return "Nova{$this->novaResourceName}Test"; | ||||||
| } | ||||||
|
|
||||||
| protected function isFixtureNeeded($type): bool | ||||||
|
|
@@ -108,15 +127,11 @@ protected function isFixtureNeeded($type): bool | |||||
|
|
||||||
| protected function doesNovaResourceExists(): bool | ||||||
RGO230 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
| { | ||||||
| $possibleNovaModelNames = [ | ||||||
| "{$this->model}NovaResource", | ||||||
| "{$this->model}Resource", | ||||||
| $this->model | ||||||
| ]; | ||||||
| $allNovaClasses = $this->getAllNovaClasses(); | ||||||
|
|
||||||
| foreach ($possibleNovaModelNames as $modelName) { | ||||||
| if ($this->classExists('nova', $modelName)) { | ||||||
| $this->novaModelName = $modelName; | ||||||
| foreach ($allNovaClasses as $class) { | ||||||
| if (Str::contains($class, $this->novaResourceName)) { | ||||||
| $this->fullNovaResourcePath = $class; | ||||||
|
|
||||||
| return true; | ||||||
| } | ||||||
|
|
@@ -125,6 +140,25 @@ protected function doesNovaResourceExists(): bool | |||||
| return false; | ||||||
| } | ||||||
|
|
||||||
| protected function getAllNovaClasses(): array | ||||||
| { | ||||||
| $classes = []; | ||||||
|
|
||||||
| $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator(app_path('Nova'))); | ||||||
|
|
||||||
| foreach ($iterator as $file) { | ||||||
| if ($file->isFile() && $file->getExtension() === 'php') { | ||||||
| $relativePath = str_replace(app_path() . DIRECTORY_SEPARATOR, '', $file->getPathname()); | ||||||
|
|
||||||
| $class = 'App\\' . str_replace(['/', '.php'], ['\\', ''], $relativePath); | ||||||
|
|
||||||
| $classes[] = $class; | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| return $classes; | ||||||
| } | ||||||
|
|
||||||
| protected function collectFilters(): array | ||||||
| { | ||||||
| $filtersFromFields = $this->getFiltersFromFields(); | ||||||
|
|
||||||
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.
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.