Skip to content

Commit 6b3eefe

Browse files
committed
refactor: use getTestingEntityName
1 parent c38edcb commit 6b3eefe

File tree

4 files changed

+27
-16
lines changed

4 files changed

+27
-16
lines changed

src/Generators/AbstractTestsGenerator.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ abstract class AbstractTestsGenerator extends EntityGenerator
1313
protected array $fakerProperties = [];
1414
protected array $getFields = [];
1515
protected bool $withAuth = false;
16-
protected string $entity;
1716

1817
const array FIXTURE_TYPES = [
1918
'create' => ['request', 'response'],
@@ -195,7 +194,7 @@ protected function generateFixtures(): void
195194
foreach ($modifications as $modification) {
196195
$excepts = ($modification === 'request') ? ['id'] : [];
197196

198-
$this->generateFixture("{$type}_" . Str::snake($this->entity) . "_{$modification}.json", Arr::except($object, $excepts));
197+
$this->generateFixture("{$type}_" . Str::snake($this->getTestingEntityName()) . "_{$modification}.json", Arr::except($object, $excepts));
199198
}
200199
}
201200
}
@@ -259,6 +258,8 @@ abstract protected function isFixtureNeeded($type): bool;
259258

260259
abstract protected function generateTests(): void;
261260

261+
abstract protected function getTestingEntityName(): string;
262+
262263
private function filterBadModelField($fields): array
263264
{
264265
return array_diff($fields, [

src/Generators/NovaTestGenerator.php

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,6 @@ class NovaTestGenerator extends AbstractTestsGenerator
2121
public function generate(): void
2222
{
2323
if (class_exists(NovaServiceProvider::class)) {
24-
if ($this->classExists('nova', "Nova{$this->model}ResourceTest")) {
25-
26-
$path = $this->getClassPath('nova', "Nova{$this->model}ResourceTest");
27-
28-
throw new ResourceAlreadyExistsException($path);
29-
}
30-
3124
$novaResources = $this->getCommonNovaResources();
3225

3326
if (count($novaResources) > 1) {
@@ -51,7 +44,11 @@ public function generate(): void
5144

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

54-
$this->entity = Str::afterLast($this->novaResourceClassName, '\\');
47+
if ($this->classExists('nova', "Nova{$this->getTestingEntityName()}Test")) {
48+
$path = $this->getClassPath('nova', "Nova{$this->getTestingEntityName()}Test");
49+
50+
throw new ResourceAlreadyExistsException($path);
51+
}
5552

5653
parent::generate();
5754
} else {
@@ -71,19 +68,19 @@ public function generateTests(): void
7168
$fileContent = $this->getStub('nova_test', [
7269
'entity_namespace' => $this->getNamespace('models', $this->modelSubFolder),
7370
'entity' => $this->model,
74-
'resource_name' => $this->entity,
71+
'resource_name' => $this->getTestingEntityName(),
7572
'resource_namespace' => $this->novaResourceClassName,
76-
'snake_resource' => Str::snake($this->entity),
73+
'snake_resource' => Str::snake($this->getTestingEntityName()),
7774
'dromedary_entity' => Str::lcfirst($this->model),
7875
'lower_entities' => $this->getPluralName(Str::snake($this->model)),
7976
'actions' => $actions,
8077
'filters' => $filters,
8178
'models_namespace' => $this->getNamespace('models'),
8279
]);
8380

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

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

8986
protected function getActions(): array
@@ -161,7 +158,7 @@ protected function loadNovaFilters()
161158

162159
public function getTestClassName(): string
163160
{
164-
return "Nova{$this->entity}Test";
161+
return "Nova{$this->getTestingEntityName()}Test";
165162
}
166163

167164
protected function isFixtureNeeded($type): bool
@@ -219,8 +216,13 @@ protected function getFilters(): array
219216

220217
protected function getDumpName(): string
221218
{
222-
$entityName = Str::snake($this->entity);
219+
$entityName = Str::snake($this->getTestingEntityName());
223220

224221
return "nova_{$entityName}_dump.sql";
225222
}
223+
224+
protected function getTestingEntityName(): string
225+
{
226+
return Str::afterLast($this->novaResourceClassName, '\\');
227+
}
226228
}

src/Generators/TestsGenerator.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,9 @@ protected function generateTests(): void
6060

6161
event(new SuccessCreateMessage($createMessage));
6262
}
63+
64+
protected function getTestingEntityName(): string
65+
{
66+
return $this->model;
67+
}
6368
}

tests/NovaTestGeneratorTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ public function testGenerateNovaTestAlreadyExists()
6868

6969
$this->mockClass(NovaTestGenerator::class, [
7070
$this->classExistsMethodCall(['nova', 'NovaPostResourceTest']),
71+
$this->getCommonNovaResourcesMock([
72+
'PostResource',
73+
]),
7174
]);
7275

7376
$this->assertExceptionThrew(

0 commit comments

Comments
 (0)