|
| 1 | +<?php |
| 2 | + |
| 3 | + |
| 4 | +use cebe\openapi\ReferenceContext; |
| 5 | +use cebe\openapi\spec\OpenApi; |
| 6 | + |
| 7 | +class ReferenceContextTest extends \PHPUnit\Framework\TestCase |
| 8 | +{ |
| 9 | + public function uriProvider() |
| 10 | + { |
| 11 | + $data = [ |
| 12 | + [ |
| 13 | + 'https://example.com/openapi.yaml', // base URI |
| 14 | + 'definitions.yaml', // referenced URI |
| 15 | + 'https://example.com/definitions.yaml', // expected result |
| 16 | + ], |
| 17 | + [ |
| 18 | + 'https://example.com/openapi.yaml', // base URI |
| 19 | + '/definitions.yaml', // referenced URI |
| 20 | + 'https://example.com/definitions.yaml', // expected result |
| 21 | + ], |
| 22 | + [ |
| 23 | + 'https://example.com/api/openapi.yaml', // base URI |
| 24 | + 'definitions.yaml', // referenced URI |
| 25 | + 'https://example.com/api/definitions.yaml', // expected result |
| 26 | + ], |
| 27 | + [ |
| 28 | + 'https://example.com/api/openapi.yaml', // base URI |
| 29 | + '/definitions.yaml', // referenced URI |
| 30 | + 'https://example.com/definitions.yaml', // expected result |
| 31 | + ], |
| 32 | + [ |
| 33 | + 'https://example.com/api/openapi.yaml', // base URI |
| 34 | + '../definitions.yaml', // referenced URI |
| 35 | + 'https://example.com/api/../definitions.yaml', // expected result |
| 36 | + ], |
| 37 | + [ |
| 38 | + '/var/www/openapi.yaml', // base URI |
| 39 | + 'definitions.yaml', // referenced URI |
| 40 | + 'file:///var/www/definitions.yaml', // expected result |
| 41 | + ], |
| 42 | + [ |
| 43 | + '/var/www/openapi.yaml', // base URI |
| 44 | + '/var/definitions.yaml', // referenced URI |
| 45 | + 'file:///var/definitions.yaml', // expected result |
| 46 | + ], |
| 47 | + |
| 48 | + ]; |
| 49 | + |
| 50 | + // absolute URLs should not be changed |
| 51 | + foreach(array_unique(array_map('current', $data)) as $url) { |
| 52 | + $data[] = [ |
| 53 | + $url, |
| 54 | + 'file:///var/www/definitions.yaml', |
| 55 | + 'file:///var/www/definitions.yaml', |
| 56 | + ]; |
| 57 | + $data[] = [ |
| 58 | + $url, |
| 59 | + 'https://example.com/definitions.yaml', |
| 60 | + 'https://example.com/definitions.yaml', |
| 61 | + ]; |
| 62 | + } |
| 63 | + |
| 64 | + return $data; |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * @dataProvider uriProvider |
| 69 | + */ |
| 70 | + public function testResolveUri($baseUri, $referencedUri, $expected) |
| 71 | + { |
| 72 | + $context = new ReferenceContext(new OpenApi([]), $baseUri); |
| 73 | + $this->assertEquals($expected, $context->resolveRelativeUri($referencedUri)); |
| 74 | + } |
| 75 | + |
| 76 | +} |
0 commit comments