|
1 | 1 | <?php
|
2 | 2 |
|
| 3 | +use cebe\openapi\spec\SecurityRequirement; |
| 4 | + |
3 | 5 | class WriterTest extends \PHPUnit\Framework\TestCase
|
4 | 6 | {
|
5 |
| - private function createOpenAPI() |
| 7 | + private function createOpenAPI($merge = []) |
6 | 8 | {
|
7 |
| - return new \cebe\openapi\spec\OpenApi([ |
| 9 | + return new \cebe\openapi\spec\OpenApi(array_merge([ |
8 | 10 | 'openapi' => '3.0.0',
|
9 | 11 | 'info' => [
|
10 | 12 | 'title' => 'Test API',
|
11 | 13 | 'version' => '1.0.0',
|
12 | 14 | ],
|
13 | 15 | 'paths' => [],
|
14 |
| - ]); |
| 16 | + ], $merge)); |
15 | 17 | }
|
16 | 18 |
|
17 | 19 | public function testWriteJson()
|
@@ -78,6 +80,108 @@ public function testWriteYaml()
|
78 | 80 | version: 1.0.0
|
79 | 81 | paths: { }
|
80 | 82 |
|
| 83 | +YAML |
| 84 | + ), |
| 85 | + $yaml |
| 86 | + ); |
| 87 | + } |
| 88 | + |
| 89 | + public function testWriteEmptySecurityJson() |
| 90 | + { |
| 91 | + $openapi = $this->createOpenAPI([ |
| 92 | + 'security' => [], |
| 93 | + ]); |
| 94 | + |
| 95 | + $json = \cebe\openapi\Writer::writeToJson($openapi); |
| 96 | + |
| 97 | + $this->assertEquals(preg_replace('~\R~', "\n", <<<JSON |
| 98 | +{ |
| 99 | + "openapi": "3.0.0", |
| 100 | + "info": { |
| 101 | + "title": "Test API", |
| 102 | + "version": "1.0.0" |
| 103 | + }, |
| 104 | + "paths": {}, |
| 105 | + "security": [] |
| 106 | +} |
| 107 | +JSON |
| 108 | + ), |
| 109 | + $json |
| 110 | + ); |
| 111 | + } |
| 112 | + |
| 113 | + |
| 114 | + public function testWriteEmptySecurityYaml() |
| 115 | + { |
| 116 | + $openapi = $this->createOpenAPI([ |
| 117 | + 'security' => [], |
| 118 | + ]); |
| 119 | + |
| 120 | + $yaml = \cebe\openapi\Writer::writeToYaml($openapi); |
| 121 | + |
| 122 | + |
| 123 | + $this->assertEquals(preg_replace('~\R~', "\n", <<<YAML |
| 124 | +openapi: 3.0.0 |
| 125 | +info: |
| 126 | + title: 'Test API' |
| 127 | + version: 1.0.0 |
| 128 | +paths: { } |
| 129 | +security: [] |
| 130 | +
|
| 131 | +YAML |
| 132 | + ), |
| 133 | + $yaml |
| 134 | + ); |
| 135 | + } |
| 136 | + |
| 137 | + public function testWriteEmptySecurityPartJson() |
| 138 | + { |
| 139 | + $openapi = $this->createOpenAPI([ |
| 140 | + 'security' => [new SecurityRequirement(['Bearer' => []])], |
| 141 | + ]); |
| 142 | + |
| 143 | + $json = \cebe\openapi\Writer::writeToJson($openapi); |
| 144 | + |
| 145 | + $this->assertEquals(preg_replace('~\R~', "\n", <<<JSON |
| 146 | +{ |
| 147 | + "openapi": "3.0.0", |
| 148 | + "info": { |
| 149 | + "title": "Test API", |
| 150 | + "version": "1.0.0" |
| 151 | + }, |
| 152 | + "paths": {}, |
| 153 | + "security": [ |
| 154 | + { |
| 155 | + "Bearer": [] |
| 156 | + } |
| 157 | + ] |
| 158 | +} |
| 159 | +JSON |
| 160 | + ), |
| 161 | + $json |
| 162 | + ); |
| 163 | + } |
| 164 | + |
| 165 | + |
| 166 | + public function testWriteEmptySecurityPartYaml() |
| 167 | + { |
| 168 | + $openapi = $this->createOpenAPI([ |
| 169 | + 'security' => [new SecurityRequirement(['Bearer' => []])], |
| 170 | + ]); |
| 171 | + |
| 172 | + $yaml = \cebe\openapi\Writer::writeToYaml($openapi); |
| 173 | + |
| 174 | + |
| 175 | + $this->assertEquals(preg_replace('~\R~', "\n", <<<YAML |
| 176 | +openapi: 3.0.0 |
| 177 | +info: |
| 178 | + title: 'Test API' |
| 179 | + version: 1.0.0 |
| 180 | +paths: { } |
| 181 | +security: |
| 182 | + - |
| 183 | + Bearer: [] |
| 184 | +
|
81 | 185 | YAML
|
82 | 186 | ),
|
83 | 187 | $yaml
|
|
0 commit comments