Completed
Pull Request — develop (#273)
by Adrian
18:46 queued 08:49
created

SwaggerStrategyTest::testSupported()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
/**
3
 * SwaggerStrategyTest
4
 */
5
6
namespace Graviton\ProxyBundle\Tests\Definition\Loader\DispersalStrategy;
7
8
use Graviton\ProxyBundle\Definition\ApiDefinition;
9
use Graviton\ProxyBundle\Definition\Loader\DispersalStrategy\SwaggerStrategy;
10
11
/**
12
 * tests for the SwaggerStrategy class
13
 *
14
 * @author  List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
15
 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
16
 * @link    http://swisscom.ch
17
 */
18
class SwaggerStrategyTest extends \PHPUnit_Framework_TestCase
19
{
20
    /**
21
     * @var SwaggerStrategy
22
     */
23
    private $sut;
24
25
    /**
26
     * @var /stdClass
27
     */
28
    private $swagger;
29
30
    /**
31
     * @inheritDoc
32
     *
33
     * @return void
34
     */
35
    protected function setUp()
36
    {
37
        /*$swaggerParserMock = $this
38
            ->getMockBuilder('Swagger\Document')
39
            ->disableOriginalConstructor()
40
            ->getMock();*/
41
42
43
        /*$this->swagger = new \stdClass();
44
        $this->swagger->swagger = "2.0";
45
        $this->swagger->paths = new \stdClass();
46
        $this->swagger->definitions = new \stdClass();
47
        $this->swagger->info = new \stdClass();
48
        $this->swagger->info->title = "test swagger";
49
        $this->swagger->info->version = "1.0.0";
50
        $this->swagger->basePath = "/api/prefix";
51
        $this->swagger->host = "testapi.local";*/
52
    }
53
54
    /**
55
     * Provides data sets for testSupported()
56
     *
57
     * @return array
58
     */
59
    public function swaggerJsonDataProvider()
60
    {
61
        $basePath = dirname(__FILE__).'/../../../resources/';
62
63
        return array(
64
            array(false, file_get_contents($basePath.'not-supported-swagger.json')),
65
            array(true, file_get_contents($basePath.'simple-swagger.json')),
66
        );
67
    }
68
69
    /**
70
     * test the supports method
71
     *
72
     * @param mixed $result  result
73
     * @param mixed $swagger swagger
74
     *
75
     * @dataProvider swaggerJsonDataProvider
76
     *
77
     * @return void
78
     */
79
    public function testSupported($result, $swagger)
80
    {
81
        $this->assertEquals($result, $this->sut->supports($swagger));
82
    }
83
84
    /**
85
     * test missing fallback data
86
     *
87
     * @expectedException        RuntimeException
88
     * @expectedExceptionMessage Missing mandatory key (host) in fallback data set.
89
     *
90
     * @return void
91
     */
92
    public function testMissingFallbackData()
93
    {
94
        $this->sut->process('{}', array());
95
    }
96
97
    /**
98
     * processing swagger
99
     *
100
     * @return void
101
     */
102
    public function testProcessSwagger()
103
    {
104
        $content = file_get_contents(dirname(__FILE__).'/../../../resources/simple-swagger.json');
105
        $swagger = json_decode($content);
106
        $orderPath = '/order/{orderId}';
107
108
        $swaggerParserMock = $this->getMockBuilder('\Swagger\Document')
109
            ->disableOriginalConstructor()
110
            ->setMethods(['getBasePath', 'setDocument', 'getOperationsById'])
111
            ->getMock();
112
113
        $response = $swagger->paths->$orderPath->get->responses;
0 ignored issues
show
Unused Code introduced by
$response is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
114
115
        $responseMock = $this->getMockBuilder('\Swagger\Object\Responses')
116
            ->disableOriginalConstructor()
117
            ->setMethods(['getHttpStatusCode'])
118
            ->getMock();
119
        $responseMock->expects($this->once())
120
            ->method('getHttpStatusCode');
121
122
123
124
        $operationMock = $this->getMockBuilder('\Swagger\Object\Operation')
125
            ->disableOriginalConstructor()
126
            ->setMethods(['getDocumentObjectProperty', 'getResponses'])
127
            ->getMock();
128
        $operationMock->expects($this->once())
129
            ->method('getDocumentObjectProperty');
130
        $operationMock->expects($this->once())
131
            ->method('getResponses')
132
            ->willReturn($responseMock);
133
134
135
        $serviceMock = $this->getMockBuilder('\Swagger\OperationReference')
136
            ->disableOriginalConstructor()
137
            ->setMethods(['getPath', 'getMethod', 'getOperation'])
138
            ->getMock();
139
        $serviceMock->expects($this->exactly(3))
140
            ->method('getPath')
141
            ->will($this->onConsecutiveCalls('/user', '/user', '/order/{orderId}'));
142
        $serviceMock->expects($this->exactly(5))
143
            ->method('getMethod')
144
            ->will($this->onConsecutiveCalls('POST', 'DELETE', 'GET', 'POST', 'GET'));
145
        $serviceMock->expects($this->exactly(2))
146
            ->method('getOperation')
147
            ->willReturn($operationMock);
148
149
150
        $operations = [$serviceMock, $serviceMock, $serviceMock];
151
152
        $swaggerParserMock
153
            ->expects($this->once())
154
            ->method('getOperationsById')
155
            ->willReturn($operations);
156
        $this->sut = new SwaggerStrategy($swaggerParserMock);
157
158
        $this->sut->process($content, array('host' => 'localhost'));
159
        /*$this->callProcessMethod(0);
160
161
        $schema = array();
162
        $schema['$ref'] = '#/definitions/Person';
163
164
        $customer = array();
165
        $otherParam = array('in' => 'blub');
166
        $customer['post']['parameters'][] = $otherParam;
167
        $customer['get']['responses']['200']['schema'] = $schema;
168
        $customerPath = '/person/customer';
169
        $this->swagger->paths->$customerPath = (object) $customer;
170
171
        $bodyParam = array('in' => 'body', 'schema' => $schema);
172
        $consultant = array();
173
        $consultant['get']['responses']['400'] = new \stdClass();
174
        $consultant['post']['parameters'][] = $bodyParam;
175
        $consultantPath = '/person/consultant';
176
        $consultantPathWithId = '/person/consultant/{id}';
177
        $this->swagger->paths->$consultantPath = (object) $consultant;
178
        $this->swagger->paths->$consultantPathWithId = (object) $consultant;
179
180
        $person = new \stdClass();
181
        $person->type = "object";
182
        $person->properties = new \stdClass();
183
        $person->properties->id = new \stdClass();
184
        $person->properties->name = new \stdClass();
185
        $this->swagger->definitions->Person = $person;
186
187
        $apiDefinition = $this->callProcessMethod(2);
188
        foreach ($apiDefinition->getEndpoints(false) as $endpoint) {
189
            $this->assertEquals($person, $apiDefinition->getSchema($endpoint));
190
        }*/
191
    }
192
193
    /**
194
     * test a delete endpoint
195
     *
196
     * @return void
197
     */
198
    public function testProcessDeleteEndpoint()
199
    {
200
        $deleteEndpoint = array();
201
        $deleteEndpoint['delete'] = new \stdClass();
202
        $path = '/delete/endpoint';
203
        $this->swagger->paths->$path = (object) $deleteEndpoint;
204
        $this->callProcessMethod(1);
205
    }
206
207
    /**
208
     * test endpoint with no schema
209
     *
210
     * @return void
211
     */
212
    public function testProcessNoSchema()
213
    {
214
        $emptyEndpoint = array();
215
        $emptyEndpoint['get']['responses']['200']['schema'] = null;
216
        $path = '/no/schema/endpoint';
217
        $this->swagger->paths->$path = (object) $emptyEndpoint;
218
        $this->callProcessMethod(1);
219
    }
220
221
    /**
222
     * test process method
223
     *
224
     * @param int $count number of endpoints
225
     *
226
     * @return ApiDefinition
227
     */
228
    private function callProcessMethod($count)
229
    {
230
        $fallbackData = array('host' => 'localhost');
231
        $apiDefinition = $this->sut->process(json_encode($this->swagger), $fallbackData);
232
        $this->assertInstanceOf('Graviton\ProxyBundle\Definition\ApiDefinition', $apiDefinition);
233
        $this->assertCount($count, $apiDefinition->getEndpoints());
234
235
        return $apiDefinition;
236
    }
237
}
238