Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
b431fb6
refactor: use resource in generate only nova tests
RGO230 Jun 25, 2025
563f2c2
fix: tests
RGO230 Jun 27, 2025
8a19c3a
fix:tests
RGO230 Jun 27, 2025
33ca7bc
fix:Tests
RGO230 Jun 27, 2025
516473b
fix:tests
RGO230 Jun 27, 2025
bc9b17e
Merge branch 'master' into 140-use-NovaResource-class-in-NovaTestGene…
RGO230 Jun 27, 2025
b5d870d
refactor: change set resource from argument ot option
RGO230 Jul 1, 2025
1f250a2
style: fix
RGO230 Jul 1, 2025
5c49cec
refactor: remove useless
RGO230 Jul 2, 2025
9a92f95
Merge branch 'master' into 140-use-NovaResource-class-in-NovaTestGene…
RGO230 Jul 8, 2025
a66d9ce
fix:tests
RGO230 Jul 8, 2025
c45d415
refactor: check only nova resources
RGO230 Jul 8, 2025
0fa88db
refactor: change isNovaResourceExists method
RGO230 Jul 8, 2025
47aed4f
fix: remove useless
RGO230 Jul 9, 2025
bf4bfc9
style: import generator
RGO230 Jul 9, 2025
fd12be4
chore:update readme
RGO230 Jul 9, 2025
a6a2747
refactor:use common resources
RGO230 Jul 11, 2025
f3fa391
style:fix
RGO230 Jul 11, 2025
d0cc813
refactor: remove useless from generator
RGO230 Sep 2, 2025
1f64ef5
Merge branch '140-use-NovaResource-class-in-NovaTestGenerator-instead…
RGO230 Sep 15, 2025
316eed1
refactor: correct set resource instead model
RGO230 Sep 16, 2025
6330b23
fix: remove useless
RGO230 Sep 16, 2025
8c14393
Merge branch '140-use-NovaResource-class-in-NovaTestGenerator-instead…
RGO230 Oct 9, 2025
e0fa9ec
Merge branch '140-use-NovaResource-class-in-NovaTestGenerator-instead…
RGO230 Oct 15, 2025
79baddf
refactor: set resource name in dump/fixtures
RGO230 Oct 20, 2025
418fe23
Merge branch '140-use-NovaResource-class-in-NovaTestGenerator-instead…
RGO230 Oct 20, 2025
558c187
refactor: use set entity in setMetaData
RGO230 Oct 22, 2025
c38edcb
Merge branch 'master' into 140-use-NovaResource-class-in-NovaTestGene…
DenTray Oct 23, 2025
6b3eefe
refactor: use getTestingEntityName
RGO230 Oct 23, 2025
4d704fa
fix: remove useless
RGO230 Oct 26, 2025
a43a514
style:fix
RGO230 Nov 2, 2025
16f9a24
Apply suggestion from @AZabolotnikov
AZabolotnikov Nov 4, 2025
32988da
style:fix
RGO230 Nov 4, 2025
63ca88d
Merge branch 'master' into 140-use-NovaResource-class-in-NovaTestGene…
RGO230 Nov 4, 2025
1d3b358
fix: imports in nova test stub
RGO230 Nov 4, 2025
c5a3c41
style: remove useless
RGO230 Nov 4, 2025
d2e1541
Merge branch '140-use-NovaResource-class-in-NovaTestGenerator-instead…
RGO230 Nov 4, 2025
9a90e13
fix: use abstract getEntityName method
RGO230 Nov 4, 2025
01346cb
fix:tests
RGO230 Nov 4, 2025
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 @@ -103,6 +103,14 @@ The following options are available:
- `--only-seeder`
- `--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

Sometimes you need to generate the stack of classes to work with some entity only inside the application without
Expand Down
4 changes: 3 additions & 1 deletion src/Commands/MakeEntityCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,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.}

{--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 @@ -228,6 +229,7 @@ protected function runGeneration(string $generator): void
->setFields($this->getFields())
->setRelations($this->relations)
->setCrudOptions($this->getCrudOptions())
->setMetaData(['resource_name' => $this->option('resource-name')])
->generate();
}

Expand Down
5 changes: 4 additions & 1 deletion src/Generators/AbstractTestsGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,11 @@ protected function getMockModel($model): array
protected function generateFixtures(): void
{
$object = $this->getFixtureValuesList($this->model);
$entity = Str::snake($this->model);

$this->createFixtureFolder();

$entity = Str::snake($this->getTestingEntityName());

foreach (self::FIXTURE_TYPES as $type => $modifications) {
if ($this->isFixtureNeeded($type)) {
foreach ($modifications as $modification) {
Expand Down Expand Up @@ -259,6 +260,8 @@ abstract protected function isFixtureNeeded($type): bool;

abstract protected function generateTests(): void;

abstract protected function getTestingEntityName(): string;

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

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

public function __construct()
{
$this->paths = config('entity-generator.paths');
Expand Down
92 changes: 61 additions & 31 deletions src/Generators/NovaTestGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,42 +15,69 @@

class NovaTestGenerator extends AbstractTestsGenerator
{
protected string $novaResourceClassName;
protected ?string $novaResourceClassName;

public function generate(): void
{
if (class_exists(NovaServiceProvider::class)) {
$this->checkResourceExists('nova', "Nova{$this->model}ResourceTest");
if (empty($this->novaResourceClassName)) {
$novaResources = $this->getCommonNovaResources();

if (count($novaResources) > 1) {
$foundedResources = implode(', ', $novaResources);

$this->throwFailureException(
EntityCreateException::class,
"Cannot create Nova{$this->model}ResourceTest cause was found a lot of suitable resources: {$foundedResources}.",
'Please, use --resource-name option.'
);
}

if (empty($novaResources)) {
// TODO: pass $this->modelSubfolder to Exception after refactoring in https://github.com/RonasIT/laravel-entity-generator/issues/179
$this->throwFailureException(
ClassNotExistsException::class,
"Cannot create Nova{$this->model}ResourceTest cause {$this->model} Nova resource does not exist.",
"Create {$this->model} Nova resource."
);
}

$novaResources = $this->getCommonNovaResources();
$this->novaResourceClassName = Arr::first($novaResources);

if (count($novaResources) > 1) {
$foundedResources = implode(', ', $novaResources);
$this->checkResourceExists('nova', $this->getTestClassName(), $this->modelSubFolder);

// TODO: Change exception message after https://github.com/RonasIT/laravel-entity-generator/issues/159 will be ready
$this->throwFailureException(
EntityCreateException::class,
"Cannot create Nova{$this->model}ResourceTest cause was found a lot of suitable resources: {$foundedResources}.",
'Make test by yourself.'
);
}
} else {
$this->checkResourceExists('nova', $this->getTestClassName(), $this->modelSubFolder);

if (empty($novaResources)) {
$this->throwFailureException(
ClassNotExistsException::class,
"Cannot create Nova{$this->model}ResourceTest cause {$this->model} Nova resource does not exist.",
"Create {$this->model} Nova resource."
);
}
if (!$this->isNovaResource($this->novaResourceClassName) || !$this->isResourceNameContainModel($this->novaResourceClassName)) {
// TODO: pass $this->modelSubfolder to Exception after refactoring in https://github.com/RonasIT/laravel-entity-generator/issues/179

$this->novaResourceClassName = Arr::first($novaResources);
$entityName = $this->getTestingEntityName();
$entityTestName = $this->getTestClassName();

$this->throwFailureException(
ClassNotExistsException::class,
"Cannot create {$entityTestName} cause {$entityName} Nova resource does not exist.",
"Create {$entityName} Nova resource."
);
}
}

parent::generate();
} else {
event(new SuccessCreateMessage("Nova is not installed and NovaTest is skipped"));
}
}

public function setMetaData(array $data): self
{
if (!empty($data['resource_name'])) {
$this->novaResourceClassName = $this->pathToNamespace($this->paths['nova'] . DIRECTORY_SEPARATOR . Str::studly($data['resource_name']));
}

return $this;
}

public function generateTests(): void
{
if (!$this->isStubExists('nova_test')) {
Expand All @@ -60,24 +87,22 @@ public function generateTests(): void
$actions = $this->getActions();
$filters = $this->collectFilters();

$resourceClass = Str::afterLast($this->novaResourceClassName, '\\');

$fileContent = $this->getStub('nova_test', [
'entity_namespace' => $this->generateNamespace($this->paths['models'], $this->modelSubFolder),
'entity_namespace' => $this->generateNamespace('models', $this->modelSubFolder),
'entity' => $this->model,
'resource_name' => $resourceClass,
'resource_name' => $this->getTestingEntityName(),
'resource_namespace' => $this->novaResourceClassName,
'snake_resource' => Str::snake($resourceClass),
'snake_resource' => Str::snake($this->getTestingEntityName()),
'dromedary_entity' => Str::lcfirst($this->model),
'lower_entities' => $this->getPluralName(Str::snake($this->model)),
'actions' => $actions,
'filters' => $filters,
'models_namespace' => $this->generateNamespace($this->paths['models']),
'models_namespace' => $this->generateNamespace('models', $this->modelSubFolder),
]);

$this->saveClass('tests', "Nova{$this->model}ResourceTest", $fileContent);
$this->saveClass('tests', $this->getTestClassName(), $fileContent);

event(new SuccessCreateMessage("Created a new Nova test: Nova{$this->model}ResourceTest"));
event(new SuccessCreateMessage("Created a new Nova test: {$this->getTestClassName()}"));
}

protected function getActions(): array
Expand Down Expand Up @@ -155,7 +180,7 @@ protected function loadNovaFilters()

public function getTestClassName(): string
{
return "Nova{$this->model}Test";
return "Nova{$this->getTestingEntityName()}Test";
}

protected function isFixtureNeeded($type): bool
Expand Down Expand Up @@ -213,8 +238,13 @@ protected function getFilters(): array

protected function getDumpName(): string
{
$modelName = Str::snake($this->model);
$entityName = Str::snake($this->getTestingEntityName());

return "nova_{$modelName}_dump.sql";
return "nova_{$entityName}_dump.sql";
}

protected function getTestingEntityName(): string
{
return Str::afterLast($this->novaResourceClassName, '\\');
}
}
5 changes: 5 additions & 0 deletions src/Generators/TestsGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,9 @@ protected function generateTests(): void

event(new SuccessCreateMessage($createMessage));
}

protected function getTestingEntityName(): string
{
return $this->model;
}
}
2 changes: 1 addition & 1 deletion stubs/nova_test.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use RonasIT\Support\Testing\ModelTestState;
use RonasIT\Support\Traits\NovaTestTrait;
use {{ $resource_namespace }};
use {{ $models_namespace }}\User;
use Models\User;

class Nova{{ $resource_name }}Test extends TestCase
{
Expand Down
64 changes: 55 additions & 9 deletions tests/CommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,10 @@ public function testCallCommand()
$this->assertGeneratedFileEquals('validation.php', 'lang/en/validation.php');
$this->assertGeneratedFileEquals('nova_resource.php', 'app/Nova/PostResource.php');
$this->assertGeneratedFileEquals('nova_test.php', 'tests/NovaPostResourceTest.php');
$this->assertGeneratedFileEquals('nova_dump.php', 'tests/fixtures/NovaPostTest/nova_post_dump.sql');
$this->assertGeneratedFileEquals('create_request.json', 'tests/fixtures/NovaPostTest/create_post_request.json');
$this->assertGeneratedFileEquals('create_response.json', 'tests/fixtures/NovaPostTest/create_post_response.json');
$this->assertGeneratedFileEquals('update_request.json', 'tests/fixtures/NovaPostTest/update_post_request.json');
$this->assertGeneratedFileEquals('nova_dump.php', 'tests/fixtures/NovaPostResourceTest/nova_post_resource_dump.sql');
$this->assertGeneratedFileEquals('create_request.json', 'tests/fixtures/NovaPostResourceTest/create_post_resource_request.json');
$this->assertGeneratedFileEquals('create_response.json', 'tests/fixtures/NovaPostResourceTest/create_post_resource_response.json');
$this->assertGeneratedFileEquals('update_request.json', 'tests/fixtures/NovaPostResourceTest/update_post_resource_request.json');
}

public function testCallCommandSubFoldersModel()
Expand All @@ -113,7 +113,7 @@ public function testCallCommandSubFoldersModel()
$this
->artisan('make:entity Forum/Post --methods=CRUD')
->assertSuccessful();

$this->assertGeneratedFileEquals('subfolder/model.php', 'RonasIT/Support/Tests/Support/Command/Models/Forum/Post.php');
$this->assertGeneratedFileEquals('subfolder/factory.php', 'RonasIT/Support/Tests/Support/Command/Factories/PostFactory.php');
$this->assertGeneratedFileEquals('subfolder/repository.php', 'app/Repositories/PostRepository.php');
Expand All @@ -137,10 +137,10 @@ public function testCallCommandSubFoldersModel()
$this->assertGeneratedFileEquals('create_response.json', 'tests/fixtures/PostTest/create_post_response.json');
$this->assertGeneratedFileEquals('update_request.json', 'tests/fixtures/PostTest/update_post_request.json');
$this->assertGeneratedFileEquals('validation.php', 'lang/en/validation.php');
$this->assertGeneratedFileEquals('nova_dump.php', 'tests/fixtures/NovaPostTest/nova_post_dump.sql');
$this->assertGeneratedFileEquals('create_request.json', 'tests/fixtures/NovaPostTest/create_post_request.json');
$this->assertGeneratedFileEquals('create_response.json', 'tests/fixtures/NovaPostTest/create_post_response.json');
$this->assertGeneratedFileEquals('update_request.json', 'tests/fixtures/NovaPostTest/update_post_request.json');
$this->assertGeneratedFileEquals('nova_dump.php', 'tests/fixtures/NovaPostResourceTest/nova_post_resource_dump.sql');
$this->assertGeneratedFileEquals('create_request.json', 'tests/fixtures/NovaPostResourceTest/create_post_resource_request.json');
$this->assertGeneratedFileEquals('create_response.json', 'tests/fixtures/NovaPostResourceTest/create_post_resource_response.json');
$this->assertGeneratedFileEquals('update_request.json', 'tests/fixtures/NovaPostResourceTest/update_post_resource_request.json');
}

public function testMakeOnly()
Expand Down Expand Up @@ -180,6 +180,52 @@ public function testMakeOnly()
$this->assertFileDoesNotExist('tests/fixtures/NovaPostTest/update_post_request.json');
}

public function testMakeOnlyNovaTest(): void
{
Carbon::setTestNow('2016-10-20 11:05:00');

$this->mockFilesystemWithPostModelAndResource();

config([
'entity-generator.paths.models' => 'RonasIT\Support\Tests\Support\Command\Models',
'entity-generator.paths.factories' => 'RonasIT\Support\Tests\Support\Command\Factories',
]);

$this->mockGeneratorOnlyNovaTests();

$this
->artisan('make:entity Post --only-nova-tests --resource-name=PostResource')
->assertSuccessful();

$this->assertFileDoesNotExist('app/Repositories/PostRepository.php');
$this->assertFileDoesNotExist('database/migrations/2016_10_20_110500_posts_create_table.php');
$this->assertFileDoesNotExist('database/factories/PostFactory.php');
$this->assertFileDoesNotExist('database/seeders/PostSeeder.php');
$this->assertFileDoesNotExist('app/Models/Post.php');
$this->assertFileDoesNotExist('app/Services/PostService.php');
$this->assertFileDoesNotExist('app/Http/Requests/Post/CreatePostRequest.php');
$this->assertFileDoesNotExist('app/Http/Requests/Post/GetPostRequest.php');
$this->assertFileDoesNotExist('app/Http/Requests/Post/SearchPostsRequest.php');
$this->assertFileDoesNotExist('app/Http/Requests/Post/UpdatePostRequest.php');
$this->assertFileDoesNotExist('app/Http/Requests/Post/DeletePostRequest.php');
$this->assertFileDoesNotExist('app/Http/Controllers/PostController.php');
$this->assertFileDoesNotExist('app/Http/Resources/Post/PostResource.php');
$this->assertFileDoesNotExist('app/Http/Resources/Post/PostsCollectionResource.php');
$this->assertFileDoesNotExist('routes/api.php');
$this->assertFileDoesNotExist('tests/PostTest.php');
$this->assertFileDoesNotExist('tests/fixtures/PostTest/dump.sql');
$this->assertFileDoesNotExist('tests/fixtures/PostTest/create_post_request.json');
$this->assertFileDoesNotExist('tests/fixtures/PostTest/create_post_response.json');
$this->assertFileDoesNotExist('tests/fixtures/PostTest/update_post_request.json');
$this->assertFileDoesNotExist('lang/en/validation.php');
$this->assertFileDoesNotExist('app/Nova/PostResource.php');
$this->assertGeneratedFileEquals('nova_test.php', 'tests/NovaPostResourceTest.php');
$this->assertGeneratedFileEquals('nova_dump.php', 'tests/fixtures/NovaPostResourceTest/nova_post_resource_dump.sql');
$this->assertGeneratedFileEquals('create_request.json', 'tests/fixtures/NovaPostResourceTest/create_post_resource_request.json');
$this->assertGeneratedFileEquals('create_response.json', 'tests/fixtures/NovaPostResourceTest/create_post_resource_response.json');
$this->assertGeneratedFileEquals('update_request.json', 'tests/fixtures/NovaPostResourceTest/update_post_resource_request.json');
}

public function testCallWithNotDefaultConfig()
{
$this->app->instance('path.base', $this->generatedFileBasePath);
Expand Down
Loading