Skip to content

Commit 5b00c4e

Browse files
committed
Add test
1 parent ca11c05 commit 5b00c4e

File tree

3 files changed

+35
-8
lines changed

3 files changed

+35
-8
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,4 +332,3 @@ Professional support, consulting as well as software development services are av
332332
https://www.cebe.cc/en/contact
333333

334334
Development of this library is sponsored by [cebe.:cloud: "Your Professional Deployment Platform"](https://cebe.cloud).
335-

src/SpecBaseObject.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,15 @@
1313
use cebe\openapi\json\JsonReference;
1414
use cebe\openapi\spec\Reference;
1515
use cebe\openapi\spec\Type;
16+
use \JsonSerializable;
1617

1718
/**
1819
* Base class for all spec objects.
1920
*
2021
* Implements property management and validation basics.
2122
*
2223
*/
23-
abstract class SpecBaseObject implements SpecObjectInterface, DocumentContextInterface, RawSpecDataInterface
24+
abstract class SpecBaseObject implements SpecObjectInterface, DocumentContextInterface, JsonSerializable, RawSpecDataInterface
2425
{
2526
private $_rawSpec;
2627
private $_properties = [];
@@ -528,6 +529,11 @@ public function getExtensions(): array
528529
return $extensions;
529530
}
530531

532+
public function jsonSerialize()
533+
{
534+
return $this->getSerializableData();
535+
}
536+
531537
public function getRawSpecData(): array
532538
{
533539
return $this->_rawSpec;

tests/spec/OpenApiTest.php

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public function testReadPetStore()
8383

8484
public function assertAllInstanceOf($className, $array)
8585
{
86-
foreach($array as $k => $v) {
86+
foreach ($array as $k => $v) {
8787
$this->assertInstanceOf($className, $v, "Asserting that item with key '$k' is instance of $className");
8888
}
8989
}
@@ -147,7 +147,7 @@ public function specProvider()
147147
/** @var $it RecursiveDirectoryIterator|RecursiveIteratorIterator */
148148
$it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator(__DIR__ . '/../../vendor/apis-guru/openapi-directory/APIs'));
149149
$it->rewind();
150-
while($it->valid()) {
150+
while ($it->valid()) {
151151
if ($it->getBasename() === 'openapi.yaml') {
152152
$apisGuruExamples[] = $it->key();
153153
}
@@ -159,10 +159,10 @@ public function specProvider()
159159
/** @var $it RecursiveDirectoryIterator|RecursiveIteratorIterator */
160160
$it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator(__DIR__ . '/../../vendor/nexmo/api-specification/definitions'));
161161
$it->rewind();
162-
while($it->valid()) {
162+
while ($it->valid()) {
163163
if ($it->getExtension() === 'yml'
164-
&& strpos($it->getSubPath(), 'common') === false
165-
&& $it->getBasename() !== 'voice.v2.yml' // contains invalid references
164+
&& strpos($it->getSubPath(), 'common') === false
165+
&& $it->getBasename() !== 'voice.v2.yml' // contains invalid references
166166
) {
167167
$nexmoExamples[] = $it->key();
168168
}
@@ -175,7 +175,7 @@ public function specProvider()
175175
$apisGuruExamples,
176176
$nexmoExamples
177177
);
178-
foreach($all as $path) {
178+
foreach ($all as $path) {
179179
yield [
180180
substr($path, strlen(__DIR__ . '/../../vendor/')),
181181
basename(dirname($path, 2)) . DIRECTORY_SEPARATOR . basename(dirname($path, 1)) . DIRECTORY_SEPARATOR . basename($path)
@@ -233,4 +233,26 @@ public function testSpecs($openApiFile)
233233
}
234234

235235
}
236+
237+
public function testJsonSerialize()
238+
{
239+
$spec = <<<YML
240+
openapi: 3.0.0
241+
info:
242+
title: Minimal API
243+
version: 1.0.0
244+
paths:
245+
/:
246+
get:
247+
summary: Retrieves a minimal response
248+
responses:
249+
'200':
250+
description: OK
251+
252+
YML;
253+
$yaml = Yaml::parse($spec);
254+
$openapi = new OpenApi($yaml);
255+
256+
$this->assertSame(json_encode($openapi), '{"openapi":"3.0.0","info":{"title":"Minimal API","version":"1.0.0"},"paths":{"\/":{"get":{"summary":"Retrieves a minimal response","responses":{"200":{"description":"OK"}}}}}}');
257+
}
236258
}

0 commit comments

Comments
 (0)