Completed
Pull Request — develop (#581)
by
unknown
14:51 queued 10:21
created

PrimitiveArrayControllerTest   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 395
Duplicated Lines 10.63 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 19
lcom 1
cbo 3
dl 42
loc 395
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 12 12 2
A testItemSchema() 10 10 1
A testCollectionSchema() 11 11 1
A testCheckGetOne() 9 9 1
A testCheckGetAll() 0 9 1
A testPostMethod() 0 51 1
B testPutMethod() 0 40 1
B testValidation() 0 105 1
A fixDateTimezone() 0 14 1
C assertFixtureData() 0 34 7
B assertItemSchema() 0 24 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * PrimitiveArrayControllerTest class file
4
 */
5
6
namespace Graviton\CoreBundle\Tests\Controller;
7
8
use Graviton\TestBundle\Test\RestTestCase;
9
use Symfony\Component\HttpFoundation\Response;
10
use GravitonDyn\TestCasePrimitiveArrayBundle\DataFixtures\MongoDB\LoadTestCasePrimitiveArrayData;
11
12
/**
13
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
14
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
15
 * @link     http://swisscom.ch
16
 */
17
class PrimitiveArrayControllerTest extends RestTestCase
18
{
19
    const DATE_FORMAT = 'Y-m-d\\TH:i:sO';
20
21
    /**
22
     * load fixtures
23
     *
24
     * @return void
25
     */
26 View Code Duplication
    public function setUp()
27
    {
28
        if (!class_exists(LoadTestCasePrimitiveArrayData::class)) {
29
            $this->markTestSkipped('TestCasePrimitiveArray definition is not loaded');
30
        }
31
32
        $this->loadFixtures(
33
            [LoadTestCasePrimitiveArrayData::class],
34
            null,
35
            'doctrine_mongodb'
36
        );
37
    }
38
39
    /**
40
     * Test item schema
41
     *
42
     * @return void
43
     */
44 View Code Duplication
    public function testItemSchema()
45
    {
46
        $client = static::createRestClient();
47
        $client->request('GET', '/schema/testcase/primitivearray/item');
48
        $this->assertEquals(Response::HTTP_OK, $client->getResponse()->getStatusCode());
49
50
        $schema = $client->getResults();
51
        $this->assertEquals('object', $schema->type);
52
        $this->assertItemSchema($schema);
53
    }
54
55
    /**
56
     * Test collection schema
57
     *
58
     * @return void
59
     */
60 View Code Duplication
    public function testCollectionSchema()
61
    {
62
        $client = static::createRestClient();
63
        $client->request('GET', '/schema/testcase/primitivearray/collection');
64
        $this->assertEquals(Response::HTTP_OK, $client->getResponse()->getStatusCode());
65
66
        $schema = $client->getResults();
67
        $this->assertEquals('array', $schema->type);
68
        $this->assertEquals('object', $schema->items->type);
69
        $this->assertItemSchema($schema->items);
70
    }
71
72
    /**
73
     * Test GET one method
74
     *
75
     * @return void
76
     */
77 View Code Duplication
    public function testCheckGetOne()
78
    {
79
        $client = static::createRestClient();
80
        $client->request('GET', '/testcase/primitivearray/testdata');
81
        $this->assertEquals(Response::HTTP_OK, $client->getResponse()->getStatusCode());
82
        $this->assertNotEmpty($client->getResults());
83
84
        $this->assertFixtureData($client->getResults());
85
    }
86
87
    /**
88
     * Test GET all method
89
     *
90
     * @return void
91
     */
92
    public function testCheckGetAll()
93
    {
94
        $client = static::createRestClient();
95
        $client->request('GET', '/testcase/primitivearray/');
96
        $this->assertEquals(Response::HTTP_OK, $client->getResponse()->getStatusCode());
97
        $this->assertCount(1, $client->getResults());
98
99
        $this->assertFixtureData($client->getResults()[0]);
100
    }
101
102
    /**
103
     * Test POST method
104
     *
105
     * @return void
106
     */
107
    public function testPostMethod()
108
    {
109
        $data = (object) [
110
            'intarray'  => [10, 20],
111
            'strarray'  => ['a', 'b'],
112
            'boolarray' => [true, false],
113
            'hasharray' => [(object) ['x' => 'y'], (object) []],
114
            'datearray' => ['2015-09-30T23:59:59+0000', '2015-10-01T00:00:01+0300'],
115
116
            'hash'      => (object) [
117
                'intarray'  => [10, 20],
118
                'strarray'  => ['a', 'b'],
119
                'boolarray' => [true, false],
120
                'hasharray' => [(object) ['x' => 'y'], (object) []],
121
                'datearray' => ['2015-09-30T23:59:59+0000', '2015-10-01T00:00:01+0300'],
122
            ],
123
124
            'arrayhash' => [
125
                (object) [
126
                    'intarray'  => [10, 20],
127
                    'strarray'  => ['a', 'b'],
128
                    'boolarray' => [true, false],
129
                    'hasharray' => [(object) ['x' => 'y'], (object) []],
130
                    'datearray' => ['2015-09-30T23:59:59+0000', '2015-10-01T00:00:01+0300'],
131
                ]
132
            ],
133
134
            'rawData'      => (object) [
135
                'hasharray' => [(object) ['x' => 'y'], (object) []],
136
                'emptyhash' => (object) []
137
            ],
138
        ];
139
140
        $client = static::createRestClient();
141
        $client->post('/testcase/primitivearray/', $data);
142
        $this->assertEquals(Response::HTTP_CREATED, $client->getResponse()->getStatusCode());
143
        $this->assertEmpty($client->getResults());
144
145
        $location = $client->getResponse()->headers->get('Location');
146
147
        $client = static::createRestClient();
148
        $client->request('GET', $location);
149
        $this->assertEquals(Response::HTTP_OK, $client->getResponse()->getStatusCode());
150
151
        $result = $client->getResults();
152
        $this->assertNotNull($result->id);
153
        unset($result->id);
154
        unset($data->rawData->hasharray[1]);
155
        unset($data->rawData->emptyhash);
156
        $this->assertEquals($this->fixDateTimezone($data), $result);
157
    }
158
159
    /**
160
     * Test PUT method
161
     *
162
     * @return void
163
     */
164
    public function testPutMethod()
165
    {
166
        $data = (object) [
167
            'id'        => 'testdata',
168
169
            'intarray'  => [10, 20],
170
            'strarray'  => ['a', 'b'],
171
            'boolarray' => [true, false],
172
            'hasharray' => [(object) ['x' => 'y'], (object) []],
173
            'datearray' => ['2015-09-30T23:59:59+0000', '2015-10-01T00:00:01+0300'],
174
175
            'hash'      => (object) [
176
                'intarray'  => [10, 20],
177
                'strarray'  => ['a', 'b'],
178
                'boolarray' => [true, false],
179
                'hasharray' => [(object) ['x' => 'y'], (object) []],
180
                'datearray' => ['2015-09-30T23:59:59+0000', '2015-10-01T00:00:01+0300'],
181
            ],
182
183
            'arrayhash' => [
184
                (object) [
185
                    'intarray'  => [10, 20],
186
                    'strarray'  => ['a', 'b'],
187
                    'boolarray' => [true, false],
188
                    'hasharray' => [(object) ['x' => 'y'], (object) []],
189
                    'datearray' => ['2015-09-30T23:59:59+0000', '2015-10-01T00:00:01+0300'],
190
                ]
191
            ],
192
        ];
193
194
        $client = static::createRestClient();
195
        $client->put('/testcase/primitivearray/testdata', $data);
196
        $this->assertEquals(Response::HTTP_NO_CONTENT, $client->getResponse()->getStatusCode());
197
        $this->assertEmpty($client->getResults());
198
199
        $client = static::createRestClient();
200
        $client->request('GET', '/testcase/primitivearray/testdata');
201
        $this->assertEquals(Response::HTTP_OK, $client->getResponse()->getStatusCode());
202
        $this->assertEquals($this->fixDateTimezone($data), $client->getResults());
203
    }
204
205
    /**
206
     * Test validation
207
     *
208
     * @return void
209
     */
210
    public function testValidation()
211
    {
212
        $data = (object) [
213
            'id'        => 'testdata',
214
215
            'intarray'  => [1, 'a'],
216
            'strarray'  => ['a', false],
217
            'boolarray' => [true, 'a'],
218
            'hasharray' => [(object) ['x' => 'y'], 1.5],
219
            'datearray' => ['2015-10-03T22:32:00+0600', 'abc'],
220
221
            'hash'      => (object) [
222
                'intarray'  => [1, 'a'],
223
                'strarray'  => ['a', false],
224
                'boolarray' => [true, 'a'],
225
                'hasharray' => [(object) ['x' => 'y'], 1.5],
226
                'datearray' => ['2015-10-03T22:32:00+0600', 'abc'],
227
            ],
228
229
            'arrayhash' => [
230
                (object) [
231
                    'intarray'  => [1, 'a'],
232
                    'strarray'  => ['a', false],
233
                    'boolarray' => [true, 'a'],
234
                    'hasharray' => [(object) ['x' => 'y'], 1.5],
235
                    'datearray' => ['2015-10-03T22:32:00+0600', 'abc'],
236
                ]
237
            ],
238
        ];
239
240
        $client = static::createRestClient();
241
        $client->put('/testcase/primitivearray/testdata', $data);
242
        $this->assertEquals(Response::HTTP_BAD_REQUEST, $client->getResponse()->getStatusCode());
243
        $this->assertNotNull($client->getResults());
244
245
        $this->assertEquals(
246
            [
247
                (object) [
248
                    'propertyPath' => 'intarray[1]',
249
                    'message'      => 'String value found, but an integer is required',
250
                ],
251
                (object) [
252
                    'propertyPath' => 'strarray[1]',
253
                    'message'      => 'Boolean value found, but a string is required',
254
                ],
255
                (object) [
256
                    'propertyPath' => 'boolarray[1]',
257
                    'message'      => 'String value found, but a boolean is required',
258
                ],
259
                (object) [
260
                    'propertyPath' => 'datearray[1]',
261
                    'message'      => 'Invalid date-time "abc", expected format YYYY-MM-DDThh:mm:ssZ '.
262
                        'or YYYY-MM-DDThh:mm:ss+hh:mm',
263
                ],
264
                (object) [
265
                    'propertyPath' => 'hasharray[1]',
266
                    'message'      => 'Double value found, but an object is required',
267
                ],
268
                (object) [
269
                    'propertyPath' => 'hash.intarray[1]',
270
                    'message'      => 'String value found, but an integer is required',
271
                ],
272
                (object) [
273
                    'propertyPath' => 'hash.strarray[1]',
274
                    'message'      => 'Boolean value found, but a string is required',
275
                ],
276
                (object) [
277
                    'propertyPath' => 'hash.boolarray[1]',
278
                    'message'      => 'String value found, but a boolean is required',
279
                ],
280
281
                (object) [
282
                    'propertyPath' => 'hash.datearray[1]',
283
                    'message'      => 'Invalid date-time "abc", expected format YYYY-MM-DDThh:mm:ssZ '.
284
                        'or YYYY-MM-DDThh:mm:ss+hh:mm',
285
                ],
286
                (object) [
287
                    'propertyPath' => 'hash.hasharray[1]',
288
                    'message'      => 'Double value found, but an object is required',
289
                ],
290
                (object) [
291
                    'propertyPath' => 'arrayhash[0].intarray[1]',
292
                    'message'      => 'String value found, but an integer is required',
293
                ],
294
                (object) [
295
                    'propertyPath' => 'arrayhash[0].strarray[1]',
296
                    'message'      => 'Boolean value found, but a string is required',
297
                ],
298
                (object) [
299
                    'propertyPath' => 'arrayhash[0].boolarray[1]',
300
                    'message'      => 'String value found, but a boolean is required',
301
                ],
302
                (object) [
303
                    'propertyPath' => 'arrayhash[0].datearray[1]',
304
                    'message'      => 'Invalid date-time "abc", expected format YYYY-MM-DDThh:mm:ssZ '.
305
                        'or YYYY-MM-DDThh:mm:ss+hh:mm',
306
                ],
307
                (object) [
308
                    'propertyPath' => 'arrayhash[0].hasharray[1]',
309
                    'message'      => 'Double value found, but an object is required',
310
                ]
311
            ],
312
            $client->getResults()
313
        );
314
    }
315
316
    /**
317
     * Fix date timezone
318
     *
319
     * @param object $data Request data
320
     * @return object
321
     */
322
    private function fixDateTimezone($data)
323
    {
324
        $converter = function (&$date) {
325
            $date = \DateTime::createFromFormat(self::DATE_FORMAT, $date)
326
                ->setTimezone(new \DateTimeZone(date_default_timezone_get()))
327
                ->format(self::DATE_FORMAT);
328
        };
329
330
        array_walk($data->datearray, $converter);
331
        array_walk($data->hash->datearray, $converter);
332
        array_walk($data->arrayhash[0]->datearray, $converter);
333
334
        return $data;
335
    }
336
337
338
    /**
339
     * Assert fixture data
340
     *
341
     * @param object $data Fixture data
342
     * @return void
343
     * @throws \PHPUnit_Framework_AssertionFailedError
344
     */
345
    private function assertFixtureData($data)
346
    {
347
        foreach ([
348
                     $data,
349
                     $data->hash,
350
                     $data->arrayhash[0],
351
                 ] as $data) {
352
            $this->assertInternalType('array', $data->intarray);
353
            foreach ($data->intarray as $value) {
354
                $this->assertInternalType('integer', $value);
355
            }
356
357
            $this->assertInternalType('array', $data->strarray);
358
            foreach ($data->strarray as $value) {
359
                $this->assertInternalType('string', $value);
360
            }
361
362
            $this->assertInternalType('array', $data->boolarray);
363
            foreach ($data->boolarray as $value) {
364
                $this->assertInternalType('boolean', $value);
365
            }
366
367
            $this->assertInternalType('array', $data->datearray);
368
            foreach ($data->datearray as $value) {
369
                $this->assertInternalType('string', $value);
370
                $this->assertInstanceOf(\DateTime::class, \DateTime::createFromFormat(self::DATE_FORMAT, $value));
371
            }
372
373
            $this->assertInternalType('array', $data->hasharray);
374
            foreach ($data->hasharray as $value) {
375
                $this->assertInternalType('object', $value);
376
            }
377
        }
378
    }
379
380
    /**
381
     * Assert item schema
382
     *
383
     * @param object $schema Item schema
384
     * @return void
385
     * @throws \PHPUnit_Framework_AssertionFailedError
386
     */
387
    private function assertItemSchema($schema)
388
    {
389
        foreach ([
390
                     $schema->properties,
391
                     $schema->properties->hash->properties,
392
                     $schema->properties->arrayhash->items->properties,
393
                 ] as $schema) {
394
            $this->assertEquals('array', $schema->intarray->type);
395
            $this->assertEquals('integer', $schema->intarray->items->type);
396
397
            $this->assertEquals('array', $schema->strarray->type);
398
            $this->assertEquals('string', $schema->strarray->items->type);
399
400
            $this->assertEquals('array', $schema->boolarray->type);
401
            $this->assertEquals('boolean', $schema->boolarray->items->type);
402
403
            $this->assertEquals('array', $schema->datearray->type);
404
            $this->assertEquals('string', $schema->datearray->items->type);
405
            $this->assertEquals('date-time', $schema->datearray->items->format);
406
407
            $this->assertEquals('array', $schema->hasharray->type);
408
            $this->assertEquals('object', $schema->hasharray->items->type);
409
        }
410
    }
411
}
412