From 52241f5cf4d9bea241d008ae92a5070224261aac Mon Sep 17 00:00:00 2001 From: eparusov Date: Fri, 17 Jan 2025 12:15:18 +0300 Subject: [PATCH 1/6] #158: ability to generate documentation for invokable controllers --- src/Services/SwaggerService.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Services/SwaggerService.php b/src/Services/SwaggerService.php index 85ed1b33..49d5869a 100755 --- a/src/Services/SwaggerService.php +++ b/src/Services/SwaggerService.php @@ -644,7 +644,7 @@ public function getConcreteRequest() $explodedController = explode('@', $controller); $class = $explodedController[0]; - $method = $explodedController[1]; + $method = $explodedController[1] ?? '__invoke'; if (!method_exists($class, $method)) { return null; From 6b1b86de4163a1b97499c52b276416d44dcdc5ab Mon Sep 17 00:00:00 2001 From: eparusov Date: Mon, 27 Jan 2025 17:26:41 +0300 Subject: [PATCH 2/6] test: increase coverage --- tests/SwaggerServiceTest.php | 19 +++++ tests/TestCase.php | 21 ++++- .../tmp_data_get_user_request_invoke.json | 82 +++++++++++++++++++ tests/support/Mock/TestEmptyRequest.php | 9 ++ .../support/Mock/TestInvokableController.php | 10 +++ 5 files changed, 137 insertions(+), 4 deletions(-) create mode 100644 tests/fixtures/SwaggerServiceTest/tmp_data_get_user_request_invoke.json create mode 100644 tests/support/Mock/TestEmptyRequest.php create mode 100644 tests/support/Mock/TestInvokableController.php diff --git a/tests/SwaggerServiceTest.php b/tests/SwaggerServiceTest.php index d4faedf4..3355c557 100644 --- a/tests/SwaggerServiceTest.php +++ b/tests/SwaggerServiceTest.php @@ -868,4 +868,23 @@ public function testAddDataDescriptionForRouteConditionals() app(SwaggerService::class)->addData($request, $response); } + + public function testAddDataWhenInvokableClass() + { + $this->mockDriverGetEmptyAndSaveTmpData($this->getJsonFixture('tmp_data_get_user_request_invoke')); + + $service = app(SwaggerService::class); + + $request = $this->generateRequest( + type: 'get', + uri: 'users', + isInvokeController: true, + ); + + $response = $this->generateResponse('example_success_user_response.json', 200, [ + 'Content-type' => 'application/json', + ]); + + $service->addData($request, $response); + } } diff --git a/tests/TestCase.php b/tests/TestCase.php index a72c09ea..f534cecc 100755 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -10,6 +10,7 @@ use Orchestra\Testbench\TestCase as BaseTest; use RonasIT\AutoDoc\AutoDocServiceProvider; use RonasIT\AutoDoc\Tests\Support\Mock\TestController; +use RonasIT\AutoDoc\Tests\Support\Mock\TestInvokableController; use Symfony\Component\HttpFoundation\Request as SymfonyRequest; use Symfony\Component\HttpFoundation\Response; @@ -114,13 +115,25 @@ protected function clearDirectory($dirPath, $exceptPaths = []): void } } - protected function generateRequest($type, $uri, $data = [], $pathParams = [], $headers = [], $routeConditions = [], $controllerMethod = 'test'): Request - { + protected function generateRequest( + $type, + $uri, + $data = [], + $pathParams = [], + $headers = [], + $routeConditions = [], + $controllerMethod = 'test', + $isInvokeController = false, + ): Request { $request = $this->getBaseRequest($type, $uri, $data, $pathParams, $headers); - return $request->setRouteResolver(function () use ($uri, $request, $controllerMethod, $routeConditions) { + return $request->setRouteResolver(function () use ($isInvokeController, $uri, $request, $controllerMethod, $routeConditions) { + $action = $isInvokeController + ? TestInvokableController::class . '@__invoke' + : TestController::class . '@' . $controllerMethod; + $route = Route::get($uri) - ->setAction(['controller' => TestController::class . '@' . $controllerMethod]) + ->setAction(['controller' => $action]) ->bind($request); foreach ($routeConditions as $condition) { diff --git a/tests/fixtures/SwaggerServiceTest/tmp_data_get_user_request_invoke.json b/tests/fixtures/SwaggerServiceTest/tmp_data_get_user_request_invoke.json new file mode 100644 index 00000000..ce6fab2e --- /dev/null +++ b/tests/fixtures/SwaggerServiceTest/tmp_data_get_user_request_invoke.json @@ -0,0 +1,82 @@ +{ + "openapi": "3.1.0", + "servers": [ + { + "url": "http:\/\/localhost" + } + ], + "paths": { + "/users": { + "get": { + "tags": [ + "users" + ], + "consumes": [], + "produces": [ + "application/json" + ], + "parameters": [], + "responses": { + "200": { + "description": "Operation successfully done", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/getUsers200ResponseObject", + "type": "object" + }, + "example": { + "id": 2, + "name": "first_client", + "likes_count": 23, + "role": { + "id": 2, + "name": "client" + }, + "type": "reader" + } + } + } + } + }, + "security": [], + "description": "", + "summary": "test empty", + "deprecated": false + } + } + }, + "components": { + "schemas": { + "getUsers200ResponseObject": { + "type": "object", + "properties": { + "id": { + "type": "integer" + }, + "name": { + "type": "string" + }, + "likes_count": { + "type": "integer" + }, + "role": { + "type": "array" + }, + "type": { + "type": "string" + } + } + } + } + }, + "info": { + "description": "This is automatically collected documentation", + "version": "0.0.0", + "title": "Name of Your Application", + "termsOfService": "", + "contact": { + "email": "your@email.com" + } + } +} diff --git a/tests/support/Mock/TestEmptyRequest.php b/tests/support/Mock/TestEmptyRequest.php new file mode 100644 index 00000000..123aa054 --- /dev/null +++ b/tests/support/Mock/TestEmptyRequest.php @@ -0,0 +1,9 @@ + Date: Fri, 4 Apr 2025 17:39:16 +0600 Subject: [PATCH 3/6] chore: small code fix --- src/Services/SwaggerService.php | 2 +- tests/SwaggerServiceTest.php | 2 +- tests/TestCase.php | 9 ++------- tests/support/Mock/TestController.php | 4 ++++ 4 files changed, 8 insertions(+), 9 deletions(-) diff --git a/src/Services/SwaggerService.php b/src/Services/SwaggerService.php index 15a9982c..979bd151 100755 --- a/src/Services/SwaggerService.php +++ b/src/Services/SwaggerService.php @@ -638,7 +638,7 @@ public function getConcreteRequest() $explodedController = explode('@', $controller); $class = $explodedController[0]; - $method = $explodedController[1] ?? '__invoke'; + $method = Arr::get($explodedController, 1, '__invoke'); if (!method_exists($class, $method)) { return null; diff --git a/tests/SwaggerServiceTest.php b/tests/SwaggerServiceTest.php index dcead737..574b363a 100644 --- a/tests/SwaggerServiceTest.php +++ b/tests/SwaggerServiceTest.php @@ -929,7 +929,7 @@ public function testAddDataWhenInvokableClass() $request = $this->generateRequest( type: 'get', uri: 'users', - isInvokeController: true, + controllerMethod: '__invoke', ); $response = $this->generateResponse('example_success_user_response.json', 200, [ diff --git a/tests/TestCase.php b/tests/TestCase.php index f534cecc..49c0e11c 100755 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -123,17 +123,12 @@ protected function generateRequest( $headers = [], $routeConditions = [], $controllerMethod = 'test', - $isInvokeController = false, ): Request { $request = $this->getBaseRequest($type, $uri, $data, $pathParams, $headers); - return $request->setRouteResolver(function () use ($isInvokeController, $uri, $request, $controllerMethod, $routeConditions) { - $action = $isInvokeController - ? TestInvokableController::class . '@__invoke' - : TestController::class . '@' . $controllerMethod; - + return $request->setRouteResolver(function () use ($uri, $request, $controllerMethod, $routeConditions) { $route = Route::get($uri) - ->setAction(['controller' => $action]) + ->setAction(['controller' => TestController::class . '@' . $controllerMethod]) ->bind($request); foreach ($routeConditions as $condition) { diff --git a/tests/support/Mock/TestController.php b/tests/support/Mock/TestController.php index 200aabba..825e6ad4 100644 --- a/tests/support/Mock/TestController.php +++ b/tests/support/Mock/TestController.php @@ -19,4 +19,8 @@ public function testRequestWithAnnotations(TestRequestWithAnnotations $request) public function testRequestWithContract(TestContract $contract, string $param) { } + + public function __invoke(TestEmptyRequest $request) + { + } } From 7e336ea5d9ca4454e6987c89ff0e688b37b88f06 Mon Sep 17 00:00:00 2001 From: nktkvlv Date: Fri, 4 Apr 2025 17:42:32 +0600 Subject: [PATCH 4/6] chore: remove TestInvokableController --- tests/support/Mock/TestInvokableController.php | 10 ---------- 1 file changed, 10 deletions(-) delete mode 100644 tests/support/Mock/TestInvokableController.php diff --git a/tests/support/Mock/TestInvokableController.php b/tests/support/Mock/TestInvokableController.php deleted file mode 100644 index 806a1bff..00000000 --- a/tests/support/Mock/TestInvokableController.php +++ /dev/null @@ -1,10 +0,0 @@ - Date: Sun, 11 May 2025 13:21:31 +0300 Subject: [PATCH 5/6] fix: tests --- tests/SwaggerServiceTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/SwaggerServiceTest.php b/tests/SwaggerServiceTest.php index 05f37618..5612d837 100644 --- a/tests/SwaggerServiceTest.php +++ b/tests/SwaggerServiceTest.php @@ -420,7 +420,7 @@ public static function getAddData(): array #[DataProvider('getAddData')] public function testAddData(?string $contentType, string $requestFixture, string $responseFixture) { - $this->mockDriverGetEmptyAndSaveTmpData($this->getJsonFixture($requestFixture)); + $this->mockDriverGetEmptyAndSaveProcessTmpData($this->getJsonFixture($requestFixture)); $service = app(SwaggerService::class); @@ -882,7 +882,7 @@ public function testSaveProductionData() public function testAddDataDescriptionForRouteConditionals() { - $this->mockDriverGetEmptyAndSaveTmpData( + $this->mockDriverGetEmptyAndSaveProcessTmpData( $this->getJsonFixture('tmp_data_get_route_parameters_description') ); @@ -940,7 +940,7 @@ public function testMergeTempDocumentation() public function testAddDataWhenInvokableClass() { - $this->mockDriverGetEmptyAndSaveTmpData($this->getJsonFixture('tmp_data_get_user_request_invoke')); + $this->mockDriverGetEmptyAndSaveProcessTmpData($this->getJsonFixture('tmp_data_get_user_request_invoke')); $service = app(SwaggerService::class); From d1988a22d5b731bfa89914223cf2d0547580a706 Mon Sep 17 00:00:00 2001 From: Ruslan Guskov Date: Mon, 12 May 2025 14:13:06 +0300 Subject: [PATCH 6/6] refactor: add typ hint for test case method --- tests/SwaggerServiceTest.php | 4 +--- tests/TestCase.php | 15 +++++++-------- 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/tests/SwaggerServiceTest.php b/tests/SwaggerServiceTest.php index 5612d837..e4580a09 100644 --- a/tests/SwaggerServiceTest.php +++ b/tests/SwaggerServiceTest.php @@ -942,8 +942,6 @@ public function testAddDataWhenInvokableClass() { $this->mockDriverGetEmptyAndSaveProcessTmpData($this->getJsonFixture('tmp_data_get_user_request_invoke')); - $service = app(SwaggerService::class); - $request = $this->generateRequest( type: 'get', uri: 'users', @@ -954,6 +952,6 @@ public function testAddDataWhenInvokableClass() 'Content-type' => 'application/json', ]); - $service->addData($request, $response); + app(SwaggerService::class)->addData($request, $response); } } diff --git a/tests/TestCase.php b/tests/TestCase.php index 69eb8819..c876c26f 100755 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -11,7 +11,6 @@ use Orchestra\Testbench\TestCase as BaseTest; use RonasIT\AutoDoc\AutoDocServiceProvider; use RonasIT\AutoDoc\Tests\Support\Mock\TestController; -use RonasIT\AutoDoc\Tests\Support\Mock\TestInvokableController; use Symfony\Component\HttpFoundation\Request as SymfonyRequest; use Symfony\Component\HttpFoundation\Response; @@ -117,13 +116,13 @@ protected function clearDirectory($dirPath, $exceptPaths = []): void } protected function generateRequest( - $type, - $uri, - $data = [], - $pathParams = [], - $headers = [], - $routeConditions = [], - $controllerMethod = 'test', + string $type, + string $uri, + array $data = [], + array $pathParams = [], + array $headers = [], + array $routeConditions = [], + string $controllerMethod = 'test', ): Request { $request = $this->getBaseRequest($type, $uri, $data, $pathParams, $headers);