Skip to content

#158: ability to generate documentation for invokable controllers #159

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Services/SwaggerService.php
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,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;
Expand Down
19 changes: 19 additions & 0 deletions tests/SwaggerServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -919,4 +919,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',
controllerMethod: '__invoke',
);

$response = $this->generateResponse('example_success_user_response.json', 200, [
'Content-type' => 'application/json',
]);

$service->addData($request, $response);
Comment on lines +927 to +939
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$service = app(SwaggerService::class);
$request = $this->generateRequest(
type: 'get',
uri: 'users',
controllerMethod: '__invoke',
);
$response = $this->generateResponse('example_success_user_response.json', 200, [
'Content-type' => 'application/json',
]);
$service->addData($request, $response);
$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);

}
}
12 changes: 10 additions & 2 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?

use Symfony\Component\HttpFoundation\Request as SymfonyRequest;
use Symfony\Component\HttpFoundation\Response;

Expand Down Expand Up @@ -114,8 +115,15 @@ 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',
Comment on lines +119 to +125
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add typehints

): Request {
$request = $this->getBaseRequest($type, $uri, $data, $pathParams, $headers);

return $request->setRouteResolver(function () use ($uri, $request, $controllerMethod, $routeConditions) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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": "[email protected]"
}
}
}
4 changes: 4 additions & 0 deletions tests/support/Mock/TestController.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,8 @@ public function testRequestWithAnnotations(TestRequestWithAnnotations $request)
public function testRequestWithContract(TestContract $contract, string $param)
{
}

public function __invoke(TestEmptyRequest $request)
{
}
}
9 changes: 9 additions & 0 deletions tests/support/Mock/TestEmptyRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace RonasIT\AutoDoc\Tests\Support\Mock;

use Illuminate\Foundation\Http\FormRequest;

class TestEmptyRequest extends FormRequest
{
}