-
Notifications
You must be signed in to change notification settings - Fork 3
159 ability to set nova resource for nova tests generator #165
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
Changes from all commits
b431fb6
563f2c2
8a19c3a
33ca7bc
516473b
bc9b17e
b5d870d
1f250a2
5c49cec
9a92f95
a66d9ce
c45d415
0fa88db
47aed4f
bf4bfc9
fd12be4
0384c56
fabd308
b84a217
3c65df3
56d96b8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -8,27 +8,46 @@ | |||||
| use RonasIT\Support\Events\SuccessCreateMessage; | ||||||
| use RonasIT\Support\Exceptions\ClassAlreadyExistsException; | ||||||
| use RonasIT\Support\Exceptions\ClassNotExistsException; | ||||||
| use RecursiveIteratorIterator; | ||||||
| use RecursiveDirectoryIterator; | ||||||
| use RonasIT\Support\Exceptions\EntityCreateException; | ||||||
| use Generator; | ||||||
|
|
||||||
| class NovaTestGenerator extends AbstractTestsGenerator | ||||||
| { | ||||||
| protected $novaModelName; | ||||||
| protected string $resourceName; | ||||||
|
|
||||||
| protected ?string $fullNovaResourcePath = null; | ||||||
|
|
||||||
| protected string $shortNovaResourceName; | ||||||
|
|
||||||
| protected string $novaPath; | ||||||
|
|
||||||
| public function __construct() | ||||||
| { | ||||||
| $this->novaPath = app_path('Nova'); | ||||||
|
|
||||||
| parent::__construct(); | ||||||
| } | ||||||
|
|
||||||
| public function generate(): void | ||||||
| { | ||||||
| if (class_exists(NovaServiceProvider::class)) { | ||||||
| if (!$this->doesNovaResourceExists()) { | ||||||
| if (!$this->classExists('models', $this->model)) { | ||||||
| $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->shortNovaResourceName}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'." | ||||||
| ); | ||||||
| } | ||||||
|
|
||||||
| if ($this->classExists('nova', "Nova{$this->model}Test")) { | ||||||
| $this->isNovaResourceExists(); | ||||||
|
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. I saw this in another request https://github.com/RonasIT/laravel-entity-generator/pull/163/files#L44 Let's take solution from Pull Request #163 |
||||||
|
|
||||||
| if ($this->classExists('nova', "Nova{$this->shortNovaResourceName}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->shortNovaResourceName}Test cause it's already exist.", | ||||||
| "Remove Nova{$this->shortNovaResourceName}Test." | ||||||
| ); | ||||||
| } | ||||||
|
|
||||||
|
|
@@ -38,6 +57,17 @@ public function generate(): void | |||||
| } | ||||||
| } | ||||||
|
|
||||||
| public function setMetaData(array $data): self | ||||||
| { | ||||||
| $resourceName = empty($data['resource_name']) ? "{$this->model}Resource" : $data['resource_name']; | ||||||
|
|
||||||
| $this->resourceName = Str::studly($resourceName); | ||||||
|
|
||||||
| $this->shortNovaResourceName = Str::afterLast($this->resourceName, '\\'); | ||||||
|
|
||||||
| return $this; | ||||||
| } | ||||||
|
|
||||||
| public function generateTests(): void | ||||||
| { | ||||||
| if (!$this->isStubExists('nova_test')) { | ||||||
|
|
@@ -50,17 +80,19 @@ public function generateTests(): void | |||||
| $fileContent = $this->getStub('nova_test', [ | ||||||
| 'url_path' => Str::kebab($this->model) . '-resources', | ||||||
| 'entity' => $this->model, | ||||||
| 'resource' => $this->shortNovaResourceName, | ||||||
| 'resource_path' => $this->fullNovaResourcePath, | ||||||
| 'entities' => $this->getPluralName($this->model), | ||||||
| 'snake_entity' => Str::snake($this->model), | ||||||
| 'snake_resource' => Str::snake($this->shortNovaResourceName), | ||||||
| 'dromedary_entity' => Str::lcfirst($this->model), | ||||||
| 'lower_entities' => $this->getPluralName(Str::snake($this->model)), | ||||||
| 'actions' => $actions, | ||||||
| 'filters' => $filters, | ||||||
| ]); | ||||||
|
|
||||||
| $this->saveClass('tests', "Nova{$this->model}Test", $fileContent); | ||||||
| $this->saveClass('tests', "Nova{$this->shortNovaResourceName}Test", $fileContent); | ||||||
|
|
||||||
| event(new SuccessCreateMessage("Created a new Nova test: Nova{$this->model}Test")); | ||||||
| event(new SuccessCreateMessage("Created a new Nova test: Nova{$this->shortNovaResourceName}Test")); | ||||||
| } | ||||||
|
|
||||||
| protected function getActions(): array | ||||||
|
|
@@ -83,46 +115,75 @@ protected function getActions(): array | |||||
|
|
||||||
| protected function loadNovaActions() | ||||||
| { | ||||||
| return app("\\App\\Nova\\{$this->novaModelName}")->actions(new NovaRequest()); | ||||||
| return app("{$this->fullNovaResourcePath}")->actions(new NovaRequest()); | ||||||
| } | ||||||
|
|
||||||
| 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->shortNovaResourceName}Test"; | ||||||
| } | ||||||
|
|
||||||
| protected function isFixtureNeeded($type): bool | ||||||
| { | ||||||
| return true; | ||||||
| } | ||||||
|
|
||||||
| protected function doesNovaResourceExists(): bool | ||||||
| protected function isNovaResourceExists(): true | ||||||
| { | ||||||
| $possibleNovaModelNames = [ | ||||||
| "{$this->model}NovaResource", | ||||||
| "{$this->model}Resource", | ||||||
| $this->model | ||||||
| ]; | ||||||
| $allNovaClasses = $this->getAllNovaClasses(); | ||||||
|
|
||||||
| foreach ($possibleNovaModelNames as $modelName) { | ||||||
| if ($this->classExists('nova', $modelName)) { | ||||||
| $this->novaModelName = $modelName; | ||||||
| $resources = []; | ||||||
|
|
||||||
| foreach ($allNovaClasses as $class) { | ||||||
| if ($class === $this->resourceName) { | ||||||
| $this->fullNovaResourcePath = "App\\Nova\\{$this->resourceName}"; | ||||||
|
|
||||||
| return true; | ||||||
| } | ||||||
|
|
||||||
| if (Str::contains($class, $this->model) && is_subclass_of("App\\Nova\\{$class}", "App\\Nova\\Resource")) { | ||||||
| $resources[] = $class; | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| return false; | ||||||
| if (!empty($resources)) { | ||||||
| $resources = implode(', ', $resources); | ||||||
|
|
||||||
| $this->throwFailureException( | ||||||
| EntityCreateException::class, | ||||||
| "Cannot create Nova{$this->shortNovaResourceName}Test cause I am found a lot of suitable resources: $resources", | ||||||
| "Please, use --resource-name option" | ||||||
| ); | ||||||
| } | ||||||
|
|
||||||
| $this->throwFailureException( | ||||||
| ClassNotExistsException::class, | ||||||
| "Cannot create Nova{$this->shortNovaResourceName}Test cause {$this->resourceName} Nova resource does not exist.", | ||||||
| "Create {$this->resourceName} Nova resource." | ||||||
| ); | ||||||
| } | ||||||
|
|
||||||
| protected function getAllNovaClasses(): Generator | ||||||
| { | ||||||
| $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($this->novaPath)); | ||||||
|
|
||||||
| foreach ($iterator as $file) { | ||||||
| if ($file->isFile() && $file->getExtension() === 'php') { | ||||||
| $relativePath = Str::after($file->getPathname(), $this->novaPath . DIRECTORY_SEPARATOR); | ||||||
|
|
||||||
| yield str_replace(['/', '.php'], ['\\', ''], $relativePath); | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| protected function collectFilters(): array | ||||||
|
|
@@ -160,7 +221,7 @@ protected function getFiltersFromFields(): array | |||||
|
|
||||||
| protected function getFilters(): array | ||||||
| { | ||||||
| $filters= []; | ||||||
| $filters = []; | ||||||
| $novaResourceFilters = $this->loadNovaFilters(); | ||||||
|
|
||||||
| foreach ($novaResourceFilters as $filter) { | ||||||
|
|
||||||
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.
pleae this feature implement by in next task, not this #159