Skip to content

Commit 5db40bd

Browse files
authored
Merge pull request vyuldashev#43 from sun-yryr/feature/generate-contact
Add: contact schema
2 parents d3fdb0d + 695de97 commit 5db40bd

File tree

3 files changed

+145
-0
lines changed

3 files changed

+145
-0
lines changed

config/openapi.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
'title' => config('app.name'),
1111
'description' => null,
1212
'version' => '1.0.0',
13+
'contact' => [],
1314
],
1415

1516
'servers' => [

src/Builders/InfoBuilder.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Vyuldashev\LaravelOpenApi\Builders;
44

5+
use GoldSpecDigital\ObjectOrientedOAS\Objects\Contact;
56
use GoldSpecDigital\ObjectOrientedOAS\Objects\Info;
67
use Illuminate\Support\Arr;
78

@@ -14,6 +15,16 @@ public function build(array $config): Info
1415
->description(Arr::get($config, 'description'))
1516
->version(Arr::get($config, 'version'));
1617

18+
if (Arr::has($config, 'contact') &&
19+
(
20+
array_key_exists('name', $config['contact']) ||
21+
array_key_exists('email', $config['contact']) ||
22+
array_key_exists('url', $config['contact'])
23+
)
24+
) {
25+
$info = $info->contact($this->buildContact($config['contact']));
26+
}
27+
1728
$extensions = $config['extensions'] ?? [];
1829

1930
foreach ($extensions as $key => $value) {
@@ -22,4 +33,12 @@ public function build(array $config): Info
2233

2334
return $info;
2435
}
36+
37+
protected function buildContact(array $config): Contact
38+
{
39+
return Contact::create()
40+
->name(Arr::get($config, 'name'))
41+
->email(Arr::get($config, 'email'))
42+
->url(Arr::get($config, 'url'));
43+
}
2544
}

tests/Builders/InfoBuilderTest.php

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Vyuldashev\LaravelOpenApi\Tests\Builders;
6+
7+
use Vyuldashev\LaravelOpenApi\Builders\InfoBuilder;
8+
use Vyuldashev\LaravelOpenApi\Tests\TestCase;
9+
10+
class InfoBuilderTest extends TestCase
11+
{
12+
/**
13+
* @dataProvider providerBuildContact
14+
* @param array $config
15+
* @param array $expected
16+
* @return void
17+
*/
18+
public function testBuildContact(array $config, array $expected): void
19+
{
20+
$SUT = new InfoBuilder();
21+
$info = $SUT->build($config);
22+
$this->assertSameAssociativeArray($expected, $info->toArray());
23+
}
24+
25+
public function providerBuildContact(): array
26+
{
27+
$common = [
28+
'title' => 'sample_title',
29+
'description' => 'sample_description',
30+
'version' => 'sample_version',
31+
];
32+
33+
return [
34+
'If all the elements are present, the correct json can be output.' => [
35+
array_merge($common, [
36+
'contact' => [
37+
'name' => 'sample_contact_name',
38+
'email' => 'sample_contact_email',
39+
'url' => 'sample_contact_url',
40+
],
41+
]),
42+
array_merge($common, [
43+
'contact' => [
44+
'name' => 'sample_contact_name',
45+
'email' => 'sample_contact_email',
46+
'url' => 'sample_contact_url',
47+
],
48+
]),
49+
],
50+
'If Contact.name does not exist, the correct json can be output.' => [
51+
array_merge($common, [
52+
'contact' => [
53+
'email' => 'sample_contact_email',
54+
'url' => 'sample_contact_url',
55+
],
56+
]),
57+
array_merge($common, [
58+
'contact' => [
59+
'email' => 'sample_contact_email',
60+
'url' => 'sample_contact_url',
61+
],
62+
]),
63+
],
64+
'If Contact.email does not exist, the correct json can be output.' => [
65+
array_merge($common, [
66+
'contact' => [
67+
'name' => 'sample_contact_name',
68+
'url' => 'sample_contact_url',
69+
],
70+
]),
71+
array_merge($common, [
72+
'contact' => [
73+
'name' => 'sample_contact_name',
74+
'url' => 'sample_contact_url',
75+
],
76+
]),
77+
],
78+
'If Contact.url does not exist, the correct json can be output.' => [
79+
array_merge($common, [
80+
'contact' => [
81+
'name' => 'sample_contact_name',
82+
'email' => 'sample_contact_email',
83+
],
84+
]),
85+
array_merge($common, [
86+
'contact' => [
87+
'name' => 'sample_contact_name',
88+
'email' => 'sample_contact_email',
89+
],
90+
]),
91+
],
92+
'If Contact does not exist, the correct json can be output.' => [
93+
array_merge($common),
94+
array_merge($common),
95+
],
96+
'If Contact.* does not exist, the correct json can be output.' => [
97+
array_merge($common, [
98+
'contact' => [],
99+
]),
100+
array_merge($common),
101+
],
102+
];
103+
}
104+
105+
/**
106+
* Assert equality as an associative array.
107+
*
108+
* @param array $expected
109+
* @param array $actual
110+
* @return void
111+
*/
112+
protected function assertSameAssociativeArray(array $expected, array $actual): void
113+
{
114+
foreach ($expected as $key => $value) {
115+
if (is_array($value)) {
116+
$this->assertSameAssociativeArray($value, $actual[$key]);
117+
unset($actual[$key]);
118+
continue;
119+
}
120+
self::assertSame($value, $actual[$key]);
121+
unset($actual[$key]);
122+
}
123+
self::assertCount(0, $actual, sprintf('[%s] does not matched keys.', join(', ', array_keys($actual))));
124+
}
125+
}

0 commit comments

Comments
 (0)