1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the KleijnWeb\SwaggerBundle package. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace KleijnWeb\SwaggerBundle\Tests\Document; |
10
|
|
|
|
11
|
|
|
use KleijnWeb\SwaggerBundle\Document\RefResolver; |
12
|
|
|
use KleijnWeb\SwaggerBundle\Document\YamlParser; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @author John Kleijn <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
class RefResolverTest extends \PHPUnit_Framework_TestCase |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @test |
21
|
|
|
*/ |
22
|
|
|
public function canResolveResourceSchemaReferences() |
23
|
|
|
{ |
24
|
|
|
$resolver = $this->construct('petstore.yml'); |
25
|
|
|
$resolver->resolve(); |
26
|
|
|
$schemas = $resolver->getDocument()->definitions; |
27
|
|
|
$propertySchema = $schemas->Pet->properties->category; |
28
|
|
|
$this->assertObjectNotHasAttribute('$ref', $propertySchema); |
29
|
|
|
$this->assertObjectHasAttribute('id', $propertySchema); |
30
|
|
|
$this->assertSame('object', $propertySchema->type); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @test |
35
|
|
|
*/ |
36
|
|
|
public function canResolveParameterSchemaReferences() |
37
|
|
|
{ |
38
|
|
|
$resolver = $this->construct('instagram.yml'); |
39
|
|
|
$pathDefinitions = $resolver->getDocument()->paths; |
40
|
|
|
$pathDefinition = $pathDefinitions->{'/users/{user-id}'}; |
41
|
|
|
$this->assertInternalType('array', $pathDefinition->parameters); |
42
|
|
|
$pathDefinition = $pathDefinitions->{'/users/{user-id}'}; |
43
|
|
|
$resolver->resolve(); |
44
|
|
|
$this->assertInternalType('array', $pathDefinition->parameters); |
45
|
|
|
$argumentPseudoSchema = $pathDefinition->parameters[0]; |
46
|
|
|
$this->assertObjectNotHasAttribute('$ref', $argumentPseudoSchema); |
47
|
|
|
$this->assertObjectHasAttribute('in', $argumentPseudoSchema); |
48
|
|
|
$this->assertSame('user-id', $argumentPseudoSchema->name); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @test |
53
|
|
|
* |
54
|
|
|
*/ |
55
|
|
|
public function canResolveExternalReferences() |
56
|
|
|
{ |
57
|
|
|
$resolver = $this->construct('composite.yml'); |
58
|
|
|
$resolver->resolve(); |
59
|
|
|
$document = $resolver->getDocument(); |
60
|
|
|
$schema = $document->responses->Created->schema; |
61
|
|
|
$this->assertObjectHasAttribute('type', $schema); |
62
|
|
|
$response = $document->paths->{'/pet'}->post->responses->{'500'}; |
63
|
|
|
$this->assertObjectHasAttribute('description', $response); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param string $path |
68
|
|
|
* |
69
|
|
|
* @return RefResolver |
70
|
|
|
*/ |
71
|
|
|
private function construct($path) |
72
|
|
|
{ |
73
|
|
|
$filePath = "src/Tests/Functional/PetStore/app/swagger/$path"; |
74
|
|
|
$contents = file_get_contents($filePath); |
75
|
|
|
$parser = new YamlParser(); |
76
|
|
|
$object = $parser->parse($contents); |
77
|
|
|
$resolver = new RefResolver($object, $filePath); |
|
|
|
|
78
|
|
|
|
79
|
|
|
return $resolver; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: