Skip to content

Commit d0c5e78

Browse files
authored
Merge pull request #159 from RonasIT/158-ability-to-generate-documentation-for-invokable-controllers
#158: ability to generate documentation for invokable controllers
2 parents 47a7888 + d1988a2 commit d0c5e78

File tree

6 files changed

+122
-3
lines changed

6 files changed

+122
-3
lines changed

src/Services/SwaggerService.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -639,7 +639,7 @@ public function getConcreteRequest()
639639
$explodedController = explode('@', $controller);
640640

641641
$class = $explodedController[0];
642-
$method = $explodedController[1];
642+
$method = Arr::get($explodedController, 1, '__invoke');
643643

644644
if (!method_exists($class, $method)) {
645645
return null;

tests/SwaggerServiceTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -937,4 +937,21 @@ public function testMergeTempDocumentation()
937937
$this->assertFileExists($tempFilePath);
938938
$this->assertFileEquals($this->generateFixturePath('tmp_data_merged.json'), $tempFilePath);
939939
}
940+
941+
public function testAddDataWhenInvokableClass()
942+
{
943+
$this->mockDriverGetEmptyAndSaveProcessTmpData($this->getJsonFixture('tmp_data_get_user_request_invoke'));
944+
945+
$request = $this->generateRequest(
946+
type: 'get',
947+
uri: 'users',
948+
controllerMethod: '__invoke',
949+
);
950+
951+
$response = $this->generateResponse('example_success_user_response.json', 200, [
952+
'Content-type' => 'application/json',
953+
]);
954+
955+
app(SwaggerService::class)->addData($request, $response);
956+
}
940957
}

tests/TestCase.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,15 @@ protected function clearDirectory($dirPath, $exceptPaths = []): void
115115
}
116116
}
117117

118-
protected function generateRequest($type, $uri, $data = [], $pathParams = [], $headers = [], $routeConditions = [], $controllerMethod = 'test'): Request
119-
{
118+
protected function generateRequest(
119+
string $type,
120+
string $uri,
121+
array $data = [],
122+
array $pathParams = [],
123+
array $headers = [],
124+
array $routeConditions = [],
125+
string $controllerMethod = 'test',
126+
): Request {
120127
$request = $this->getBaseRequest($type, $uri, $data, $pathParams, $headers);
121128

122129
return $request->setRouteResolver(function () use ($uri, $request, $controllerMethod, $routeConditions) {
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
{
2+
"openapi": "3.1.0",
3+
"servers": [
4+
{
5+
"url": "http:\/\/localhost"
6+
}
7+
],
8+
"paths": {
9+
"/users": {
10+
"get": {
11+
"tags": [
12+
"users"
13+
],
14+
"consumes": [],
15+
"produces": [
16+
"application/json"
17+
],
18+
"parameters": [],
19+
"responses": {
20+
"200": {
21+
"description": "Operation successfully done",
22+
"content": {
23+
"application/json": {
24+
"schema": {
25+
"$ref": "#/components/schemas/getUsers200ResponseObject",
26+
"type": "object"
27+
},
28+
"example": {
29+
"id": 2,
30+
"name": "first_client",
31+
"likes_count": 23,
32+
"role": {
33+
"id": 2,
34+
"name": "client"
35+
},
36+
"type": "reader"
37+
}
38+
}
39+
}
40+
}
41+
},
42+
"security": [],
43+
"description": "",
44+
"summary": "test empty",
45+
"deprecated": false
46+
}
47+
}
48+
},
49+
"components": {
50+
"schemas": {
51+
"getUsers200ResponseObject": {
52+
"type": "object",
53+
"properties": {
54+
"id": {
55+
"type": "integer"
56+
},
57+
"name": {
58+
"type": "string"
59+
},
60+
"likes_count": {
61+
"type": "integer"
62+
},
63+
"role": {
64+
"type": "array"
65+
},
66+
"type": {
67+
"type": "string"
68+
}
69+
}
70+
}
71+
}
72+
},
73+
"info": {
74+
"description": "This is automatically collected documentation",
75+
"version": "0.0.0",
76+
"title": "Name of Your Application",
77+
"termsOfService": "",
78+
"contact": {
79+
"email": "your@email.com"
80+
}
81+
}
82+
}

tests/support/Mock/TestController.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,8 @@ public function testRequestWithAnnotations(TestRequestWithAnnotations $request)
1919
public function testRequestWithContract(TestContract $contract, string $param)
2020
{
2121
}
22+
23+
public function __invoke(TestEmptyRequest $request)
24+
{
25+
}
2226
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace RonasIT\AutoDoc\Tests\Support\Mock;
4+
5+
use Illuminate\Foundation\Http\FormRequest;
6+
7+
class TestEmptyRequest extends FormRequest
8+
{
9+
}

0 commit comments

Comments
 (0)