Completed
Push — master ( 6ebd1c...df9bae )
by John
10:14
created

SwaggerDocumentTest::canGetPathDefinitions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 11
Ratio 100 %
Metric Value
dl 11
loc 11
rs 9.4285
cc 1
eloc 7
nc 1
nop 0
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\Dev\Document;
10
11
use KleijnWeb\SwaggerBundle\Document\SwaggerDocument;
12
use org\bovigo\vfs\vfsStream;
13
use org\bovigo\vfs\vfsStreamDirectory;
14
use org\bovigo\vfs\vfsStreamWrapper;
15
16
/**
17
 * @author John Kleijn <[email protected]>
18
 */
19
class SwaggerDocumentTest extends \PHPUnit_Framework_TestCase
20
{
21
    /**
22
     * @test
23
     * @expectedException \InvalidArgumentException
24
     */
25
    public function willFailWhenPathDoesNotExist()
26
    {
27
        new SwaggerDocument('/this/is/total/bogus');
28
    }
29
30
    /**
31
     * @test
32
     */
33
    public function willLoadDefinitionIntoArrayObject()
34
    {
35
        $this->assertInstanceOf('ArrayObject', self::getPetStoreDocument()->getDefinition());
36
    }
37
38
    /**
39
     * @test
40
     */
41 View Code Duplication
    public function canGetPathDefinitions()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
42
    {
43
        $actual = self::getPetStoreDocument()->getPathDefinitions();
44
        $this->assertInternalType('array', $actual);
45
46
        // Check a few keys
47
        $this->assertArrayHasKey('/pet', $actual);
48
        $this->assertArrayHasKey('/pet/findByStatus', $actual);
49
        $this->assertArrayHasKey('/store/inventory', $actual);
50
        $this->assertArrayHasKey('/user', $actual);
51
    }
52
53
    /**
54
     * @test
55
     */
56 View Code Duplication
    public function getOperationDefinition()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
57
    {
58
        $actual = self::getPetStoreDocument()->getOperationDefinition('/store/inventory', 'get');
59
        $this->assertInternalType('array', $actual);
60
61
        // Check a few keys
62
        $this->assertArrayHasKey('parameters', $actual);
63
        $this->assertArrayHasKey('responses', $actual);
64
        $this->assertArrayHasKey('security', $actual);
65
    }
66
67
    /**
68
     * @test
69
     */
70
    public function getOperationDefinitionHttpMethodIsCaseInsensitive()
71
    {
72
        self::getPetStoreDocument()->getOperationDefinition('/store/inventory', 'GET');
73
    }
74
75
76
    /**
77
     * @expectedException \InvalidArgumentException
78
     * @test
79
     */
80
    public function getOperationDefinitionWillFailOnUnknownHttpMethod()
81
    {
82
        self::getPetStoreDocument()->getOperationDefinition('/store/inventory', 'post');
83
    }
84
85
    /**
86
     * @expectedException \InvalidArgumentException
87
     * @test
88
     */
89
    public function getOperationDefinitionWillFailOnUnknownPath()
90
    {
91
        self::getPetStoreDocument()->getOperationDefinition('/this/is/total/bogus', 'post');
92
    }
93
94
    /**
95
     * @test
96
     */
97
    public function canWriteValidYamlToFileSystem()
98
    {
99
        $originalHash = md5_file('src/Tests/Functional/PetStore/app/swagger/petstore.yml');
100
101
        $document = self::getPetStoreDocument();
102
        $document->write();
103
104
        $newHash = md5_file('src/Tests/Functional/PetStore/app/swagger/petstore.yml');
105
106
        $this->assertSame($originalHash, $newHash);
107
    }
108
109
    /**
110
     * @test
111
     */
112
    public function gettingArrayCopyWillLeaveEmptyArraysAsEmptyArrays()
113
    {
114
        $document = self::getPetStoreDocument();
115
        $data = $document->getArrayCopy();
116
117
        $emptyParameters = $data['paths']['/store/inventory']['get']['parameters'];
118
        $emptyAuthSpec = $data['paths']['/store/inventory']['get']['security'][0]['api_key'];
119
120
        $this->assertSame([], $emptyParameters);
121
        $this->assertSame([], $emptyAuthSpec);
122
    }
123
124
    /**
125
     * @test
126
     */
127 View Code Duplication
    public function canWriteModifiedYamlToFileSystem()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
128
    {
129
        $originalHash = md5_file('src/Tests/Functional/PetStore/app/swagger/petstore.yml');
130
        vfsStreamWrapper::register();
131
        vfsStreamWrapper::setRoot(new vfsStreamDirectory('canWriteModifiedYamlToFileSystem'));
132
133
        $modifiedPath = vfsStream::url('canWriteModifiedYamlToFileSystem/modified.yml');
134
135
        $document = self::getPetStoreDocument();
136
        $definition = $document->getDefinition();
137
        $definition->version = '0.0.2';
0 ignored issues
show
Bug introduced by
The property version does not seem to exist in ArrayObject.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
138
        $document->write($modifiedPath);
139
140
        $newHash = md5_file($modifiedPath);
141
142
        $this->assertNotSame($originalHash, $newHash);
143
    }
144
145
    /**
146
     * @test
147
     */
148 View Code Duplication
    public function canModifiedYamlWrittenToFileSystemHandlesEmptyArraysCorrectly()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
149
    {
150
        vfsStreamWrapper::register();
151
        vfsStreamWrapper::setRoot(
152
            new vfsStreamDirectory('canModifiedYamlWrittenToFileSystemHandlesEmptyArraysCorrectly')
153
        );
154
155
        $modifiedPath = vfsStream::url('canModifiedYamlWrittenToFileSystemHandlesEmptyArraysCorrectly/modified.yml');
156
157
        $document = self::getPetStoreDocument();
158
        $definition = $document->getDefinition();
159
        $definition->version = '0.0.2';
0 ignored issues
show
Bug introduced by
The property version does not seem to exist in ArrayObject.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
160
        $document->write($modifiedPath);
161
162
        $content = file_get_contents($modifiedPath);
163
        $this->assertNotRegExp('/\: \{  \}/', $content);
164
    }
165
166
    /**
167
     * @test
168
     */
169 View Code Duplication
    public function canResolveResourceSchemaReferences()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
170
    {
171
        $document = self::getPetStoreDocument();
172
        $schemas = $document->getResourceSchemas();
173
        $propertySchema = $schemas['Pet']['properties']['category'];
174
        $this->assertArrayNotHasKey('$ref', $propertySchema);
175
        $this->assertArrayHasKey('id', $propertySchema);
176
        $this->assertSame('object', $propertySchema['type']);
177
    }
178
179
    /**
180
     * @test
181
     */
182 View Code Duplication
    public function canResolveParameterSchemaReferences()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
183
    {
184
        $document = new SwaggerDocument('src/Tests/Functional/PetStore/app/swagger/instagram.yml');
185
        $pathDefinitions = $document->getPathDefinitions();
186
        $argumentPseudoSchema = $pathDefinitions['/users/{user-id}']['parameters'][0];
187
        $this->assertArrayNotHasKey('$ref', $argumentPseudoSchema);
188
        $this->assertArrayHasKey('in', $argumentPseudoSchema);
189
        $this->assertSame('user-id', $argumentPseudoSchema['name']);
190
    }
191
192
    /**
193
     * @return SwaggerDocument
194
     */
195
    public static function getPetStoreDocument()
196
    {
197
        return new SwaggerDocument('src/Tests/Functional/PetStore/app/swagger/petstore.yml');
198
    }
199
}
200