Skip to content

Commit 86eb7e7

Browse files
committed
BugFix: Views path when sub-directory is specified, is generated incorrectly
Use same path for views and routs when generating views
1 parent 4153600 commit 86eb7e7

File tree

3 files changed

+68
-15
lines changed

3 files changed

+68
-15
lines changed

src/Console/Commands/ViewMakeCommand.php

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,15 @@ protected function getStub($fileName = null)
180180
protected function replacePlaceholders($stub, $name, $path = null)
181181
{
182182
$modelSlug = Str::slug(Str::plural(str_to_words($name), 2));
183+
184+
$dir = $this->option('dir');
185+
if ($dir) {
186+
$dir = str_replace('/', '.', $dir);
187+
$viewDir = $dir . '.' . $modelSlug;
188+
} else {
189+
$viewDir = $modelSlug;
190+
}
191+
183192
$viewLabel = str_to_words($name);
184193
$viewLabelPlural = Str::plural(str_to_words($name));
185194
$viewName = Str::camel($name);
@@ -192,20 +201,10 @@ protected function replacePlaceholders($stub, $name, $path = null)
192201
'$model$' => $name,
193202
'$rows$' => '$' . Str::camel(Str::plural($name, 2)),
194203
'$row$' => '$' . Str::camel(Str::singular($name)),
195-
'$routeBase$' => $modelSlug,
196-
'$viewDir$' => $modelSlug,
204+
'$routeBase$' => $viewDir,
205+
'$viewDir$' => $viewDir,
197206
]);
198207

199-
$dir = $this->option('dir');
200-
201-
if ($dir) {
202-
$dir = str_replace('/', '.', $dir);
203-
$stub = str_replace('$dir$', $dir . '.', $stub);
204-
} else {
205-
$stub = str_replace('$dir$', '', $stub);
206-
}
207-
208-
209208
return str_replace(
210209
array_keys($replace), array_values($replace), $stub
211210
);

tests/Feature/CrayCommandTest.php

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function test_it_scaffolds_crud_artifacts()
4040
$this->assertFileExists(app_path('Http/Requests/PostUpdateRequest.php'));
4141
$this->assertFileExists(app_path('Http/Requests/PostStoreRequest.php'));
4242
$this->assertFileExists(base_path('database/factories/PostFactory.php'));
43-
$this->assertFileExists(resource_path('views/posts'));
43+
$this->assertDirectoryDoesNotExist(resource_path('views/posts'));
4444
$this->assertFileExists(resource_path("views/posts/index.blade.php"));
4545
$this->assertFileExists(resource_path("views/posts/create.blade.php"));
4646
$this->assertFileExists(resource_path("views/posts/_form.blade.php"));
@@ -73,7 +73,7 @@ public function test_it_scaffolds_crud_artifacts_model_in_models_dir_with_namesp
7373
$this->assertFileDoesNotExist(app_path('Http/Requests/PostUpdateRequest.php'));
7474
$this->assertFileDoesNotExist(app_path('Http/Requests/PostStoreRequest.php'));
7575
$this->assertFileDoesNotExist(base_path('database/factories/PostFactory.php'));
76-
$this->assertFileDoesNotExist(resource_path('views/posts'));
76+
$this->assertDirectoryDoesNotExist(resource_path('views/posts'));
7777

7878
$this->artisan('cray Models/Post');
7979

