|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace League\OpenAPIValidation\Tests\FromCommunity; |
| 6 | + |
| 7 | +use cebe\openapi\Reader; |
| 8 | +use League\OpenAPIValidation\PSR7\PathFinder; |
| 9 | +use PHPUnit\Framework\TestCase; |
| 10 | + |
| 11 | +/** |
| 12 | + * @see https://github.com/thephpleague/openapi-psr7-validator/issues/79 |
| 13 | + */ |
| 14 | +final class Issue79Test extends TestCase |
| 15 | +{ |
| 16 | + /** |
| 17 | + * @dataProvider provideSpecAndOperationToMatch() |
| 18 | + */ |
| 19 | + public function testItFindsMatchingOperationWithTheRightServer( |
| 20 | + string $spec, |
| 21 | + string $path, |
| 22 | + string $method, |
| 23 | + string $expectedPath |
| 24 | + ) : void { |
| 25 | + $pathFinder = new PathFinder(Reader::readFromYaml($spec), $path, $method); |
| 26 | + $opAddrs = $pathFinder->search(); |
| 27 | + |
| 28 | + $this->assertCount(1, $opAddrs); |
| 29 | + $this->assertEquals($expectedPath, $opAddrs[0]->path()); |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * @return string[] |
| 34 | + */ |
| 35 | + public function provideSpecAndOperationToMatch() : iterable |
| 36 | + { |
| 37 | + yield 'Server override on the operation level' => [ |
| 38 | + <<<YAML |
| 39 | +openapi: "3.0.0" |
| 40 | +info: |
| 41 | + title: Uber API |
| 42 | + description: Move your app forward with the Uber API |
| 43 | + version: "1.0.0" |
| 44 | +servers: |
| 45 | + - url: /v1 |
| 46 | +paths: |
| 47 | + /products/{id}: |
| 48 | + servers: |
| 49 | + - url: /v2 |
| 50 | + get: |
| 51 | + summary: Product Types |
| 52 | + servers: |
| 53 | + - url: /v3 |
| 54 | +YAML |
| 55 | +, |
| 56 | + '/v3/products/10', |
| 57 | + 'get', |
| 58 | + '/products/{id}', |
| 59 | + ]; |
| 60 | + |
| 61 | + yield 'Server override on the path level' => [ |
| 62 | + <<<YAML |
| 63 | +openapi: "3.0.0" |
| 64 | +info: |
| 65 | + title: Uber API |
| 66 | + description: Move your app forward with the Uber API |
| 67 | + version: "1.0.0" |
| 68 | +servers: |
| 69 | + - url: /v1 |
| 70 | +paths: |
| 71 | + /products/{id}: |
| 72 | + servers: |
| 73 | + - url: /v2 |
| 74 | + get: |
| 75 | + summary: Product Types |
| 76 | +YAML |
| 77 | +, |
| 78 | + '/v2/products/10', |
| 79 | + 'get', |
| 80 | + '/products/{id}', |
| 81 | + ]; |
| 82 | + |
| 83 | + yield 'Server from the root level' => [ |
| 84 | + <<<YAML |
| 85 | +openapi: "3.0.0" |
| 86 | +info: |
| 87 | + title: Uber API |
| 88 | + description: Move your app forward with the Uber API |
| 89 | + version: "1.0.0" |
| 90 | +servers: |
| 91 | + - url: /v1 |
| 92 | +paths: |
| 93 | + /products/{id}: |
| 94 | + get: |
| 95 | + summary: Product Types |
| 96 | +YAML |
| 97 | +, |
| 98 | + '/v1/products/10', |
| 99 | + 'get', |
| 100 | + '/products/{id}', |
| 101 | + ]; |
| 102 | + |
| 103 | + yield 'Default server' => [ |
| 104 | + <<<YAML |
| 105 | +openapi: "3.0.0" |
| 106 | +info: |
| 107 | + title: Uber API |
| 108 | + description: Move your app forward with the Uber API |
| 109 | + version: "1.0.0" |
| 110 | +paths: |
| 111 | + /products/{id}: |
| 112 | + get: |
| 113 | + summary: Product Types |
| 114 | +YAML |
| 115 | +, |
| 116 | + '/products/10', |
| 117 | + 'get', |
| 118 | + '/products/{id}', |
| 119 | + ]; |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * @dataProvider provideSpecAndOperationToNotMatch() |
| 124 | + */ |
| 125 | + public function testItDoesNotFindMatchingOperationWithTheWrongServer( |
| 126 | + string $spec, |
| 127 | + string $path, |
| 128 | + string $method |
| 129 | + ) : void { |
| 130 | + $pathFinder = new PathFinder(Reader::readFromYaml($spec), $path, $method); |
| 131 | + $opAddrs = $pathFinder->search(); |
| 132 | + |
| 133 | + $this->assertCount(0, $opAddrs); |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * @return string[] |
| 138 | + */ |
| 139 | + public function provideSpecAndOperationToNotMatch() : iterable |
| 140 | + { |
| 141 | + yield 'Server override on the operation level' => [ |
| 142 | + <<<YAML |
| 143 | +openapi: "3.0.0" |
| 144 | +info: |
| 145 | + title: Uber API |
| 146 | + description: Move your app forward with the Uber API |
| 147 | + version: "1.0.0" |
| 148 | +servers: |
| 149 | + - url: /v1 |
| 150 | +paths: |
| 151 | + /products/{id}: |
| 152 | + servers: |
| 153 | + - url: /v2 |
| 154 | + get: |
| 155 | + summary: Product Types |
| 156 | + servers: |
| 157 | + - url: /v3 |
| 158 | +YAML |
| 159 | +, |
| 160 | + '/v2/products/10', |
| 161 | + 'get', |
| 162 | + ]; |
| 163 | + |
| 164 | + yield 'Server override on the path level' => [ |
| 165 | + <<<YAML |
| 166 | +openapi: "3.0.0" |
| 167 | +info: |
| 168 | + title: Uber API |
| 169 | + description: Move your app forward with the Uber API |
| 170 | + version: "1.0.0" |
| 171 | +servers: |
| 172 | + - url: /v1 |
| 173 | +paths: |
| 174 | + /products/{id}: |
| 175 | + servers: |
| 176 | + - url: /v2 |
| 177 | + get: |
| 178 | + summary: Product Types |
| 179 | +YAML |
| 180 | +, |
| 181 | + '/v1/products/10', |
| 182 | + 'get', |
| 183 | + ]; |
| 184 | + |
| 185 | + yield 'Server from the root level' => [ |
| 186 | + <<<YAML |
| 187 | +openapi: "3.0.0" |
| 188 | +info: |
| 189 | + title: Uber API |
| 190 | + description: Move your app forward with the Uber API |
| 191 | + version: "1.0.0" |
| 192 | +servers: |
| 193 | + - url: /v1 |
| 194 | +paths: |
| 195 | + /products/{id}: |
| 196 | + get: |
| 197 | + summary: Product Types |
| 198 | +YAML |
| 199 | +, |
| 200 | + '/products/10', |
| 201 | + 'get', |
| 202 | + ]; |
| 203 | + } |
| 204 | +} |
0 commit comments