Completed
Push — develop ( a312c8...058245 )
by
unknown
11s
created

PrimitiveArrayControllerTest::testPostMethod()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 51
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 51
rs 9.4109
c 0
b 0
f 0
cc 1
eloc 37
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
            'rawData'      => (object) [],
194
        ];
195
196
        $client = static::createRestClient();
197
        $client->put('/testcase/primitivearray/testdata', $data);
198
        $this->assertEquals(Response::HTTP_NO_CONTENT, $client->getResponse()->getStatusCode());
199
        $this->assertEmpty($client->getResults());
200
201
        $client = static::createRestClient();
202
        $client->request('GET', '/testcase/primitivearray/testdata');
203
        $this->assertEquals(Response::HTTP_OK, $client->getResponse()->getStatusCode());
204
        $this->assertEquals($this->fixDateTimezone($data), $client->getResults());
205
    }
206
207
    /**
208
     * Test validation
209
     *
210
     * @return void
211
     */
212
    public function testValidation()
213
    {
214
        $data = (object) [
215
            'id'        => 'testdata',
216
217
            'intarray'  => [1, 'a'],
218
            'strarray'  => ['a', false],
219
            'boolarray' => [true, 'a'],
220
            'hasharray' => [(object) ['x' => 'y'], 1.5],
221
            'datearray' => ['2015-10-03T22:32:00+0600', 'abc'],
222
223
            'hash'      => (object) [
224
                'intarray'  => [1, 'a'],
225
                'strarray'  => ['a', false],
226
                'boolarray' => [true, 'a'],
227
                'hasharray' => [(object) ['x' => 'y'], 1.5],
228
                'datearray' => ['2015-10-03T22:32:00+0600', 'abc'],
229
            ],
230
231
            'arrayhash' => [
232
                (object) [
233
                    'intarray'  => [1, 'a'],
234
                    'strarray'  => ['a', false],
235
                    'boolarray' => [true, 'a'],
236
                    'hasharray' => [(object) ['x' => 'y'], 1.5],
237
                    'datearray' => ['2015-10-03T22:32:00+0600', 'abc'],
238
                ]
239
            ],
240
        ];
241
242
        $client = static::createRestClient();
243
        $client->put('/testcase/primitivearray/testdata', $data);
244
        $this->assertEquals(Response::HTTP_BAD_REQUEST, $client->getResponse()->getStatusCode());
245
        $this->assertNotNull($client->getResults());
246
247
        $this->assertEquals(
248
            [
249
                (object) [
250
                    'propertyPath' => 'intarray[1]',
251
                    'message'      => 'String value found, but an integer is required',
252
                ],
253
                (object) [
254
                    'propertyPath' => 'strarray[1]',
255
                    'message'      => 'Boolean value found, but a string is required',
256
                ],
257
                (object) [
258
                    'propertyPath' => 'boolarray[1]',
259
                    'message'      => 'String value found, but a boolean is required',
260
                ],
261
                (object) [
262
                    'propertyPath' => 'datearray[1]',
263
                    'message'      => 'Invalid date-time "abc", expected format YYYY-MM-DDThh:mm:ssZ '.
264
                        'or YYYY-MM-DDThh:mm:ss+hh:mm',
265
                ],
266
                (object) [
267
                    'propertyPath' => 'hasharray[1]',
268
                    'message'      => 'Double value found, but an object is required',
269
                ],
270
                (object) [
271
                    'propertyPath' => 'hash.intarray[1]',
272
                    'message'      => 'String value found, but an integer is required',
273
                ],
274
                (object) [
275
                    'propertyPath' => 'hash.strarray[1]',
276
                    'message'      => 'Boolean value found, but a string is required',
277
                ],
278
                (object) [
279
                    'propertyPath' => 'hash.boolarray[1]',
280
                    'message'      => 'String value found, but a boolean is required',
281
                ],
282
283
                (object) [
284
                    'propertyPath' => 'hash.datearray[1]',
285
                    'message'      => 'Invalid date-time "abc", expected format YYYY-MM-DDThh:mm:ssZ '.
286
                        'or YYYY-MM-DDThh:mm:ss+hh:mm',
287
                ],
288
                (object) [
289
                    'propertyPath' => 'hash.hasharray[1]',
290
                    'message'      => 'Double value found, but an object is required',
291
                ],
292
                (object) [
293
                    'propertyPath' => 'arrayhash[0].intarray[1]',
294
                    'message'      => 'String value found, but an integer is required',
295
                ],
296
                (object) [
297
                    'propertyPath' => 'arrayhash[0].strarray[1]',
298
                    'message'      => 'Boolean value found, but a string is required',
299
                ],
300
                (object) [
301
                    'propertyPath' => 'arrayhash[0].boolarray[1]',
302
                    'message'      => 'String value found, but a boolean is required',
303
                ],
304
                (object) [
305
                    'propertyPath' => 'arrayhash[0].datearray[1]',
306
                    'message'      => 'Invalid date-time "abc", expected format YYYY-MM-DDThh:mm:ssZ '.
307
                        'or YYYY-MM-DDThh:mm:ss+hh:mm',
308
                ],
309
                (object) [
310
                    'propertyPath' => 'arrayhash[0].hasharray[1]',
311
                    'message'      => 'Double value found, but an object is required',
312
                ]
313
            ],
314
            $client->getResults()
315
        );
316
    }
