Skip to content

Commit b3f56b9

Browse files
authored
Merge pull request #91 from RonasIT/fix-nova-test-template
fix: nova model state name in test template
2 parents 78b3590 + 4c8696a commit b3f56b9

File tree

9 files changed

+88
-101
lines changed

9 files changed

+88
-101
lines changed

src/Generators/NovaTestGenerator.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ public function generateTests(): void
4747
'url_path' => Str::kebab($this->model) . '-resources',
4848
'entity' => $this->model,
4949
'entities' => $this->getPluralName($this->model),
50-
'lower_entity' => Str::snake($this->model),
50+
'snake_entity' => Str::snake($this->model),
51+
'dromedary_entity' => Str::lcfirst($this->model),
5152
'lower_entities' => $this->getPluralName(Str::snake($this->model)),
5253
'actions' => $actions,
5354
'filters' => $filters,

stubs/nova_test.blade.php

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,21 @@ class Nova{{$entity}}Test extends TestCase
1313
use NovaTestTrait;
1414

1515
protected static User $user;
16-
protected static ModelTestState ${{$lower_entity}}State;
16+
protected static ModelTestState ${{$dromedary_entity}}State;
1717

1818
public function setUp(): void
1919
{
2020
parent::setUp();
2121

2222
self::$user ??= User::find(1);
23-
self::${{$lower_entity}}State ??= new ModelTestState({{$entity}}::class);
23+
self::${{$dromedary_entity}}State ??= new ModelTestState({{$entity}}::class);
2424

2525
$this->skipDocumentationCollecting();
2626
}
2727

2828
public function testCreate(): void
2929
{
30-
$data = $this->getJsonFixture('create_{{$lower_entity}}_request.json');
30+
$data = $this->getJsonFixture('create_{{$snake_entity}}_request.json');
3131

3232
$response = $this->actingAs(self::$user, 'web')->json('post', '/nova-api/{{$url_path}}', $data);
3333

@@ -37,25 +37,23 @@ public function testCreate(): void
3737
$response->assertCreated();
3838
@endif
3939

40-
$this->assertEqualsFixture('create_{{$lower_entity}}_response.json', $response->json());
40+
$this->assertEqualsFixture('create_{{$snake_entity}}_response.json', $response->json());
4141

4242
// TODO: Need to remove after first successful start
43-
self::${{$lower_entity}}State->assertChangesEqualsFixture('create_{{$lower_entities}}_state.json', true);
43+
self::${{$dromedary_entity}}State->assertChangesEqualsFixture('create_{{$lower_entities}}_state.json', true);
4444
}
4545

4646
public function testCreateNoAuth(): void
4747
{
48-
$data = $this->getJsonFixture('create_{{$lower_entity}}_request.json');
49-
50-
$response = $this->json('post', '/nova-api/{{$url_path}}', $data);
48+
$response = $this->json('post', '/nova-api/{{$url_path}}');
5149

5250
@if($shouldUseStatus)
5351
$response->assertStatus(Response::HTTP_UNAUTHORIZED);
5452
@else
5553
$response->assertUnauthorized();
5654
@endif
5755

58-
self::${{$lower_entity}}State->assertNotChanged();
56+
self::${{$dromedary_entity}}State->assertNotChanged();
5957
}
6058

6159
public function testCreateValidationError(): void
@@ -71,12 +69,12 @@ public function testCreateValidationError(): void
7169
// TODO: Need to remove after first successful start
7270
$this->assertEqualsFixture('create_validation_response.json', $response->json(), true);
7371

74-
self::${{$lower_entity}}State->assertNotChanged();
72+
self::${{$dromedary_entity}}State->assertNotChanged();
7573
}
7674

7775
public function testUpdate(): void
7876
{
79-
$data = $this->getJsonFixture('update_{{$lower_entity}}_request.json');
77+
$data = $this->getJsonFixture('update_{{$snake_entity}}_request.json');
8078

8179
$response = $this->actingAs(self::$user, 'web')->json('put', '/nova-api/{{$url_path}}/1', $data);
8280

@@ -87,12 +85,12 @@ public function testUpdate(): void
8785
@endif
8886

8987
// TODO: Need to remove after first successful start
90-
self::${{$lower_entity}}State->assertChangesEqualsFixture('update_{{$lower_entities}}_state.json', true);
88+
self::${{$dromedary_entity}}State->assertChangesEqualsFixture('update_{{$lower_entities}}_state.json', true);
9189
}
9290

