11<?php
22
33use cebe \openapi \Reader ;
4+ use cebe \openapi \ReferenceContext ;
45use cebe \openapi \spec \Example ;
56use cebe \openapi \spec \OpenApi ;
67use cebe \openapi \spec \Parameter ;
@@ -18,17 +19,17 @@ class ReferenceTest extends \PHPUnit\Framework\TestCase
1819 /** @var MockWebServer */
1920 private $ server ;
2021
21- // protected function setUp(): void
22- // {
23- // $this->server = new MockWebServer();
24- // $this->server->stop();
25- // $this->server->start();
26- // }
27- //
28- // protected function tearDown(): void
29- // {
30- // $this->server->stop();
31- // }
22+ protected function setUp (): void
23+ {
24+ $ this ->server = new MockWebServer ();
25+ $ this ->server ->stop ();
26+ $ this ->server ->start ();
27+ }
28+
29+ protected function tearDown (): void
30+ {
31+ $ this ->server ->stop ();
32+ }
3233
3334 public function testResolveInDocument ()
3435 {
@@ -82,7 +83,7 @@ public function testResolveInDocument()
8283 $ this ->assertInstanceOf (Reference::class, $ petResponse ->content ['application/json ' ]->examples ['frog ' ]);
8384 $ this ->assertInstanceOf (Reference::class, $ openapi ->paths ->getPath ('/pet/1 ' )->get ->responses ['200 ' ]);
8485
85- $ openapi ->resolveReferences (new \ cebe \ openapi \ ReferenceContext ($ openapi , 'file:///tmp/openapi.yaml ' ));
86+ $ openapi ->resolveReferences (new ReferenceContext ($ openapi , 'file:///tmp/openapi.yaml ' ));
8687
8788 $ this ->assertInstanceOf (Schema::class, $ refSchema = $ petResponse ->content ['application/json ' ]->schema );
8889 $ this ->assertInstanceOf (Example::class, $ refExample = $ petResponse ->content ['application/json ' ]->examples ['frog ' ]);
@@ -141,7 +142,7 @@ public function testResolveCyclicReferenceInDocument()
141142 $ this ->assertInstanceOf (Reference::class, $ response ->content ['application/json ' ]->examples ['frog ' ]);
142143
143144// $this->expectException(\cebe\openapi\exceptions\UnresolvableReferenceException::class);
144- $ openapi ->resolveReferences (new \ cebe \ openapi \ ReferenceContext ($ openapi , 'file:///tmp/openapi.yaml ' ));
145+ $ openapi ->resolveReferences (new ReferenceContext ($ openapi , 'file:///tmp/openapi.yaml ' ));
145146
146147 $ this ->assertInstanceOf (Schema::class, $ petItems = $ openapi ->components ->schemas ['Pet ' ]->properties ['id ' ]->items );
147148 $ this ->assertInstanceOf (Schema::class, $ refSchema = $ response ->content ['application/json ' ]->schema );
@@ -176,7 +177,7 @@ public function testResolveFile()
176177 $ this ->assertInstanceOf (Reference::class, $ petItems = $ openapi ->components ->schemas ['Pet ' ]);
177178 $ this ->assertInstanceOf (Reference::class, $ petItems = $ openapi ->components ->schemas ['Dog ' ]);
178179
179- $ openapi ->resolveReferences (new \ cebe \ openapi \ ReferenceContext ($ openapi , $ file ));
180+ $ openapi ->resolveReferences (new ReferenceContext ($ openapi , $ file ));
180181
181182 $ this ->assertInstanceOf (Schema::class, $ petItems = $ openapi ->components ->schemas ['Pet ' ]);
182183 $ this ->assertInstanceOf (Schema::class, $ petItems = $ openapi ->components ->schemas ['Dog ' ]);
@@ -205,7 +206,7 @@ public function testResolveFileInSubdir()
205206 $ this ->assertInstanceOf (Reference::class, $ openapi ->components ->schemas ['Dog ' ]);
206207 $ this ->assertInstanceOf (Reference::class, $ openapi ->components ->parameters ['Parameter.PetId ' ]);
207208
208- $ openapi ->resolveReferences (new \ cebe \ openapi \ ReferenceContext ($ openapi , $ file ));
209+ $ openapi ->resolveReferences (new ReferenceContext ($ openapi , $ file ));
209210
210211 $ this ->assertInstanceOf (Schema::class, $ openapi ->components ->schemas ['Pet ' ]);
211212 $ this ->assertInstanceOf (Schema::class, $ openapi ->components ->schemas ['Dog ' ]);
@@ -247,69 +248,69 @@ public function testResolveFileInSubdirWithMultipleRelativePaths()
247248 $ this ->assertTrue ($ result );
248249 }
249250
250- // public function testResolveFileHttp()
251- // {
252- // $this->server->setResponseOfPath(
253- // '/cebe/php-openapi/290389bbd337cf4d70ecedfd3a3d886715e19552/tests/spec/data/reference/definitions.yaml',
254- // '
255- // Pet:
256- // type: object
257- // properties:
258- // id:
259- // type: integer
260- // format: int64
261- // Dog:
262- // type: object
263- // properties:
264- // name:
265- // type: string
266- // '
267- // );
268- //
269- // $this->server->setResponseOfPath(
270- // '/cebe/php-openapi/290389bbd337cf4d70ecedfd3a3d886715e19552/tests/spec/data/reference/base.yaml',
271- // '
272- // openapi: 3.0.0
273- // info:
274- // title: Link Example
275- // version: 1.0.0
276- // components:
277- // schemas:
278- // Pet:
279- // $ref: definitions.yaml#/Pet
280- // Dog:
281- // $ref: ##ABSOLUTEPATH##/definitions.yaml#/Dog
282- // paths:
283- // \'/pet\':
284- // get:
285- // responses:
286- // 200:
287- // description: return a pet
288- // '
289- // );
290- //
291- // // $file = 'https://raw.githubusercontent.com/cebe/php-openapi/290389bbd337cf4d70ecedfd3a3d886715e19552/tests/spec/data/reference/base.yaml';
292- //
293- // $host = $this->server->getHost(). ':'. $this->server->getPort();
294- // $path = '/cebe/php-openapi/290389bbd337cf4d70ecedfd3a3d886715e19552/tests/spec/data/reference/base.yaml';
295- // $file = 'http://'. $host. $path;
296- // /** @var $openapi OpenApi */
297- // $openapi = Reader::readFromYaml(str_replace('##ABSOLUTEPATH##', dirname($file), file_get_contents($file)));
298- //
299- // $result = $openapi->validate();
300- // $this->assertEquals([], $openapi->getErrors());
301- // $this->assertTrue($result);
302- //
303- // $this->assertInstanceOf(Reference::class, $petItems = $openapi->components->schemas['Pet']);
304- // $this->assertInstanceOf(Reference::class, $petItems = $openapi->components->schemas['Dog']);
305- //
306- // $openapi->resolveReferences(new \cebe\openapi\ ReferenceContext($openapi, $file));
307- //
308- // $this->assertInstanceOf(Schema::class, $petItems = $openapi->components->schemas['Pet']);
309- // $this->assertInstanceOf(Schema::class, $petItems = $openapi->components->schemas['Dog']);
310- // $this->assertArrayHasKey('id', $openapi->components->schemas['Pet']->properties);
311- // $this->assertArrayHasKey('name', $openapi->components->schemas['Dog']->properties);
312- // }
251+ public function testResolveFileHttp ()
252+ {
253+ $ this ->server ->setResponseOfPath (
254+ '/cebe/php-openapi/290389bbd337cf4d70ecedfd3a3d886715e19552/tests/spec/data/reference/definitions.yaml ' ,
255+ '
256+ Pet:
257+ type: object
258+ properties:
259+ id:
260+ type: integer
261+ format: int64
262+ Dog:
263+ type: object
264+ properties:
265+ name:
266+ type: string
267+ '
268+ );
269+
270+ $ this ->server ->setResponseOfPath (
271+ '/cebe/php-openapi/290389bbd337cf4d70ecedfd3a3d886715e19552/tests/spec/data/reference/base.yaml ' ,
272+ '
273+ openapi: 3.0.0
274+ info:
275+ title: Link Example
276+ version: 1.0.0
277+ components:
278+ schemas:
279+ Pet:
280+ $ref: definitions.yaml#/Pet
281+ Dog:
282+ $ref: ##ABSOLUTEPATH##/definitions.yaml#/Dog
283+ paths:
284+ \'/pet \':
285+ get:
286+ responses:
287+ 200:
288+ description: return a pet
289+ '
290+ );
291+
292+ // $file = 'https://raw.githubusercontent.com/cebe/php-openapi/290389bbd337cf4d70ecedfd3a3d886715e19552/tests/spec/data/reference/base.yaml';
293+
294+ $ host = $ this ->server ->getHost () . ': ' . $ this ->server ->getPort ();
295+ $ path = '/cebe/php-openapi/290389bbd337cf4d70ecedfd3a3d886715e19552/tests/spec/data/reference/base.yaml ' ;
296+ $ file = 'http:// ' . $ host . $ path ;
297+ /** @var $openapi OpenApi */
298+ $ openapi = Reader::readFromYaml (str_replace ('##ABSOLUTEPATH## ' , dirname ($ file ), file_get_contents ($ file )));
299+
300+ $ result = $ openapi ->validate ();
301+ $ this ->assertEquals ([], $ openapi ->getErrors ());
302+ $ this ->assertTrue ($ result );
303+
304+ $ this ->assertInstanceOf (Reference::class, $ petItems = $ openapi ->components ->schemas ['Pet ' ]);
305+ $ this ->assertInstanceOf (Reference::class, $ petItems = $ openapi ->components ->schemas ['Dog ' ]);
306+
307+ $ openapi ->resolveReferences (new ReferenceContext ($ openapi , $ file ));
308+
309+ $ this ->assertInstanceOf (Schema::class, $ petItems = $ openapi ->components ->schemas ['Pet ' ]);
310+ $ this ->assertInstanceOf (Schema::class, $ petItems = $ openapi ->components ->schemas ['Dog ' ]);
311+ $ this ->assertArrayHasKey ('id ' , $ openapi ->components ->schemas ['Pet ' ]->properties );
312+ $ this ->assertArrayHasKey ('name ' , $ openapi ->components ->schemas ['Dog ' ]->properties );
313+ }
313314
314315 public function testResolvePaths ()
315316 {
@@ -371,7 +372,7 @@ enum:
371372
372373YAML;
373374 $ openapi = Reader::readFromYaml ($ schema );
374- $ openapi ->resolveReferences (new \ cebe \ openapi \ ReferenceContext ($ openapi , $ this ->createFileUri (__DIR__ . '/data/reference/definitions.yaml ' )));
375+ $ openapi ->resolveReferences (new ReferenceContext ($ openapi , $ this ->createFileUri (__DIR__ . '/data/reference/definitions.yaml ' )));
375376
376377 $ this ->assertTrue (isset ($ openapi ->components ->schemas ['Pet ' ]));
377378 $ this ->assertEquals (['One ' , 'Two ' ], $ openapi ->components ->schemas ['Pet ' ]->properties ['typeA ' ]->enum );
@@ -413,7 +414,7 @@ public function testTransitiveReference()
413414YAML;
414415
415416 $ openapi = Reader::readFromYaml ($ schema );
416- $ openapi ->resolveReferences (new \ cebe \ openapi \ ReferenceContext ($ openapi , 'file:///tmp/openapi.yaml ' ));
417+ $ openapi ->resolveReferences (new ReferenceContext ($ openapi , 'file:///tmp/openapi.yaml ' ));
417418
418419 $ this ->assertTrue (isset ($ openapi ->components ->schemas ['City ' ]));
419420 $ this ->assertTrue (isset ($ openapi ->components ->schemas ['Named ' ]));
@@ -452,7 +453,7 @@ public function testTransitiveReferenceToFile()
452453YAML;
453454
454455 $ openapi = Reader::readFromYaml ($ schema );
455- $ openapi ->resolveReferences (new \ cebe \ openapi \ ReferenceContext ($ openapi , $ this ->createFileUri (__DIR__ . '/data/reference/definitions.yaml ' )));
456+ $ openapi ->resolveReferences (new ReferenceContext ($ openapi , $ this ->createFileUri (__DIR__ . '/data/reference/definitions.yaml ' )));
456457
457458 $ this ->assertTrue (isset ($ openapi ->components ->schemas ['Dog ' ]));
458459 $ this ->assertEquals ('object ' , $ openapi ->components ->schemas ['Dog ' ]->type );
@@ -490,12 +491,12 @@ public function testTransitiveReferenceCyclic()
490491 $ this ->expectException (\cebe \openapi \exceptions \UnresolvableReferenceException::class);
491492 $ this ->expectExceptionMessage ('Cyclic reference detected on a Reference Object. ' );
492493
493- $ openapi ->resolveReferences (new \ cebe \ openapi \ ReferenceContext ($ openapi , 'file:///tmp/openapi.yaml ' ));
494+ $ openapi ->resolveReferences (new ReferenceContext ($ openapi , 'file:///tmp/openapi.yaml ' ));
494495 }
495496
496497 public function testTransitiveReferenceOverTwoFiles ()
497498 {
498- $ openapi = Reader::readFromYamlFile (__DIR__ . '/data/reference/structure.yaml ' , OpenApi::class, \ cebe \ openapi \ ReferenceContext::RESOLVE_MODE_INLINE );
499+ $ openapi = Reader::readFromYamlFile (__DIR__ . '/data/reference/structure.yaml ' , OpenApi::class, ReferenceContext::RESOLVE_MODE_INLINE );
499500
500501 $ yaml = \cebe \openapi \Writer::writeToYaml ($ openapi );
501502
@@ -530,7 +531,7 @@ public function testTransitiveReferenceOverTwoFiles()
530531
531532 public function testReferencedCommonParamsInReferencedPath ()
532533 {
533- $ openapi = Reader::readFromYamlFile (__DIR__ . '/data/reference/ReferencedCommonParamsInReferencedPath.yml ' , OpenApi::class, \ cebe \ openapi \ ReferenceContext::RESOLVE_MODE_INLINE );
534+ $ openapi = Reader::readFromYamlFile (__DIR__ . '/data/reference/ReferencedCommonParamsInReferencedPath.yml ' , OpenApi::class, ReferenceContext::RESOLVE_MODE_INLINE );
534535 $ yaml = \cebe \openapi \Writer::writeToYaml ($ openapi );
535536 $ expected = <<<YAML
536537openapi: 3.0.0
@@ -595,7 +596,7 @@ enum:
595596
596597 public function testResolveRelativePathInline ()
597598 {
598- $ openapi = Reader::readFromYamlFile (__DIR__ . '/data/reference/openapi_models.yaml ' , OpenApi::class, \ cebe \ openapi \ ReferenceContext::RESOLVE_MODE_INLINE );
599+ $ openapi = Reader::readFromYamlFile (__DIR__ . '/data/reference/openapi_models.yaml ' , OpenApi::class, ReferenceContext::RESOLVE_MODE_INLINE );
599600
600601 $ yaml = \cebe \openapi \Writer::writeToYaml ($ openapi );
601602
@@ -648,7 +649,7 @@ public function testResolveRelativePathInline()
648649
649650 public function testResolveRelativePathAll ()
650651 {
651- $ openapi = Reader::readFromYamlFile (__DIR__ . '/data/reference/openapi_models.yaml ' , OpenApi::class, \ cebe \ openapi \ ReferenceContext::RESOLVE_MODE_ALL );
652+ $ openapi = Reader::readFromYamlFile (__DIR__ . '/data/reference/openapi_models.yaml ' , OpenApi::class, ReferenceContext::RESOLVE_MODE_ALL );
652653
653654 $ yaml = \cebe \openapi \Writer::writeToYaml ($ openapi );
654655
0 commit comments