Skip to content

Commit 4e55afc

Browse files
Only generate unique test setup statements (#161)
1 parent ed40914 commit 4e55afc

File tree

4 files changed

+209
-2
lines changed

4 files changed

+209
-2
lines changed

src/Generators/TestGenerator.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ protected function buildTestCases(Controller $controller)
371371
}
372372
$call .= ');';
373373

374-
$body = implode(PHP_EOL . PHP_EOL, array_map([$this, 'buildLines'], array_filter($setup)));
374+
$body = implode(PHP_EOL . PHP_EOL, array_map([$this, 'buildLines'], $this->uniqueSetupLines($setup)));
375375
$body .= PHP_EOL . PHP_EOL;
376376
$body .= str_pad(' ', 8) . $call;
377377
$body .= PHP_EOL . PHP_EOL;
@@ -448,7 +448,7 @@ private function buildFormRequestName(Controller $controller, string $name)
448448
return $controller->name() . Str::studly($name) . 'Request';
449449
}
450450

451-
return $controller->namespace() .'\\'. $controller->name() . Str::studly($name) . 'Request';
451+
return $controller->namespace() . '\\' . $controller->name() . Str::studly($name) . 'Request';
452452
}
453453

454454
private function buildFormRequestTestCase(string $controller, string $action, string $form_request)
@@ -572,4 +572,13 @@ private function splitField($field)
572572

573573
return [null, $field];
574574
}
575+
576+
private function uniqueSetupLines(array $setup)
577+
{
578+
return collect($setup)->filter()
579+
->map(function ($lines) {
580+
return array_unique($lines);
581+
})
582+
->toArray();
583+
}
575584
}

tests/Feature/Generator/TestGeneratorTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ public function controllerTreeDataProvider()
111111
return [
112112
['definitions/readme-example.bp', 'tests/Feature/Http/Controllers/PostControllerTest.php', 'tests/readme-example.php'],
113113
['definitions/respond-statements.bp', 'tests/Feature/Http/Controllers/Api/PostControllerTest.php', 'tests/respond-statements.php'],
114+
['definitions/full-crud-example.bp', 'tests/Feature/Http/Controllers/PostControllerTest.php', 'tests/full-crud-example.php'],
114115
];
115116
}
116117
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
models:
2+
Post:
3+
title: string:400
4+
content: longtext
5+
published_at: nullable timestamp
6+
7+
controllers:
8+
Post:
9+
index:
10+
query: all
11+
render: posts.index with:posts
12+
13+
create:
14+
render: posts.create with:post
15+
16+
store:
17+
validate: title, content
18+
save: post
19+
redirect: posts.index
20+
21+
show:
22+
find: post.id
23+
render: posts.show with:post
24+
25+
edit:
26+
find: post.id
27+
render: posts.edit with:post
28+
29+
update:
30+
validate: title, content
31+
find: post.id
32+
save: post
33+
redirect: posts.index
34+
35+
destroy:
36+
find: post.id
37+
delete: post
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
<?php
2+
3+
namespace Tests\Feature\Http\Controllers;
4+
5+
use App\Post;
6+
use Illuminate\Foundation\Testing\RefreshDatabase;
7+
use Illuminate\Foundation\Testing\WithFaker;
8+
use JMac\Testing\Traits\AdditionalAssertions;
9+
use Tests\TestCase;
10+
11+
/**
12+
* @see \App\Http\Controllers\PostController
13+
*/
14+
class PostControllerTest extends TestCase
15+
{
16+
use AdditionalAssertions, RefreshDatabase, WithFaker;
17+
18+
/**
19+
* @test
20+
*/
21+
public function index_displays_view()
22+
{
23+
$posts = factory(Post::class, 3)->create();
24+
25+
$response = $this->get(route('post.index'));
26+
27+
$response->assertOk();
28+
$response->assertViewIs('posts.index');
29+
$response->assertViewHas('posts');
30+
}
31+
32+
33+
/**
34+
* @test
35+
*/
36+
public function create_displays_view()
37+
{
38+
$response = $this->get(route('post.create'));
39+
40+
$response->assertOk();
41+
$response->assertViewIs('posts.create');
42+
$response->assertViewHas('post');
43+
}
44+
45+
46+
/**
47+
* @test
48+
*/
49+
public function store_uses_form_request_validation()
50+
{
51+
$this->assertActionUsesFormRequest(
52+
\App\Http\Controllers\PostController::class,
53+
'store',
54+
\App\Http\Requests\PostStoreRequest::class
55+
);
56+
}
57+
58+
/**
59+
* @test
60+
*/
61+
public function store_saves_and_redirects()
62+
{
63+
$title = $this->faker->sentence(4);
64+
$content = $this->faker->paragraphs(3, true);
65+
66+
$response = $this->post(route('post.store'), [
67+
'title' => $title,
68+
'content' => $content,
69+
]);
70+
71+
$posts = Post::query()
72+
->where('title', $title)
73+
->where('content', $content)
74+
->get();
75+
$this->assertCount(1, $posts);
76+
$post = $posts->first();
77+
78+
$response->assertRedirect(route('posts.index'));
79+
}
80+
81+
82+
/**
83+
* @test
84+
*/
85+
public function show_displays_view()
86+
{
87+
$post = factory(Post::class)->create();
88+
89+
$response = $this->get(route('post.show', $post));
90+
91+
$response->assertOk();
92+
$response->assertViewIs('posts.show');
93+
$response->assertViewHas('post');
94+
}
95+
96+
97+
/**
98+
* @test
99+
*/
100+
public function edit_displays_view()
101+
{
102+
$post = factory(Post::class)->create();
103+
104+
$response = $this->get(route('post.edit', $post));
105+
106+
$response->assertOk();
107+
$response->assertViewIs('posts.edit');
108+
$response->assertViewHas('post');
109+
}
110+
111+
112+
/**
113+
* @test
114+
*/
115+
public function update_uses_form_request_validation()
116+
{
117+
$this->assertActionUsesFormRequest(
118+
\App\Http\Controllers\PostController::class,
119+
'update',
120+
\App\Http\Requests\PostUpdateRequest::class
121+
);
122+
}
123+
124+
/**
125+
* @test
126+
*/
127+
public function update_saves_and_redirects()
128+
{
129+
$post = factory(Post::class)->create();
130+
$title = $this->faker->sentence(4);
131+
$content = $this->faker->paragraphs(3, true);
132+
133+
$response = $this->put(route('post.update', $post), [
134+
'title' => $title,
135+
'content' => $content,
136+
]);
137+
138+
$posts = Post::query()
139+
->where('title', $title)
140+
->where('content', $content)
141+
->get();
142+
$this->assertCount(1, $posts);
143+
$post = $posts->first();
144+
145+
$response->assertRedirect(route('posts.index'));
146+
}
147+
148+
149+
/**
150+
* @test
151+
*/
152+
public function destroy_deletes()
153+
{
154+
$post = factory(Post::class)->create();
155+
156+
$response = $this->delete(route('post.destroy', $post));
157+
158+
$this->assertDeleted($post);
159+
}
160+
}

0 commit comments

Comments
 (0)