-
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
Changes from all commits
b431fb6
563f2c2
8a19c3a
33ca7bc
516473b
bc9b17e
b5d870d
1f250a2
5c49cec
9a92f95
a66d9ce
c45d415
0fa88db
47aed4f
bf4bfc9
fd12be4
a6a2747
f3fa391
d0cc813
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,58 @@ | |||||||||||||||||||||||||||||||||||
| 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}Resource 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'." | ||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| $resource = $this->resourceName ?? $this->getNovaResource(); | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| $this->shortNovaResourceName = Str::afterLast($resource, '\\'); | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| $this->fullNovaResourcePath = "App\\Nova\\{$resource}"; | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| if (!class_exists($this->fullNovaResourcePath)) { | ||||||||||||||||||||||||||||||||||||
| $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->resourceName} Nova resource does not exist.", | ||||||||||||||||||||||||||||||||||||
| "Create {$this->resourceName} Nova resource." | ||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| if ($this->classExists('nova', "Nova{$this->model}Test")) { | ||||||||||||||||||||||||||||||||||||
| 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 +69,13 @@ public function generate(): void | |||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| public function setMetaData(array $data): self | ||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||
| $this->resourceName = !empty($data['resource_name']) ? Str::studly($data['resource_name']) : null; | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| return $this; | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| public function generateTests(): void | ||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||
| if (!$this->isStubExists('nova_test')) { | ||||||||||||||||||||||||||||||||||||
|
|
@@ -58,9 +96,9 @@ public function generateTests(): void | |||||||||||||||||||||||||||||||||||
| '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 +121,82 @@ 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 getNovaResource(): string | ||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||
| $commonResources = $this->getCommonNovaResources(); | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| if (count($commonResources) > 1) { | ||||||||||||||||||||||||||||||||||||
| $commonResources = implode(', ', $commonResources); | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| $this->throwFailureException( | ||||||||||||||||||||||||||||||||||||
| EntityCreateException::class, | ||||||||||||||||||||||||||||||||||||
| "Cannot create Nova{$this->model}Resource Test cause was found a lot of suitable resources: $commonResources", | ||||||||||||||||||||||||||||||||||||
| "Please, use --resource-name option" | ||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| return array_pop($commonResources); | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| protected function getNovaFiles(): Generator | ||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||
| $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($this->novaPath)); | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| foreach ($iterator as $file) { | ||||||||||||||||||||||||||||||||||||
| if ($file->isFile() && $file->getExtension() === 'php') { | ||||||||||||||||||||||||||||||||||||
| yield $file; | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| protected function getCommonNovaResources(): array | ||||||||||||||||||||||||||||||||||||
|
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
|
||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||
| $possibleNovaModelNames = [ | ||||||||||||||||||||||||||||||||||||
| "{$this->model}NovaResource", | ||||||||||||||||||||||||||||||||||||
| "{$this->model}Resource", | ||||||||||||||||||||||||||||||||||||
| $this->model | ||||||||||||||||||||||||||||||||||||
| ]; | ||||||||||||||||||||||||||||||||||||
| $resources = []; | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| foreach ($this->getNovaFiles() as $file) { | ||||||||||||||||||||||||||||||||||||
| $relativePath = Str::after($file->getPathname(), $this->novaPath . DIRECTORY_SEPARATOR); | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| foreach ($possibleNovaModelNames as $modelName) { | ||||||||||||||||||||||||||||||||||||
| if ($this->classExists('nova', $modelName)) { | ||||||||||||||||||||||||||||||||||||
| $this->novaModelName = $modelName; | ||||||||||||||||||||||||||||||||||||
| $class = str_replace(['/', '.php'], ['\\', ''], $relativePath); | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| return true; | ||||||||||||||||||||||||||||||||||||
| if ($this->isNovaResource($class) && $this->isResourceNameContainModel($class)) { | ||||||||||||||||||||||||||||||||||||
| $resources[] = $class; | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| return false; | ||||||||||||||||||||||||||||||||||||
| return $resources; | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| protected function isNovaResource(string $resource): bool | ||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||
| return is_subclass_of("App\\Nova\\{$resource}", "Laravel\\Nova\\Resource"); | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| protected function isResourceNameContainModel(string $resource): bool | ||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||
| return Str::afterLast(str_replace('Resource', '', $resource), '\\') === $this->model; | ||||||||||||||||||||||||||||||||||||
|
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. contain or hard equal? Are you sure? If resourse will have name WelcomeBonusDraftResource.php |
||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| protected function collectFilters(): array | ||||||||||||||||||||||||||||||||||||
|
|
@@ -160,7 +234,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.