Skip to content

Commit de5f355

Browse files
authored
Merge pull request #44 from sun-yryr/feature/generate-server
Add: generate servers.description & servers.variables
2 parents 5db40bd + 8332df0 commit de5f355

File tree

3 files changed

+171
-1
lines changed

3 files changed

+171
-1
lines changed

config/openapi.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
'servers' => [
1717
[
1818
'url' => env('APP_URL'),
19+
'description' => null,
20+
'variables' => [],
1921
],
2022
],
2123

src/Builders/ServersBuilder.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace Vyuldashev\LaravelOpenApi\Builders;
44

55
use GoldSpecDigital\ObjectOrientedOAS\Objects\Server;
6+
use GoldSpecDigital\ObjectOrientedOAS\Objects\ServerVariable;
7+
use Illuminate\Support\Arr;
68

79
class ServersBuilder
810
{
@@ -13,7 +15,25 @@ class ServersBuilder
1315
public function build(array $config): array
1416
{
1517
return collect($config)
16-
->map(static fn(array $server) => Server::create()->url($server['url']))
18+
->map(static function (array $server) {
19+
$variables = collect(Arr::get($server, 'variables'))
20+
->map(function (array $variable, string $key) {
21+
$serverVariable = ServerVariable::create($key)
22+
->default(Arr::get($variable, 'default'))
23+
->description(Arr::get($variable, 'description'));
24+
if (is_array(Arr::get($variable, 'enum'))) {
25+
return $serverVariable->enum(...Arr::get($variable, 'enum'));
26+
}
27+
28+
return $serverVariable;
29+
})
30+
->toArray();
31+
32+
return Server::create()
33+
->url(Arr::get($server, 'url'))
34+
->description(Arr::get($server, 'description'))
35+
->variables(...$variables);
36+
})
1737
->toArray();
1838
}
1939
}
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Vyuldashev\LaravelOpenApi\Tests\Builders;
6+
7+
use Vyuldashev\LaravelOpenApi\Builders\ServersBuilder;
8+
use Vyuldashev\LaravelOpenApi\Tests\TestCase;
9+
10+
class ServersBuilderTest extends TestCase
11+
{
12+
/**
13+
* @dataProvider providerBuild
14+
* @param array $config
15+
* @param array $expected
16+
* @return void
17+
*/
18+
public function testBuild(array $config, array $expected): void
19+
{
20+
$SUT = new ServersBuilder();
21+
$servers = $SUT->build($config);
22+
$this->assertSameAssociativeArray($expected[0], $servers[0]->toArray());
23+
}
24+
25+
public function providerBuild(): array
26+
{
27+
return [
28+
'If the variables field does not exist, it is possible to output the correct json.' => [
29+
[[
30+
'url' => 'http://example.com',
31+
'description' => 'sample_description',
32+
'variables' => [],
33+
]],
34+
[[
35+
'url' => 'http://example.com',
36+
'description' => 'sample_description',
37+
]],
38+
],
39+
'If the variables field is present, it can output the correct json.' => [
40+
[[
41+
'url' => 'http://example.com',
42+
'description' => 'sample_description',
43+
'variables' => [
44+
'variable_name' => [
45+
'default' => 'variable_defalut',
46+
'description' => 'variable_description',
47+
],
48+
],
49+
]],
50+
[[
51+
'url' => 'http://example.com',
52+
'description' => 'sample_description',
53+
'variables' => [
54+
'variable_name' => [
55+
'default' => 'variable_defalut',
56+
'description' => 'variable_description',
57+
],
58+
],
59+
]],
60+
],
61+
'If there is a variables field containing enum, it can output the correct json.' => [
62+
[[
63+
'url' => 'http://example.com',
64+
'description' => 'sample_description',
65+
'variables' => [
66+
'variable_name' => [
67+
'default' => 'variable_defalut',
68+
'description' => 'variable_description',
69+
'enum' => [
70+
'A',
71+
'B',
72+
'C',
73+
],
74+
],
75+
],
76+
]],
77+
[[
78+
'url' => 'http://example.com',
79+
'description' => 'sample_description',
80+
'variables' => [
81+
'variable_name' => [
82+
'default' => 'variable_defalut',
83+
'description' => 'variable_description',
84+
'enum' => [
85+
'A',
86+
'B',
87+
'C',
88+
],
89+
],
90+
],
91+
]],
92+
],
93+
'If there are variables fields in multiple formats, it is possible to output the correct json.' => [
94+
[[
95+
'url' => 'http://example.com',
96+
'description' => 'sample_description',
97+
'variables' => [
98+
'variable_name' => [
99+
'default' => 'variable_defalut',
100+
'description' => 'variable_description',
101+
'enum' => ['A', 'B'],
102+
],
103+
'variable_name_B' => [
104+
'default' => 'sample',
105+
'description' => 'sample',
106+
],
107+
],
108+
]],
109+
[[
110+
'url' => 'http://example.com',
111+
'description' => 'sample_description',
112+
'variables' => [
113+
'variable_name' => [
114+
'default' => 'variable_defalut',
115+
'description' => 'variable_description',
116+
'enum' => ['A', 'B'],
117+
],
118+
'variable_name_B' => [
119+
'default' => 'sample',
120+
'description' => 'sample',
121+
],
122+
],
123+
]],
124+
],
125+
];
126+
}
127+
128+
/**
129+
* Assert equality as an associative array.
130+
*
131+
* @param array $expected
132+
* @param array $actual
133+
* @return void
134+
*/
135+
protected function assertSameAssociativeArray(array $expected, array $actual): void
136+
{
137+
foreach ($expected as $key => $value) {
138+
if (is_array($value)) {
139+
$this->assertSameAssociativeArray($value, $actual[$key]);
140+
unset($actual[$key]);
141+
continue;
142+
}
143+
self::assertSame($value, $actual[$key]);
144+
unset($actual[$key]);
145+
}
146+
self::assertCount(0, $actual, sprintf('[%s] does not matched keys.', join(', ', array_keys($actual))));
147+
}
148+
}

0 commit comments

Comments
 (0)