@@ -82,7 +82,7 @@ public function test_it_scaffolds_crud_artifacts_model_in_models_dir_with_namesp
8282
$this->assertFileExists(app_path('Http/Requests/PostUpdateRequest.php'));
8383
$this->assertFileExists(app_path('Http/Requests/PostStoreRequest.php'));
8484
$this->assertFileExists(base_path('database/factories/PostFactory.php'));
85-
$this->assertFileExists(resource_path('views/posts'));
85+
$this->assertDirectoryExists(resource_path('views/posts'));
8686
$this->assertFileExists(resource_path("views/posts/index.blade.php"));
8787
$this->assertFileExists(resource_path("views/posts/create.blade.php"));
8888
$this->assertFileExists(resource_path("views/posts/_form.blade.php"));
@@ -106,4 +106,53 @@ public function test_it_scaffolds_crud_artifacts_model_in_models_dir_with_namesp
106106
Request created successfully in /app/Http/Requests/PostUpdateRequest.php" . PHP_EOL;
107107
$this->assertSame($expectedOutput, $actualOutput);
108108
}
109+
110+
public function test_it_generates_views_and_the_controller_under_the_given_directory_when_controller_directory_is_specified()
111+
{
112+
//Make sure no artifact related to Post exists
113+
$this->assertFileDoesNotExist(app_path('Models/Post.php'));
114+
$this->assertFileDoesNotExist(app_path('Http/Controllers/Dashboard/PostController.php'));
115+
$this->assertFileDoesNotExist(app_path('Http/Requests/PostUpdateRequest.php'));
116+
$this->assertFileDoesNotExist(app_path('Http/Requests/PostStoreRequest.php'));
117+
$this->assertFileDoesNotExist(base_path('database/factories/PostFactory.php'));
118+
$this->assertDirectoryDoesNotExist(resource_path('views/dashboard/posts'));
119+
120+
$this->artisan('cray Models/Post --controller-dir=dashboard --views-dir=dashboard');
121+
122+
$this->assertFileExists(app_path('Models/Post.php'));
123+
$this->assertFileExists(app_path('Http/Controllers/Dashboard/PostController.php'));
124+
$this->assertFileExists(app_path('Http/Requests/PostUpdateRequest.php'));
125+
$this->assertFileExists(app_path('Http/Requests/PostStoreRequest.php'));
126+
$this->assertFileExists(base_path('database/factories/PostFactory.php'));
127+
$this->assertDirectoryExists(resource_path('views/dashboard/posts'));
128+
$this->assertFileExists(resource_path("views/dashboard/posts/index.blade.php"));
129+
$this->assertFileExists(resource_path("views/dashboard/posts/create.blade.php"));
130+
$this->assertFileExists(resource_path("views/dashboard/posts/_form.blade.php"));
131+
$this->assertFileExists(resource_path("views/dashboard/posts/edit.blade.php"));
132+
$this->assertFileExists(resource_path("views/dashboard/posts/show.blade.php"));
133+
$this->assertFileExists(resource_path("views/dashboard/posts/modals/delete.blade.php"));
134+
135+
$actualOutput = Artisan::output();
136+
137+
$expectedOutput = "Factory created successfully in /database/factories/PostFactory.php
138+
Created Migration: 2020_03_14_153546_create_posts_table
139+
Model created successfully in /app/Models/Post.php
140+
Controller created successfully in /app/Http/Controllers/Dashboard/PostController.php
141+
View created successfully in /resources/views/dashboard/posts/index.blade.php
142+
View created successfully in /resources/views/dashboard/posts/create.blade.php
143+
View created successfully in /resources/views/dashboard/posts/_form.blade.php
144+
View created successfully in /resources/views/dashboard/posts/edit.blade.php
145+
View created successfully in /resources/views/dashboard/posts/show.blade.php
146+
View created successfully in /resources/views/dashboard/posts/modals/delete.blade.php
147+
Request created successfully in /app/Http/Requests/PostStoreRequest.php
148+
Request created successfully in /app/Http/Requests/PostUpdateRequest.php" . PHP_EOL;
149+
$this->assertSame($expectedOutput, $actualOutput);
150+
}
151+
152+
public function test_it_generates_view_paths_correctly_when_subdirectory_is_specified_for_the_controller()
153+
{
154+
$this->artisan('cray Models/Post --controller-dir=dashboard --views-dir=dashboard/system');
155+
$createBladeView = file_get_contents(resource_path('views/dashboard/system/posts/create.blade.php'));
156+
$this->assertStringContainsString("@include('dashboard.system.posts._form')", $createBladeView,'Include path is correct');
157+
}
109158
}

tests/TestCase.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ public function removeGeneratedFiles()
3939
unlink(resource_path('views/posts'));
4040
}
4141

42+
if (file_exists(resource_path('views/dashboard/posts'))) {
43+
44+
$this->rmdirRecursive(resource_path('views/dashboard/posts'));
45+
}
46+
4247
if (file_exists(base_path('database/factories/PostFactory.php'))) {
4348
unlink(base_path('database/factories/PostFactory.php'));
4449
}

0 commit comments

Comments
 (0)