Skip to content

Commit ee2675e

Browse files
authored
Merge pull request #88 from RonasIT/fix-handling-no-boung-contracts
Fix handling no boung contracts
2 parents 2c252d6 + db3107a commit ee2675e

File tree

6 files changed

+171
-20
lines changed

6 files changed

+171
-20
lines changed

readme.md

Lines changed: 44 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -57,22 +57,7 @@ passing PHPUnit tests.
5757
5858
### Basic usage
5959
60-
1. Create test for API endpoint:
61-
62-
```php
63-
public function testUpdate()
64-
{
65-
$response = $this->json('put', '/users/1', [
66-
'name': 'Updated User',
67-
'is_active': true,
68-
'age': 22
69-
]);
70-
71-
$response->assertStatus(Response::HTTP_NO_CONTENT);
72-
}
73-
```
74-
75-
2. Create request class:
60+
1. Create request class:
7661
7762
```php
7863
<?php
@@ -119,7 +104,6 @@ passing PHPUnit tests.
119104
}
120105
121106
```
122-
123107
> ***Note***
124108
>
125109
> For correct working of plugin you'll have to dispose all the validation rules
@@ -128,9 +112,49 @@ passing PHPUnit tests.
128112
> Plugin will take validation rules from the request class and generate fields description
129113
> of input parameter.
130114
131-
3. Run tests
132-
4. Go to route defined in the `auto-doc.route` config
133-
5. Profit!
115+
2. Create a controller and a method for your route:
116+
117+
```php
118+
<?php
119+
120+
namespace App\Http\Controllers;
121+
122+
use App\Http\Requests\Users\UpdateUserDataRequest;
123+
124+
class UserController extends Controller
125+
{
126+
public function update(UpdateUserDataRequest $request, UserService $service, $id)
127+
{
128+
// do something here...
129+
130+
return response('', Response::HTTP_NO_CONTENT);
131+
}
132+
}
133+
```
134+
135+
> ***Note***
136+
>
137+
> Dependency injection of request class is optional but if it not presents,
138+
> the "Parameters" block in the API documentation will be empty.
139+
140+
3. Create test for API endpoint:
141+
142+
```php
143+
public function testUpdate()
144+
{
145+
$response = $this->json('put', '/users/1', [
146+
'name': 'Updated User',
147+
'is_active': true,
148+
'age': 22
149+
]);
150+
151+
$response->assertStatus(Response::HTTP_NO_CONTENT);
152+
}
153+
```
154+
155+
4. Run tests
156+
5. Go to route defined in the `auto-doc.route` config
157+
6. Profit!
134158
135159
![img.png](resources/assets/images/img.png)
136160

src/Traits/GetDependenciesTrait.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ protected function getClassByInterface($interfaceName)
4646

4747
$implementation = Arr::get($bindings, "{$interfaceName}.concrete");
4848

49+
if (empty($implementation)) {
50+
return null;
51+
}
52+
4953
$classFields = (new ReflectionFunction($implementation))->getStaticVariables();
5054

5155
return $classFields['concrete'];

tests/SwaggerServiceTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,4 +329,21 @@ public function testLimitResponseData()
329329

330330
$service->addData($request, $response);
331331
}
332+
333+
public function testAddDataWithoutBoundContract()
334+
{
335+
config(['auto-doc.response_example_limit_count' => 1]);
336+
337+
$this->mockDriverGetEmptyAndSaveTpmData($this->getJsonFixture('tmp_data_search_users_empty_request'));
338+
339+
$service = app(SwaggerService::class);
340+
341+
$request = $this->generateRequest('get', '/api/users', [], [], [], 'testRequestWithContract');
342+
343+
$response = $this->generateResponse('example_success_users_response.json', 200, [
344+
'Content-type' => 'application/json'
345+
]);
346+
347+
$service->addData($request, $response);
348+
}
332349
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
{
2+
"swagger": "2.0",
3+
"host": "localhost",
4+
"basePath": "\/",
5+
"schemes": [],
6+
"paths":
7+
{
8+
"\/api\/users":
9+
{
10+
"get":
11+
{
12+
"tags": ["api"],
13+
"produces": ["application\/json"],
14+
"parameters": [],
15+
"responses":
16+
{
17+
"200":
18+
{
19+
"description": "OK",
20+
"schema":
21+
{
22+
"example":
23+
{
24+
"current_page": 1,
25+
"data": [
26+
{
27+
"id": 1,
28+
"first_name": "Billy",
29+
"last_name": "Coleman",
30+
"email": "billy.coleman@example.com",
31+
"created_at": null,
32+
"updated_at": null,
33+
"role_id": 1,
34+
"date_of_birth": "1986-05-20",
35+
"phone": "+79535482530",
36+
"position": "admin",
37+
"starts_on": "2022-04-16 00:00:00",
38+
"hr_id": null,
39+
"manager_id": null,
40+
"lead_id": null,
41+
"avatar_id": null,
42+
"deleted_at": null,
43+
"company_id": 1
44+
}
45+
],
46+
"first_page_url": "http:\/\/localhost\/api\/users?page=1",
47+
"from": 1,
48+
"last_page": 1,
49+
"last_page_url": "http:\/\/localhost\/api\/users?page=1",
50+
"links": [
51+
{
52+
"url": null,
53+
"label": "&laquo; Previous",
54+
"active": false
55+
},
56+
{
57+
"url": "http:\/\/localhost\/api\/users?page=1",
58+
"label": "1",
59+
"active": true
60+
},
61+
{
62+
"url": null,
63+
"label": "Next &raquo;",
64+
"active": false
65+
}],
66+
"next_page_url": null,
67+
"path": "http:\/\/localhost\/api\/users",
68+
"per_page": 20,
69+
"prev_page_url": null,
70+
"to": 1,
71+
"total": 1
72+
}
73+
}
74+
}
75+
},
76+
"security": [],
77+
"description": "",
78+
"consumes": []
79+
}
80+
}
81+
},
82+
"definitions": [],
83+
"info":
84+
{
85+
"description": "This is automatically collected documentation",
86+
"version": "0.0.0",
87+
"title": "Name of Your Application",
88+
"termsOfService": "",
89+
"contact":
90+
{
91+
"email": "your@email.com"
92+
}
93+
}
94+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace RonasIT\Support\Tests\Support\Mock;
4+
5+
interface TestContract
6+
{
7+
public function find(string $resourceType, string $resourceId): ?object;
8+
}

tests/support/Mock/TestController.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,8 @@ public function testRequestWithoutRuleType(TestRequestWithoutRuleType $request)
1515
public function testRequestWithAnnotations(TestRequestWithAnnotations $request)
1616
{
1717
}
18+
19+
public function testRequestWithContract(TestContract $contract)
20+
{
21+
}
1822
}

0 commit comments

Comments
 (0)