1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Test cases for basic coverage for Analytics Bundle |
4
|
|
|
*/ |
5
|
|
|
namespace Graviton\AnalyticsBundle\Tests\Controller; |
6
|
|
|
|
7
|
|
|
use Symfony\Component\HttpFoundation\Response; |
8
|
|
|
use Graviton\TestBundle\Test\RestTestCase; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Basic functional test for Analytics |
12
|
|
|
* |
13
|
|
|
* @author List of contributors <https://github.com/libgraviton/graviton/graphs/contributors> |
14
|
|
|
* @license https://opensource.org/licenses/MIT MIT License |
15
|
|
|
* @link http://swisscom.ch |
16
|
|
|
*/ |
17
|
|
|
class DefaultControllerTest extends RestTestCase |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Initial setup |
21
|
|
|
* @return void |
22
|
|
|
*/ |
23
|
|
|
public function setUp() |
24
|
|
|
{ |
25
|
|
|
$this->loadFixtures( |
26
|
|
|
array( |
27
|
|
|
'Graviton\CoreBundle\DataFixtures\MongoDB\LoadAppData', |
28
|
|
|
'GravitonDyn\CustomerBundle\DataFixtures\MongoDB\LoadCustomerData', |
29
|
|
|
), |
30
|
|
|
null, |
31
|
|
|
'doctrine_mongodb' |
32
|
|
|
); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* test options request |
37
|
|
|
* @return void |
38
|
|
|
*/ |
39
|
|
View Code Duplication |
public function testOptions() |
40
|
|
|
{ |
41
|
|
|
$client = static::createRestClient(); |
42
|
|
|
$client->request('OPTIONS', '/analytics/schema/app'); |
43
|
|
|
$this->assertEquals(Response::HTTP_NO_CONTENT, $client->getResponse()->getStatusCode()); |
44
|
|
|
$this->assertEquals('GET, OPTIONS', $client->getResponse()->headers->get('Access-Control-Allow-Methods')); |
45
|
|
|
$this->assertEmpty($client->getResults()); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Testing basic functionality |
50
|
|
|
* @return void |
51
|
|
|
*/ |
52
|
|
View Code Duplication |
public function testIndex() |
53
|
|
|
{ |
54
|
|
|
$client = static::createClient(); |
55
|
|
|
|
56
|
|
|
// Let's get information from the schema |
57
|
|
|
$client->request('GET', '/analytics/schema/app'); |
58
|
|
|
$content = $client->getResponse()->getContent(); |
59
|
|
|
$schema = json_decode($content); |
60
|
|
|
|
61
|
|
|
// Check schema |
62
|
|
|
$sampleSchema = json_decode( |
63
|
|
|
'{ |
64
|
|
|
"title": "Application usage", |
65
|
|
|
"description": "Data use for application access", |
66
|
|
|
"type": "object", |
67
|
|
|
"properties": { |
68
|
|
|
"id": { |
69
|
|
|
"title": "ID", |
70
|
|
|
"description": "Unique identifier", |
71
|
|
|
"type": "string" |
72
|
|
|
}, |
73
|
|
|
"count": { |
74
|
|
|
"title": "count", |
75
|
|
|
"description": "Sum of result", |
76
|
|
|
"type": "integer" |
77
|
|
|
} |
78
|
|
|
}, |
79
|
|
|
"x-params": [] |
80
|
|
|
}' |
81
|
|
|
); |
82
|
|
|
$this->assertEquals($sampleSchema, $schema); |
83
|
|
|
|
84
|
|
|
// Let's get information from the count |
85
|
|
|
$client->request('GET', '/analytics/app'); |
86
|
|
|
$content = $client->getResponse()->getContent(); |
87
|
|
|
$data = json_decode($content); |
88
|
|
|
|
89
|
|
|
// Counter data result of aggregate |
90
|
|
|
$sampleData = json_decode('{"_id":"app-count","count":2}'); |
91
|
|
|
$this->assertEquals($sampleData, $data); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Testing basic functionality |
96
|
|
|
* @return void |
97
|
|
|
*/ |
98
|
|
|
public function testApp2Index() |
99
|
|
|
{ |
100
|
|
|
$client = static::createClient(); |
101
|
|
|
|
102
|
|
|
// Let's get information from the count |
103
|
|
|
$client->request('GET', '/analytics/app2'); |
104
|
|
|
$content = $client->getResponse()->getContent(); |
105
|
|
|
$data = json_decode($content); |
106
|
|
|
|
107
|
|
|
// Counter data result of aggregate |
108
|
|
|
$sampleData = json_decode('{"_id":"app-count-2","count":1}'); |
109
|
|
|
$this->assertEquals($sampleData, $data); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Testing basic functionality |
114
|
|
|
* @return void |
115
|
|
|
*/ |
116
|
|
View Code Duplication |
public function testCustomerCreateDateFilteringIndex() |
117
|
|
|
{ |
118
|
|
|
$client = static::createClient(); |
119
|
|
|
|
120
|
|
|
// Let's get information from the count |
121
|
|
|
$client->request('GET', '/analytics/customer-created-by-date'); |
122
|
|
|
$content = $client->getResponse()->getContent(); |
123
|
|
|
$data = json_decode($content); |
124
|
|
|
|
125
|
|
|
// Counter data result of aggregate |
126
|
|
|
$sampleData = json_decode( |
127
|
|
|
'[ |
128
|
|
|
{ |
129
|
|
|
"_id": "100", |
130
|
|
|
"customerNumber": 1100, |
131
|
|
|
"name": "Acme Corps.", |
132
|
|
|
"created_year": 2014, |
133
|
|
|
"created_month": 7 |
134
|
|
|
} |
135
|
|
|
]' |
136
|
|
|
); |
137
|
|
|
$this->assertEquals($sampleData, $data); |
138
|
|
|
|
139
|
|
|
// Let's get information from the count, but cached version |
140
|
|
|
$client->request('GET', '/analytics/customer-created-by-date'); |
141
|
|
|
$content = $client->getResponse()->getContent(); |
142
|
|
|
$data = json_decode($content); |
143
|
|
|
|
144
|
|
|
// Counter data result of aggregate |
145
|
|
|
$sampleData = json_decode( |
146
|
|
|
'[ |
147
|
|
|
{ |
148
|
|
|
"_id": "100", |
149
|
|
|
"customerNumber": 1100, |
150
|
|
|
"name": "Acme Corps.", |
151
|
|
|
"created_year": 2014, |
152
|
|
|
"created_month": 7 |
153
|
|
|
} |
154
|
|
|
]' |
155
|
|
|
); |
156
|
|
|
$this->assertEquals($sampleData, $data); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* test to see if required params are required |
161
|
|
|
* |
162
|
|
|
* @return void |
163
|
|
|
*/ |
164
|
|
|
public function testMissingParamExceptions() |
165
|
|
|
{ |
166
|
|
|
$client = static::createClient(); |
167
|
|
|
|
168
|
|
|
$client->request('GET', '/analytics/customer-date-with-param'); |
169
|
|
|
$this->assertEquals(500, $client->getResponse()->getStatusCode()); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* test to see application of param |
174
|
|
|
* |
175
|
|
|
* @dataProvider paramHandlingWithIntDataProvider |
176
|
|
|
* |
177
|
|
|
* @param int $yearFrom year from |
178
|
|
|
* @param int $yearTo year to |
179
|
|
|
* @param int $numRecords number of records |
180
|
|
|
* |
181
|
|
|
* @return void |
182
|
|
|
*/ |
183
|
|
|
public function testParamHandlingWithInt($yearFrom, $yearTo, $numRecords) |
184
|
|
|
{ |
185
|
|
|
$client = static::createRestClient(); |
186
|
|
|
$client->request('GET', '/analytics/customer-date-with-param?yearFrom='.$yearFrom.'&yearTo='.$yearTo); |
187
|
|
|
|
188
|
|
|
$this->assertEquals($numRecords, count($client->getResults())); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* data provider |
193
|
|
|
* |
194
|
|
|
* @return array data |
|
|
|
|
195
|
|
|
*/ |
196
|
|
View Code Duplication |
public function paramHandlingWithIntDataProvider() |
197
|
|
|
{ |
198
|
|
|
return [ |
199
|
|
|
[ |
200
|
|
|
1999, |
201
|
|
|
9999, |
202
|
|
|
4 |
203
|
|
|
], |
204
|
|
|
[ |
205
|
|
|
2014, |
206
|
|
|
2014, |
207
|
|
|
1 |
208
|
|
|
], |
209
|
|
|
[ |
210
|
|
|
2014, |
211
|
|
|
2015, |
212
|
|
|
2 |
213
|
|
|
], |
214
|
|
|
[ |
215
|
|
|
2014, |
216
|
|
|
2016, |
217
|
|
|
3 |
218
|
|
|
] |
219
|
|
|
]; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* test to see application of param with int on string field, if the correct records are returned |
224
|
|
|
* |
225
|
|
|
* @dataProvider paramHandlingWithIntOnStringFieldDataProvider |
226
|
|
|
* |
227
|
|
|
* @param string $groupId group id |
228
|
|
|
* @param int $numRecords number of records |
229
|
|
|
* @param array $idList list of ids |
230
|
|
|
* |
231
|
|
|
* @return void |
232
|
|
|
*/ |
233
|
|
|
public function testParamHandlingWithIntOnStringField($groupId, $numRecords, $idList) |
234
|
|
|
{ |
235
|
|
|
$client = static::createRestClient(); |
236
|
|
|
|
237
|
|
|
$url = '/analytics/customer-with-int-param-string-field'; |
238
|
|
|
if (!is_null($groupId)) { |
239
|
|
|
$url .= '?groupId='.$groupId; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
$client->request('GET', $url); |
243
|
|
|
|
244
|
|
|
$this->assertEquals($numRecords, count($client->getResults())); |
245
|
|
|
|
246
|
|
|
foreach ($client->getResults() as $result) { |
247
|
|
|
$this->assertContains($result->{'_id'}, $idList); |
248
|
|
|
} |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* data provider |
253
|
|
|
* |
254
|
|
|
* @return array data |
|
|
|
|
255
|
|
|
*/ |
256
|
|
|
public function paramHandlingWithIntOnStringFieldDataProvider() |
257
|
|
|
{ |
258
|
|
|
return [ |
259
|
|
|
[ |
260
|
|
|
100, |
261
|
|
|
3, |
262
|
|
|
['100', '101', '102'] |
263
|
|
|
], |
264
|
|
|
[ |
265
|
|
|
null, // testing default value of 100 as defined in params! |
266
|
|
|
3, |
267
|
|
|
['100', '101', '102'] |
268
|
|
|
], |
269
|
|
|
[ |
270
|
|
|
200, |
271
|
|
|
3, |
272
|
|
|
['100', '101', '103'] |
273
|
|
|
] |
274
|
|
|
]; |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* see if array properties are properly handled |
279
|
|
|
* |
280
|
|
|
* @dataProvider paramHandlingArrayWithIntOnStringFieldDataProvider |
281
|
|
|
* |
282
|
|
|
* @param string $groupId group id |
283
|
|
|
* @param array $idList list of ids |
284
|
|
|
* |
285
|
|
|
* @return void |
286
|
|
|
*/ |
287
|
|
|
public function testParamArrayHandlingWithIntOnStringField($groupId, $idList) |
288
|
|
|
{ |
289
|
|
|
$client = static::createRestClient(); |
290
|
|
|
$client->request( |
291
|
|
|
'GET', |
292
|
|
|
'/analytics/customer-with-int-param-array-field?groupId='.implode(',', $groupId) |
293
|
|
|
); |
294
|
|
|
|
295
|
|
|
$this->assertEquals(count($idList), count($client->getResults())); |
296
|
|
|
|
297
|
|
|
foreach ($client->getResults() as $result) { |
298
|
|
|
$this->assertContains($result->{'_id'}, $idList); |
299
|
|
|
} |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* data provider |
304
|
|
|
* |
305
|
|
|
* @return array data |
|
|
|
|
306
|
|
|
*/ |
307
|
|
|
public function paramHandlingArrayWithIntOnStringFieldDataProvider() |
308
|
|
|
{ |
309
|
|
|
return [ |
310
|
|
|
[ |
311
|
|
|
[100], |
312
|
|
|
['100', '101', '102'] |
313
|
|
|
], |
314
|
|
|
[ |
315
|
|
|
[200], |
316
|
|
|
['100', '101', '103'] |
317
|
|
|
], |
318
|
|
|
[ |
319
|
|
|
[100,200], |
320
|
|
|
['100', '101', '102', '103'] |
321
|
|
|
] |
322
|
|
|
]; |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
/** |
326
|
|
|
* test to see if the params are mentioned in the schema |
327
|
|
|
* |
328
|
|
|
* @return void |
329
|
|
|
*/ |
330
|
|
|
public function testParamsInSchema() |
331
|
|
|
{ |
332
|
|
|
$client = static::createRestClient(); |
333
|
|
|
$client->request('GET', '/analytics/schema/customer-date-with-param'); |
334
|
|
|
|
335
|
|
|
$this->assertEquals(2, count($client->getResults()->{'x-params'})); |
336
|
|
|
$this->assertEquals(true, count($client->getResults()->{'x-params'}[0]->required)); |
337
|
|
|
$this->assertEquals(true, count($client->getResults()->{'x-params'}[1]->required)); |
338
|
|
|
} |
339
|
|
|
|
340
|
|
|
/** |
341
|
|
|
* tests conversion of MongoDates in output as well as replacement of Date instances (and maybe others) |
342
|
|
|
* in the pipeline script |
343
|
|
|
* |
344
|
|
|
* @return void |
345
|
|
|
*/ |
346
|
|
|
public function testDateHandlingInOutput() |
347
|
|
|
{ |
348
|
|
|
$client = static::createRestClient(); |
349
|
|
|
$client->request( |
350
|
|
|
'GET', |
351
|
|
|
'/analytics/customer-datehandling' |
352
|
|
|
); |
353
|
|
|
|
354
|
|
|
$expectedResult = [ |
355
|
|
|
[ |
356
|
|
|
'_id' => '100', |
357
|
|
|
'createDate' => '2014-07-15T10:23:31+0000', |
358
|
|
|
'age' => date('Y') - 2014, |
359
|
|
|
'sub' => [ |
360
|
|
|
'createDate' => '2014-07-15T10:23:31+0000' |
361
|
|
|
] |
362
|
|
|
], |
363
|
|
|
[ |
364
|
|
|
'_id' => '101', |
365
|
|
|
'createDate' => '2015-07-15T10:23:31+0000', |
366
|
|
|
'age' => date('Y') - 2015, |
367
|
|
|
'sub' => [ |
368
|
|
|
'createDate' => '2015-07-15T10:23:31+0000' |
369
|
|
|
] |
370
|
|
|
], |
371
|
|
|
[ |
372
|
|
|
'_id' => '102', |
373
|
|
|
'createDate' => '2016-07-15T10:23:31+0000', |
374
|
|
|
'age' => date('Y') - 2016, |
375
|
|
|
'sub' => [ |
376
|
|
|
'createDate' => '2016-07-15T10:23:31+0000' |
377
|
|
|
] |
378
|
|
|
], |
379
|
|
|
[ |
380
|
|
|
'_id' => '103', |
381
|
|
|
'createDate' => '2017-07-15T10:23:31+0000', |
382
|
|
|
'age' => date('Y') - 2017, |
383
|
|
|
'sub' => [ |
384
|
|
|
'createDate' => '2017-07-15T10:23:31+0000' |
385
|
|
|
] |
386
|
|
|
], |
387
|
|
|
]; |
388
|
|
|
|
389
|
|
|
$this->assertEquals( |
390
|
|
|
json_decode(json_encode($expectedResult)), // make objects |
391
|
|
|
$client->getResults() |
392
|
|
|
); |
393
|
|
|
} |
394
|
|
|
} |
395
|
|
|
|
This check looks for the generic type
array
as a return type and suggests a more specific type. This type is inferred from the actual code.