9391
public function testUpdateNotExists(): void
9492
{
95-
$data = $this->getJsonFixture('update_{{$lower_entity}}_request.json');
93+
$data = $this->getJsonFixture('update_{{$snake_entity}}_request.json');
9694

9795
$response = $this->actingAs(self::$user, 'web')->json('put', '/nova-api/{{$url_path}}/0', $data);
9896

@@ -105,9 +103,7 @@ public function testUpdateNotExists(): void
105103

106104
public function testUpdateNoAuth(): void
107105
{
108-
$data = $this->getJsonFixture('update_{{$lower_entity}}_request.json');
109-
110-
$response = $this->json('put', '/nova-api/{{$url_path}}/1', $data);
106+
$response = $this->json('put', '/nova-api/{{$url_path}}/1');
111107

112108
@if($shouldUseStatus)
113109
$response->assertStatus(Response::HTTP_UNAUTHORIZED);
@@ -157,7 +153,7 @@ public function testDelete(): void
157153
@endif
158154

159155
// TODO: Need to remove after first successful start
160-
self::${{$lower_entity}}State->assertChangesEqualsFixture('delete_{{$lower_entities}}_state.json', true);
156+
self::${{$dromedary_entity}}State->assertChangesEqualsFixture('delete_{{$lower_entities}}_state.json', true);
161157
}
162158

163159
public function testDeleteNotExists(): void
@@ -197,7 +193,7 @@ public function testGet(): void
197193
@endif
198194

199195
// TODO: Need to remove after first successful start
200-
$this->assertEqualsFixture('get_{{$lower_entity}}_response.json', $response->json(), true);
196+
$this->assertEqualsFixture('get_{{$snake_entity}}_response.json', $response->json(), true);
201197
}
202198

203199
public function testGetNotExists(): void
@@ -224,10 +220,7 @@ public function testGetNoAuth(): void
224220

225221
public function testSearchUnauthorized(): void
226222
{
227-
$response = $this->json('get', '/nova-api/{{$url_path}}', [
228-
'orderBy' => 'id',
229-
'orderByDirection' => 'asc'
230-
]);
223+
$response = $this->json('get', '/nova-api/{{$url_path}}');
231224

232225
@if($shouldUseStatus)
233226
$response->assertStatus(Response::HTTP_UNAUTHORIZED);
@@ -281,7 +274,7 @@ public function testRun{{$entity}}Actions($action, $request, ${{$lower_entities}
281274
$this->assertEmpty($response->getContent());
282275

283276
// TODO: Need to remove after first successful start
284-
self::${{$lower_entity}}State->assertChangesEqualsFixture(${{$lower_entities}}StateFixture, true);
277+
self::${{$dromedary_entity}}State->assertChangesEqualsFixture(${{$lower_entities}}StateFixture, true);
285278
}
286279

287280
public function get{{$entity}}ActionsData(): array
@@ -292,7 +285,7 @@ public function get{{$entity}}ActionsData(): array
292285
'request' => [
293286
'resources' => '1,2',
294287
],
295-
'response_fixture' => 'get_{{$lower_entity}}_actions_{{$action['fixture']}}.json',
288+
'response_fixture' => 'get_{{$snake_entity}}_actions_{{$action['fixture']}}.json',
296289
],
297290
@endforeach
298291
];
@@ -323,7 +316,7 @@ public function get{{$entity}}FiltersData(): array
323316
'request' => [
324317
'{{$filter['name']}}' => $this->novaSearchParams(['search term']),
325318
],
326-
'response_fixture' => 'filter_{{$lower_entity}}_by_{{$filter['fixture_name']}}.json',
319+
'response_fixture' => 'filter_{{$snake_entity}}_by_{{$filter['fixture_name']}}.json',
327320
],
328321
@endforeach
329322
];

tests/NovaTestGeneratorTest.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ public function testCreateNovaTestsResourceNotExists()
1717
$mock = $this->mockClassExistsFunction();
1818

1919
$this->expectException(ClassNotExistsException::class);
20-
$this->expectExceptionMessage("Cannot create NovaPostTest cause Post Nova resource does not exist. Create Post Nova resource.");
20+
$this->expectExceptionMessage("Cannot create NovaWelcomeBonusTest cause WelcomeBonus Nova resource does not exist. Create WelcomeBonus Nova resource.");
2121

2222
$generatorMock = $this->getGeneratorMockForNonExistingNovaResource();
2323

