Completed
Push — master ( aff5fa...b0939b )
by Łukasz
19:47
created

SelectionTest::testToFieldDefinitionMultiple()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 64

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 64
rs 8.7853
c 0
b 0
f 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
/**
4
 * File containing the SelectionTest class.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 */
9
namespace eZ\Publish\Core\Persistence\Legacy\Tests\Content\FieldValue\Converter;
10
11
use eZ\Publish\API\Repository\LanguageService;
12
use eZ\Publish\Core\FieldType\FieldSettings;
13
use eZ\Publish\SPI\Persistence\Content\FieldValue;
14
use eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldValue;
15
use eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldDefinition;
16
use eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter\SelectionConverter;
17
use eZ\Publish\SPI\Persistence\Content\Type\FieldDefinition as PersistenceFieldDefinition;
18
use eZ\Publish\SPI\Persistence\Content\FieldTypeConstraints;
19
use PHPUnit\Framework\TestCase;
20
21
/**
22
 * Test case for Selection converter in Legacy storage.
23
 */
24
class SelectionTest extends TestCase
25
{
26
    /**
27
     * @var \eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter\SelectionConverter
28
     */
29
    protected $converter;
30
31
    protected function setUp()
32
    {
33
        parent::setUp();
34
        $languageServiceMock = $this->createMock(LanguageService::class);
35
36
        $this->converter = new SelectionConverter($languageServiceMock);
37
    }
38
39
    /**
40
     * @group fieldType
41
     * @group selection
42
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter\SelectionConverter::toStorageValue
43
     */
44 View Code Duplication
    public function testToStorageValue()
45
    {
46
        $fieldValue = new FieldValue();
47
        $fieldValue->data = array(1, 3);
48
        $fieldValue->sortKey = '1-3';
49
50
        $expectedStorageFieldValue = new StorageFieldValue();
51
        $expectedStorageFieldValue->dataText = '1-3';
52
        $expectedStorageFieldValue->sortKeyString = '1-3';
53
54
        $actualStorageFieldValue = new StorageFieldValue();
55
56
        $this->converter->toStorageValue($fieldValue, $actualStorageFieldValue);
57
58
        $this->assertEquals(
59
            $expectedStorageFieldValue,
60
            $actualStorageFieldValue
61
        );
62
    }
63
64
    /**
65
     * @group fieldType
66
     * @group selection
67
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter\SelectionConverter::toStorageValue
68
     */
69 View Code Duplication
    public function testToStorageValueEmpty()
70
    {
71
        $fieldValue = new FieldValue();
72
        $fieldValue->data = array();
73
        $fieldValue->sortKey = '';
74
75
        $expectedStorageFieldValue = new StorageFieldValue();
76
        $expectedStorageFieldValue->dataText = '';
77
        $expectedStorageFieldValue->sortKeyString = '';
78
79
        $actualStorageFieldValue = new StorageFieldValue();
80
81
        $this->converter->toStorageValue($fieldValue, $actualStorageFieldValue);
82
83
        $this->assertEquals(
84
            $expectedStorageFieldValue,
85
            $actualStorageFieldValue
86
        );
87
    }
88
89
    /**
90
     * @group fieldType
91
     * @group selection
92
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter\SelectionConverter::toFieldValue
93
     */
94 View Code Duplication
    public function testToFieldValue()
95
    {
96
        $storageFieldValue = new StorageFieldValue();
97
        $storageFieldValue->dataText = '1-3';
98
        $storageFieldValue->sortKeyString = '1-3';
99
100
        $expectedFieldValue = new FieldValue();
101
        $expectedFieldValue->data = array(1, 3);
102
        $expectedFieldValue->sortKey = '1-3';
103
104
        $actualFieldValue = new FieldValue();
105
106
        $this->converter->toFieldValue($storageFieldValue, $actualFieldValue);
107
108
        $this->assertEquals(
109
            $expectedFieldValue,
110
            $actualFieldValue
111
        );
112
    }
113
114
    /**
115
     * @group fieldType
116
     * @group selection
117
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter\SelectionConverter::toFieldValue
118
     */
119 View Code Duplication
    public function testToFieldValueEmpty()
120
    {
121
        $storageFieldValue = new StorageFieldValue();
122
        $storageFieldValue->dataText = '';
123
        $storageFieldValue->sortKeyString = '';
124
125
        $expectedFieldValue = new FieldValue();
126
        $expectedFieldValue->data = array();
127
        $expectedFieldValue->sortKey = '';
128
129
        $actualFieldValue = new FieldValue();
130
131
        $this->converter->toFieldValue($storageFieldValue, $actualFieldValue);
132
133
        $this->assertEquals(
134
            $expectedFieldValue,
135
            $actualFieldValue
136
        );
137
    }
138
139
    /**
140
     * @group fieldType
141
     * @group selection
142
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter\SelectionConverter::toStorageFieldDefinition
143
     */
144 View Code Duplication
    public function testToStorageFieldDefinitionMultiple()
145
    {
146
        $fieldDefinition = new PersistenceFieldDefinition(
147
            array(
148
                'fieldTypeConstraints' => new FieldTypeConstraints(
149
                    array(
150
                        'fieldSettings' => new FieldSettings(
151
                            array(
152
                                'isMultiple' => true,
153
                                'options' => array(
154
                                    0 => 'First',
155
                                    1 => 'Second',
156
                                    2 => 'Third',
157
                                ),
158
                            )
159
                        ),
160
                    )
161
                ),
162
            )
163
        );
164
165
        $expectedStorageFieldDefinition = new StorageFieldDefinition();
166
        $expectedStorageFieldDefinition->dataInt1 = 1;
167
        $expectedStorageFieldDefinition->dataText5 = <<<EOT
168
<?xml version="1.0" encoding="utf-8"?>
169
<ezselection><options><option id="0" name="First"/><option id="1" name="Second"/><option id="2" name="Third"/></options></ezselection>
170
171
EOT;
172
173
        $actualStorageFieldDefinition = new StorageFieldDefinition();
174
175
        $this->converter->toStorageFieldDefinition($fieldDefinition, $actualStorageFieldDefinition);
176
177
        $this->assertEquals($expectedStorageFieldDefinition, $actualStorageFieldDefinition);
178
    }
179
180
    /**
181
     * @group fieldType
182
     * @group selection
183
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter\SelectionConverter::toStorageFieldDefinition
184
     */
185
    public function testToStorageFieldDefinitionSingle()
186
    {
187
        $fieldDefinition = new PersistenceFieldDefinition(
188
            array(
189
                'fieldTypeConstraints' => new FieldTypeConstraints(
190
                    array(
191
                        'fieldSettings' => new FieldSettings(
192
                            array(
193
                                'isMultiple' => false,
194
                                'options' => array(
195
                                    0 => 'First',
196
                                ),
197
                            )
198
                        ),
199
                    )
200
                ),
201
            )
202
        );
203
204
        $expectedStorageFieldDefinition = new StorageFieldDefinition();
205
        $expectedStorageFieldDefinition->dataInt1 = 0;
206
        $expectedStorageFieldDefinition->dataText5 = <<<EOT
207
<?xml version="1.0" encoding="utf-8"?>
208
<ezselection><options><option id="0" name="First"/></options></ezselection>
209
210
EOT;
211
212
        $actualStorageFieldDefinition = new StorageFieldDefinition();
213
214
        $this->converter->toStorageFieldDefinition($fieldDefinition, $actualStorageFieldDefinition);
215
216
        $this->assertEquals($expectedStorageFieldDefinition, $actualStorageFieldDefinition);
217
    }
218
219
    /**
220
     * @group fieldType
221
     * @group selection
222
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter\SelectionConverter::toFieldDefinition
223
     */
224
    public function testToFieldDefinitionMultiple()
225
    {
226
        $storageFieldDefinition = new StorageFieldDefinition();
227
        $storageFieldDefinition->dataInt1 = 1;
228
        $storageFieldDefinition->dataText5 = <<<EOT
229
<?xml version="1.0" encoding="utf-8"?>
230
<ezselection>
231
  <options>
232
    <option id="0" name="First"/>
233
    <option id="1" name="Second"/>
234
    <option id="2" name="Third"/>
235
  </options>
236
</ezselection>
237
EOT;
238
239
        $expectedFieldDefinition = new PersistenceFieldDefinition(
240
            array(
241
                'name' => array(
242
                    'eng-GB' => 'test name',
243
                ),
244
                'mainLanguageCode' => 'eng-GB',
245
                'fieldTypeConstraints' => new FieldTypeConstraints(
246
                    array(
247
                        'fieldSettings' => new FieldSettings(
248
                            array(
249
                                'isMultiple' => true,
250
                                'options' => array(
251
                                    0 => 'First',
252
                                    1 => 'Second',
253
                                    2 => 'Third',
254
                                ),
255
                                'multilingualOptions' => array(
256
                                    'eng-GB' => array(
257
                                        0 => 'First',
258
                                        1 => 'Second',
259
                                        2 => 'Third',
260
                                    ),
261
                                ),
262
                            )
263
                        ),
264
                    )
265
                ),
266
                'defaultValue' => new FieldValue(
267
                    array(
268
                        'data' => array(),
269
                        'sortKey' => '',
270
                    )
271
                ),
272
            )
273
        );
274
275
        $actualFieldDefinition = new PersistenceFieldDefinition(
276
            array(
277
                'name' => array(
278
                    'eng-GB' => 'test name',
279
                ),
280
                'mainLanguageCode' => 'eng-GB',
281
            )
282
        );
283
284
        $this->converter->toFieldDefinition($storageFieldDefinition, $actualFieldDefinition);
285
286
        $this->assertEquals($expectedFieldDefinition, $actualFieldDefinition);
287
    }
288
289
    /**
290
     * @group fieldType
291
     * @group selection
292
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter\SelectionConverter::toFieldDefinition
293
     */
294
    public function testToFieldDefinitionSingleEmpty()
295
    {
296
        $storageFieldDefinition = new StorageFieldDefinition();
297
        $storageFieldDefinition->dataInt1 = 0;
298
        $storageFieldDefinition->dataText5 = <<<EOT
299
<?xml version="1.0" encoding="utf-8"?>
300
<ezselection>
301
  <options>
302
  </options>
303
</ezselection>
304
EOT;
305
306
        $expectedFieldDefinition = new PersistenceFieldDefinition(
307
            array(
308
                'name' => array(
309
                    'eng-GB' => 'test name',
310
                ),
311
                'mainLanguageCode' => 'eng-GB',
312
                'fieldTypeConstraints' => new FieldTypeConstraints(
313
                    array(
314
                        'fieldSettings' => new FieldSettings(
315
                            array(
316
                                'isMultiple' => false,
317
                                'options' => array(),
318
                                'multilingualOptions' => array(
319
                                    'eng-GB' => array(),
320
                                ),
321
                            )
322
                        ),
323
                    )
324
                ),
325
                'defaultValue' => new FieldValue(array('data' => array())),
326
            )
327
        );
328
329
        $actualFieldDefinition = new PersistenceFieldDefinition(
330
            array(
331
                'name' => array(
332
                    'eng-GB' => 'test name',
333
                ),
334
                'mainLanguageCode' => 'eng-GB',
335
            )
336
        );
337
338
        $this->converter->toFieldDefinition($storageFieldDefinition, $actualFieldDefinition);
339
340
        $this->assertEquals($expectedFieldDefinition, $actualFieldDefinition);
341
    }
342
}
343