Completed
Push — master ( 386146...a2d301 )
by John
07:40
created

RefResolverTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
lcom 1
cbo 3
dl 0
loc 112
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A canParseNumericMap() 0 15 1
A willParseArrayAsArrayAndObjectAsObject() 0 17 1
A canResolveResourceSchemaReferences() 0 10 1
A canResolveParameterSchemaReferences() 0 14 1
A canResolveExternalReferences() 0 10 1
A construct() 0 9 1
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 Symfony\Component\Yaml\Yaml;
13
14
/**
15
 * @author John Kleijn <[email protected]>
16
 */
17
class RefResolverTest extends \PHPUnit_Framework_TestCase
18
{
19
    /**
20
     * Check Symfony\Yaml bug
21
     *
22
     * @see https://github.com/symfony/symfony/issues/17709
23
     *
24
     * @test
25
     */
26
    public function canParseNumericMap()
27
    {
28
        $yaml = <<<YAML
29
map:
30
  1: one
31
  2: two
32
YAML;
33
        $actual = Yaml::parse($yaml, true, false, true);
34
        $this->assertInternalType('object', $actual);
35
        $this->assertInternalType('object', $actual->map);
36
        $this->assertTrue(property_exists($actual->map, '1'));
37
        $this->assertTrue(property_exists($actual->map, '2'));
38
        $this->assertSame('one', $actual->map->{'1'});
39
        $this->assertSame('two', $actual->map->{'2'});
40
    }
41
42
    /**
43
     * Check Symfony\Yaml bug
44
     *
45
     * @see https://github.com/symfony/symfony/pull/17711
46
     *
47
     * @test
48
     */
49
    public function willParseArrayAsArrayAndObjectAsObject()
50
    {
51
        $yaml = <<<YAML
52
array:
53
  - key: one
54
  - key: two
55
YAML;
56
57
        $actual = Yaml::parse($yaml, true, false, true);
58
        $this->assertInternalType('object', $actual);
59
60
        $this->assertInternalType('array', $actual->array);
61
        $this->assertInternalType('object', $actual->array[0]);
62
        $this->assertInternalType('object', $actual->array[1]);
63
        $this->assertSame('one', $actual->array[0]->key);
64
        $this->assertSame('two', $actual->array[1]->key);
65
    }
66
67
    /**
68
     * @test
69
     */
70
    public function canResolveResourceSchemaReferences()
71
    {
72
        $resolver = $this->construct('petstore.yml');
73
        $resolver->resolve();
74
        $schemas = $resolver->getDocument()->definitions;
75
        $propertySchema = $schemas->Pet->properties->category;
76
        $this->assertObjectNotHasAttribute('$ref', $propertySchema);
77
        $this->assertObjectHasAttribute('id', $propertySchema);
78
        $this->assertSame('object', $propertySchema->type);
79
    }
80
81
    /**
82
     * @test
83
     */
84
    public function canResolveParameterSchemaReferences()
85
    {
86
        $resolver = $this->construct('instagram.yml');
87
        $pathDefinitions = $resolver->getDocument()->paths;
88
        $pathDefinition = $pathDefinitions->{'/users/{user-id}'};
89
        $this->assertInternalType('array', $pathDefinition->parameters);
90
        $pathDefinition = $pathDefinitions->{'/users/{user-id}'};
91
        $resolver->resolve();
92
        $this->assertInternalType('array', $pathDefinition->parameters);
93
        $argumentPseudoSchema = $pathDefinition->parameters[0];
94
        $this->assertObjectNotHasAttribute('$ref', $argumentPseudoSchema);
95
        $this->assertObjectHasAttribute('in', $argumentPseudoSchema);
96
        $this->assertSame('user-id', $argumentPseudoSchema->name);
97
    }
98
99
    /**
100
     * @test
101
     *
102
     */
103
    public function canResolveExternalReferences()
104
    {
105
        $resolver = $this->construct('composite.yml');
106
        $resolver->resolve();
107
        $document = $resolver->getDocument();
108
        $schema = $document->responses->Created->schema;
109
        $this->assertObjectHasAttribute('type', $schema);
110
        $response = $document->paths->{'/pet'}->post->responses->{'500'};
111
        $this->assertObjectHasAttribute('description', $response);
112
    }
113
114
    /**
115
     * @param string $path
116
     *
117
     * @return RefResolver
118
     */
119
    private function construct($path)
120
    {
121
        $filePath = "src/Tests/Functional/PetStore/app/swagger/$path";
122
        $contents = file_get_contents($filePath);
123
        $object = Yaml::parse($contents, true, false, true);
124
        $resolver = new RefResolver($object, $filePath);
125
126
        return $resolver;
127
    }
128
}
129