2424
try {
2525
$generatorMock
26-
->setModel('Post')
26+
->setModel('WelcomeBonus')
2727
->generate();
2828
} finally {
2929
$mock->disable();
@@ -37,13 +37,13 @@ public function testCreateNovaTestAlreadyExists()
3737
$mock = $this->mockClassExistsFunction();
3838

3939
$this->expectException(ClassAlreadyExistsException::class);
40-
$this->expectExceptionMessage("Cannot create NovaPostTest cause it's already exist. Remove NovaPostTest.");
40+
$this->expectExceptionMessage("Cannot create NovaWelcomeBonusTest cause it's already exist. Remove NovaWelcomeBonusTest.");
4141

4242
$generatorMock = $this->getGeneratorMockForExistingNovaResourceTest();
4343

4444
try {
4545
$generatorMock
46-
->setModel('Post')
46+
->setModel('WelcomeBonus')
4747
->generate();
4848
} finally {
4949
$mock->disable();
@@ -60,16 +60,16 @@ public function testCreateWithActions()
6060
$this->mockNovaResourceTestGenerator();
6161

6262
app(NovaTestGenerator::class)
63-
->setModel('Post')
63+
->setModel('WelcomeBonus')
6464
->generate();
6565

6666
$this->rollbackToDefaultBasePath();
6767

68-
$this->assertGeneratedFileEquals('created_resource_test.php', 'tests/NovaPostTest.php');
69-
$this->assertGeneratedFileEquals('dump.sql', 'tests/fixtures/NovaPostTest/nova_post_dump.sql');
70-
$this->assertGeneratedFileEquals('create_post_request.json', 'tests/fixtures/NovaPostTest/create_post_request.json');
71-
$this->assertGeneratedFileEquals('create_post_response.json', 'tests/fixtures/NovaPostTest/create_post_response.json');
72-
$this->assertGeneratedFileEquals('update_post_request.json', 'tests/fixtures/NovaPostTest/update_post_request.json');
68+
$this->assertGeneratedFileEquals('created_resource_test.php', 'tests/NovaWelcomeBonusTest.php');
69+
$this->assertGeneratedFileEquals('dump.sql', 'tests/fixtures/NovaWelcomeBonusTest/nova_welcome_bonus_dump.sql');
70+
$this->assertGeneratedFileEquals('create_welcome_bonus_request.json', 'tests/fixtures/NovaWelcomeBonusTest/create_welcome_bonus_request.json');
71+
$this->assertGeneratedFileEquals('create_welcome_bonus_response.json', 'tests/fixtures/NovaWelcomeBonusTest/create_welcome_bonus_response.json');
72+
$this->assertGeneratedFileEquals('update_welcome_bonus_request.json', 'tests/fixtures/NovaWelcomeBonusTest/update_welcome_bonus_request.json');
7373

7474
$functionMock->disable();
7575
}

tests/Support/NovaTestMockTrait.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -85,23 +85,23 @@ public function getGeneratorMockForExistingNovaResourceTest(): MockInterface
8585
$mock
8686
->shouldAllowMockingProtectedMethods()
8787
->shouldReceive('classExists')
88-
->with('nova', 'Post')
88+
->with('nova', 'WelcomeBonus')
8989
->andReturn(true);
9090

9191
$mock
9292
->shouldReceive('classExists')
93-
->with('nova', 'PostResource')
93+
->with('nova', 'WelcomeBonusResource')
9494
->andReturn(false);
9595

9696
$mock
9797
->shouldReceive('classExists')
98-
->with('nova', 'PostNovaResource')
98+
->with('nova', 'WelcomeBonusNovaResource')
9999
->andReturn(false);
100100

101101
$mock
102102
->shouldReceive('classExists')
103103
->once()
104-
->with('nova', 'NovaPostTest')
104+
->with('nova', 'NovaWelcomeBonusTest')
105105
->andReturn(true);
106106

107107
return $mock;
@@ -137,20 +137,20 @@ public function mockFilesystem(): void
137137
'app' => [
138138
'Nova' => [
139139
'Actions' => [
140-
'PublishPostAction.php' => '<?php',
141-
'ArchivePostAction.php' => '<?php',
140+
'PublishWelcomeBonusAction.php' => '<?php',
141+
'ArchiveWelcomeBonusAction.php' => '<?php',
142142
'BlockCommentAction.php' => '<?php',
143-
'UnPublishPostAction.txt' => 'text',
143+
'UnPublishWelcomeBonusAction.txt' => 'text',
144144
],
145-
'Post.php' => '<?php',
145+
'WelcomeBonus.php' => '<?php',
146146
],
147147
'Models' => [
148-
'Post.php' => '<?php',
148+
'WelcomeBonus.php' => '<?php',
149149
]
150150
],
151151
'tests' => [
152152
'fixtures' => [
153-
'NovaPostTest' => [],
153+
'NovaWelcomeBonusTest' => [],
154154
]
155155
]
156156
];

0 commit comments

Comments
 (0)