1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace erasys\OpenApi\Tests\Validator; |
4
|
|
|
|
5
|
|
|
use erasys\OpenApi\Spec\v3 as OASv3; |
6
|
|
|
use erasys\OpenApi\Validator\DocumentValidator; |
7
|
|
|
use LogicException; |
8
|
|
|
use PHPUnit\Framework\TestCase; |
9
|
|
|
use stdClass; |
10
|
|
|
|
11
|
|
|
class ValidationSystemTest extends TestCase |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var DocumentValidator |
15
|
|
|
*/ |
16
|
|
|
private $defaultValidator; |
17
|
|
|
|
18
|
|
|
protected function setUp() |
19
|
|
|
{ |
20
|
|
|
if (!$this->defaultValidator) { |
21
|
|
|
$this->defaultValidator = new DocumentValidator(); |
22
|
|
|
} |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function testNonExistingSpecFileCausesLogicException() |
26
|
|
|
{ |
27
|
|
|
$this->expectException(LogicException::class); |
28
|
|
|
$this->expectExceptionMessageRegExp('/The default schema file cannot be found/i'); |
29
|
|
|
|
30
|
|
|
new class extends DocumentValidator |
31
|
|
|
{ |
32
|
|
|
protected $defaultJsonSchemaFile = '/../Spec/v3/schemas/vFooBar.yml'; |
33
|
|
|
}; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function testJsonSchemaReturnsExpectedObject() |
37
|
|
|
{ |
38
|
|
|
$schema = $this->defaultValidator->getJsonSchema(); |
39
|
|
|
$this->assertInstanceOf(stdClass::class, $schema); |
40
|
|
|
$this->assertObjectHasAttribute('type', $schema); |
41
|
|
|
$this->assertEquals('object', $schema->type); |
42
|
|
|
$this->assertObjectHasAttribute('properties', $schema); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function testValidateSimpleDocumentValid() |
46
|
|
|
{ |
47
|
|
|
$doc = new OASv3\Document( |
48
|
|
|
new OASv3\Info('My API', '1.0.0', 'My API description'), |
49
|
|
|
[ |
50
|
|
|
'/foo/bar' => new OASv3\PathItem( |
51
|
|
|
[ |
52
|
|
|
'get' => new OASv3\Operation( |
53
|
|
|
[ |
54
|
|
|
'200' => new OASv3\Response('Successful response.'), |
55
|
|
|
'default' => new OASv3\Response('Default error response.'), |
56
|
|
|
] |
57
|
|
|
), |
58
|
|
|
] |
59
|
|
|
), |
60
|
|
|
] |
61
|
|
|
); |
62
|
|
|
|
63
|
|
|
$result = $this->defaultValidator->validate($doc); |
64
|
|
|
|
65
|
|
|
$this->assertTrue($result->isValid(), json_encode($result->getErrors())); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function testValidateSimpleDocumentInvalid() |
69
|
|
|
{ |
70
|
|
|
$doc = new OASv3\Document( |
71
|
|
|
new OASv3\Info('My API', '1.0.0', 'My API description'), |
72
|
|
|
[] |
73
|
|
|
); |
74
|
|
|
|
75
|
|
|
$result = $this->defaultValidator->validate($doc); |
76
|
|
|
$json = $doc->toJson(); |
77
|
|
|
|
78
|
|
|
$this->assertFalse($result->isValid(), 'This document should be invalid: ' . $json); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|