Skip to content

Commit c8c9c94

Browse files
committed
fix: use invocable controllers with DI with bind by contracts
1 parent 49b21a0 commit c8c9c94

File tree

7 files changed

+152
-9
lines changed

7 files changed

+152
-9
lines changed

src/Services/SwaggerService.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -655,10 +655,7 @@ public function getConcreteRequest()
655655
return null;
656656
}
657657

658-
$parameters = $this->resolveClassMethodDependencies(
659-
app($class),
660-
$method
661-
);
658+
$parameters = $this->resolveClassMethodDependencies(app($class), $method);
662659

663660
return Arr::first($parameters, function ($key) {
664661
return preg_match('/Request/', $key);

src/Traits/GetDependenciesTrait.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@ protected function getClassByInterface($interfaceName)
3939
return null;
4040
}
4141

42-
$classFields = (new ReflectionFunction($implementation))->getStaticVariables();
43-
44-
return $classFields['concrete'];
42+
return get_class($implementation(app()));
4543
}
4644
}

tests/SwaggerServiceTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@
1111
use RonasIT\AutoDoc\Exceptions\UnsupportedDocumentationViewerException;
1212
use RonasIT\AutoDoc\Exceptions\WrongSecurityConfigException;
1313
use RonasIT\AutoDoc\Services\SwaggerService;
14+
use RonasIT\AutoDoc\Tests\Support\Mock\InvokableTestController;
1415
use RonasIT\AutoDoc\Tests\Support\Mock\TestContract;
1516
use RonasIT\AutoDoc\Tests\Support\Mock\TestNotificationSetting;
1617
use RonasIT\AutoDoc\Tests\Support\Mock\TestRequest;
18+
use RonasIT\AutoDoc\Tests\Support\Mock\TestRequestContract;
1719
use RonasIT\AutoDoc\Tests\Support\Traits\SwaggerServiceMockTrait;
1820
use stdClass;
1921

@@ -873,4 +875,24 @@ public function testAddDataWhenInvokableClass()
873875

874876
app(SwaggerService::class)->addData($request, $response);
875877
}
878+
879+
public function testAddDataWhenInvokableClassWithBindingContract()
880+
{
881+
$this->mockDriverGetEmptyAndSaveProcessTmpData($this->getJsonFixture('tmp_data_get_user_request_invoke_bind_closure'));
882+
883+
$this->app->bind(TestRequestContract::class, fn () => new TestRequest());
884+
885+
$request = $this->generateRequest(
886+
type: 'get',
887+
uri: 'users',
888+
controllerMethod: '__invoke',
889+
controllerClass: InvokableTestController::class,
890+
);
891+
892+
$response = $this->generateResponse('example_success_user_response.json', 200, [
893+
'Content-type' => 'application/json',
894+
]);
895+
896+
app(SwaggerService::class)->addData($request, $response);
897+
}
876898
}

tests/TestCase.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,12 +126,13 @@ protected function generateRequest(
126126
array $headers = [],
127127
array $routeConditions = [],
128128
string $controllerMethod = 'test',
129+
string $controllerClass = TestController::class,
129130
): Request {
130131
$request = $this->getBaseRequest($type, $uri, $data, $pathParams, $headers);
131132

132-
return $request->setRouteResolver(function () use ($uri, $request, $controllerMethod, $routeConditions) {
133+
return $request->setRouteResolver(function () use ($uri, $request, $controllerMethod, $routeConditions, $controllerClass) {
133134
$route = Route::get($uri)
134-
->setAction(['controller' => TestController::class . '@' . $controllerMethod])
135+
->setAction(['controller' => $controllerClass . '@' . $controllerMethod])
135136
->bind($request);
136137

137138
foreach ($routeConditions as $condition) {
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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+
{
20+
"in": "query",
21+
"name": "query",
22+
"description": "string, required",
23+
"schema": {
24+
"type": "string"
25+
},
26+
"required": true
27+
},
28+
{
29+
"in": "query",
30+
"name": "user_id",
31+
"description": "integer, with_to_array_rule_string_name",
32+
"schema": {
33+
"type": "integer"
34+
}
35+
},
36+
{
37+
"in": "query",
38+
"name": "is_email_enabled",
39+
"description": "test_rule_without_to_string",
40+
"schema": {
41+
"type": "string"
42+
}
43+
}
44+
],
45+
"responses": {
46+
"200": {
47+
"description": "Operation successfully done",
48+
"content": {
49+
"application\/json": {
50+
"schema": {
51+
"type": "object",
52+
"$ref": "#\/components\/schemas\/getUsers200ResponseObject"
53+
},
54+
"example": {
55+
"id": 2,
56+
"name": "first_client",
57+
"likes_count": 23,
58+
"role": {
59+
"id": 2,
60+
"name": "client"
61+
},
62+
"type": "reader"
63+
}
64+
}
65+
}
66+
}
67+
},
68+
"security": [],
69+
"description": "",
70+
"deprecated": false,
71+
"summary": "test"
72+
}
73+
}
74+
},
75+
"components": {
76+
"schemas": {
77+
"getUsers200ResponseObject": {
78+
"type": "object",
79+
"properties": {
80+
"id": {
81+
"type": "integer"
82+
},
83+
"name": {
84+
"type": "string"
85+
},
86+
"likes_count": {
87+
"type": "integer"
88+
},
89+
"role": {
90+
"type": "array"
91+
},
92+
"type": {
93+
"type": "string"
94+
}
95+
}
96+
}
97+
}
98+
},
99+
"info": {
100+
"description": "This is automatically collected documentation",
101+
"version": "0.0.0",
102+
"title": "Name of Your Application",
103+
"termsOfService": "",
104+
"contact": {
105+
"email": "your@email.com"
106+
}
107+
}
108+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace RonasIT\AutoDoc\Tests\Support\Mock;
4+
5+
class InvokableTestController
6+
{
7+
public function __invoke(TestRequestContract $someVar)
8+
{
9+
}
10+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace RonasIT\AutoDoc\Tests\Support\Mock;
4+
5+
interface TestRequestContract
6+
{
7+
}

0 commit comments

Comments
 (0)