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

Merged
Merged
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 @@ -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;
Expand Down
17 changes: 17 additions & 0 deletions tests/SwaggerServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
11 changes: 9 additions & 2 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
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
{
}