|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of the KleijnWeb\SwaggerBundle package. |
|
4
|
|
|
* |
|
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
6
|
|
|
* file that was distributed with this source code. |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace KleijnWeb\SwaggerBundle\Tests\Document; |
|
10
|
|
|
|
|
11
|
|
|
use KleijnWeb\SwaggerBundle\Document\Loader; |
|
12
|
|
|
use KleijnWeb\SwaggerBundle\Document\RefResolver; |
|
13
|
|
|
use KleijnWeb\SwaggerBundle\Document\YamlParser; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @author John Kleijn <[email protected]> |
|
17
|
|
|
*/ |
|
18
|
|
|
class RefResolverTest extends \PHPUnit_Framework_TestCase |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @test |
|
22
|
|
|
*/ |
|
23
|
|
|
public function canResolveResourceSchemaReferences() |
|
24
|
|
|
{ |
|
25
|
|
|
$resolver = $this->construct('petstore.yml'); |
|
26
|
|
|
$resolver->resolve(); |
|
27
|
|
|
|
|
28
|
|
|
$schemas = $resolver->getDefinition()->definitions; |
|
29
|
|
|
$propertySchema = $schemas->Pet->properties->category; |
|
30
|
|
|
|
|
31
|
|
|
$this->assertObjectNotHasAttribute('$ref', $propertySchema); |
|
32
|
|
|
$this->assertObjectHasAttribute('x-ref-id', $propertySchema); |
|
33
|
|
|
$this->assertSame('object', $propertySchema->type); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @test |
|
38
|
|
|
*/ |
|
39
|
|
|
public function canResolveParameterSchemaReferences() |
|
40
|
|
|
{ |
|
41
|
|
|
$resolver = $this->construct('instagram.yml'); |
|
42
|
|
|
$pathDefinitions = $resolver->getDefinition()->paths; |
|
43
|
|
|
$pathDefinition = $pathDefinitions->{'/users/{user-id}'}; |
|
44
|
|
|
|
|
45
|
|
|
$this->assertInternalType('array', $pathDefinition->parameters); |
|
46
|
|
|
$pathDefinition = $pathDefinitions->{'/users/{user-id}'}; |
|
47
|
|
|
|
|
48
|
|
|
$resolver->resolve(); |
|
49
|
|
|
|
|
50
|
|
|
$this->assertInternalType('array', $pathDefinition->parameters); |
|
51
|
|
|
$argumentPseudoSchema = $pathDefinition->parameters[0]; |
|
52
|
|
|
|
|
53
|
|
|
$this->assertObjectNotHasAttribute('$ref', $argumentPseudoSchema); |
|
54
|
|
|
$this->assertObjectHasAttribute('in', $argumentPseudoSchema); |
|
55
|
|
|
$this->assertSame('user-id', $argumentPseudoSchema->name); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @test |
|
60
|
|
|
*/ |
|
61
|
|
|
public function canResolveReferencesWithSlashed() |
|
62
|
|
|
{ |
|
63
|
|
|
$resolver = $this->construct('partials/slashes.yml'); |
|
64
|
|
|
$this->assertSame('thevalue', $resolver->resolve()->Foo->bar); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @test |
|
69
|
|
|
* |
|
70
|
|
|
*/ |
|
71
|
|
|
public function canResolveExternalReferencesInExample() |
|
72
|
|
|
{ |
|
73
|
|
|
$resolver = $this->construct('composite.yml'); |
|
74
|
|
|
$document = $resolver->resolve(); |
|
75
|
|
|
|
|
76
|
|
|
$this->assertObjectHasAttribute('schema', $document->responses->Created); |
|
77
|
|
|
|
|
78
|
|
|
$response = $document->paths->{'/pet'}->post->responses->{'500'}; |
|
79
|
|
|
|
|
80
|
|
|
$this->assertObjectHasAttribute('description', $response); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @test |
|
85
|
|
|
*/ |
|
86
|
|
|
public function canUnResolve() |
|
87
|
|
|
{ |
|
88
|
|
|
$resolver = $this->construct('composite.yml'); |
|
89
|
|
|
|
|
90
|
|
|
$expected = clone $resolver->getDefinition(); |
|
91
|
|
|
$resolver->resolve(); |
|
92
|
|
|
$document = $resolver->unresolve(); |
|
93
|
|
|
|
|
94
|
|
|
$this->assertObjectNotHasAttribute('schema', $document->responses->Created); |
|
95
|
|
|
$this->assertEquals($expected, $document); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @dataProvider externalReferenceProvider |
|
100
|
|
|
* @test |
|
101
|
|
|
* |
|
102
|
|
|
* @param mixed $expected |
|
103
|
|
|
* @param string $fileUrl |
|
104
|
|
|
* @param string $uri |
|
105
|
|
|
* @param array|\stdClass $content |
|
106
|
|
|
* |
|
107
|
|
|
*/ |
|
108
|
|
|
public function willProperlyResolveExternalReferences($expected, $fileUrl, $uri, $content) |
|
109
|
|
|
{ |
|
110
|
|
|
$mockLoader = $this |
|
111
|
|
|
->getMockBuilder('KleijnWeb\SwaggerBundle\Document\Loader') |
|
112
|
|
|
->disableOriginalConstructor() |
|
113
|
|
|
->getMock(); |
|
114
|
|
|
|
|
115
|
|
|
$object = (object)['$ref' => $uri]; |
|
116
|
|
|
$resolver = new RefResolver($object, '/somedir/faux', $mockLoader); |
|
117
|
|
|
|
|
118
|
|
|
$mockLoader |
|
119
|
|
|
->expects($this->once()) |
|
120
|
|
|
->method('load') |
|
121
|
|
|
->with($fileUrl) |
|
122
|
|
|
->willReturn($content); |
|
123
|
|
|
|
|
124
|
|
|
$value = $resolver->resolve(); |
|
125
|
|
|
|
|
126
|
|
|
$this->assertSame($expected, $value); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @return array |
|
131
|
|
|
*/ |
|
132
|
|
|
public static function externalReferenceProvider() |
|
133
|
|
|
{ |
|
134
|
|
|
return [ |
|
135
|
|
|
[ |
|
136
|
|
|
'foo', |
|
137
|
|
|
'somedir/entities.json', |
|
138
|
|
|
'entities.json#/definitions/SomeType', |
|
139
|
|
|
(object)['definitions' => (object)['SomeType' => 'foo']] |
|
140
|
|
|
], |
|
141
|
|
|
[ |
|
142
|
|
|
'bar', |
|
143
|
|
|
'somedir/entities.yaml', |
|
144
|
|
|
'entities.yaml#/definitions/SomeType', |
|
145
|
|
|
(object)['definitions' => (object)['SomeType' => 'bar']] |
|
146
|
|
|
], |
|
147
|
|
|
[ |
|
148
|
|
|
'mary', |
|
149
|
|
|
'somedir/entities/had/a/little.yaml', |
|
150
|
|
|
'entities/had/a/little.yaml#/Lamb', |
|
151
|
|
|
(object)['Lamb' => 'mary'] |
|
152
|
|
|
], |
|
153
|
|
|
[ |
|
154
|
|
|
'wow', |
|
155
|
|
|
'wss://many:[email protected]:8080/so.yml?much', |
|
156
|
|
|
'wss://many:[email protected]:8080/so.yml?much#/flexibility', |
|
157
|
|
|
(object)['flexibility' => 'wow'] |
|
158
|
|
|
], |
|
159
|
|
|
[ |
|
160
|
|
|
'local', |
|
161
|
|
|
'somedir/local.yml', |
|
162
|
|
|
'file://local.yml#/definitions/SomeType', |
|
163
|
|
|
(object)['definitions' => (object)['SomeType' => 'local']] |
|
164
|
|
|
] |
|
165
|
|
|
]; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* @param string $path |
|
170
|
|
|
* |
|
171
|
|
|
* @return RefResolver |
|
172
|
|
|
*/ |
|
173
|
|
|
private function construct($path) |
|
174
|
|
|
{ |
|
175
|
|
|
$filePath = "src/Tests/Functional/PetStore/app/swagger/$path"; |
|
176
|
|
|
$contents = file_get_contents($filePath); |
|
177
|
|
|
$parser = new YamlParser(); |
|
178
|
|
|
/** @var object $object */ |
|
179
|
|
|
$object = $parser->parse($contents); |
|
180
|
|
|
$resolver = new RefResolver($object, $filePath); |
|
181
|
|
|
|
|
182
|
|
|
return $resolver; |
|
183
|
|
|
} |
|
184
|
|
|
} |
|
185
|
|
|
|