1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace erasys\OpenApi\Tests; |
4
|
|
|
|
5
|
|
|
use erasys\OpenApi\Spec\v3 as OASv3; |
6
|
|
|
use erasys\OpenApi\Validator\DocumentValidator; |
7
|
|
|
use PHPUnit\Framework\TestCase; |
8
|
|
|
|
9
|
|
|
class ValidatorSystemTest extends TestCase |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var DocumentValidator |
13
|
|
|
*/ |
14
|
|
|
private $defaultValidator; |
15
|
|
|
|
16
|
|
|
protected function setUp() |
17
|
|
|
{ |
18
|
|
|
if (!$this->defaultValidator) { |
19
|
|
|
$this->defaultValidator = new DocumentValidator(); |
20
|
|
|
} |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function testValidateSimpleDocumentValid() |
24
|
|
|
{ |
25
|
|
|
$doc = new OASv3\Document( |
26
|
|
|
new OASv3\Info('My API', '1.0.0', 'My API description'), |
27
|
|
|
[ |
28
|
|
|
'/foo/bar' => new OASv3\PathItem( |
29
|
|
|
[ |
30
|
|
|
'get' => new OASv3\Operation( |
31
|
|
|
[ |
32
|
|
|
'200' => new OASv3\Response('Successful response.'), |
33
|
|
|
'default' => new OASv3\Response('Default error response.'), |
34
|
|
|
] |
35
|
|
|
), |
36
|
|
|
] |
37
|
|
|
), |
38
|
|
|
] |
39
|
|
|
); |
40
|
|
|
|
41
|
|
|
$result = $this->defaultValidator->validate($doc); |
42
|
|
|
|
43
|
|
|
$this->assertTrue($result->isValid(), json_encode($result->getErrors())); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function testValidateSimpleDocumentInvalid() |
47
|
|
|
{ |
48
|
|
|
$doc = new OASv3\Document( |
49
|
|
|
new OASv3\Info('My API', '1.0.0', 'My API description'), |
50
|
|
|
[] |
51
|
|
|
); |
52
|
|
|
|
53
|
|
|
$result = $this->defaultValidator->validate($doc); |
54
|
|
|
$json = $doc->toJson(); |
55
|
|
|
|
56
|
|
|
$this->assertFalse($result->isValid(), 'This document should be invalid: ' . $json); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|