Completed
Pull Request — master (#52)
by John
06:05
created

RefResolverTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

6 Methods

Rating   Name   Duplication   Size   Complexity  
A canResolveResourceSchemaReferences() 0 10 1
A canResolveParameterSchemaReferences() 0 14 1
A canResolveReferencesWithSlashed() 0 5 1
A canResolveExternalReferences() 0 8 1
A canUnResolve() 0 9 1
A construct() 0 10 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 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->getDefinition()->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->getDefinition()->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
    public function canResolveReferencesWithSlashed()
55
    {
56
        $resolver = $this->construct('partials/slashes.yml');
57
        $this->assertSame('thevalue', $resolver->resolve()->Foo->bar);
58
    }
59
60
    /**
61
     * @test
62
     *
63
     */
64
    public function canResolveExternalReferences()
65
    {
66
        $resolver = $this->construct('composite.yml');
67
        $document = $resolver->resolve();
68
        $this->assertObjectHasAttribute('schema', $document->responses->Created);
69
        $response = $document->paths->{'/pet'}->post->responses->{'500'};
70
        $this->assertObjectHasAttribute('description', $response);
71
    }
72
73
    /**
74
     * @test
75
     */
76
    public function canUnResolve()
77
    {
78
        $resolver = $this->construct('composite.yml');
79
        $expected = clone $resolver->getDefinition();
80
        $resolver->resolve();
81
        $document = $resolver->unresolve();
82
        $this->assertObjectNotHasAttribute('schema', $document->responses->Created);
83
        $this->assertEquals($expected, $document);
84
    }
85
86
    /**
87
     * @param string $path
88
     *
89
     * @return RefResolver
90
     */
91
    private function construct($path)
92
    {
93
        $filePath = "src/Tests/Functional/PetStore/app/swagger/$path";
94
        $contents = file_get_contents($filePath);
95
        $parser = new YamlParser();
96
        $object = $parser->parse($contents);
97
        $resolver = new RefResolver($object, $filePath);
0 ignored issues
show
Bug introduced by
It seems like $object defined by $parser->parse($contents) on line 96 can be null; however, KleijnWeb\SwaggerBundle\...Resolver::__construct() does not accept null, maybe add an additional type check?

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:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
98
99
        return $resolver;
100
    }
101
}
102