Skip to content

Commit baa6568

Browse files
authored
Merge pull request #72 from RonasIT/avardugin/unit-tests
add unit test
2 parents cf7bb3d + b991b05 commit baa6568

19 files changed

+537
-25
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
"autoload-dev": {
3636
"psr-4": {
3737
"RonasIT\\Support\\Tests\\": "tests/",
38-
"RonasIT\\Support\\Tests\\Support\\Traits\\": "tests/support/Traits"
38+
"RonasIT\\Support\\Tests\\Support\\": "tests/support/"
3939
}
4040
},
4141
"extra": {

src/Services/SwaggerService.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -737,7 +737,7 @@ protected function getClassAnnotations($class): array
737737
protected function replaceNullValues($parameters, $types, &$example)
738738
{
739739
foreach ($parameters as $parameter => $value) {
740-
if (is_null($value) && in_array($parameter, $types)) {
740+
if (is_null($value) && array_key_exists($parameter, $types)) {
741741
$example[$parameter] = $this->getDefaultValueByType($types[$parameter]['type']);
742742
} elseif (is_array($value)) {
743743
$this->replaceNullValues($value, $types, $example[$parameter]);

tests/SwaggerServiceTest.php

Lines changed: 75 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,37 @@ public function testConstructorDriverClassNotImplementsInterface()
5050
app(SwaggerService::class);
5151
}
5252

53-
public function testAddData()
53+
public function getAddData(): array
5454
{
55-
$this->mockDriverSaveTmpData($this->getJsonFixture('tmp_data_search_roles_request'));
55+
return [
56+
[
57+
'contentType' => 'application/json',
58+
'requestFixture' => 'tmp_data_search_roles_request',
59+
'responseFixture' => 'example_success_roles_response.json',
60+
],
61+
[
62+
'contentType' => 'application/pdf',
63+
'requestFixture' => 'tmp_data_search_roles_request_pdf',
64+
'responseFixture' => 'example_success_pdf_type_response.json',
65+
],
66+
[
67+
'contentType' => 'text/plain',
68+
'requestFixture' => 'tmp_data_search_roles_request_plain_text',
69+
'responseFixture' => 'example_success_plain_text_type_response.json',
70+
],
71+
];
72+
}
73+
74+
/**
75+
* @dataProvider getAddData
76+
*
77+
* @param string $contentType
78+
* @param string $requestFixture
79+
* @param string $responseFixture
80+
*/
81+
public function testAddData(string $contentType, string $requestFixture, string $responseFixture)
82+
{
83+
$this->mockDriverSaveTmpData($this->getJsonFixture($requestFixture));
5684

5785
$service = app(SwaggerService::class);
5886

@@ -62,8 +90,8 @@ public function testAddData()
6290
'Content-type' => 'application/json'
6391
]);
6492

65-
$response = new Response($this->getFixture('example_success_roles_response.json'), 200, [
66-
'Content-type' => 'application/json',
93+
$response = new Response($this->getFixture($responseFixture), 200, [
94+
'Content-type' => $contentType,
6795
'authorization' => 'Bearer some_token'
6896
]);
6997

@@ -139,4 +167,47 @@ public function testAddDataWithPathParameters()
139167

140168
$service->addData($request, $response);
141169
}
170+
171+
public function testAddDataClosureRequest()
172+
{
173+
config(['auto-doc.security' => 'jwt']);
174+
175+
$this->mockDriverSaveTmpData($this->getJsonFixture('tmp_data_search_roles_closure_request'));
176+
177+
$service = app(SwaggerService::class);
178+
179+
$request = $this->generateClosureRequest('get', 'users/roles', [
180+
'with' => ['users']
181+
]);
182+
183+
$response = new Response($this->getFixture('example_success_roles_closure_response.json'), 200, [
184+
'Content-type' => 'application/json',
185+
'authorization' => 'Bearer some_token'
186+
]);
187+
188+
$service->addData($request, $response);
189+
}
190+
191+
public function testAddDataPostRequest()
192+
{
193+
config(['auto-doc.security' => 'jwt']);
194+
195+
$this->mockDriverSaveTmpData($this->getJsonFixture('tmp_data_post_request'));
196+
197+
$service = app(SwaggerService::class);
198+
199+
$request = $this->generateRequest('post', 'users', [
200+
'users' => [1,2],
201+
'query' => null
202+
], [], [
203+
'authorization' => 'Bearer some_token'
204+
]);
205+
206+
$response = new Response($this->getFixture('example_success_users_post_response.json'), 200, [
207+
'Content-type' => 'application/json',
208+
'authorization' => 'Bearer some_token'
209+
]);
210+
211+
$service->addData($request, $response);
212+
}
142213
}

tests/TestCase.php

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Illuminate\Support\Facades\Route;
88
use Orchestra\Testbench\TestCase as BaseTest;
99
use RonasIT\Support\AutoDoc\AutoDocServiceProvider;
10+
use RonasIT\Support\Tests\Support\Mock\TestController;
1011
use Symfony\Component\HttpFoundation\Request as SymfonyRequest;
1112

1213
class TestCase extends BaseTest
@@ -61,6 +62,26 @@ protected function clearDirectory($dirPath, $exceptPaths = [])
6162
}
6263

6364
protected function generateRequest($type, $uri, $data = [], $pathParams = [], $headers = []): Request
65+
{
66+
$request = $this->getBaseRequest($type, $uri, $data, $pathParams, $headers);
67+
68+
return $request->setRouteResolver(function () use ($uri, $request) {
69+
return Route::get($uri)
70+
->setAction(['controller' => TestController::class . '@index'])
71+
->bind($request);
72+
});
73+
}
74+
75+
protected function generateClosureRequest($type, $uri, $data = [], $pathParams = [], $headers = []): Request
76+
{
77+
$request = $this->getBaseRequest($type, $uri, $data, $pathParams, $headers);
78+
79+
return $request->setRouteResolver(function () use ($uri) {
80+
return Route::get($uri);
81+
});
82+
}
83+
84+
protected function getBaseRequest($type, $uri, $data = [], $pathParams = [], $headers = []): Request
6485
{
6586
$realUri = $uri;
6687

@@ -77,13 +98,7 @@ protected function generateRequest($type, $uri, $data = [], $pathParams = [], $h
7798
$this->transformHeadersToServerVars($headers)
7899
);
79100

80-
$request = Request::createFromBase($symfonyRequest);
81-
82-
$request->setRouteResolver(function () use ($uri) {
83-
return Route::get($uri);
84-
});
85-
86-
return $request;
101+
return Request::createFromBase($symfonyRequest);
87102
}
88103

89104
protected function addGlobalPrefix($prefix = '/global')
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
[
2+
{
3+
"id": 1,
4+
"name": "admin",
5+
"users": [
6+
{
7+
"id": 1,
8+
"name": "admin"
9+
}
10+
]
11+
},
12+
{
13+
"id": 2,
14+
"name": "client",
15+
"users": [
16+
{
17+
"id": 2,
18+
"name": "first_client"
19+
},
20+
{
21+
"id": 3,
22+
"name": "second_client"
23+
}
24+
]
25+
}
26+
]
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[
2+
{
3+
"id": 1,
4+
"name": "admin",
5+
"users": [
6+
{
7+
"id": 1,
8+
"name": "admin"
9+
}
10+
]
11+
}
12+
]
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
[
2+
{
3+
"id": 1,
4+
"name": "admin",
5+
"users": [
6+
{
7+
"id": 1,
8+
"name": "admin"
9+
}
10+
]
11+
},
12+
{
13+
"id": 2,
14+
"name": "client",
15+
"users": [
16+
{
17+
"id": 2,
18+
"name": "first_client"
19+
},
20+
{
21+
"id": 3,
22+
"name": "second_client"
23+
}
24+
]
25+
}
26+
]
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
[
2+
{
3+
"id": 1,
4+
"name": "admin",
5+
"users": [
6+
{
7+
"id": 1,
8+
"name": "admin"
9+
}
10+
]
11+
},
12+
{
13+
"id": 2,
14+
"name": "client",
15+
"users": [
16+
{
17+
"id": 2,
18+
"name": "first_client"
19+
},
20+
{
21+
"id": 3,
22+
"name": "second_client"
23+
}
24+
]
25+
}
26+
]

tests/fixtures/SwaggerServiceTest/tmp_data_get_user_request.json

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,18 @@
2727
"description": "",
2828
"required": true,
2929
"type": "string"
30+
},
31+
{
32+
"in": "query",
33+
"name": "query",
34+
"description": "string, required",
35+
"type": "string",
36+
"required": true
3037
}
3138
],
3239
"responses": {
3340
"200": {
34-
"description": "OK",
41+
"description": "Operation successfully done",
3542
"schema": {
3643
"example": {
3744
"id": 2,
@@ -46,7 +53,8 @@
4653
}
4754
},
4855
"security": [],
49-
"description": ""
56+
"description": "",
57+
"summary": "test"
5058
}
5159
}
5260
},
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
{
2+
"swagger": "2.0",
3+
"host": "localhost",
4+
"basePath": "/",
5+
"schemes": [],
6+
"paths": {
7+
"/users": {
8+
"post": {
9+
"tags": [
10+
"users"
11+
],
12+
"consumes": ["application/x-www-form-urlencoded"],
13+
"produces": [
14+
"application/json"
15+
],
16+
"parameters": [
17+
{
18+
"in": "body",
19+
"name": "body",
20+
"description": "",
21+
"required": true,
22+
"schema": {
23+
"$ref": "#/definitions/usersObject"
24+
}
25+
}
26+
],
27+
"responses": {
28+
"200": {
29+
"description": "Operation successfully done",
30+
"schema": {
31+
"example": [
32+
{
33+
"id": 1,
34+
"name": "admin",
35+
"users": [
36+
{
37+
"id": 1,
38+
"name": "admin"
39+
}
40+
]
41+
},
42+
{
43+
"id": 2,
44+
"name": "client",
45+
"users": [
46+
{
47+
"id": 2,
48+
"name": "first_client"
49+
},
50+
{
51+
"id": 3,
52+
"name": "second_client"
53+
}
54+
]
55+
}
56+
]
57+
}
58+
}
59+
},
60+
"security": [
61+
{
62+
"jwt": []
63+
}
64+
],
65+
"description": "",
66+
"summary": "test"
67+
}
68+
}
69+
},
70+
"definitions": {
71+
"usersObject": {
72+
"type": "object",
73+
"properties": {
74+
"query": {
75+
"type": "string",
76+
"description": ""
77+
}
78+
},
79+
"example": {
80+
"users": [1,2],
81+
"query": null
82+
},
83+
"required": ["query"]
84+
}
85+
},
86+
"info": {
87+
"description": "This is automatically collected documentation",
88+
"version": "0.0.0",
89+
"title": "Name of Your Application",
90+
"termsOfService": "",
91+
"contact": {
92+
"email": "your@email.com"
93+
}
94+
},
95+
"securityDefinitions": {
96+
"jwt": {
97+
"type": "apiKey",
98+
"name": "authorization",
99+
"in": "header"
100+
}
101+
}
102+
}

0 commit comments

Comments
 (0)