Skip to content

Commit 5e1b91a

Browse files
authored
Merge pull request #83 from RonasIT/fix-nova-test-generator
fix: generate nova test cases
2 parents a9682ad + 0c8efd1 commit 5e1b91a

File tree

5 files changed

+57
-51
lines changed

5 files changed

+57
-51
lines changed

src/Generators/AbstractTestsGenerator.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,17 @@ protected function createDump(): void
5555
mkdir_recursively($fixturePath);
5656
}
5757

58-
file_put_contents($this->getFixturesPath('dump.sql'), $content);
58+
$dumpName = $this->getDumpName();
59+
60+
file_put_contents($this->getFixturesPath($dumpName), $content);
5961

6062
event(new SuccessCreateMessage("Created a new Test dump on path: "
61-
. "{$this->paths['tests']}/fixtures/{$this->getTestClassName()}/dump.sql"));
63+
. "{$this->paths['tests']}/fixtures/{$this->getTestClassName()}/{$dumpName}"));
64+
}
65+
66+
protected function getDumpName(): string
67+
{
68+
return 'dump.sql';
6269
}
6370

6471
protected function getInserts(): array

src/Generators/NovaTestGenerator.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function generateTests(): void
4444
$filters = $this->collectFilters();
4545

4646
$fileContent = $this->getStub('nova_test', [
47-
'url_path' => $this->getPluralName(Str::kebab($this->model)),
47+
'url_path' => Str::kebab($this->model) . '-resources',
4848
'entity' => $this->model,
4949
'entities' => $this->getPluralName($this->model),
5050
'lower_entity' => Str::snake($this->model),
@@ -171,4 +171,11 @@ protected function getFilters(): array
171171

172172
return $filters;
173173
}
174+
175+
protected function getDumpName(): string
176+
{
177+
$modelName = Str::snake($this->model);
178+
179+
return "nova_{$modelName}_dump.sql";
180+
}
174181
}

stubs/nova_test.blade.php

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,20 @@
33

44
use App\Models\{{$entity}};
55
use RonasIT\Support\Tests\ModelTestState;
6-
use RonasIT\Support\Traits\AuthTestTrait;
76
@if($shouldUseStatus)
87
use Symfony\Component\HttpFoundation\Response;
98
@endif
109

