diff --git a/src/Services/SwaggerService.php b/src/Services/SwaggerService.php index 571b0ecf..263c8c9a 100755 --- a/src/Services/SwaggerService.php +++ b/src/Services/SwaggerService.php @@ -639,7 +639,7 @@ public function getConcreteRequest() $explodedController = explode('@', $controller); $class = $explodedController[0]; - $method = $explodedController[1]; + $method = Arr::get($explodedController, 1, '__invoke'); if (!method_exists($class, $method)) { return null; diff --git a/tests/SwaggerServiceTest.php b/tests/SwaggerServiceTest.php index 51ed48f4..e4580a09 100644 --- a/tests/SwaggerServiceTest.php +++ b/tests/SwaggerServiceTest.php @@ -937,4 +937,21 @@ public function testMergeTempDocumentation() $this->assertFileExists($tempFilePath); $this->assertFileEquals($this->generateFixturePath('tmp_data_merged.json'), $tempFilePath); } + + public function testAddDataWhenInvokableClass() + { + $this->mockDriverGetEmptyAndSaveProcessTmpData($this->getJsonFixture('tmp_data_get_user_request_invoke')); + + $request = $this->generateRequest( + type: 'get', + uri: 'users', + controllerMethod: '__invoke', + ); + + $response = $this->generateResponse('example_success_user_response.json', 200, [ + 'Content-type' => 'application/json', + ]); + + app(SwaggerService::class)->addData($request, $response); + } } diff --git a/tests/TestCase.php b/tests/TestCase.php index 91706921..c876c26f 100755 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -115,8 +115,15 @@ protected function clearDirectory($dirPath, $exceptPaths = []): void } } - protected function generateRequest($type, $uri, $data = [], $pathParams = [], $headers = [], $routeConditions = [], $controllerMethod = 'test'): Request - { + protected function generateRequest( + string $type, + string $uri, + array $data = [], + array $pathParams = [], + array $headers = [], + array $routeConditions = [], + string $controllerMethod = 'test', + ): Request { $request = $this->getBaseRequest($type, $uri, $data, $pathParams, $headers); return $request->setRouteResolver(function () use ($uri, $request, $controllerMethod, $routeConditions) { 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/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) + { + } } 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 @@ +