Completed
Push — feature/EVO-7278-security-and-... ( ae4d63...67cd5b )
by
unknown
26:50 queued 21:17
created

NullExtrefControllerTest::testPostMethod()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 17
nc 2
nop 2
1
<?php
2
/**
3
 * NullExtrefControllerTest 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 NullExtrefControllerTest extends RestTestCase
17
{
18
    /**
19
     * load fixtures
20
     *
21
     * @return void
22
     */
23
    public function setUp()
24
    {
25
        if (!class_exists('GravitonDyn\TestCaseNullExtrefBundle\DataFixtures\MongoDB\LoadTestCaseNullExtrefData')) {
26
            $this->markTestSkipped('TestCaseNullExtref definition is not loaded');
27
        }
28
29
        $this->loadFixtures(
30
            ['GravitonDyn\TestCaseNullExtrefBundle\DataFixtures\MongoDB\LoadTestCaseNullExtrefData'],
31
            null,
32
            'doctrine_mongodb'
33
        );
34
    }
35
36
    /**
37
     * Test GET one method
38
     *
39
     * @return void
40
     */
41 View Code Duplication
    public function testCheckGetOne()
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...
42
    {
43
        $client = static::createRestClient();
44
        $client->request('GET', '/testcase/nullextref/testdata');
45
        $this->assertEquals(Response::HTTP_OK, $client->getResponse()->getStatusCode());
46
        $this->assertNotEmpty($client->getResults());
47
48
        $data = $client->getResults();
49
        $expectedRefVal = 'http://localhost/core/app/admin';
50
51
        $this->assertFalse(isset($data->optionalExtref->{'$ref'}));
52
        $this->assertTrue(isset($data->requiredExtref->{'$ref'}));
53
        $this->assertEquals($expectedRefVal, $data->requiredExtref->{'$ref'});
54
55
        $this->assertFalse(isset($data->optionalExtrefArray[0]->{'$ref'}));
56
        $this->assertTrue(isset($data->requiredExtrefArray[0]->{'$ref'}));
57
        $this->assertEquals($expectedRefVal, $data->requiredExtrefArray[0]->{'$ref'});
58
59
        $this->assertFalse(isset($data->optionalExtrefDeep[0]->deep[0]->deep->deep[0]->{'$ref'}));
60
        $this->assertTrue(isset($data->requiredExtrefDeep[0]->deep[0]->deep->deep[0]->{'$ref'}));
61
        $this->assertEquals($expectedRefVal, $data->requiredExtrefDeep[0]->deep[0]->deep->deep[0]->{'$ref'});
62
    }
63
64
    /**
65
     * Test GET all method
66
     *
67
     * @return void
68
     */
69 View Code Duplication
    public function testCheckGetAll()
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...
70
    {
71
        $client = static::createRestClient();
72
        $client->request('GET', '/testcase/nullextref/');
73
        $this->assertEquals(Response::HTTP_OK, $client->getResponse()->getStatusCode());
74
        $this->assertCount(1, $client->getResults());
75
76
        $data = $client->getResults()[0];
77
        $expectedRefVal = 'http://localhost/core/app/admin';
78
79
        $this->assertFalse(isset($data->optionalExtref->{'$ref'}));
80
        $this->assertTrue(isset($data->requiredExtref->{'$ref'}));
81
        $this->assertEquals($expectedRefVal, $data->requiredExtref->{'$ref'});
82
83
        $this->assertFalse(isset($data->optionalExtrefArray[0]->{'$ref'}));
84
        $this->assertTrue(isset($data->requiredExtrefArray[0]->{'$ref'}));
85
        $this->assertEquals($expectedRefVal, $data->requiredExtrefArray[0]->{'$ref'});
86
87
        $this->assertFalse(isset($data->optionalExtrefDeep[0]->deep[0]->deep->deep[0]->{'$ref'}));
88
        $this->assertTrue(isset($data->requiredExtrefDeep[0]->deep[0]->deep->deep[0]->{'$ref'}));
89
        $this->assertEquals($expectedRefVal, $data->requiredExtrefDeep[0]->deep[0]->deep->deep[0]->{'$ref'});
90
    }
91
92
    /**
93
     * Test POST method
94
     *
95
     * @param array $data        Data to POST
96
     * @param array $compareData optional data to compare return to
0 ignored issues
show
Documentation introduced by
Should the type for parameter $compareData not be null|array? Also, consider making the array more specific, something like array<String>, or String[].

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive. In addition it looks for parameters that have the generic type array and suggests a stricter type like array<String>.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
97
     *
98
     * @return void
99
     * @dataProvider dataTestData
100
     */
101
    public function testPostMethod(array $data, array $compareData = null)
102
    {
103
        $client = static::createRestClient();
104
        $client->post('/testcase/nullextref/', $data);
105
        $this->assertEquals(Response::HTTP_CREATED, $client->getResponse()->getStatusCode());
106
        $this->assertEmpty($client->getResults());
107
108
        $location = $client->getResponse()->headers->get('Location');
109
110
        $client = static::createRestClient();
111
        $client->request('GET', $location);
112
        $this->assertEquals(Response::HTTP_OK, $client->getResponse()->getStatusCode());
113
114
        $result = $client->getResults();
115
        $this->assertNotNull($result->id);
116
        unset($result->id);
117
118
        if (is_null($compareData)) {
119
            $compareData = $data;
120
        }
121
122
        $this->assertJsonStringEqualsJsonString(
123
            json_encode($this->removeNullRefs($compareData)),
124
            json_encode($client->getResults())
125
        );
126
    }
