Skip to content

Commit 41e4a62

Browse files
authored
Fix incorrect imports when using a custom namespace for models (#186)
1 parent 3e41811 commit 41e4a62

File tree

6 files changed

+176
-1
lines changed

6 files changed

+176
-1
lines changed

src/Generators/ControllerGenerator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ private function buildMethods(Controller $controller)
8686

8787
if (in_array($name, ['edit', 'update', 'show', 'destroy'])) {
8888
$context = Str::singular($controller->prefix());
89-
$reference = config('blueprint.namespace') . '\\' . $context;
89+
$reference = $this->fullyQualifyModelReference($controller->namespace(), Str::camel($context));
9090
$variable = '$' . Str::camel($context);
9191

9292
// TODO: verify controller prefix references a model

tests/Feature/Generator/ControllerGeneratorTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,36 @@ public function output_writes_migration_for_controller_tree($definition, $path,
7070
$this->assertEquals(['created' => [$path]], $this->subject->output($tree));
7171
}
7272

73+
/**
74+
* @test
75+
*/
76+
public function output_generates_controllers_with_models_with_custom_namespace_correctly()
77+
{
78+
$definition = 'definitions/custom-models-namespace.bp';
79+
$path = 'app/Http/Controllers/TagController.php';
80+
$controller = 'controllers/custom-models-namespace.php';
81+
82+
$this->app['config']->set('blueprint.models_namespace', 'Models');
83+
84+
$this->files->expects('stub')
85+
->with('controller/class.stub')
86+
->andReturn(file_get_contents('stubs/controller/class.stub'));
87+
$this->files->expects('stub')
88+
->with('controller/method.stub')
89+
->andReturn(file_get_contents('stubs/controller/method.stub'));
90+
91+
$this->files->expects('exists')
92+
->with(dirname($path))
93+
->andReturnTrue();
94+
$this->files->expects('put')
95+
->with($path, $this->fixture($controller));
96+
97+
$tokens = $this->blueprint->parse($this->fixture($definition));
98+
$tree = $this->blueprint->analyze($tokens);
99+
100+
$this->assertEquals(['created' => [$path]], $this->subject->output($tree));
101+
}
102+
73103
/**
74104
* @test
75105
*/

tests/Feature/Generator/ModelGeneratorTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,42 @@ public function output_generates_models_with_guarded_property_when_config_option
243243
$this->assertEquals(['created' => ['app/Comment.php']], $this->subject->output($tree));
244244
}
245245

246+
/**
247+
* @test
248+
*/
249+
public function output_generates_models_with_custom_namespace_correctly()
250+
{
251+
$definition = 'definitions/custom-models-namespace.bp';
252+
$path = 'app/Models/Tag.php';
253+
$model = 'models/custom-models-namespace.php';
254+
255+
$this->app['config']->set('blueprint.models_namespace', 'Models');
256+
257+
$this->files->expects('stub')
258+
->with('model/class.stub')
259+
->andReturn(file_get_contents('stubs/model/class.stub'));
260+
$this->files->expects('stub')
261+
->with('model/fillable.stub')
262+
->andReturn(file_get_contents('stubs/model/fillable.stub'));
263+
$this->files->expects('stub')
264+
->with('model/casts.stub')
265+
->andReturn(file_get_contents('stubs/model/casts.stub'));
266+
$this->files->expects('stub')
267+
->with('model/method.stub')
268+
->andReturn(file_get_contents('stubs/model/method.stub'));
269+
270+
$this->files->expects('exists')
271+
->with('app/Models')
272+
->andReturnTrue();
273+
$this->files->expects('put')
274+
->with($path, $this->fixture($model));
275+
276+
$tokens = $this->blueprint->parse($this->fixture($definition));
277+
$tree = $this->blueprint->analyze($tokens);
278+
279+
$this->assertEquals(['created' => [$path]], $this->subject->output($tree));
280+
}
281+
246282
public function modelTreeDataProvider()
247283
{
248284
return [
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
namespace App\Http\Controllers;
4+
5+
use App\Http\Requests\TagStoreRequest;
6+
use App\Http\Requests\TagUpdateRequest;
7+
use App\Http\Resources\Tag as TagResource;
8+
use App\Http\Resources\TagCollection;
9+
use App\Models\Tag;
10+
use Illuminate\Http\Request;
11+
12+
class TagController extends Controller
13+
{
14+
/**
15+
* @param \Illuminate\Http\Request $request
16+
* @return \App\Http\Resources\TagCollection
17+
*/
18+
public function index(Request $request)
19+
{
20+
$tags = Tag::all();
21+
22+
return new TagCollection($tags);
23+
}
24+
25+
/**
26+
* @param \App\Http\Requests\TagStoreRequest $request
27+
* @return \App\Http\Resources\Tag
28+
*/
29+
public function store(TagStoreRequest $request)
30+
{
31+
$tag = Tag::create($request->all());
32+
33+
return new TagResource($tag);
34+
}
35+
36+
/**
37+
* @param \Illuminate\Http\Request $request
38+
* @param \App\Models\Tag $tag
39+
* @return \App\Http\Resources\Tag
40+
*/
41+
public function show(Request $request, Tag $tag)
42+
{
43+
return new TagResource($tag);
44+
}
45+
46+
/**
47+
* @param \App\Http\Requests\TagUpdateRequest $request
48+
* @param \App\Models\Tag $tag
49+
* @return \App\Http\Resources\Tag
50+
*/
51+
public function update(TagUpdateRequest $request, Tag $tag)
52+
{
53+
return new TagResource($tag);
54+
}
55+
56+
/**
57+
* @param \Illuminate\Http\Request $request
58+
* @param \App\Models\Tag $tag
59+
* @return \Illuminate\Http\Response
60+
*/
61+
public function destroy(Request $request, Tag $tag)
62+
{
63+
$tag->delete();
64+
65+
return response()->noContent(200);
66+
}
67+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
models:
2+
Tag:
3+
name: string:50
4+
icon: string nullable
5+
active: boolean default:true
6+
softDeletes
7+
8+
controllers:
9+
Tag:
10+
resource: api
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
use Illuminate\Database\Eloquent\SoftDeletes;
7+
8+
class Tag extends Model
9+
{
10+
use SoftDeletes;
11+
12+
/**
13+
* The attributes that are mass assignable.
14+
*
15+
* @var array
16+
*/
17+
protected $fillable = [
18+
'name',
19+
'icon',
20+
'active',
21+
];
22+
23+
/**
24+
* The attributes that should be cast to native types.
25+
*
26+
* @var array
27+
*/
28+
protected $casts = [
29+
'id' => 'integer',
30+
'active' => 'boolean',
31+
];
32+
}

0 commit comments

Comments
 (0)