Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions ReadMe.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,14 @@ Syntax:

--only-resource : Set this flag if you want to create only resource.

--only-nova-resource : Set this flag if you want to create only nova resource.

--only-nova-tests : Set this flag if you want to create only nova resource tests.

--resource-name[=RESOURCE-NAME] : Override the default (App\\Nova\\ModelResource) Nova resource name. Used only with --only-nova-tests.

please, use flag variable with double screening and double quotes without Nova directory, for example --resource-name="Resources\\Banner\\BannerResource"

#### Mode combination options

--only-entity : Generate stack of classes to work with entity inside the app (Migration/Model/Service/Repository)
Expand Down
4 changes: 3 additions & 1 deletion src/Commands/MakeEntityCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ class MakeEntityCommand extends Command
{--only-seeder : Set this flag if you want to create only seeder.}
{--only-nova-resource : Set this flag if you want to create only nova resource.}
{--only-nova-tests : Set this flag if you want to create only nova resource tests.}

{--resource-name= : Override the default Nova resource name. Used only with --only-nova-tests.}
Copy link
Contributor

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


{--methods=CRUD : Set types of methods to create. Affect on routes, requests classes, controller\'s methods and tests methods.}

{--i|integer=* : Add integer field to entity.}
Expand Down Expand Up @@ -218,6 +219,7 @@ protected function runGeneration($generator)
->setFields($this->getFields())
->setRelations($this->getRelations())
->setCrudOptions($this->getCrudOptions())
->setMetaData(['resource_name' => $this->option('resource-name')])
->generate();
}

Expand Down
5 changes: 5 additions & 0 deletions src/Generators/EntityGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ public function setRelations(RelationsDTO $relations)
return $this;
}

public function setMetaData(array $data): self
{
return $this;
}

public function __construct()
{
$this->paths = config('entity-generator.paths');
Expand Down
111 changes: 86 additions & 25 deletions src/Generators/NovaTestGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Copy link
Contributor

Choose a reason for hiding this comment

The 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."
);
}

Expand All @@ -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')) {
Expand All @@ -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
Expand All @@ -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());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return app("{$this->fullNovaResourcePath}")->fields(new NovaRequest());
return app($this->fullNovaResourcePath)->fields(new NovaRequest());

}

protected function loadNovaFilters()
{
return app("\\App\\Nova\\{$this->novaModelName}")->filters(new NovaRequest());
return app("{$this->fullNovaResourcePath}")->filters(new NovaRequest());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return app("{$this->fullNovaResourcePath}")->filters(new NovaRequest());
return app($this->fullNovaResourcePath)->filters(new NovaRequest());

}

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
Expand Down Expand Up @@ -160,7 +221,7 @@ protected function getFiltersFromFields(): array

protected function getFilters(): array
{
$filters= [];
$filters = [];
$novaResourceFilters = $this->loadNovaFilters();

foreach ($novaResourceFilters as $filter) {
Expand Down
Loading