Skip to content

Commit c1218a1

Browse files
authored
Fixing missing use in CRUDdy controller tests
1 parent 3e23519 commit c1218a1

File tree

4 files changed

+54
-4
lines changed

4 files changed

+54
-4
lines changed

src/Generators/TestGenerator.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ protected function buildTestCases(Controller $controller)
107107
: config('blueprint.namespace');
108108

109109
if (in_array($name, ['edit', 'update', 'show', 'destroy'])) {
110+
$this->addImport($controller, $modelNamespace . '\\' . $model);
111+
110112
$setup['data'][] = sprintf('$%s = %s::factory()->create();', $variable, $model);
111113
}
112114

tests/Feature/Generators/TestGeneratorTest.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,12 @@ public function output_generates_test_for_controller_tree_l8($definition, $path,
7070
->with($dirname, 0755, true);
7171

7272
$this->filesystem->expects('put')
73-
->with($path, $this->fixture($test));
73+
->with($path, $this->fixture($test));
7474
}
7575

7676
$tokens = $this->blueprint->parse($this->fixture($definition));
7777
$tree = $this->blueprint->analyze($tokens);
78+
7879
$this->assertEquals(['created' => array_keys($paths)], $this->subject->output($tree));
7980
}
8081

@@ -97,17 +98,20 @@ public function output_works_for_pascal_case_definition_l8()
9798
$this->filesystem->expects('exists')
9899
->with(dirname($certificateControllerTest))
99100
->andReturnTrue();
101+
100102
$this->filesystem->expects('put')
101103
->with($certificateControllerTest, $this->fixture('tests/certificate-pascal-case-example.php'));
102104

103105
$this->filesystem->expects('exists')
104106
->with(dirname($certificateTypeControllerTest))
105107
->andReturnTrue();
108+
106109
$this->filesystem->expects('put')
107110
->with($certificateTypeControllerTest, $this->fixture('tests/certificate-type-pascal-case-example.php'));
108111

109112
$tokens = $this->blueprint->parse($this->fixture('drafts/pascal-case.yaml'));
110113
$tree = $this->blueprint->analyze($tokens);
114+
111115
$this->assertEquals(['created' => [$certificateControllerTest, $certificateTypeControllerTest]], $this->subject->output($tree));
112116
}
113117

@@ -123,11 +127,14 @@ public function output_generates_test_for_controller_tree_using_cached_model_l8(
123127
$this->filesystem->expects('stub')
124128
->with('test.case.stub')
125129
->andReturn($this->stub('test.case.stub'));
130+
126131
$this->filesystem->expects('exists')
127132
->with('tests/Feature/Http/Controllers')
128133
->andReturnFalse();
134+
129135
$this->filesystem->expects('makeDirectory')
130136
->with('tests/Feature/Http/Controllers', 0755, true);
137+
131138
$this->filesystem->expects('put')
132139
->with('tests/Feature/Http/Controllers/UserControllerTest.php', $this->fixture('tests/reference-cache.php'));
133140

@@ -161,17 +168,21 @@ public function output_generates_tests_with_models_with_custom_namespace_correct
161168
$this->filesystem->expects('stub')
162169
->with('test.case.stub')
163170
->andReturn($this->stub('test.case.stub'));
171+
164172
$dirname = dirname($path);
165173
$this->filesystem->expects('exists')
166174
->with($dirname)
167175
->andReturnFalse();
176+
168177
$this->filesystem->expects('makeDirectory')
169-
->with($dirname, 0755, true);
178+
->with($dirname, 0755, true);
179+
170180
$this->filesystem->expects('put')
171181
->with($path, $this->fixture($test));
172182

173183
$tokens = $this->blueprint->parse($this->fixture($definition));
174184
$tree = $this->blueprint->analyze($tokens);
185+
175186
$this->assertEquals(['created' => [$path]], $this->subject->output($tree));
176187
}
177188

@@ -187,8 +198,8 @@ public function output_using_return_types()
187198
$this->app['config']->set('blueprint.use_return_types', true);
188199

189200
$this->filesystem->expects('stub')
190-
->with('test.class.stub')
191-
->andReturn($this->stub('test.class.stub'));
201+
->with('test.class.stub')
202+
->andReturn($this->stub('test.class.stub'));
192203

193204
$this->filesystem->expects('stub')
194205
->with('test.case.stub')
@@ -219,6 +230,7 @@ public function controllerTreeDataProvider()
219230
['drafts/readme-example-notification-model.yaml', 'tests/Feature/Http/Controllers/PostControllerTest.php', 'tests/readme-example-notification.php'],
220231
['drafts/respond-statements.yaml', 'tests/Feature/Http/Controllers/Api/PostControllerTest.php', 'tests/respond-statements.php'],
221232
['drafts/full-crud-example.yaml', 'tests/Feature/Http/Controllers/PostControllerTest.php', 'tests/full-crud-example.php'],
233+
['drafts/crud-show-only.yaml', 'tests/Feature/Http/Controllers/PostControllerTest.php', 'tests/crud-show-only.php'],
222234
['drafts/model-reference-validate.yaml', 'tests/Feature/Http/Controllers/CertificateControllerTest.php', 'tests/api-shorthand-validation.php'],
223235
['drafts/controllers-only-no-context.yaml', 'tests/Feature/Http/Controllers/ReportControllerTest.php', 'tests/controllers-only-no-context.php'],
224236
['drafts/call-to-a-member-function-columns-on-null.yaml', [
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
models:
2+
Post:
3+
title: string:400
4+
content: longtext
5+
published_at: nullable timestamp
6+
7+
controllers:
8+
Post:
9+
show:
10+
render: posts.show with:post
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Tests\Feature\Http\Controllers;
4+
5+
use App\Models\Post;
6+
use Tests\TestCase;
7+
8+
/**
9+
* @see \App\Http\Controllers\PostController
10+
*/
11+
class PostControllerTest extends TestCase
12+
{
13+
/**
14+
* @test
15+
*/
16+
public function show_displays_view()
17+
{
18+
$post = Post::factory()->create();
19+
20+
$response = $this->get(route('post.show', $post));
21+
22+
$response->assertOk();
23+
$response->assertViewIs('posts.show');
24+
$response->assertViewHas('post');
25+
}
26+
}

0 commit comments

Comments
 (0)