Skip to content

Commit 8e42370

Browse files
committed
Added tests, enhanced PHPDocs
1 parent 4462172 commit 8e42370

File tree

3 files changed

+57
-5
lines changed

3 files changed

+57
-5
lines changed

src/spec/Paths.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public function __construct(array $data)
6060
if ($givenType === 'object') {
6161
$givenType = get_class($object);
6262
}
63-
throw new \TypeError(sprintf('Path MUST be either array or PathItem object, "%s" given', $givenType));
63+
throw new TypeErrorException(sprintf('Path MUST be either array or PathItem object, "%s" given', $givenType));
6464
}
6565
}
6666
}

src/spec/Responses.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use ArrayAccess;
1111
use ArrayIterator;
1212
use cebe\openapi\DocumentContextInterface;
13+
use cebe\openapi\exceptions\TypeErrorException;
1314
use cebe\openapi\exceptions\UnresolvableReferenceException;
1415
use cebe\openapi\json\JsonPointer;
1516
use cebe\openapi\ReferenceContext;
@@ -37,8 +38,8 @@ class Responses implements SpecObjectInterface, DocumentContextInterface, ArrayA
3738

3839
/**
3940
* Create an object from spec data.
40-
* @param array $data spec data read from YAML or JSON
41-
* @throws \cebe\openapi\exceptions\TypeErrorException in case invalid data is supplied.
41+
* @param array[]|Response[]|Reference[] $data spec data read from YAML or JSON
42+
* @throws TypeErrorException in case invalid data is supplied.
4243
*/
4344
public function __construct(array $data)
4445
{
@@ -48,10 +49,16 @@ public function __construct(array $data)
4849
if (preg_match('~^(?:default|[1-5](?:[0-9][0-9]|XX))$~', $statusCode)) {
4950
if ($response instanceof Response || $response instanceof Reference) {
5051
$this->_responses[$statusCode] = $response;
51-
} elseif (isset($response['$ref'])) {
52+
} elseif (is_array($response) && isset($response['$ref'])) {
5253
$this->_responses[$statusCode] = new Reference($response, Response::class);
53-
} else {
54+
} elseif (is_array($response)) {
5455
$this->_responses[$statusCode] = new Response($response);
56+
} else {
57+
$givenType = gettype($response);
58+
if ($givenType === 'object') {
59+
$givenType = get_class($response);
60+
}
61+
throw new TypeErrorException(sprintf('Response MUST be either an array, a Response or a Reference object, "%s" given', $givenType));
5562
}
5663
} else {
5764
$this->_errors[] = "Responses: $statusCode is not a valid HTTP status code.";

tests/spec/PathTest.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
use cebe\openapi\spec\Operation;
55
use cebe\openapi\spec\PathItem;
66
use 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

Comments
 (0)