Completed
Push — master ( 6abda5...b08bdd )
by Narcotic
56:44 queued 45:19
created

testRecordOriginHandlingWithNoIdInPayload()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 22
rs 9.2
cc 1
eloc 13
nc 1
nop 0
1
<?php
2
/**
3
 * test for RecordOriginConstraint
4
 */
5
6
namespace Graviton\SchemaBundle\Tests\ConstraintBuilder;
7
8
use Graviton\TestBundle\Test\RestTestCase;
9
use Symfony\Component\HttpFoundation\Response;
10
11
/**
12
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
13
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
14
 * @link     http://swisscom.ch
15
 */
16
class RecordOriginConstraintTest extends RestTestCase
17
{
18
19
    /**
20
     * load fixtures
21
     *
22
     * @return void
23
     */
24
    public function setUp()
25
    {
26
        $this->loadFixtures(
27
            array(
28
                'GravitonDyn\CustomerBundle\DataFixtures\MongoDB\LoadCustomerData',
29
            ),
30
            null,
31
            'doctrine_mongodb'
32
        );
33
    }
34
35
    /**
36
     * test the validation of the RecordOriginConstraint
37
     *
38
     * @dataProvider createDataProvider
39
     *
40
     * @param object  $entity           The object to create
41
     * @param integer $expectedStatus   Header status code
42
     * @param string  $expectedResponse Post data result of post
43
     *
44
     * @return void
45
     */
46 View Code Duplication
    public function testRecordOriginHandlingOnCreate($entity, $expectedStatus, $expectedResponse)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
47
    {
48
        $client = static::createRestClient();
49
        $client->post('/person/customer/', $entity);
50
51
        $response = $client->getResponse();
52
        $this->assertEquals($expectedStatus, $response->getStatusCode());
53
        $this->assertEquals($expectedResponse, $client->getResults());
54
    }
55
56
    /**
57
     * tests for the case if user doesn't provide an id in payload.. constraint
58
     * must take the id from the request in that case.
59
     *
60
     * @return void
61
     */
62
    public function testRecordOriginHandlingWithNoIdInPayload()
63
    {
64
        $record = (object) [
65
            //'id' => '' - no, no id.. that's the point ;-)
66
            'customerNumber' => 555,
67
            'name' => 'Muster Hans'
68
        ];
69
70
        $client = static::createRestClient();
71
        $client->put('/person/customer/100', $record);
72
73
        $this->assertEquals(
74
            (object) [
75
                'propertyPath' => 'recordOrigin',
76
                'message' => 'Prohibited modification attempt on record with recordOrigin of core'
77
            ],
78
            $client->getResults()[0]
79
        );
80
81
        $this->assertEquals(1, count($client->getResults()));
82
        $this->assertEquals(Response::HTTP_BAD_REQUEST, $client->getResponse()->getStatusCode());
83
    }
84
85
    /**
86
     * Test the validation of the RecordOriginConstraint
87
     *
88
     * @param array   $fieldsToSet      Fields to be modified
89
     * @param integer $expectedStatus   Header status code
90
     * @param string  $expectedResponse Result to be returned
91
     * @param boolean $checkSavedEntry  To check db for correct result
92
     *
93
     * @dataProvider updateDataProvider
94
     *
95
     * @return void
96
     */
97
    public function testRecordOriginHandlingOnUpdate(
98
        $fieldsToSet,
99
        $expectedStatus,
100
        $expectedResponse,
101
        $checkSavedEntry = true
102
    ) {
103
        $client = static::createRestClient();
104
        $client->request('GET', '/person/customer/100');
105
        $result = $client->getResults();
106
107
        // apply changes
108
        foreach ($fieldsToSet as $key => $val) {
109
            $result->{$key} = $val;
110
        }
111
112
        $expectedObject = $result;
113
114
        $client = static::createRestClient();
115
        $client->put('/person/customer/100', $result);
116
117
        $response = $client->getResponse();
118
        $this->assertEquals($expectedStatus, $response->getStatusCode());
119
        $this->assertEquals($expectedResponse, $client->getResults());
120
121
        if ($checkSavedEntry) {
122
            // fetch it again and compare
123
            $client = static::createRestClient();
124
            $client->request('GET', '/person/customer/100');
125
            $this->assertEquals($expectedObject, $client->getResults());
126
        }
127
    }
128
129
    /**
130
     * Test the validation of the RecordOriginConstraint
131
     *
132
     * @param array   $ops              PATCH operations
133
     * @param integer $expectedStatus   Header status code
134
     * @param string  $expectedResponse Result to be returned
135
     *
136
     * @dataProvider patchDataProvider
137
     *
138
     * @return void
139
     */
140 View Code Duplication
    public function testRecordOriginHandlingOnPatch(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
141
        $ops,
142
        $expectedStatus,
143
        $expectedResponse
144
    ) {
145
        $client = static::createRestClient();
146
147
        $client->request('PATCH', '/person/customer/100', [], [], [], json_encode($ops));
148
149
        $response = $client->getResponse();
150
        $this->assertEquals($expectedStatus, $response->getStatusCode());
151
        $this->assertEquals($expectedResponse, $client->getResults());
152
    }
153
154
    /**
155
     * test to see if DELETE on a recordorigin: core is denied
156
     *
157
     * @return void
158
     */
159
    public function testDeleteHandling()
160
    {
161
        $client = static::createRestClient();
162
        $client->request('DELETE', '/person/customer/100');
163
        $response = $client->getResponse();
164
165
        $this->assertEquals(Response::HTTP_BAD_REQUEST, $response->getStatusCode());
166
        $this->assertEquals(
167
            (object) [
168
                'propertyPath' => 'recordOrigin',
169
                'message' => 'Must not be one of the following keywords: core'
170
            ],
171
            $client->getResults()
172
        );
173
    }
174
175
    /**
176
     * Data provider for POST related stuff
177
     *
178
     * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<string,array<string,null|array>>.

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...
179
     */
180
    public function createDataProvider()
181
    {
182
        $baseObj = [
183
            'customerNumber' => 888,
184
            'name' => 'Muster Hans'
185
        ];
186
187
        return [
188
189
            /*** STUFF THAT SHOULD BE ALLOWED ***/
190
191
            'create-allowed-object' => [
192
                'entity' => (object) array_merge(
193
                    $baseObj,
194
                    [
195
                        'recordOrigin' => 'hans'
196
                    ]
197
                ),
198
                'httpStatusExpected' => Response::HTTP_CREATED,
199
                'expectedResponse' => null
200
            ],
201
202
            /*** STUFF THAT SHOULD BE DENIED ***/
203
204
            'create-recordorigin-core' => [
205
                'entity' => (object) array_merge(
206
                    $baseObj,
207
                    [
208
                        'recordOrigin' => 'core'
209
                    ]
210
                ),
211
                'httpStatusExpected' => Response::HTTP_BAD_REQUEST,
212
                'expectedResponse' => [
213
                    (object) [
214
                        'propertyPath' => 'recordOrigin',
215
                        'message' => 'Creating documents with the recordOrigin field having a '.
216
                            'value of core is not permitted.'
217
                    ]
218
                ]
219
            ]
220
        ];
221
    }
222
223
    /**
224
     * Data provider for PUT related stuff
225
     *
226
     * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<string,array>.

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...
227
     */
228
    public function updateDataProvider()
229
    {
230
        $expectedErrorOutput = [
231
            (object) [
232
                'propertyPath' => 'recordOrigin',
233
                'message' => 'Prohibited modification attempt on record with recordOrigin of core'
234
            ]
235
        ];
236
237
        return [
238
239
            /*** STUFF THAT SHOULD BE ALLOWED ***/
240
            'create-allowed-object' => [
241
                'fieldsToSet' => [
242
                    'addedField' => (object) [
243
                        'some' => 'property',
244
                        'another' => 'one'
245
                    ]
246
                ],
247
                'httpStatusExpected' => Response::HTTP_NO_CONTENT,
248
                'expectedResponse' => null
249
            ],
250
            'subproperty-modification' => [
251
                'fieldsToSet' => [
252
                    'someObject' => (object) [
253
                        'oneField' => 'value',
254
                        'twoField' => 'twofield'
255
                    ]
256
                ],
257
                'httpStatusExpected' => Response::HTTP_NO_CONTENT,
258
                'expectedResponse' => null
259
            ],
260
261
            /*** STUFF THAT NEEDS TO BE DENIED ***/
262
            'denied-subproperty-modification' => [
263
                'fieldsToSet' => [
264
                    'someObject' => (object) [
265
                        'oneField' => 'changed-value'
266
                    ]
267
                ],
268
                'httpStatusExpected' => Response::HTTP_BAD_REQUEST,
269
                'expectedResponse' => $expectedErrorOutput,
270
                'checkSavedEntry' => false
271
            ],
272
            'denied-try-change-recordorigin' => [
273
                'fieldsToSet' => [
274
                    'recordOrigin' => 'hans'
275
                ],
276
                'httpStatusExpected' => Response::HTTP_BAD_REQUEST,
277
                'expectedResponse' => $expectedErrorOutput,
278
                'checkSavedEntry' => false
279
            ],
280
        ];
281
    }
282
283
    /**
284
     * Data provider for PATCH related stuff
285
     *
286
     * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<string,array>.

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...
287
     */
288
    public function patchDataProvider()
289
    {
290
        $expectedErrorOutput = [
291
            (object) [
292
                'propertyPath' => 'recordOrigin',
293
                'message' => 'Prohibited modification attempt on record with recordOrigin of core'
294
            ]
295
        ];
296
297
        return [
298
299
            /*** STUFF THAT SHOULD BE ALLOWED ***/
300
301
            'patch-allowed-attribute' => [
302
                'ops' => [
303
                    [
304
                        'op' => 'add',
305
                        'path' => '/someObject/twoField',
306
                        'value' => 'myValue'
307
                    ]
308
                ],
309
                'httpStatusExpected' => Response::HTTP_OK,
310
                'expectedResponse' => null
311
            ],
312
            'patch-add-object-data' => [
313
                'ops' => [
314
                    [
315
                        'op' => 'add',
316
                        'path' => '/addedField',
317
                        'value' => [
318
                            'someProperty' => 'someValue',
319
                            'anotherOne' => 'oneMore'
320
                        ]
321
                    ]
322
                ],
323
                'httpStatusExpected' => Response::HTTP_OK,
324
                'expectedResponse' => null
325
            ],
326
327
            /*** STUFF THAT NEEDS TO BE DENIED ***/
328
            'patch-denied-subproperty' => [
329
                'ops' => [
330
                    [
331
                        'op' => 'add',
332
                        'path' => '/someObject/oneField',
333
                        'value' => 'myValue'
334
                    ]
335
                ],
336
                'httpStatusExpected' => Response::HTTP_BAD_REQUEST,
337
                'expectedResponse' => $expectedErrorOutput
338
            ],
339
            'patch-denied-recordorigin-change' => [
340
                'ops' => [
341
                    [
342
                        'op' => 'replace',
343
                        'path' => '/recordOrigin',
344
                        'value' => 'hans'
345
                    ]
346
                ],
347
                'httpStatusExpected' => Response::HTTP_BAD_REQUEST,
348
                'expectedResponse' => $expectedErrorOutput
349
            ],
350
351
        ];
352
    }
353
}
354