Completed
Push — feature/other-validation ( 3b2e71...c4ebdf )
by Narcotic
184:05 queued 119:47
created

testPostWithoutFieldInOptionalHash()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 71
Code Lines 47

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 71
rs 9.1369
cc 1
eloc 47
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
 * RequiredHashControllerTest class file
4
 */
5
6
namespace Graviton\CoreBundle\Tests\Controller;
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 RequiredHashControllerTest extends RestTestCase
17
{
18
    /**
19
     * load fixtures
20
     *
21
     * @return void
22
     */
23
    public function setUp()
24
    {
25
        if (!class_exists('GravitonDyn\TestCaseRequiredHashBundle\DataFixtures\MongoDB\LoadTestCaseRequiredHashData')) {
26
            $this->markTestSkipped('TestCaseRequiredHashData definition is not loaded');
27
        }
28
29
        $this->loadFixtures(
30
            ['GravitonDyn\TestCaseRequiredHashBundle\DataFixtures\MongoDB\LoadTestCaseRequiredHashData'],
31
            null,
32
            'doctrine_mongodb'
33
        );
34
    }
35
36
    /**
37
     * Test POST method with optional hash
38
     *
39
     * @return void
40
     */
41
    public function testPostWithOptionalHash()
42
    {
43
        $data = [
44
            'name'         => __METHOD__,
45
            'optionalHash' => [
46
                'name'     => 'abc',
47
                'value'    => 123,
48
                'optional' => '2015-09-03T12:00:00+0000',
49
50
                'optionalSubHash' => [
51
                    'name'     => 'abc',
52
                    'value'    => 123,
53
                    'optional' => '2015-09-03T12:00:00+0000',
54
                ],
55
                'requiredSubHash' => [
56
                    'name'     => 'abc',
57
                    'value'    => 123,
58
                    'optional' => '2015-09-03T12:00:00+0000',
59
                ],
60
            ],
61
            'requiredHash' => [
62
                'name'     => 'abc',
63
                'value'    => 123,
64
                'optional' => '2015-09-03T12:00:00+0000',
65
66
                'optionalSubHash' => [
67
                    'name'     => 'abc',
68
                    'value'    => 123,
69
                    'optional' => '2015-09-03T12:00:00+0000',
70
                ],
71
                'requiredSubHash' => [
72
                    'name'     => 'abc',
73
                    'value'    => 123,
74
                    'optional' => '2015-09-03T12:00:00+0000',
75
                ],
76
            ],
77
        ];
78
79
        $client = static::createRestClient();
80
        $client->post('/testcase/requiredhash/', $data);
81
        $this->assertEquals(Response::HTTP_CREATED, $client->getResponse()->getStatusCode());
82
        $this->assertEmpty($client->getResults());
83
    }
84
85
    /**
86
     * Test POST method without optional hash
87
     *
88
     * @return void
89
     */
90
    public function testPostWithoutOptionalHash()
91
    {
92
        $data = [
93
            'name'         => __METHOD__,
94
            'requiredHash' => [
95
                'name'     => 'abc',
96
                'value'    => 123,
97
                'optional' => '2015-09-03T12:00:00+0000',
98
99
                'requiredSubHash' => [
100
                    'name'     => 'abc',
101
                    'value'    => 123,
102
                    'optional' => '2015-09-03T12:00:00+0000',
103
                ],
104
            ],
105
        ];
106
107
        $client = static::createRestClient();
108
        $client->post('/testcase/requiredhash/', $data);
109
        $this->assertEquals(Response::HTTP_CREATED, $client->getResponse()->getStatusCode());
110
        $this->assertEmpty($client->getResults());
111
    }
112
113
    /**
114
     * Test POST method without field in optional hash
115
     *
116
     * @return void
117
     */
118
    public function testPostWithoutFieldInOptionalHash()
119
    {
120
        $data = [
121
            'name'         => __METHOD__,
122
            'optionalHash' => [
123
                'name'     => 'abc',
124
            ],
125
            'requiredHash' => [
126
                'name'     => 'abc',
127
                'value'    => 123,
128
                'optional' => '2015-09-03T12:00:00+0000',
129
130
                'optionalSubHash' => [
131
                    'name'     => 'abc',
132
                ],
133
                'requiredSubHash' => [
134
                    'name'     => 'abc',
135
                    'value'    => 123,
136
                    'optional' => '2015-09-03T12:00:00+0000',
137
                ],
138
            ],
139
        ];
140
141
        $client = static::createRestClient();
142
        $client->post('/testcase/requiredhash/', $data);
143
        $this->assertEquals(Response::HTTP_BAD_REQUEST, $client->getResponse()->getStatusCode());
144
        $this->assertEquals(
145
            [
146
                (object) [
147
                    'propertyPath'  => 'optionalHash.value',
148
                    'message'       => 'The property value is required',
149
                ],
150
                (object) [
151
                    'propertyPath'  => 'optionalHash.requiredSubHash',
152
                    'message'       => 'The property requiredSubHash is required',
153
                ],
154
                (object) [
155
                    'propertyPath'  => 'requiredHash.optionalSubHash.value',
156
                    'message'       => 'The property value is required',
157
                ],
158
            ],
159
            $client->getResults()
160
        );
161
162
        // add requiredSubHash and check deeper properties
163
        $data['optionalHash']['requiredSubHash'] = (object) [];
164
        $client = static::createRestClient();
165
        $client->post('/testcase/requiredhash/', $data);
166
        $this->assertEquals(Response::HTTP_BAD_REQUEST, $client->getResponse()->getStatusCode());
167
        $this->assertEquals(
168
            [
169
                (object) [
170
                    'propertyPath'  => 'optionalHash.value',
171
                    'message'       => 'The property value is required',
172
                ],
173
                (object) [
174
                    'propertyPath'  => 'optionalHash.requiredSubHash.name',
175
                    'message'       => 'The property name is required',
176
                ],
177
                (object) [
178
                    'propertyPath'  => 'optionalHash.requiredSubHash.value',
179
                    'message'       => 'The property value is required',
180
                ],
181
                (object) [
182
                    'propertyPath'  => 'requiredHash.optionalSubHash.value',
183
                    'message'       => 'The property value is required',
184
                ]
185
            ],
186
            $client->getResults()
187
        );
188
    }
189
190
    /**
191
     * Test POST method without required hash
192
     *
193
     * @return void
194
     */
195
    public function testPostWithoutRequiredHash()
196
    {
197
        $data = [
198
            'name' => __METHOD__,
199
            'requiredHash' => (object) []
200
        ];
201
202
        $client = static::createRestClient();
203
        $client->post('/testcase/requiredhash/', $data);
204
        $this->assertEquals(Response::HTTP_BAD_REQUEST, $client->getResponse()->getStatusCode());
205
        $this->assertEquals(
206
            [
207
                (object) [
208
                    'propertyPath'  => 'requiredHash.name',
209
                    'message'       => 'The property name is required',
210
                ],
211
                (object) [
212
                    'propertyPath'  => 'requiredHash.value',
213
                    'message'       => 'The property value is required',
214
                ],
215
                (object) [
216
                    'propertyPath'  => 'requiredHash.requiredSubHash',
217
                    'message'       => 'The property requiredSubHash is required',
218
                ]
219
            ],
220
            $client->getResults()
221
        );
222
    }
223
224
    /**
225
     * Test POST method with empty optional hash
226
     *
227
     * @return void
228
     */
229
    public function testPostWithEmptyOptionalHash()
230
    {
231
        $data = [
232
            'name'         => __METHOD__,
233
            'optionalHash' => [
234
                'name'     => null,
235
                'value'    => null,
236
                'optional' => null,
237
            ],
238
            'requiredHash' => [
239
                'name'     => 'abc',
240
                'value'    => 123,
241
                'optional' => '2015-09-03T12:00:00+0000',
242
            ],
243
        ];
244
245
        $client = static::createRestClient();
246
        $client->post('/testcase/requiredhash/', $data);
247
        $this->assertEquals(Response::HTTP_BAD_REQUEST, $client->getResponse()->getStatusCode());
248
        $this->assertEquals(
249
            [
250
                (object) [
251
                    'propertyPath'  => 'optionalHash.requiredSubHash',
252
                    'message'       => 'The property requiredSubHash is required',
253
                ],
254
                (object) [
255
                    'propertyPath'  => 'optionalHash.name',
256
                    'message'       => 'NULL value found, but a string is required',
257
                ],
258
                (object) [
259
                    'propertyPath'  => 'optionalHash.value',
260
                    'message'       => 'NULL value found, but an integer is required',
261
                ],
262
                (object) [
263
                    'propertyPath'  => 'requiredHash.requiredSubHash',
264
                    'message'       => 'The property requiredSubHash is required',
265
                ]
266
            ],
267
            $client->getResults()
268
        );
269
    }
270
271
    /**
272
     * Test POST method with empty required hash
273
     *
274
     * @return void
275
     */
276
    public function testPostWithEmptyRequiredHash()
277
    {
278
        $data = [
279
            'name'         => __METHOD__,
280
            'requiredHash' => [
281
                'name'     => null,
282
                'value'    => null,
283
                'optional' => null,
284
            ],
285
        ];
286
287
        $client = static::createRestClient();
288
        $client->post('/testcase/requiredhash/', $data);
289
        $this->assertEquals(Response::HTTP_BAD_REQUEST, $client->getResponse()->getStatusCode());
290
        $this->assertEquals(
291
            [
292
                (object) [
293
                    'propertyPath'  => 'requiredHash.requiredSubHash',
294
                    'message'       => 'The property requiredSubHash is required',
295
                ],
296
                (object) [
297
                    'propertyPath'  => 'requiredHash.name',
298
                    'message'       => 'NULL value found, but a string is required',
299
                ],
300
                (object) [
301
                    'propertyPath'  => 'requiredHash.value',
302
                    'message'       => 'NULL value found, but an integer is required',
303
                ]
304
            ],
305
            $client->getResults()
306
        );
307
    }
308
309
    /**
310
     * check that schema does not contain realId artefacts
311
     *
312
     * @return void
313
     */
314 View Code Duplication
    public function testCollectionHasNoRealId()
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...
315
    {
316
        $client = static::createRestclient();
317
        $client->request('GET', '/schema/testcase/requiredhash/collection');
318
319
        $response = $client->getResponse();
320
321
        $this->assertEquals(Response::HTTP_OK, $response->getStatusCode());
322
        $this->assertNotContains('realId', $response->getContent());
323
    }
324
}
325