Completed
Push — feature/service-wrapper-for-sw... ( faf928...5dac9b )
by Samuel
13:45 queued 01:09
created

SwaggerStrategyTest::testProcessDeleteEndpoint()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 8
rs 9.4286
cc 1
eloc 6
nc 1
nop 0
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
     * @dataProvider swaggerJsonDataProvider
73
     *
74
     * @return void
75
     */
76
    public function testSupported($result, $swagger)
77
    {
78
        $this->assertEquals($result, $this->sut->supports($swagger));
79
    }
80
81
    /**
82
     * test missing fallback data
83
     *
84
     * @expectedException        RuntimeException
85
     * @expectedExceptionMessage Missing mandatory key (host) in fallback data set.
86
     *
87
     * @return void
88
     */
89
    public function testMissingFallbackData()
90
    {
91
        $this->sut->process('{}', array());
92
    }
93
94
    /**
95
     * processing swagger
96
     *
97
     * @return void
98
     */
99
    public function testProcessSwagger()
100
    {
101
        $content = file_get_contents(dirname(__FILE__).'/../../../resources/simple-swagger.json');
102
        $swagger = json_decode($content);
103
        $orderPath = '/order/{orderId}';
104
105
        $swaggerParserMock = $this->getMockBuilder('\Swagger\Document')
106
            ->disableOriginalConstructor()
107
            ->setMethods(['getBasePath', 'setDocument', 'getOperationsById'])
108
            ->getMock();
109
110
        $response = $swagger->paths->$orderPath->get->responses;
111
112
        $responseMock = $this->getMockBuilder('\Swagger\Object\Responses')
113
            ->disableOriginalConstructor()
114
            ->setMethods(['getHttpStatusCode'])
115
            ->getMock();
116
        $responseMock->expects($this->once())
117
            ->method('getHttpStatusCode')
118
119
120
121
        $operationMock = $this->getMockBuilder('\Swagger\Object\Operation')
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

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