1110
class Nova{{$entity}}Test extends TestCase
1211
{
13-
use AuthTestTrait;
14-
15-
protected static $user;
16-
protected static ${{$lower_entity}}State;
12+
protected static User $user;
13+
protected static ModelTestState ${{$lower_entity}}State;
1714

1815
public function setUp(): void
1916
{
2017
parent::setUp();
2118

22-
self::$user = 1;
19+
self::$user = User::find(1);
2320
self::${{$lower_entity}}State ??= new ModelTestState({{$entity}}::class);
2421

2522
$this->skipDocumentationCollecting();
@@ -29,7 +26,7 @@ public function testCreate(): void
2926
{
3027
$data = $this->getJsonFixture('create_{{$lower_entity}}_request.json');
3128

32-
$response = $this->actingViaSession(self::$user)->json('post', '/nova-api/{{$url_path}}', $data);
29+
$response = $this->actingAs(self::$user, 'web')->json('post', '/nova-api/{{$url_path}}', $data);
3330

3431
@if($shouldUseStatus)
3532
$response->assertStatus(Response::HTTP_CREATED);
@@ -60,7 +57,7 @@ public function testCreateNoAuth(): void
6057

6158
public function testCreateValidationError(): void
6259
{
63-
$response = $this->actingViaSession(self::$user)->json('post', '/nova-api/{{$url_path}}');
60+
$response = $this->actingAs(self::$user, 'web')->json('post', '/nova-api/{{$url_path}}');
6461

6562
@if($shouldUseStatus)
6663
$response->assertStatus(Response::HTTP_UNPROCESSABLE_ENTITY);
@@ -78,7 +75,7 @@ public function testUpdate(): void
7875
{
7976
$data = $this->getJsonFixture('update_{{$lower_entity}}_request.json');
8077

81-
$response = $this->actingViaSession(self::$user)->json('put', '/nova-api/{{$url_path}}/1', $data);
78+
$response = $this->actingAs(self::$user, 'web')->json('put', '/nova-api/{{$url_path}}/1', $data);
8279

8380
@if($shouldUseStatus)
8481
$response->assertStatus(Response::HTTP_NO_CONTENT);
@@ -94,7 +91,7 @@ public function testUpdateNotExists(): void
9491
{
9592
$data = $this->getJsonFixture('update_{{$lower_entity}}_request.json');
9693

97-
$response = $this->actingViaSession(self::$user)->json('put', '/nova-api/{{$url_path}}/0', $data);
94+
$response = $this->actingAs(self::$user, 'web')->json('put', '/nova-api/{{$url_path}}/0', $data);
9895

9996
@if($shouldUseStatus)
10097
$response->assertStatus(Response::HTTP_NOT_FOUND);
@@ -118,7 +115,7 @@ public function testUpdateNoAuth(): void
118115

119116
public function testUpdateValidationError(): void
120117
{
121-
$response = $this->actingViaSession(self::$user)->json('put', '/nova-api/{{$url_path}}/4');
118+
$response = $this->actingAs(self::$user, 'web')->json('put', '/nova-api/{{$url_path}}/4');
122119

123120
@if($shouldUseStatus)
124121
$response->assertStatus(Response::HTTP_UNPROCESSABLE_ENTITY);
@@ -132,7 +129,7 @@ public function testUpdateValidationError(): void
132129

133130
public function testGetUpdatableFields(): void
134131
{
135-
$response = $this->actingViaSession(self::$user)->json('get', '/nova-api/{{$url_path}}/1/update-fields');
132+
$response = $this->actingAs(self::$user, 'web')->json('get', '/nova-api/{{$url_path}}/1/update-fields');
136133

137134
@if($shouldUseStatus)
138135
$response->assertStatus(Response::HTTP_OK);
@@ -146,7 +143,7 @@ public function testGetUpdatableFields(): void
146143

147144
public function testDelete(): void
148145
{
149-
$response = $this->actingViaSession(self::$user)->json('delete', '/nova-api/{{$url_path}}', [
146+
$response = $this->actingAs(self::$user, 'web')->json('delete', '/nova-api/{{$url_path}}', [
150147
'resources' => [1, 2]
151148
]);
152149

@@ -162,7 +159,7 @@ public function testDelete(): void
162159

163160
public function testDeleteNotExists(): void
164161
{
165-
$response = $this->actingViaSession(self::$user)->json('delete', '/nova-api/{{$url_path}}', [
162+
$response = $this->actingAs(self::$user, 'web')->json('delete', '/nova-api/{{$url_path}}', [
166163
'resources' => [0]
167164
]);
168165

@@ -188,7 +185,7 @@ public function testDeleteNoAuth(): void
188185

189186
public function testGet(): void
190187
{
191-
$response = $this->actingViaSession(self::$user)->json('get', '/nova-api/{{$url_path}}/1');
188+
$response = $this->actingAs(self::$user, 'web')->json('get', '/nova-api/{{$url_path}}/1');
192189

193190
@if($shouldUseStatus)
194191
$response->assertStatus(Response::HTTP_OK);
@@ -202,7 +199,7 @@ public function testGet(): void
202199

203200
public function testGetNotExists(): void
204201
{
205-
$response = $this->actingViaSession(self::$user)->json('get', '/nova-api/{{$url_path}}/0');
202+
$response = $this->actingAs(self::$user, 'web')->json('get', '/nova-api/{{$url_path}}/0');
206203

207204
@if($shouldUseStatus)
208205
$response->assertStatus(Response::HTTP_NOT_FOUND);
@@ -238,7 +235,7 @@ public function testSearchUnauthorized(): void
238235

239236
public function testGetFieldsVisibleOnCreate(): void
240237
{
241-
$response = $this->actingViaSession(self::$user)->json('get', '/nova-api/{{$url_path}}/creation-fields');
238+
$response = $this->actingAs(self::$user, 'web')->json('get', '/nova-api/{{$url_path}}/creation-fields');
242239

243240
@if($shouldUseStatus)
244241
$response->assertStatus(Response::HTTP_OK);
@@ -270,8 +267,7 @@ public function getRun{{$entity}}ActionsData(): array
270267
*/
271268
public function testRun{{$entity}}Actions($action, $request, ${{$lower_entities}}StateFixture): void
272269
{
273-
$request['action'] = $action;
274-
$response = $this->actingViaSession(self::$user)->json('post', "/nova-api/{{$url_path}}/action", $request);
270+
$response = $this->actingAs(self::$user, 'web')->json('post', "/nova-api/{{$url_path}}/action?action={$action}", $request);
275271

276272
@if($shouldUseStatus)
277273
$response->assertStatus(Response::HTTP_OK);
@@ -304,7 +300,7 @@ public function get{{$entity}}ActionsData(): array
304300
*/
305301
public function testGet{{$entity}}Actions(array $request, string $responseFixture): void
306302
{
307-
$response = $this->actingViaSession(self::$user)->json('get', '/nova-api/{{$url_path}}/actions', $request);
303+
$response = $this->actingAs(self::$user, 'web')->json('get', '/nova-api/{{$url_path}}/actions', $request);
308304

309305
@if($shouldUseStatus)
310306
$response->assertStatus(Response::HTTP_OK);
@@ -335,7 +331,7 @@ public function get{{$entity}}FiltersData(): array
335331
*/
336332
public function testFilter{{$entity}}(array $filters, string $responseFixture): void
337333
{
338-
$response = $this->actingViaSession(self::$user)->json('get', '/nova-api/{{$url_path}}', [
334+
$response = $this->actingAs(self::$user, 'web')->json('get', '/nova-api/{{$url_path}}', [
339335
'filters' => base64_encode(json_encode($filters))
340336
]);
341337

tests/NovaTestGeneratorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public function testCreateWithActions()
7777
$this->rollbackToDefaultBasePath();
7878

7979
$this->assertGeneratedFileEquals('created_resource_test.php', 'tests/NovaPostTest.php');
80-
$this->assertGeneratedFileEquals('dump.sql', 'tests/fixtures/NovaPostTest/dump.sql');
80+
$this->assertGeneratedFileEquals('dump.sql', 'tests/fixtures/NovaPostTest/nova_post_dump.sql');
8181
$this->assertGeneratedFileEquals('create_post_request.json', 'tests/fixtures/NovaPostTest/create_post_request.json');
8282
$this->assertGeneratedFileEquals('create_post_response.json', 'tests/fixtures/NovaPostTest/create_post_response.json');
8383
$this->assertGeneratedFileEquals('update_post_request.json', 'tests/fixtures/NovaPostTest/update_post_request.json');

tests/fixtures/NovaTestGeneratorTest/created_resource_test.php

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,17 @@
44

55
use App\Models\Post;
66
use RonasIT\Support\Tests\ModelTestState;
7-
use RonasIT\Support\Traits\AuthTestTrait;
87

98
class NovaPostTest extends TestCase
109
{
11-
use AuthTestTrait;
12-
13-
protected static $user;
14-
protected static $postState;
10+
protected static User $user;
11+
protected static ModelTestState $postState;
1512

1613
public function setUp(): void
1714
{
1815
parent::setUp();
1916

20-
self::$user = 1;
17+
self::$user = User::find(1);
2118
self::$postState ??= new ModelTestState(Post::class);
2219

2320
$this->skipDocumentationCollecting();
@@ -27,7 +24,7 @@ public function testCreate(): void
2724
{
2825
$data = $this->getJsonFixture('create_post_request.json');
2926

30-
$response = $this->actingViaSession(self::$user)->json('post', '/nova-api/posts', $data);
27+
$response = $this->actingAs(self::$user, 'web')->json('post', '/nova-api/post-resources', $data);
3128

3229
$response->assertCreated();
3330

@@ -41,7 +38,7 @@ public function testCreateNoAuth(): void
4138
{
4239
$data = $this->getJsonFixture('create_post_request.json');
4340

44-
$response = $this->json('post', '/nova-api/posts', $data);
41+
$response = $this->json('post', '/nova-api/post-resources', $data);
4542

4643
$response->assertUnauthorized();
4744

@@ -50,7 +47,7 @@ public function testCreateNoAuth(): void
5047

5148
public function testCreateValidationError(): void
5249
{
53-
$response = $this->actingViaSession(self::$user)->json('post', '/nova-api/posts');
50+
$response = $this->actingAs(self::$user, 'web')->json('post', '/nova-api/post-resources');
5451

5552
$response->assertUnprocessable();
5653

@@ -64,7 +61,7 @@ public function testUpdate(): void
6461
{
6562
$data = $this->getJsonFixture('update_post_request.json');
6663

67-
$response = $this->actingViaSession(self::$user)->json('put', '/nova-api/posts/1', $data);
64+
$response = $this->actingAs(self::$user, 'web')->json('put', '/nova-api/post-resources/1', $data);
6865

6966
$response->assertNoContent();
7067

@@ -76,7 +73,7 @@ public function testUpdateNotExists(): void
7673
{
7774
$data = $this->getJsonFixture('update_post_request.json');
7875

79-
$response = $this->actingViaSession(self::$user)->json('put', '/nova-api/posts/0', $data);
76+
$response = $this->actingAs(self::$user, 'web')->json('put', '/nova-api/post-resources/0', $data);
8077

8178
$response->assertNotFound();
8279
}
@@ -85,14 +82,14 @@ public function testUpdateNoAuth(): void
8582
{
8683
$data = $this->getJsonFixture('update_post_request.json');
8784

88-
$response = $this->json('put', '/nova-api/posts/1', $data);
85+
$response = $this->json('put', '/nova-api/post-resources/1', $data);
8986

9087
$response->assertUnauthorized();
9188
}
9289

9390
public function testUpdateValidationError(): void
9491
{
95-
$response = $this->actingViaSession(self::$user)->json('put', '/nova-api/posts/4');
92+
$response = $this->actingAs(self::$user, 'web')->json('put', '/nova-api/post-resources/4');
9693

9794
$response->assertUnprocessable();
9895

@@ -102,7 +99,7 @@ public function testUpdateValidationError(): void
10299

103100
public function testGetUpdatableFields(): void
104101
{
105-
$response = $this->actingViaSession(self::$user)->json('get', '/nova-api/posts/1/update-fields');
102+
$response = $this->actingAs(self::$user, 'web')->json('get', '/nova-api/post-resources/1/update-fields');
106103

107104
$response->assertOk();
108105

@@ -112,7 +109,7 @@ public function testGetUpdatableFields(): void
112109

113110
public function testDelete(): void
114111
{
115-
$response = $this->actingViaSession(self::$user)->json('delete', '/nova-api/posts', [
112+
$response = $this->actingAs(self::$user, 'web')->json('delete', '/nova-api/post-resources', [
116113
'resources' => [1, 2]
117114
]);
118115

@@ -124,7 +121,7 @@ public function testDelete(): void
124121

125122
public function testDeleteNotExists(): void
126123
{
127-
$response = $this->actingViaSession(self::$user)->json('delete', '/nova-api/posts', [
124+
$response = $this->actingAs(self::$user, 'web')->json('delete', '/nova-api/post-resources', [
128125
'resources' => [0]
129126
]);
130127

@@ -133,7 +130,7 @@ public function testDeleteNotExists(): void
133130

134131
public function testDeleteNoAuth(): void
135132
{
136-
$response = $this->json('delete', '/nova-api/posts', [
133+
$response = $this->json('delete', '/nova-api/post-resources', [
137134
'resources' => [1, 2]
138135
]);
139136

@@ -142,7 +139,7 @@ public function testDeleteNoAuth(): void
142139

143140
public function testGet(): void
144141
{
145-
$response = $this->actingViaSession(self::$user)->json('get', '/nova-api/posts/1');
142+
$response = $this->actingAs(self::$user, 'web')->json('get', '/nova-api/post-resources/1');
146143

147144
$response->assertOk();
148145

@@ -152,21 +149,21 @@ public function testGet(): void
152149

153150
public function testGetNotExists(): void
154151
{
155-
$response = $this->actingViaSession(self::$user)->json('get', '/nova-api/posts/0');
152+
$response = $this->actingAs(self::$user, 'web')->json('get', '/nova-api/post-resources/0');
156153

157154
$response->assertNotFound();
158155
}
159156

160157
public function testGetNoAuth(): void
161158
{
162-
$response = $this->json('get', '/nova-api/posts/1');
159+
$response = $this->json('get', '/nova-api/post-resources/1');
163160

164161
$response->assertUnauthorized();
165162
}
166163

167164
public function testSearchUnauthorized(): void
168165
{
169-
$response = $this->json('get', '/nova-api/posts', [
166+
$response = $this->json('get', '/nova-api/post-resources', [
170167
'orderBy' => 'id',
171168
'orderByDirection' => 'asc'
172169
]);
@@ -176,7 +173,7 @@ public function testSearchUnauthorized(): void
176173

177174
public function testGetFieldsVisibleOnCreate(): void
178175
{
179-
$response = $this->actingViaSession(self::$user)->json('get', '/nova-api/posts/creation-fields');
176+
$response = $this->actingAs(self::$user, 'web')->json('get', '/nova-api/post-resources/creation-fields');
180177

181178
$response->assertOk();
182179

@@ -209,8 +206,7 @@ public function getRunPostActionsData(): array
209206
*/
210207
public function testRunPostActions($action, $request, $postsStateFixture): void
211208
{
212-
$request['action'] = $action;
213-
$response = $this->actingViaSession(self::$user)->json('post', "/nova-api/posts/action", $request);
209+
$response = $this->actingAs(self::$user, 'web')->json('post', "/nova-api/post-resources/action?action={$action}", $request);
214210

215211
$response->assertOk();
216212

@@ -243,7 +239,7 @@ public function getPostActionsData(): array
243239
*/
244240
public function testGetPostActions(array $request, string $responseFixture): void
245241
{
246-
$response = $this->actingViaSession(self::$user)->json('get', '/nova-api/posts/actions', $request);
242+
$response = $this->actingAs(self::$user, 'web')->json('get', '/nova-api/post-resources/actions', $request);
247243

248244
$response->assertOk();
249245

@@ -274,7 +270,7 @@ public function getPostFiltersData(): array
274270
*/
275271
public function testFilterPost(array $filters, string $responseFixture): void
276272
{
277-
$response = $this->actingViaSession(self::$user)->json('get', '/nova-api/posts', [
273+
$response = $this->actingAs(self::$user, 'web')->json('get', '/nova-api/post-resources', [
278274
'filters' => base64_encode(json_encode($filters))
279275
]);
280276

0 commit comments

Comments
 (0)