Completed
Push — feature/wrong-form-translatabl... ( 311579...42bacf )
by Narcotic
08:56
created

tyTranslatable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 11
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 11
loc 11
rs 9.4285
cc 1
eloc 7
nc 1
nop 0
1
<?php
2
/**
3
 * TranslatableArrayControllerTest class file
4
 */
5
6
namespace Graviton\CoreBundle\Tests\Controller;
7
8
use Graviton\I18nBundle\DataFixtures\MongoDB\LoadLanguageData;
9
use Graviton\I18nBundle\DataFixtures\MongoDB\LoadMultiLanguageData;
10
use Graviton\TestBundle\Client;
11
use Graviton\TestBundle\Test\RestTestCase;
12
use GravitonDyn\TestCaseTranslatableArrayBundle\Document\TestCaseTranslatableArray;
13
use GravitonDyn\TestCaseTranslatableArrayBundle\DataFixtures\MongoDB\LoadTestCaseTranslatableArrayData;
14
use Symfony\Component\HttpFoundation\Response;
15
16
/**
17
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
18
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
19
 * @link     http://swisscom.ch
20
 */
21
class TranslatableArrayControllerTest extends RestTestCase
22
{
23
    /**
24
     * load fixtures
25
     *
26
     * @return void
27
     */
28
    public function setUp()
29
    {
30
        if (!class_exists(TestCaseTranslatableArray::class)) {
31
            $this->markTestSkipped(sprintf('%s definition is not loaded', TestCaseTranslatableArray::class));
32
        }
33
34
        $this->loadFixtures(
35
            [
36
                LoadLanguageData::class,
37
                LoadMultiLanguageData::class,
38
                LoadTestCaseTranslatableArrayData::class,
39
            ],
40
            null,
41
            'doctrine_mongodb'
42
        );
43
    }
44
45
    /**
46
     * Test item schema
47
     *
48
     * @return void
49
     */
50 View Code Duplication
    public function testItemSchema()
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...
51
    {
52
        $client = static::createRestClient();
53
        $this->getRequest($client, '/schema/testcase/translatable-array/item');
54
        $this->assertEquals(Response::HTTP_OK, $client->getResponse()->getStatusCode());
55
56
        $schema = $client->getResults();
57
        $this->assertEquals('object', $schema->type);
58
        $this->assertItemSchema($schema);
59
    }
60
61
    /**
62
     * Test collection schema
63
     *
64
     * @return void
65
     */
66 View Code Duplication
    public function testCollectionSchema()
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...
67
    {
68
        $client = static::createRestClient();
69
        $this->getRequest($client, '/schema/testcase/translatable-array/collection');
70
        $this->assertEquals(Response::HTTP_OK, $client->getResponse()->getStatusCode());
71
72
        $schema = $client->getResults();
73
        $this->assertEquals('array', $schema->type);
74
        $this->assertEquals('object', $schema->items->type);
75
        $this->assertItemSchema($schema->items);
76
    }
77
78
    /**
79
     * Test GET one method
80
     *
81
     * @return void
82
     */
83 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...
84
    {
85
        $client = static::createRestClient();
86
        $this->getRequest($client, '/testcase/translatable-array/testdata');
87
        $this->assertEquals(Response::HTTP_OK, $client->getResponse()->getStatusCode());
88
        $this->assertNotEmpty($client->getResults());
89
90
        $this->assertFixtureData($client->getResults());
91
    }
92
93
    /**
94
     * Test GET all method
95
     *
96
     * @return void
97
     */
98 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...
99
    {
100
        $client = static::createRestClient();
101
        $this->getRequest($client, '/testcase/translatable-array/');
102
        $this->assertEquals(Response::HTTP_OK, $client->getResponse()->getStatusCode());
103
        $this->assertCount(1, $client->getResults());
104
105
        $this->assertFixtureData($client->getResults()[0]);
106
    }
107
108
    /**
109
     * Test POST method
110
     *
111
     * @return void
112
     */
113
    public function testPostMethod()
114
    {
115
        $data = $this->getPostData();
116
117
        $client = static::createRestClient();
118
        $client->post('/testcase/translatable-array/', $data);
119
        $this->assertEquals(Response::HTTP_CREATED, $client->getResponse()->getStatusCode());
120
        $this->assertEmpty($client->getResults());
121
122
        $location = $client->getResponse()->headers->get('Location');
123
124
        $client = static::createRestClient();
125
        $this->getRequest($client, $location, ['en', 'de']);
126
        $this->assertEquals(Response::HTTP_OK, $client->getResponse()->getStatusCode());
127
128
        $result = $client->getResults();
129
        $this->assertNotNull($result->id);
130
        unset($result->id);
131
        $this->assertEquals($data, $result);
132
    }
133
134
    /**
135
     * Test PUT method
136
     *
137
     * @return void
138
     */
139
    public function testPutMethod()
140
    {
141
        $data = $this->getPostData();
142
143
        $client = static::createRestClient();
144
        $client->put('/testcase/translatable-array/testdata', $data);
145
        $this->assertEquals(Response::HTTP_NO_CONTENT, $client->getResponse()->getStatusCode());
146
        $this->assertEmpty($client->getResults());
147
148
        $client = static::createRestClient();
149
        $this->getRequest($client, '/testcase/translatable-array/testdata', ['en', 'de']);
150
        $this->assertEquals(Response::HTTP_OK, $client->getResponse()->getStatusCode());
151
152
        $result = $client->getResults();
153
        $this->assertNotNull($result->id);
154
        unset($result->id);
155
        $this->assertEquals($data, $result);
156
    }
157
158
    /**
159
     * Test validation
160
     *
161
     * @return void
162
     */
163
    public function testValidation()
164
    {
165
        $data = (object) [
166
            'id'    => 'testdata',
167
            'field' => (object) [
168
                'de' => 'No "en" translation',
169
            ],
170
            'array' => [
171
                'Invalid value',
172
                (object) ['Invalid' => 'value'],
173
            ],
174
            'deep'  => (object) [
175
                'deep' => [
176
                    (object) [
177
                        'field' => 'Invalid value',
178
                        'array' => 'Invalid value',
179
                    ],
180
                    (object) [
181
                        'field' => (object) [
182
                            'en' => 'Valid value',
183
                        ],
184
                        'array' => [
185
                            (object) [],
186
                            (object) [
187
                                'en' => 'Valid value',
188
                            ],
189
                        ],
190
                    ],
191
                ],
192
            ],
193
        ];
194
195
        $client = static::createRestClient();
196
        $client->put('/testcase/translatable-array/testdata', $data);
197
        $this->assertEquals(Response::HTTP_BAD_REQUEST, $client->getResponse()->getStatusCode());
198
        $this->assertEquals(
199
            [
200
                (object) [
201
                    'propertyPath' => 'children[field]',
202
                    'message'      => 'This value is not valid.'
203
                ],
204
                (object) [
205
                    'propertyPath' => 'children[array].children[0]',
206
                    'message'      => 'This value is not valid.',
207
                ],
208
                (object) [
209
                    'propertyPath' => 'children[array].children[1]',
210
                    'message'      => 'This value is not valid.',
211
                ],
212
213
                (object) [
214
                    'propertyPath' => 'children[deep].children[deep].children[0].children[field]',
215
                    'message'      => 'This value is not valid.',
216
                ],
217
                (object) [
218
                    'propertyPath' => 'children[deep].children[deep].children[0].children[array]',
219
                    'message'      => 'This value is not valid.',
220
                ],
221
222
                (object) [
223
                    'propertyPath' => 'children[deep].children[deep].children[1].children[array].children[0]',
224
                    'message'      => 'This value is not valid.',
225
                ],
226
            ],
227
            $client->getResults()
228
        );
229
    }
230
231
    /**
232
     * Assert fixture data
233
     *
234
     * @param object $data Fixture data
235
     * @return void
236
     * @throws \PHPUnit_Framework_AssertionFailedError
237
     */
238
    private function assertFixtureData($data)
239
    {
240
        $this->assertEquals(
241
            (object) [
242
                'id'    => 'testdata',
243
                'field' => (object) [
244
                    'en' => 'EN-1',
245
                ],
246
                'array' => [
247
                    (object) [
248
                        'en' => 'EN-2',
249
                    ],
250
                    (object) [
251
                        'en' => 'EN-3',
252
                    ],
253
                ],
254
                'deep'  => (object) [
255
                    'deep' => [
256
                        (object) [
257
                            'field' => (object) [
258
                                'en' => 'EN-4',
259
                            ],
260
                            'array' => [
261
                                (object) [
262
                                    'en' => 'EN-5',
263
                                ],
264
                                (object) [
265
                                    'en' => 'EN-6',
266
                                ],
267
                            ],
268
                        ],
269
                        (object) [
270
                            'field' => (object) [
271
                                'en' => 'EN-7',
272
                            ],
273
                            'array' => [
274
                                (object) [
275
                                    'en' => 'EN-8',
276
                                ],
277
                            ],
278
                        ],
279
                    ],
280
                ],
281
            ],
282
            $data
283
        );
284
    }
285
286
    /**
287
     * Assert item schema
288
     *
289
     * @param object $schema Item schema
290
     * @return void
291
     * @throws \PHPUnit_Framework_AssertionFailedError
292
     */
293
    private function assertItemSchema($schema)
294
    {
295
        foreach ([
296
                     $schema->properties,
297
                     $schema->properties->deep->properties->deep->items->properties,
298
                 ] as $schema) {
299
            $this->assertEquals('object', $schema->field->type);
300
            $this->assertEquals('string', $schema->field->properties->de->type);
301
            $this->assertEquals('string', $schema->field->properties->en->type);
302
            $this->assertEquals('string', $schema->field->properties->fr->type);
303
304
            $this->assertEquals('array', $schema->array->type);
305
            $this->assertEquals('object', $schema->array->items->type);
306
            $this->assertEquals('string', $schema->array->items->properties->de->type);
307
            $this->assertEquals('string', $schema->array->items->properties->en->type);
308
            $this->assertEquals('string', $schema->array->items->properties->fr->type);
309
        }
310
    }
311
312
    /**
313
     * Make a get request
314
     *
315
     * @param Client $client    HTTP client
316
     * @param string $url       URL
317
     * @param array  $languages Languages
318
     * @return void
319
     */
320
    private function getRequest(Client $client, $url, array $languages = ['en'])
321
    {
322
        $client->request('GET', $url, [], [], ['HTTP_ACCEPT_LANGUAGE' => implode(',', $languages)]);
323
    }
324
325
    /**
326
     * Get post JSON
327
     *
328
     * @return string
329
     */
330
    private function getPostData()
331
    {
332
        return (object) [
333
            'field' => (object) [
334
                'en' => 'EN-10',
335
                'de' => 'DE-10',
336
            ],
337
            'array' => [
338
                (object) [
339
                    'en' => 'EN-20',
340
                    'de' => 'DE-20',
341
                ],
342
            ],
343
            'deep'  => (object) [
344
                'deep' => [
345
                    (object) [
346
                        'field' => (object) [
347
                            'en' => 'EN-30',
348
                            'de' => 'DE-30',
349
                        ],
350
                        'array' => [
351
                            (object) [
352
                                'en' => 'EN-40',
353
                                'de' => 'DE-40',
354
                            ],
355
                            (object) [
356
                                'en' => 'EN-50',
357
                                'de' => 'DE-50',
358
                            ],
359
                            (object) [
360
                                'en' => 'EN-60',
361
                                'de' => 'DE-60',
362
                            ],
363
                        ],
364
                    ],
365
                    (object) [
366
                        'field' => (object) [
367
                            'en' => 'EN-70',
368
                            'de' => 'DE-70',
369
                        ],
370
                        'array' => [],
371
                    ],
372
                ]
373
            ]
374
        ];
375
    }
376
}
377