127
128
    /**
129
     * Test PUT method
130
     *
131
     * @param array $data        Data to PUT
132
     * @param array $compareData optional data to compare return to
0 ignored issues
show
Documentation introduced by
Should the type for parameter $compareData not be null|array? Also, consider making the array more specific, something like array<String>, or String[].

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive. In addition it looks for parameters that have the generic type array and suggests a stricter type like array<String>.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
133
     *
134
     * @return void
135
     * @dataProvider dataTestData
136
     */
137
    public function testPutMethod(array $data, array $compareData = null)
138
    {
139
        $data['id'] = 'testdata';
140
141
        $client = static::createRestClient();
142
        $client->put('/testcase/nullextref/testdata', $data);
143
        $this->assertEquals(Response::HTTP_NO_CONTENT, $client->getResponse()->getStatusCode());
144
        $this->assertEmpty($client->getResults());
145
146
        $client = static::createRestClient();
147
        $client->request('GET', '/testcase/nullextref/testdata');
148
        $this->assertEquals(Response::HTTP_OK, $client->getResponse()->getStatusCode());
149
150
        if (is_null($compareData)) {
151
            $compareData = $data;
152
        } else {
153
            $compareData['id'] = $data['id'];
154
        }
155
156
        $this->assertJsonStringEqualsJsonString(
157
            json_encode($this->removeNullRefs($compareData)),
158
            json_encode($client->getResults())
159
        );
160
    }
161
162
    /**
163
     * Remove null $ref recursively
164
     *
165
     * @param array $data Data to process
166
     * @return array|object
167
     */
168
    private function removeNullRefs(array $data)
169
    {
170
        foreach ($data as $key => $value) {
171
            if ($key === '$ref' && $value === null) {
172
                unset($data[$key]);
173
                if ($data === []) {
174
                    return (object) $data;
175
                }
176
            } elseif (is_array($value)) {
177
                $data[$key] = $this->removeNullRefs($value);
178
            }
179
        }
180
        return $data;
181
    }
182
183
    /**
184
     * Data for tests
185
     *
186
     * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<string,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...
187
     */
188
    public function dataTestData()
189
    {
190
        return [
191
            'empty refs' => [
192
                [
193
                    'optionalExtref'      => ['$ref' => null],
194
                    'requiredExtref'      => ['$ref' => 'http://localhost/core/app/admin'],
195
                    'optionalExtrefArray' => [
196
                        ['$ref' => null],
197
                    ],
198
                    'requiredExtrefArray' => [
199
                        ['$ref' => 'http://localhost/core/app/admin'],
200
                    ],
201
                    'optionalExtrefDeep'  => [
202
                        [
203
                            'deep' => [
204
                                [
205
                                    'deep' => [
206
                                        'deep' => [
207
                                            ['$ref' => null],
208
                                        ],
209
                                    ],
210
                                ],
211
                            ],
212
                        ],
213
                    ],
214
                    'requiredExtrefDeep'  => [
215
                        [
216
                            'deep' => [
217
                                [
218
                                    'deep' => [
219
                                        'deep' => [
220
                                            ['$ref' => 'http://localhost/core/app/admin'],
221
                                        ],
222
                                    ],
223
                                ],
224
                            ],
225
                        ],
226
                    ],
227
                ],
228
                [
229
                    'requiredExtref'      => ['$ref' => 'http://localhost/core/app/admin'],
230
                    'optionalExtrefArray' => [],
231
                    'requiredExtrefArray' => [
232
                        ['$ref' => 'http://localhost/core/app/admin'],
233
                    ],
234
                    'optionalExtrefDeep'  => [
235
                        [
236
                            'deep' => [
237
                                [
238
                                    'deep' => [
239
                                        'deep' => [],
240
                                    ],
241
                                ],
242
                            ],
243
                        ],
244
                    ],
245
                    'requiredExtrefDeep'  => [
246
                        [
247
                            'deep' => [
248
                                [
249
                                    'deep' => [
250
                                        'deep' => [
251
                                            ['$ref' => 'http://localhost/core/app/admin'],
252
                                        ],
253
                                    ],
254
                                ],
255
                            ],
256
                        ],
257
                    ],
258
                ],
259
            ],
260
            'filled refs' => [
261
                [
262
                    'optionalExtref'      => ['$ref' => 'http://localhost/core/app/tablet'],
263
                    'requiredExtref'      => ['$ref' => 'http://localhost/core/app/admin'],
264
                    'optionalExtrefArray' => [
265
                        ['$ref' => 'http://localhost/core/app/tablet'],
266
                    ],
267
                    'requiredExtrefArray' => [
268
                        ['$ref' => 'http://localhost/core/app/admin'],
269
                    ],
270
                    'optionalExtrefDeep'  => [
271
                        [
272
                            'deep' => [
273
                                [
274
                                    'deep' => [
275
                                        'deep' => [
276
                                            ['$ref' => 'http://localhost/core/app/tablet'],
277
                                        ],
278
                                    ],
279
                                ],
280
                            ],
281
                        ],
282
                    ],
283
                    'requiredExtrefDeep'  => [
284
                        [
285
                            'deep' => [
286
                                [
287
                                    'deep' => [
288
                                        'deep' => [
289
                                            ['$ref' => 'http://localhost/core/app/admin'],
290
                                        ],
291
                                    ],
292
                                ],
293
                            ],
294
                        ],
295
                    ],
296
                ]
297
            ],
298
        ];
299
    }
300
}
301