Skip to content

Commit d417c28

Browse files
committed
test MediaType, Example, Info. Implement Example
1 parent ca42d7a commit d417c28

7 files changed

Lines changed: 226 additions & 4 deletions

File tree

Makefile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,12 @@ install:
1515
test:
1616
vendor/bin/phpunit
1717

18+
# find spec classes that are not mentioned in tests with @covers yet
19+
coverage:
20+
grep -rhPo '@covers .+' tests |cut -c 28- |sort > /tmp/php-openapi-covA
21+
grep -rhPo 'class \w+' src/spec/ | awk '{print $$2}' |grep -v '^Type$$' | sort > /tmp/php-openapi-covB
22+
diff /tmp/php-openapi-covB /tmp/php-openapi-covA
23+
24+
1825
.PHONY: all check-style fix-style install test
1926

src/SpecBaseObject.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,14 +189,14 @@ protected function requireProperties(array $names)
189189
protected function validateEmail(string $property)
190190
{
191191
if (!empty($this->$property) && strpos($this->$property, '@') === false) {
192-
$this->addError(__CLASS__ . '::$'.$property.' does not seem to be a valid email address: ' . $this->$property);
192+
$this->addError(get_class($this) . '::$'.$property.' does not seem to be a valid email address: ' . $this->$property);
193193
}
194194
}
195195

196196
protected function validateUrl(string $property)
197197
{
198198
if (!empty($this->$property) && strpos($this->$property, '//') === false) {
199-
$this->addError(__CLASS__ . '::$'.$property.' does not seem to be a valid URL: ' . $this->$property);
199+
$this->addError(get_class($this) . '::$'.$property.' does not seem to be a valid URL: ' . $this->$property);
200200
}
201201
}
202202

src/spec/Example.php

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,30 @@
1414
*
1515
* @link https://github.com/OAI/OpenAPI-Specification/blob/3.0.2/versions/3.0.2.md#exampleObject
1616
*
17+
* @property-read string $summary
18+
* @property-read string $description
19+
* @property-read mixed $value
20+
* @property-read string $externalValue
1721
*/
18-
class Example
22+
class Example extends SpecBaseObject
1923
{
20-
// TODO implement
24+
/**
25+
* @return array array of attributes available in this object.
26+
*/
27+
protected function attributes(): array
28+
{
29+
return [
30+
'summary' => Type::STRING,
31+
'description' => Type::STRING,
32+
'value' => Type::ANY,
33+
'externalValue' => Type::STRING,
34+
];
35+
}
36+
37+
/**
38+
* Perform validation on this object, check data against OpenAPI Specification rules.
39+
*/
40+
protected function performValidation()
41+
{
42+
}
2143
}

