Completed
Push — master ( 00ae50...77d591 )
by Narcotic
27:26 queued 18:13
created

paramHandlingWithIntDataProvider()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 14

Duplication

Lines 25
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 25
loc 25
rs 8.8571
cc 1
eloc 14
nc 1
nop 0
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
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use integer[][].

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.

Loading history...
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 View Code Duplication
    public function testParamHandlingWithIntOnStringField($groupId, $numRecords, $idList)
234
    {
235
        $client = static::createRestClient();
236
        $client->request('GET', '/analytics/customer-with-int-param-string-field?groupId='.$groupId);
237
238
        $this->assertEquals($numRecords, count($client->getResults()));
239
240
        foreach ($client->getResults() as $result) {
241
            $this->assertContains($result->{'_id'}, $idList);
242
        }
243
    }
244
245
    /**
246
     * data provider
247
     *
248
     * @return array data
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<integer|string[]>[].

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.

Loading history...
249
     */
250
    public function paramHandlingWithIntOnStringFieldDataProvider()
251
    {
252
        return [
253
            [
254
                100,
255
                3,
256
                ['100', '101', '102']
257
            ],
258
            [
259
                200,
260
                3,
261
                ['100', '101', '103']
262
            ]
263
        ];
264
    }
265
266
    /**
267
     * see if array properties are properly handled
268
     *
269
     * @dataProvider paramHandlingArrayWithIntOnStringFieldDataProvider
270
     *
271
     * @param string $groupId group id
272
     * @param array  $idList  list of ids
273
     *
274
     * @return void
275
     */
276 View Code Duplication
    public function testParamArrayHandlingWithIntOnStringField($groupId, $idList)
277
    {
278
        $client = static::createRestClient();
279
        $client->request(
280
            'GET',
281
            '/analytics/customer-with-int-param-array-field?groupId='.implode(',', $groupId)
282
        );
283
284
        $this->assertEquals(count($idList), count($client->getResults()));
285
286
        foreach ($client->getResults() as $result) {
287
            $this->assertContains($result->{'_id'}, $idList);
288
        }
289
    }
290
291
    /**
292
     * data provider
293
     *
294
     * @return array data
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<integer|string>[][].

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.

Loading history...
295
     */
296
    public function paramHandlingArrayWithIntOnStringFieldDataProvider()
297
    {
298
        return [
299
            [
300
                [100],
301
                ['100', '101', '102']
302
            ],
303
            [
304
                [200],
305
                ['100', '101', '103']
306
            ],
307
            [
308
                [100,200],
309
                ['100', '101', '102', '103']
310
            ]
311
        ];
312
    }
313
314
    /**
315
     * test to see if the params are mentioned in the schema
316
     *
317
     * @return void
318
     */
319
    public function testParamsInSchema()
320
    {
321
        $client = static::createRestClient();
322
        $client->request('GET', '/analytics/schema/customer-date-with-param');
323
324
        $this->assertEquals(2, count($client->getResults()->{'x-params'}));
325
        $this->assertEquals(true, count($client->getResults()->{'x-params'}[0]->required));
326
        $this->assertEquals(true, count($client->getResults()->{'x-params'}[1]->required));
327
    }
328
}
329