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+ }
0 commit comments