Completed
Pull Request — master (#81)
by John
03:37
created

SwaggerSpecificationTest   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 12
c 2
b 0
f 0
lcom 1
cbo 3
dl 0
loc 122
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A canApplyRecursiveCallback() 0 9 1
A canModifyValuesUsingRecursiveCallback() 0 18 3
A canStopRecursiveProcessingByReturningFalse() 0 16 2
A canGetPathDefinitions() 0 11 1
A getOperationDefinition() 0 10 1
A getOperationDefinitionHttpMethodIsCaseInsensitive() 0 4 1
A getOperationDefinitionWillFailOnUnknownHttpMethod() 0 4 1
A getOperationDefinitionWillFailOnUnknownPath() 0 4 1
A getPetStoreDocument() 0 6 1
1
<?php declare(strict_types = 1);
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\DocumentRepository;
12
use KleijnWeb\SwaggerBundle\Document\Specification;
13
14
/**
15
 * @author John Kleijn <[email protected]>
16
 */
17
class SwaggerSpecificationTest extends \PHPUnit_Framework_TestCase
18
{
19
    /**
20
     * @test
21
     */
22
    public function canApplyRecursiveCallback()
23
    {
24
        $keys = [];
25
        self::getPetStoreDocument()->apply(function ($value, $key) use (&$keys) {
26
            $keys[] = $key;
27
        });
28
29
        $this->assertGreaterThan(1000, count($keys));
30
    }
31
32
    /**
33
     * @test
34
     */
35
    public function canModifyValuesUsingRecursiveCallback()
36
    {
37
        $spec = self::getPetStoreDocument();
38
39
        $spec->apply(function (&$value) {
40
            if (is_scalar($value)) {
41
                $value = __CLASS__;
42
            }
43
        });
44
45
        $spec->apply(function ($value) use (&$values) {
46
            if (is_scalar($value)) {
47
                $values[] = $value;
48
            }
49
        });
50
51
        $this->assertCount(1, array_unique($values));
52
    }
53
54
    /**
55
     * @test
56
     */
57
    public function canStopRecursiveProcessingByReturningFalse()
58
    {
59
        $spec = self::getPetStoreDocument();
60
61
        $spec->apply(function ($value, $key) use (&$keys) {
62
63
            if ($key === 'paths') {
64
65
                return false;
66
            }
67
            $keys[] = $key;
68
69
            return true;
70
        });
71
        $this->assertCount(31, $keys);
72
    }
73
74
    /**
75
     * @test
76
     */
77
    public function canGetPathDefinitions()
78
    {
79
        $actual = self::getPetStoreDocument()->getPaths();
80
        $this->assertinstanceOf('\stdClass', $actual);
81
82
        // Check a few attributes
83
        $this->assertObjectHasAttribute('/pet', $actual);
84
        $this->assertObjectHasAttribute('/pet/findByStatus', $actual);
85
        $this->assertObjectHasAttribute('/store/inventory', $actual);
86
        $this->assertObjectHasAttribute('/user', $actual);
87
    }
88
89
    /**
90
     * @test
91
     */
92
    public function getOperationDefinition()
93
    {
94
        $actual = self::getPetStoreDocument()->getOperationDefinition('/store/inventory', 'get');
0 ignored issues
show
Deprecated Code introduced by
The method KleijnWeb\SwaggerBundle\...etOperationDefinition() has been deprecated.

This method has been deprecated.

Loading history...
95
        $this->assertinstanceOf('\stdClass', $actual);
96
97
        // Check a few attributes
98
        $this->assertObjectHasAttribute('parameters', $actual);
99
        $this->assertObjectHasAttribute('responses', $actual);
100
        $this->assertObjectHasAttribute('security', $actual);
101
    }
102
103
    /**
104
     * @test
105
     */
106
    public function getOperationDefinitionHttpMethodIsCaseInsensitive()
107
    {
108
        self::getPetStoreDocument()->getOperationDefinition('/store/inventory', 'GET');
0 ignored issues
show
Deprecated Code introduced by
The method KleijnWeb\SwaggerBundle\...etOperationDefinition() has been deprecated.

This method has been deprecated.

Loading history...
109
    }
110
111
    /**
112
     * @expectedException \InvalidArgumentException
113
     * @test
114
     */
115
    public function getOperationDefinitionWillFailOnUnknownHttpMethod()
116
    {
117
        self::getPetStoreDocument()->getOperationDefinition('/store/inventory', 'post');
0 ignored issues
show
Deprecated Code introduced by
The method KleijnWeb\SwaggerBundle\...etOperationDefinition() has been deprecated.

This method has been deprecated.

Loading history...
118
    }
119
120
    /**
121
     * @expectedException \InvalidArgumentException
122
     * @test
123
     */
124
    public function getOperationDefinitionWillFailOnUnknownPath()
125
    {
126
        self::getPetStoreDocument()->getOperationDefinition('/this/is/total/bogus', 'post');
0 ignored issues
show
Deprecated Code introduced by
The method KleijnWeb\SwaggerBundle\...etOperationDefinition() has been deprecated.

This method has been deprecated.

Loading history...
127
    }
128
129
    /**
130
     * @return Specification
131
     */
132
    public static function getPetStoreDocument()
133
    {
134
        $repository = new DocumentRepository('src/Tests/Functional/PetStore');
135
136
        return $repository->get('app/swagger/petstore.yml');
137
    }
138
}
139