|
| 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