Completed
Push — master ( 031f19...3fe677 )
by John
02:56
created

willParseArrayAsArrayAndObjectAsObject()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 17
rs 9.4285
cc 1
eloc 11
nc 1
nop 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A RefResolverTest::canResolveExternalReferences() 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->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);
0 ignored issues
show
Bug introduced by
It seems like $object defined by $parser->parse($contents) on line 76 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...
78
79
        return $resolver;
80
    }
81
}
82