Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions config/openapi.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
'servers' => [
[
'url' => env('APP_URL'),
'description' => null,
'variables' => [],
],
],

Expand Down
22 changes: 21 additions & 1 deletion src/Builders/ServersBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Vyuldashev\LaravelOpenApi\Builders;

use GoldSpecDigital\ObjectOrientedOAS\Objects\Server;
use GoldSpecDigital\ObjectOrientedOAS\Objects\ServerVariable;
use Illuminate\Support\Arr;

class ServersBuilder
{
Expand All @@ -13,7 +15,25 @@ class ServersBuilder
public function build(array $config): array
{
return collect($config)
->map(static fn(array $server) => Server::create()->url($server['url']))
->map(static function (array $server) {
$variables = collect(Arr::get($server, 'variables'))
->map(function (array $variable, string $key) {
$serverVariable = ServerVariable::create($key)
->default(Arr::get($variable, 'default'))
->description(Arr::get($variable, 'description'));
if (is_array(Arr::get($variable, 'enum'))) {
return $serverVariable->enum(...Arr::get($variable, 'enum'));
}

return $serverVariable;
})
->toArray();

return Server::create()
->url(Arr::get($server, 'url'))
->description(Arr::get($server, 'description'))
->variables(...$variables);
})
->toArray();
}
}
148 changes: 148 additions & 0 deletions tests/Builders/ServersBuilderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
<?php

declare(strict_types=1);

namespace Vyuldashev\LaravelOpenApi\Tests\Builders;

use Vyuldashev\LaravelOpenApi\Builders\ServersBuilder;
use Vyuldashev\LaravelOpenApi\Tests\TestCase;

class ServersBuilderTest extends TestCase
{
/**
* @dataProvider providerBuild
* @param array $config
* @param array $expected
* @return void
*/
public function testBuild(array $config, array $expected): void
{
$SUT = new ServersBuilder();
$servers = $SUT->build($config);
$this->assertSameAssociativeArray($expected[0], $servers[0]->toArray());
}

public function providerBuild(): array
{
return [
'If the variables field does not exist, it is possible to output the correct json.' => [
[[
'url' => 'http://example.com',
'description' => 'sample_description',
'variables' => [],
]],
[[
'url' => 'http://example.com',
'description' => 'sample_description',
]],
],
'If the variables field is present, it can output the correct json.' => [
[[
'url' => 'http://example.com',
'description' => 'sample_description',
'variables' => [
'variable_name' => [
'default' => 'variable_defalut',
'description' => 'variable_description',
],
],
]],
[[
'url' => 'http://example.com',
'description' => 'sample_description',
'variables' => [
'variable_name' => [
'default' => 'variable_defalut',
'description' => 'variable_description',
],
],
]],
],
'If there is a variables field containing enum, it can output the correct json.' => [
[[
'url' => 'http://example.com',
'description' => 'sample_description',
'variables' => [
'variable_name' => [
'default' => 'variable_defalut',
'description' => 'variable_description',
'enum' => [
'A',
'B',
'C',
],
],
],
]],
[[
'url' => 'http://example.com',
'description' => 'sample_description',
'variables' => [
'variable_name' => [
'default' => 'variable_defalut',
'description' => 'variable_description',
'enum' => [
'A',
'B',
'C',
],
],
],
]],
],
'If there are variables fields in multiple formats, it is possible to output the correct json.' => [
[[
'url' => 'http://example.com',
'description' => 'sample_description',
'variables' => [
'variable_name' => [
'default' => 'variable_defalut',
'description' => 'variable_description',
'enum' => ['A', 'B'],
],
'variable_name_B' => [
'default' => 'sample',
'description' => 'sample',
],
],
]],
[[
'url' => 'http://example.com',
'description' => 'sample_description',
'variables' => [
'variable_name' => [
'default' => 'variable_defalut',
'description' => 'variable_description',
'enum' => ['A', 'B'],
],
'variable_name_B' => [
'default' => 'sample',
'description' => 'sample',
],
],
]],
],
];
}

/**
* Assert equality as an associative array.
*
* @param array $expected
* @param array $actual
* @return void
*/
protected function assertSameAssociativeArray(array $expected, array $actual): void
{
foreach ($expected as $key => $value) {
if (is_array($value)) {
$this->assertSameAssociativeArray($value, $actual[$key]);
unset($actual[$key]);
continue;
}
self::assertSame($value, $actual[$key]);
unset($actual[$key]);
}
self::assertCount(0, $actual, sprintf('[%s] does not matched keys.', join(', ', array_keys($actual))));
}
}