44use cebe \openapi \spec \Operation ;
55use cebe \openapi \spec \PathItem ;
66use cebe \openapi \spec \Paths ;
7+ use cebe \openapi \spec \Response ;
8+ use cebe \openapi \spec \Responses ;
79
810/**
911 * @covers \cebe\openapi\spec\Paths
@@ -63,6 +65,48 @@ public function testRead()
6365 }
6466 }
6567
68+ public function testCreateionFromObjects ()
69+ {
70+ $ paths = new Paths ([
71+ '/pets ' => new PathItem ([
72+ 'get ' => new Operation ([
73+ 'responses ' => new Responses ([
74+ 200 => new Response (['description ' => 'A list of pets. ' ]),
75+ 404 => ['description ' => 'The pets list is gone 🙀 ' ],
76+ ])
77+ ])
78+ ])
79+ ]);
80+
81+ $ this ->assertTrue ($ paths ->hasPath ('/pets ' ));
82+ $ this ->assertInstanceOf (PathItem::class, $ paths ->getPath ('/pets ' ));
83+ $ this ->assertInstanceOf (PathItem::class, $ paths ['/pets ' ]);
84+ $ this ->assertInstanceOf (Operation::class, $ paths ->getPath ('/pets ' )->get );
85+
86+ $ this ->assertSame ('A list of pets. ' , $ paths ->getPath ('/pets ' )->get ->responses ->getResponse (200 )->description );
87+ $ this ->assertSame ('The pets list is gone 🙀 ' , $ paths ->getPath ('/pets ' )->get ->responses ->getResponse (404 )->description );
88+ }
89+
90+ public function badPathsConfigProvider ()
91+ {
92+ yield [['/pets ' => 'foo ' ], 'Path MUST be either array or PathItem object, "string" given ' ];
93+ yield [['/pets ' => 42 ], 'Path MUST be either array or PathItem object, "integer" given ' ];
94+ yield [['/pets ' => false ], 'Path MUST be either array or PathItem object, "boolean" given ' ];
95+ yield [['/pets ' => new stdClass ()], 'Path MUST be either array or PathItem object, "stdClass" given ' ];
96+ // The last one can be supported in future, but now SpecBaseObjects::__construct() requires array explicitly
97+ }
98+
99+ /**
100+ * @dataProvider badPathsConfigProvider
101+ */
102+ public function testPathsCanNotBeCreatedFromBullshit ($ config , $ expectedException )
103+ {
104+ $ this ->expectException (\cebe \openapi \exceptions \TypeErrorException::class);
105+ $ this ->expectExceptionMessage ($ expectedException );
106+
107+ new Paths ($ config );
108+ }
109+
66110 public function testInvalidPath ()
67111 {
68112 /** @var $paths Paths */
@@ -88,4 +132,5 @@ public function testInvalidPath()
88132 ], $ paths ->getErrors ());
89133 $ this ->assertFalse ($ result );
90134 }
135+
91136}
0 commit comments