|
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
|
|
|
* @inheritDoc |
|
27
|
|
|
* |
|
28
|
|
|
* @return void |
|
29
|
|
|
*/ |
|
30
|
|
|
protected function setUp() |
|
31
|
|
|
{ |
|
32
|
|
|
$swaggerParserMock = $this->getMockBuilder('\Swagger\Document') |
|
33
|
|
|
->disableOriginalConstructor() |
|
34
|
|
|
->setMethods(['setDocument']) |
|
35
|
|
|
->getMock(); |
|
36
|
|
|
$this->sut = new SwaggerStrategy($swaggerParserMock); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Provides data sets for testSupported() |
|
41
|
|
|
* |
|
42
|
|
|
* @return array |
|
43
|
|
|
*/ |
|
44
|
|
|
public function swaggerJsonDataProvider() |
|
45
|
|
|
{ |
|
46
|
|
|
$basePath = dirname(__FILE__).'/../../../resources/'; |
|
47
|
|
|
|
|
48
|
|
|
return array( |
|
49
|
|
|
array(false, file_get_contents($basePath.'not-supported-swagger.json')), |
|
50
|
|
|
array(true, file_get_contents($basePath.'simple-swagger.json')), |
|
51
|
|
|
); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* test the supports method |
|
56
|
|
|
* |
|
57
|
|
|
* @param mixed $result result |
|
58
|
|
|
* @param mixed $swagger swagger |
|
59
|
|
|
* |
|
60
|
|
|
* @dataProvider swaggerJsonDataProvider |
|
61
|
|
|
* |
|
62
|
|
|
* @return void |
|
63
|
|
|
*/ |
|
64
|
|
|
public function testSupported($result, $swagger) |
|
65
|
|
|
{ |
|
66
|
|
|
$this->assertEquals($result, $this->sut->supports($swagger)); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* test missing fallback data |
|
71
|
|
|
* |
|
72
|
|
|
* @expectedException RuntimeException |
|
73
|
|
|
* @expectedExceptionMessage Missing mandatory key (host) in fallback data set. |
|
74
|
|
|
* |
|
75
|
|
|
* @return void |
|
76
|
|
|
*/ |
|
77
|
|
|
public function testMissingFallbackData() |
|
78
|
|
|
{ |
|
79
|
|
|
$this->sut->process('{}', array()); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* processing swagger |
|
84
|
|
|
* |
|
85
|
|
|
* @return void |
|
86
|
|
|
*/ |
|
87
|
|
|
public function testProcessSwagger() |
|
88
|
|
|
{ |
|
89
|
|
|
$content = file_get_contents(dirname(__FILE__).'/../../../resources/simple-swagger.json'); |
|
90
|
|
|
$swagger = json_decode($content); |
|
91
|
|
|
$orderPath = '/order/{orderId}'; |
|
92
|
|
|
|
|
93
|
|
|
$swaggerParserMock = $this->getMockBuilder('\Swagger\Document') |
|
94
|
|
|
->disableOriginalConstructor() |
|
95
|
|
|
->setMethods(['getBasePath', 'setDocument', 'getOperationsById', 'getSchemaResolver']) |
|
96
|
|
|
->getMock(); |
|
97
|
|
|
|
|
98
|
|
|
$statusCode = 200; |
|
99
|
|
|
$responseSchema = $swagger->paths->$orderPath->get->responses->$statusCode->schema; |
|
100
|
|
|
$orderDefinition = $swagger->definitions->Order; |
|
101
|
|
|
$userDefinition = $swagger->definitions->User; |
|
102
|
|
|
|
|
103
|
|
|
$responseSchemaMock = $this->getMockBuilder('\Swagger\Object\AbstractObject') |
|
104
|
|
|
->disableOriginalConstructor() |
|
105
|
|
|
->setMethods(['getDocument']) |
|
106
|
|
|
->getMockForAbstractClass(); |
|
107
|
|
|
$responseSchemaMock->expects($this->any()) |
|
108
|
|
|
->method('getDocument') |
|
109
|
|
|
->will($this->onConsecutiveCalls($responseSchema, $orderDefinition, $userDefinition)); |
|
110
|
|
|
|
|
111
|
|
|
$responseMock = $this->getMockBuilder('\Swagger\Object\Response') |
|
112
|
|
|
->disableOriginalConstructor() |
|
113
|
|
|
->setMethods(['getSchema']) |
|
114
|
|
|
->getMock(); |
|
115
|
|
|
$responseMock->expects($this->once()) |
|
116
|
|
|
->method('getSchema') |
|
117
|
|
|
->willReturn($responseSchemaMock); |
|
118
|
|
|
|
|
119
|
|
|
$responsesMock = $this->getMockBuilder('\Swagger\Object\Responses') |
|
120
|
|
|
->disableOriginalConstructor() |
|
121
|
|
|
->setMethods(['getHttpStatusCode']) |
|
122
|
|
|
->getMock(); |
|
123
|
|
|
$responsesMock->expects($this->once()) |
|
124
|
|
|
->method('getHttpStatusCode') |
|
125
|
|
|
->with($this->equalTo($statusCode)) |
|
126
|
|
|
->willReturn($responseMock); |
|
127
|
|
|
|
|
128
|
|
|
|
|
129
|
|
|
$operationMock = $this->getMockBuilder('\Swagger\Object\Operation') |
|
130
|
|
|
->disableOriginalConstructor() |
|
131
|
|
|
->setMethods(['getDocumentObjectProperty', 'getResponses']) |
|
132
|
|
|
->getMock(); |
|
133
|
|
|
$operationMock->expects($this->once()) |
|
134
|
|
|
->method('getResponses') |
|
135
|
|
|
->willReturn($responsesMock); |
|
136
|
|
|
|
|
137
|
|
|
|
|
138
|
|
|
$serviceMock = $this->getMockBuilder('\Swagger\OperationReference') |
|
139
|
|
|
->disableOriginalConstructor() |
|
140
|
|
|
->setMethods(['getPath', 'getMethod', 'getOperation']) |
|
141
|
|
|
->getMock(); |
|
142
|
|
|
$serviceMock->expects($this->exactly(3)) |
|
143
|
|
|
->method('getPath') |
|
144
|
|
|
->will($this->onConsecutiveCalls('/user', '/user', '/order/{orderId}')); |
|
145
|
|
|
$serviceMock->expects($this->exactly(5)) |
|
146
|
|
|
->method('getMethod') |
|
147
|
|
|
->will($this->onConsecutiveCalls('POST', 'DELETE', 'GET', 'POST', 'GET')); |
|
148
|
|
|
$serviceMock->expects($this->exactly(2)) |
|
149
|
|
|
->method('getOperation') |
|
150
|
|
|
->willReturn($operationMock); |
|
151
|
|
|
|
|
152
|
|
|
|
|
153
|
|
|
$operations = [$serviceMock, $serviceMock, $serviceMock]; |
|
154
|
|
|
|
|
155
|
|
|
$schemaResolverMock = $this->getMockBuilder('\Swagger\SchemaResolver') |
|
156
|
|
|
->disableOriginalConstructor() |
|
157
|
|
|
->setMethods(['resolveReference']) |
|
158
|
|
|
->getMock(); |
|
159
|
|
|
$schemaResolverMock->expects($this->exactly(2)) |
|
160
|
|
|
->method('resolveReference') |
|
161
|
|
|
->withAnyParameters() |
|
162
|
|
|
->willReturn($responseSchemaMock); |
|
163
|
|
|
|
|
164
|
|
|
$swaggerParserMock |
|
165
|
|
|
->expects($this->once()) |
|
166
|
|
|
->method('getOperationsById') |
|
167
|
|
|
->willReturn($operations); |
|
168
|
|
|
$swaggerParserMock |
|
169
|
|
|
->expects($this->exactly(2)) |
|
170
|
|
|
->method('getSchemaResolver') |
|
171
|
|
|
->willReturn($schemaResolverMock); |
|
172
|
|
|
|
|
173
|
|
|
$this->sut = new SwaggerStrategy($swaggerParserMock); |
|
174
|
|
|
|
|
175
|
|
|
$apiDefinition = $this->sut->process($content, array('host' => 'localhost')); |
|
176
|
|
|
$this->assertInstanceOf('Graviton\ProxyBundle\Definition\ApiDefinition', $apiDefinition); |
|
177
|
|
|
$this->assertCount(2, $apiDefinition->getEndpoints()); |
|
178
|
|
|
} |
|
179
|
|
|
} |
|
180
|
|
|
|