src/spec/MediaType.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
/**
1313
* Each Media Type Object provides schema and examples for the media type identified by its key.
1414
*
15+
* @link https://github.com/OAI/OpenAPI-Specification/blob/3.0.2/versions/3.0.2.md#mediaTypeObject
16+
*
1517
* @property-read Schema|Reference|null $schema
1618
* @property-read mixed $example
1719
* @property-read Example[]|Reference[] $examples

src/spec/Paths.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ class Paths implements SpecObjectInterface
2727

2828
private $_errors = [];
2929

30+
31+
/**
32+
* Create an object from spec data.
33+
* @param array $data spec data read from YAML or JSON
34+
*/
3035
public function __construct(array $data)
3136
{
3237
foreach ($data as $path => $object) {

tests/spec/InfoTest.php

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
<?php
2+
3+
use cebe\openapi\Reader;
4+
use cebe\openapi\spec\Contact;
5+
use cebe\openapi\spec\Info;
6+
use cebe\openapi\spec\License;
7+
8+
/**
9+
* @covers \cebe\openapi\spec\Info
10+
* @covers \cebe\openapi\spec\Contact
11+
* @covers \cebe\openapi\spec\License
12+
*/
13+
class InfoTest extends \PHPUnit\Framework\TestCase
14+
{
15+
public function testRead()
16+
{
17+
/** @var $info Info */
18+
$info = Reader::readFromYaml(<<<'YAML'
19+
title: Sample Pet Store App
20+
description: This is a sample server for a pet store.
21+
termsOfService: http://example.com/terms/
22+
contact:
23+
name: API Support
24+
url: http://www.example.com/support
25+
email: support@example.com
26+
license:
27+
name: Apache 2.0
28+
url: https://www.apache.org/licenses/LICENSE-2.0.html
29+
version: 1.0.1
30+
YAML
31+
, Info::class);
32+
33+
$result = $info->validate();
34+
$this->assertEquals([], $info->getErrors());
35+
$this->assertTrue($result);
36+
37+
$this->assertEquals('Sample Pet Store App', $info->title);
38+
$this->assertEquals('This is a sample server for a pet store.', $info->description);
39+
$this->assertEquals('http://example.com/terms/', $info->termsOfService);
40+
$this->assertEquals('1.0.1', $info->version);
41+
42+
$this->assertInstanceOf(Contact::class, $info->contact);
43+
$this->assertEquals('API Support', $info->contact->name);
44+
$this->assertEquals('http://www.example.com/support', $info->contact->url);
45+
$this->assertEquals('support@example.com', $info->contact->email);
46+
$this->assertInstanceOf(License::class, $info->license);
47+
$this->assertEquals('Apache 2.0', $info->license->name);
48+
$this->assertEquals('https://www.apache.org/licenses/LICENSE-2.0.html', $info->license->url);
49+
}
50+
51+
public function testReadInvalid()
52+
{
53+
/** @var $info Info */
54+
$info = Reader::readFromYaml(<<<'YAML'
55+
description: This is a sample server for a pet store.
56+
termsOfService: http://example.com/terms/
57+
contact:
58+
name: API Support
59+
url: http://www.example.com/support
60+
email: support@example.com
61+
YAML
62+
, Info::class);
63+
64+
$result = $info->validate();
65+
$this->assertEquals([
66+
'Missing required property: title',
67+
'Missing required property: version',
68+
], $info->getErrors());
69+
$this->assertFalse($result);
70+
71+
}
72+
73+
public function testReadInvalidContact()
74+
{
75+
/** @var $info Info */
76+
$info = Reader::readFromYaml(<<<'YAML'
77+
title: test
78+
version: 1.0
79+
contact:
80+
name: API Support
81+
url: www.example.com/support
82+
email: support.example.com
83+
YAML
84+
, Info::class);
85+
86+
$result = $info->validate();
87+
$this->assertEquals([
88+
'cebe\openapi\spec\Contact::$email does not seem to be a valid email address: support.example.com',
89+
'cebe\openapi\spec\Contact::$url does not seem to be a valid URL: www.example.com/support',
90+
], $info->getErrors());
91+
$this->assertFalse($result);
92+
93+
$this->assertInstanceOf(Contact::class, $info->contact);
94+
$this->assertNull($info->license);
95+
96+
}
97+
98+
public function testReadInvalidLicense()
99+
{
100+
/** @var $info Info */
101+
$info = Reader::readFromYaml(<<<'YAML'
102+
title: test
103+
version: 1.0
104+
license:
105+
url: www.apache.org/licenses/LICENSE-2.0.html
106+
YAML
107+
, Info::class);
108+
109+
$result = $info->validate();
110+
$this->assertEquals([
111+
'Missing required property: name',
112+
'cebe\openapi\spec\License::$url does not seem to be a valid URL: www.apache.org/licenses/LICENSE-2.0.html',
113+
], $info->getErrors());
114+
$this->assertFalse($result);
115+
116+
$this->assertInstanceOf(License::class, $info->license);
117+
$this->assertNull($info->contact);
118+
119+
}
120+
}

tests/spec/MediaTypeTest.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
use cebe\openapi\Reader;
4+
use cebe\openapi\spec\MediaType;
5+
use cebe\openapi\spec\Example;
6+
7+
/**
8+
* @covers \cebe\openapi\spec\MediaType
9+
* @covers \cebe\openapi\spec\Example
10+
*/
11+
class MediaTypeTest extends \PHPUnit\Framework\TestCase
12+
{
13+
public function testRead()
14+
{
15+
/** @var $mediaType MediaType */
16+
$mediaType = Reader::readFromYaml(<<<'YAML'
17+
schema:
18+
$ref: "#/components/schemas/Pet"
19+
examples:
20+
cat:
21+
summary: An example of a cat
22+
value:
23+
name: Fluffy
24+
petType: Cat
25+
color: White
26+
gender: male
27+
breed: Persian
28+
dog:
29+
summary: An example of a dog with a cat's name
30+
value:
31+
name: Puma
32+
petType: Dog
33+
color: Black
34+
gender: Female
35+
breed: Mixed
36+
frog:
37+
$ref: "#/components/examples/frog-example"
38+
YAML
39+
, MediaType::class);
40+
41+
$result = $mediaType->validate();
42+
$this->assertEquals([], $mediaType->getErrors());
43+
$this->assertTrue($result);
44+
45+
//$this->assertEquals('schema', $mediaType->name);// TODO support for reference
46+
$this->assertInternalType('array', $mediaType->examples);
47+
$this->assertCount(3, $mediaType->examples);
48+
$this->assertArrayHasKey('cat', $mediaType->examples);
49+
$this->assertArrayHasKey('dog', $mediaType->examples);
50+
$this->assertArrayHasKey('frog', $mediaType->examples);
51+
$this->assertInstanceOf(Example::class, $mediaType->examples['cat']);
52+
$this->assertInstanceOf(Example::class, $mediaType->examples['dog']);
53+
$this->assertInstanceOf(Example::class, $mediaType->examples['frog']);
54+
55+
$this->assertEquals('An example of a cat', $mediaType->examples['cat']->summary);
56+
$expectedCat = [ // TODO we might actually expect this to be an object of stdClass
57+
'name' => 'Fluffy',
58+
'petType' => 'Cat',
59+
'color' => 'White',
60+
'gender' => 'male',
61+
'breed' => 'Persian',
62+
];
63+
$this->assertEquals($expectedCat, $mediaType->examples['cat']->value);
64+
65+
}
66+
}

0 commit comments

Comments
 (0)