Skip to content
5 changes: 3 additions & 2 deletions src/Generators/AbstractTestsGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,6 @@ protected function getMockModel($model): array
protected function generateFixtures(): void
{
$object = $this->getFixtureValuesList($this->model);
$entity = Str::snake($this->model);

$this->createFixtureFolder();

Expand All @@ -195,7 +194,7 @@ protected function generateFixtures(): void
foreach ($modifications as $modification) {
$excepts = ($modification === 'request') ? ['id'] : [];

$this->generateFixture("{$type}_{$entity}_{$modification}.json", Arr::except($object, $excepts));
$this->generateFixture("{$type}_" . Str::snake($this->getTestingEntityName()) . "_{$modification}.json", Arr::except($object, $excepts));
Copy link
Contributor

Choose a reason for hiding this comment

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

please revert

}
}
}
Expand Down Expand Up @@ -259,6 +258,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
34 changes: 18 additions & 16 deletions src/Generators/NovaTestGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,6 @@ class NovaTestGenerator extends AbstractTestsGenerator
public function generate(): void
{
if (class_exists(NovaServiceProvider::class)) {
if ($this->classExists('nova', "Nova{$this->model}ResourceTest")) {

$path = $this->getClassPath('nova', "Nova{$this->model}ResourceTest");

throw new ResourceAlreadyExistsException($path);
}

$novaResources = $this->getCommonNovaResources();

if (count($novaResources) > 1) {
Expand All @@ -51,6 +44,12 @@ public function generate(): void

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

if ($this->classExists('nova', "Nova{$this->getTestingEntityName()}Test")) {
$path = $this->getClassPath('nova', "Nova{$this->getTestingEntityName()}Test");

throw new ResourceAlreadyExistsException($path);
}

parent::generate();
} else {
event(new SuccessCreateMessage("Nova is not installed and NovaTest is skipped"));
Expand All @@ -66,24 +65,22 @@ public function generateTests(): void
$actions = $this->getActions();
$filters = $this->collectFilters();

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

$fileContent = $this->getStub('nova_test', [
'entity_namespace' => $this->getNamespace('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->getNamespace('models'),
]);

$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 @@ -161,7 +158,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 @@ -219,8 +216,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 @@ -53,4 +53,9 @@ protected function generateTests(): void

event(new SuccessCreateMessage($createMessage));
}

protected function getTestingEntityName(): string
{
return $this->model;
}
}
16 changes: 8 additions & 8 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 Down Expand Up @@ -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
27 changes: 15 additions & 12 deletions tests/NovaTestGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ public function testGenerateNovaTestAlreadyExists()

$this->mockClass(NovaTestGenerator::class, [
$this->classExistsMethodCall(['nova', 'NovaPostResourceTest']),
$this->getCommonNovaResourcesMock([
'PostResource',
]),
]);

$this->assertExceptionThrew(
Expand Down Expand Up @@ -107,10 +110,10 @@ public function testNovaTestStubNotExist()
->generate();

$this->assertFileDoesNotExist('tests/NovaWelcomeBonusTest.php');
Copy link

Copilot AI Nov 4, 2025

Choose a reason for hiding this comment

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

The assertion checks for the old test class name NovaWelcomeBonusTest which is inconsistent with the new naming convention. Since the Nova resource is WelcomeBonusResource, the expected test class name should be NovaWelcomeBonusResourceTest. Consider updating this assertion to verify the correct behavior by checking that tests/NovaWelcomeBonusResourceTest.php does not exist when the stub is missing.

Suggested change
$this->assertFileDoesNotExist('tests/NovaWelcomeBonusTest.php');
$this->assertFileDoesNotExist('tests/NovaWelcomeBonusResourceTest.php');

Copilot uses AI. Check for mistakes.
$this->assertGeneratedFileEquals('dump.sql', 'tests/fixtures/NovaWelcomeBonusTest/nova_welcome_bonus_dump.sql');
$this->assertGeneratedFileEquals('create_welcome_bonus_request.json', 'tests/fixtures/NovaWelcomeBonusTest/create_welcome_bonus_request.json');
$this->assertGeneratedFileEquals('create_welcome_bonus_response.json', 'tests/fixtures/NovaWelcomeBonusTest/create_welcome_bonus_response.json');
$this->assertGeneratedFileEquals('update_welcome_bonus_request.json', 'tests/fixtures/NovaWelcomeBonusTest/update_welcome_bonus_request.json');
$this->assertGeneratedFileEquals('dump.sql', 'tests/fixtures/NovaWelcomeBonusResourceTest/nova_welcome_bonus_resource_dump.sql');
$this->assertGeneratedFileEquals('create_welcome_bonus_request.json', 'tests/fixtures/NovaWelcomeBonusResourceTest/create_welcome_bonus_resource_request.json');
$this->assertGeneratedFileEquals('create_welcome_bonus_response.json', 'tests/fixtures/NovaWelcomeBonusResourceTest/create_welcome_bonus_resource_response.json');
$this->assertGeneratedFileEquals('update_welcome_bonus_request.json', 'tests/fixtures/NovaWelcomeBonusResourceTest/update_welcome_bonus_resource_request.json');

$this->assertEventPushed(
className: WarningEvent::class,
Expand Down Expand Up @@ -140,10 +143,10 @@ public function testDumpStubNotExist()
->generate();

$this->assertGeneratedFileEquals('created_resource_test.php', 'tests/NovaWelcomeBonusResourceTest.php');
$this->assertFileDoesNotExist('tests/fixtures/NovaWelcomeBonusTest/nova_welcome_bonus_dump.sql');
$this->assertGeneratedFileEquals('create_welcome_bonus_request.json', 'tests/fixtures/NovaWelcomeBonusTest/create_welcome_bonus_request.json');
$this->assertGeneratedFileEquals('create_welcome_bonus_response.json', 'tests/fixtures/NovaWelcomeBonusTest/create_welcome_bonus_response.json');
$this->assertGeneratedFileEquals('update_welcome_bonus_request.json', 'tests/fixtures/NovaWelcomeBonusTest/update_welcome_bonus_request.json');
$this->assertFileDoesNotExist('tests/fixtures/NovaWelcomeBonusResourceTest/nova_welcome_bonus_resource_dump.sql');
$this->assertGeneratedFileEquals('create_welcome_bonus_request.json', 'tests/fixtures/NovaWelcomeBonusResourceTest/create_welcome_bonus_resource_request.json');
$this->assertGeneratedFileEquals('create_welcome_bonus_response.json', 'tests/fixtures/NovaWelcomeBonusResourceTest/create_welcome_bonus_resource_response.json');
$this->assertGeneratedFileEquals('update_welcome_bonus_request.json', 'tests/fixtures/NovaWelcomeBonusResourceTest/update_welcome_bonus_resource_request.json');

$this->assertEventPushed(
className: WarningEvent::class,
Expand Down Expand Up @@ -177,10 +180,10 @@ public function testSuccess()
->generate();

$this->assertGeneratedFileEquals('created_resource_test.php', 'tests/NovaWelcomeBonusResourceTest.php');
$this->assertGeneratedFileEquals('dump.sql', 'tests/fixtures/NovaWelcomeBonusTest/nova_welcome_bonus_dump.sql');
$this->assertGeneratedFileEquals('create_welcome_bonus_request.json', 'tests/fixtures/NovaWelcomeBonusTest/create_welcome_bonus_request.json');
$this->assertGeneratedFileEquals('create_welcome_bonus_response.json', 'tests/fixtures/NovaWelcomeBonusTest/create_welcome_bonus_response.json');
$this->assertGeneratedFileEquals('update_welcome_bonus_request.json', 'tests/fixtures/NovaWelcomeBonusTest/update_welcome_bonus_request.json');
$this->assertGeneratedFileEquals('dump.sql', 'tests/fixtures/NovaWelcomeBonusResourceTest/nova_welcome_bonus_resource_dump.sql');
$this->assertGeneratedFileEquals('create_welcome_bonus_request.json', 'tests/fixtures/NovaWelcomeBonusResourceTest/create_welcome_bonus_resource_request.json');
$this->assertGeneratedFileEquals('create_welcome_bonus_response.json', 'tests/fixtures/NovaWelcomeBonusResourceTest/create_welcome_bonus_resource_response.json');
$this->assertGeneratedFileEquals('update_welcome_bonus_request.json', 'tests/fixtures/NovaWelcomeBonusResourceTest/update_welcome_bonus_resource_request.json');
}

public function testGenerateNovaPackageNotInstall()
Expand Down