Completed
Push — ezp-29724 ( 1025e5...495423 )
by
unknown
30:13 queued 07:05
created

SelectionTest::testToFieldDefinitionMultiple()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 46

Duplication

Lines 0
Ratio 0 %

Importance

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