317
318
    /**
319
     * Fix date timezone
320
     *
321
     * @param object $data Request data
322
     * @return object
323
     */
324
    private function fixDateTimezone($data)
325
    {
326
        $converter = function (&$date) {
327
            $date = \DateTime::createFromFormat(self::DATE_FORMAT, $date)
328
                ->setTimezone(new \DateTimeZone(date_default_timezone_get()))
329
                ->format(self::DATE_FORMAT);
330
        };
331
332
        array_walk($data->datearray, $converter);
333
        array_walk($data->hash->datearray, $converter);
334
        array_walk($data->arrayhash[0]->datearray, $converter);
335
336
        return $data;
337
    }
338
339
340
    /**
341
     * Assert fixture data
342
     *
343
     * @param object $data Fixture data
344
     * @return void
345
     * @throws \PHPUnit_Framework_AssertionFailedError
346
     */
347
    private function assertFixtureData($data)
348
    {
349
        foreach ([
350
                     $data,
351
                     $data->hash,
352
                     $data->arrayhash[0],
353
                 ] as $data) {
354
            $this->assertInternalType('array', $data->intarray);
355
            foreach ($data->intarray as $value) {
356
                $this->assertInternalType('integer', $value);
357
            }
358
359
            $this->assertInternalType('array', $data->strarray);
360
            foreach ($data->strarray as $value) {
361
                $this->assertInternalType('string', $value);
362
            }
363
364
            $this->assertInternalType('array', $data->boolarray);
365
            foreach ($data->boolarray as $value) {
366
                $this->assertInternalType('boolean', $value);
367
            }
368
369
            $this->assertInternalType('array', $data->datearray);
370
            foreach ($data->datearray as $value) {
371
                $this->assertInternalType('string', $value);
372
                $this->assertInstanceOf(\DateTime::class, \DateTime::createFromFormat(self::DATE_FORMAT, $value));
373
            }
374
375
            $this->assertInternalType('array', $data->hasharray);
376
            foreach ($data->hasharray as $value) {
377
                $this->assertInternalType('object', $value);
378
            }
379
        }
380
    }
381
382
    /**
383
     * Assert item schema
384
     *
385
     * @param object $schema Item schema
386
     * @return void
387
     * @throws \PHPUnit_Framework_AssertionFailedError
388
     */
389
    private function assertItemSchema($schema)
390
    {
391
        foreach ([
392
                     $schema->properties,
393
                     $schema->properties->hash->properties,
394
                     $schema->properties->arrayhash->items->properties,
395
                 ] as $schema) {
396
            $this->assertEquals('array', $schema->intarray->type);
397
            $this->assertEquals('integer', $schema->intarray->items->type);
398
399
            $this->assertEquals('array', $schema->strarray->type);
400
            $this->assertEquals('string', $schema->strarray->items->type);
401
402
            $this->assertEquals('array', $schema->boolarray->type);
403
            $this->assertEquals('boolean', $schema->boolarray->items->type);
404
405
            $this->assertEquals('array', $schema->datearray->type);
406
            $this->assertEquals('string', $schema->datearray->items->type);
407
            $this->assertEquals('date-time', $schema->datearray->items->format);
408
409
            $this->assertEquals('array', $schema->hasharray->type);
410
            $this->assertEquals('object', $schema->hasharray->items->type);
411
        }
412
    }
413
}
414