Completed
Push — master ( 97e40f...89ec5c )
by André
40:26 queued 12:35
created

RelationListTest::testToFieldDefinitionMultiple()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 51
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

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