diff --git a/src/Generators/Statements/ResourceGenerator.php b/src/Generators/Statements/ResourceGenerator.php index 9d7a178e..11187407 100644 --- a/src/Generators/Statements/ResourceGenerator.php +++ b/src/Generators/Statements/ResourceGenerator.php @@ -42,7 +42,7 @@ public function output(Tree $tree): array continue; } - $path = $this->getPath($statement->name()); + $path = $this->getPath(($controller->namespace() ? $controller->namespace().'/' : '').$statement->name()); if ($this->files->exists($path)) { continue; @@ -52,7 +52,7 @@ public function output(Tree $tree): array $this->files->makeDirectory(dirname($path), 0755, true); } - $this->files->put($path, $this->populateStub($stub, $statement)); + $this->files->put($path, $this->populateStub($stub, $controller, $statement)); $output['created'][] = $path; } @@ -72,9 +72,13 @@ protected function getPath(string $name) return Blueprint::appPath().'/Http/Resources/'.$name.'.php'; } - protected function populateStub(string $stub, ResourceStatement $resource) + protected function populateStub(string $stub, Controller $controller, ResourceStatement $resource) { - $stub = str_replace('{{ namespace }}', config('blueprint.namespace').'\\Http\\Resources', $stub); + $namespace = config('blueprint.namespace') + .'\\Http\\Resources' + .($controller->namespace() ? '\\'.$controller->namespace() : ''); + + $stub = str_replace('{{ namespace }}', $namespace, $stub); $stub = str_replace('{{ import }}', $resource->collection() ? 'Illuminate\\Http\\Resources\\Json\\ResourceCollection' : 'Illuminate\\Http\\Resources\\Json\\JsonResource', $stub); $stub = str_replace('{{ parentClass }}', $resource->collection() ? 'ResourceCollection' : 'JsonResource', $stub); $stub = str_replace('{{ class }}', $resource->name(), $stub); diff --git a/tests/Feature/Generator/ControllerGeneratorTest.php b/tests/Feature/Generator/ControllerGeneratorTest.php index d5a214b0..cd3e78b4 100644 --- a/tests/Feature/Generator/ControllerGeneratorTest.php +++ b/tests/Feature/Generator/ControllerGeneratorTest.php @@ -175,6 +175,7 @@ public function controllerTreeDataProvider() ['drafts/respond-statements.yaml', 'app/Http/Controllers/Api/PostController.php', 'controllers/respond-statements.php'], ['drafts/resource-statements.yaml', 'app/Http/Controllers/UserController.php', 'controllers/resource-statements.php'], ['drafts/save-without-validation.yaml', 'app/Http/Controllers/PostController.php', 'controllers/save-without-validation.php'], + ['drafts/api-routes-example.yaml', 'app/Http/Controllers/Api/CertificateController.php', 'controllers/api-routes-example.php'], ]; } } diff --git a/tests/Feature/Generator/Statements/ResourceGeneratorTest.php b/tests/Feature/Generator/Statements/ResourceGeneratorTest.php index 646b8601..9c745237 100644 --- a/tests/Feature/Generator/Statements/ResourceGeneratorTest.php +++ b/tests/Feature/Generator/Statements/ResourceGeneratorTest.php @@ -100,4 +100,40 @@ public function output_writes_resources_for_render_statements() $this->assertEquals(['created' => ['app/Http/Resources/UserCollection.php', 'app/Http/Resources/User.php']], $this->subject->output($tree)); } + + /** + * @test + */ + public function output_writes_namespaced_classes() + { + $this->files->expects('stub') + ->with('resource.stub') + ->andReturn(file_get_contents('stubs/resource.stub')); + + $this->files->shouldReceive('exists') + ->with('app/Http/Resources/Api') + ->andReturns(false, true); + $this->files->expects('makeDirectory') + ->with('app/Http/Resources/Api', 0755, true); + + $this->files->expects('exists') + ->times(3) + ->with('app/Http/Resources/Api/Certificate.php') + ->andReturns(false, true, true); + $this->files->expects('put') + ->with('app/Http/Resources/Api/Certificate.php', $this->fixture('resources/certificate.php')); + + $this->files->expects('exists') + ->with('app/Http/Resources/Api/CertificateCollection.php') + ->andReturns(false); + $this->files->expects('put') + ->with('app/Http/Resources/Api/CertificateCollection.php', $this->fixture('resources/certificate-collection.php')); + + $tokens = $this->blueprint->parse($this->fixture('drafts/api-routes-example.yaml')); + $tree = $this->blueprint->analyze($tokens); + + $this->assertEquals([ + 'created' => ['app/Http/Resources/Api/CertificateCollection.php', 'app/Http/Resources/Api/Certificate.php'], + ], $this->subject->output($tree)); + } } diff --git a/tests/fixtures/controllers/api-routes-example.php b/tests/fixtures/controllers/api-routes-example.php new file mode 100644 index 00000000..66464c15 --- /dev/null +++ b/tests/fixtures/controllers/api-routes-example.php @@ -0,0 +1,70 @@ +validated()); + + return new CertificateResource($certificate); + } + + /** + * @param \Illuminate\Http\Request $request + * @param \App\Certificate $certificate + * @return \App\Http\Resources\Api\Certificate + */ + public function show(Request $request, Certificate $certificate) + { + return new CertificateResource($certificate); + } + + /** + * @param \App\Http\Requests\Api\CertificateUpdateRequest $request + * @param \App\Certificate $certificate + * @return \App\Http\Resources\Api\Certificate + */ + public function update(CertificateUpdateRequest $request, Certificate $certificate) + { + $certificate->update($request->validated()); + + return new CertificateResource($certificate); + } + + /** + * @param \Illuminate\Http\Request $request + * @param \App\Certificate $certificate + * @return \Illuminate\Http\Response + */ + public function destroy(Request $request, Certificate $certificate) + { + $certificate->delete(); + + return response()->noContent(200); + } +} diff --git a/tests/fixtures/drafts/api-routes-example.yaml b/tests/fixtures/drafts/api-routes-example.yaml index 2174602e..c4cd5702 100644 --- a/tests/fixtures/drafts/api-routes-example.yaml +++ b/tests/fixtures/drafts/api-routes-example.yaml @@ -8,5 +8,5 @@ models: remarks: text nullable controllers: - Certificate: + Api/Certificate: resource: api diff --git a/tests/fixtures/resources/certificate-collection.php b/tests/fixtures/resources/certificate-collection.php new file mode 100644 index 00000000..dc102284 --- /dev/null +++ b/tests/fixtures/resources/certificate-collection.php @@ -0,0 +1,21 @@ + $this->collection, + ]; + } +} diff --git a/tests/fixtures/resources/certificate.php b/tests/fixtures/resources/certificate.php new file mode 100644 index 00000000..2cdb029d --- /dev/null +++ b/tests/fixtures/resources/certificate.php @@ -0,0 +1,27 @@ + $this->id, + 'name' => $this->name, + 'certificate_type_id' => $this->certificate_type_id, + 'reference' => $this->reference, + 'document' => $this->document, + 'expiry_date' => $this->expiry_date, + 'remarks' => $this->remarks, + ]; + } +} diff --git a/tests/fixtures/routes/api-routes.php b/tests/fixtures/routes/api-routes.php index b23763b2..c4995442 100644 --- a/tests/fixtures/routes/api-routes.php +++ b/tests/fixtures/routes/api-routes.php @@ -1,3 +1,3 @@ -Route::apiResource('certificate', 'CertificateController'); +Route::apiResource('certificate', 'Api\CertificateController');