Completed
Push — EZP-29891 ( 916cf6...0402ff )
by
unknown
16:53
created

NameSchemaBase   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 280
Duplicated Lines 37.5 %

Coupling/Cohesion

Components 1
Dependencies 10

Importance

Changes 0
Metric Value
dl 105
loc 280
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A testResolve() 0 15 1
A testResolveWithSettings() 0 29 1
B providerForTestResolve() 0 96 1
A getFields() 47 47 1
A getFieldDefinitions() 26 26 1
A buildTestObjects() 22 22 1
A setConfiguration() 10 10 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/**
4
 * File contains: eZ\Publish\Core\Repository\Tests\Service\Integration\NameSchemaBase 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\Repository\Tests\Service\Integration;
10
11
use eZ\Publish\Core\Repository\Tests\Service\Integration\Base as BaseServiceTest;
12
use eZ\Publish\Core\Repository\Values\Content\Content;
13
use eZ\Publish\Core\Repository\Values\Content\VersionInfo;
14
use eZ\Publish\Core\Repository\Values\ContentType\ContentType;
15
use eZ\Publish\Core\Repository\Values\ContentType\FieldDefinition;
16
use eZ\Publish\API\Repository\Values\Content\Field;
17
use eZ\Publish\Core\FieldType\TextLine\Value as TextLineValue;
18
19
/**
20
 * Test case for NameSchema service.
21
 */
22
abstract class NameSchemaBase extends BaseServiceTest
23
{
24
    /**
25
     * Test eZ\Publish\Core\Repository\Helper\NameSchemaService method.
26
     *
27
     * @covers \eZ\Publish\Core\Repository\Helper\NameSchemaService::resolve
28
     * @dataProvider providerForTestResolve
29
     */
30
    public function testResolve($nameSchema, $expectedName)
31
    {
32
        $service = $this->repository->getNameSchemaService();
33
34
        list($content, $contentType) = $this->buildTestObjects();
35
36
        $name = $service->resolve(
37
            $nameSchema,
38
            $contentType,
39
            $content->fields,
40
            $content->versionInfo->languageCodes
41
        );
42
43
        self::assertEquals($expectedName, $name);
44
    }
45
46
    /**
47
     * Test eZ\Publish\Core\Repository\Helper\NameSchemaService method.
48
     *
49
     * @covers \eZ\Publish\Core\Repository\Helper\NameSchemaService::resolve
50
     */
51
    public function testResolveWithSettings()
52
    {
53
        $service = $this->repository->getNameSchemaService();
54
55
        $this->setConfiguration(
56
            $service,
57
            array(
58
                'limit' => 38,
59
                'sequence' => '...',
60
            )
61
        );
62
63
        list($content, $contentType) = $this->buildTestObjects();
64
65
        $name = $service->resolve(
66
            'Hello, <text1> and <text2> and then goodbye and hello again',
67
            $contentType,
68
            $content->fields,
69
            $content->versionInfo->languageCodes
70
        );
71
72
        self::assertEquals(
73
            array(
74
                'eng-GB' => 'Hello, one and two and then goodbye...',
75
                'cro-HR' => 'Hello, jedan and dva and then goodb...',
76
            ),
77
            $name
78
        );
79
    }
80
81
    public function providerForTestResolve()
82
    {
83
        return array(
84
            array(
85
                '<text1>',
86
                array(
87
                    'eng-GB' => 'one',
88
                    'cro-HR' => 'jedan',
89
                ),
90
            ),
91
            array(
92
                '<text1> <text2>',
93
                array(
94
                    'eng-GB' => 'one two',
95
                    'cro-HR' => 'jedan dva',
96
                ),
97
            ),
98
            array(
99
                'Hello <text1>',
100
                array(
101
                    'eng-GB' => 'Hello one',
102
                    'cro-HR' => 'Hello jedan',
103
                ),
104
            ),
105
            array(
106
                'Hello, <text1> and <text2> and then goodbye',
107
                array(
108
                    'eng-GB' => 'Hello, one and two and then goodbye',
109
                    'cro-HR' => 'Hello, jedan and dva and then goodbye',
110
                ),
111
            ),
112
            array(
113
                '<text1|text2>',
114
                array(
115
                    'eng-GB' => 'one',
116
                    'cro-HR' => 'jedan',
117
                ),
118
            ),
119
            array(
120
                '<text2|text1>',
121
                array(
122
                    'eng-GB' => 'two',
123
                    'cro-HR' => 'dva',
124
                ),
125
            ),
126
            array(
127
                '<text3|text1>',
128
                array(
129
                    'eng-GB' => 'one',
130
                    'cro-HR' => 'jedan',
131
                ),
132
            ),
133
            array(
134
                '<(<text1> <text2>)>',
135
                array(
136
                    'eng-GB' => 'one two',
137
                    'cro-HR' => 'jedan dva',
138
                ),
139
            ),
140
            array(
141
                '<(<text3|text2>)>',
142
                array(
143
                    'eng-GB' => 'two',
144
                    'cro-HR' => 'dva',
145
                ),
146
            ),
147
            array(
148
                '<text3|(<text3|text2>)>',
149
                array(
150
                    'eng-GB' => 'two',
151
                    'cro-HR' => 'dva',
152
                ),
153
            ),
154
            array(
155
                '<text3|(Hello <text2> and <text1>!)>',
156
                array(
157
                    'eng-GB' => 'Hello two and one!',
158
                    'cro-HR' => 'Hello dva and jedan!',
159
                ),
160
            ),
161
            array(
162
                '<text3|(Hello <text3> and <text1>)|text2>!',
163
                array(
164
                    'eng-GB' => 'Hello  and one!',
165
                    'cro-HR' => 'Hello  and jedan!',
166
                ),
167
            ),
168
            array(
169
                '<text3|(Hello <text3|text2> and <text1>)|text2>!',
170
                array(
171
                    'eng-GB' => 'Hello two and one!',
172
                    'cro-HR' => 'Hello dva and jedan!',
173
                ),
174
            ),
175
        );
176
    }
177
178
    /**
179
     * @return \eZ\Publish\API\Repository\Values\Content\Field[]
180
     */
181 View Code Duplication
    protected function getFields()
182
    {
183
        return array(
184
            new Field(
185
                array(
186
                    'languageCode' => 'eng-GB',
187
                    'fieldDefIdentifier' => 'text1',
188
                    'value' => new TextLineValue('one'),
189
                )
190
            ),
191
            new Field(
192
                array(
193
                    'languageCode' => 'eng-GB',
194
                    'fieldDefIdentifier' => 'text2',
195
                    'value' => new TextLineValue('two'),
196
                )
197
            ),
198
            new Field(
199
                array(
200
                    'languageCode' => 'eng-GB',
201
                    'fieldDefIdentifier' => 'text3',
202
                    'value' => new TextLineValue(''),
203
                )
204
            ),
205
            new Field(
206
                array(
207
                    'languageCode' => 'cro-HR',
208
                    'fieldDefIdentifier' => 'text1',
209
                    'value' => new TextLineValue('jedan'),
210
                )
211
            ),
212
            new Field(
213
                array(
214
                    'languageCode' => 'cro-HR',
215
                    'fieldDefIdentifier' => 'text2',
216
                    'value' => new TextLineValue('dva'),
217
                )
218
            ),
219
            new Field(
220
                array(
221
                    'languageCode' => 'cro-HR',
222
                    'fieldDefIdentifier' => 'text3',
223
                    'value' => new TextLineValue(''),
224
                )
225
            ),
226
        );
227
    }
228
229
    /**
230
     * @return \eZ\Publish\Core\Repository\Values\ContentType\FieldDefinition[]
231
     */
232 View Code Duplication
    protected function getFieldDefinitions()
233
    {
234
        return array(
235
            new FieldDefinition(
236
                array(
237
                    'id' => '1',
238
                    'identifier' => 'text1',
239
                    'fieldTypeIdentifier' => 'ezstring',
240
                )
241
            ),
242
            new FieldDefinition(
243
                array(
244
                    'id' => '2',
245
                    'identifier' => 'text2',
246
                    'fieldTypeIdentifier' => 'ezstring',
247
                )
248
            ),
249
            new FieldDefinition(
250
                array(
251
                    'id' => '3',
252
                    'identifier' => 'text3',
253
                    'fieldTypeIdentifier' => 'ezstring',
254
                )
255
            ),
256
        );
257
    }
258
259
    /**
260
     * Builds stubbed content for testing purpose.
261
     *
262
     * @return \eZ\Publish\API\Repository\Values\Content\Content
263
     */
264 View Code Duplication
    protected function buildTestObjects($nameSchema = '<name_schema>', $urlAliasSchema = '<urlalias_schema>')
265
    {
266
        $contentType = new ContentType(
267
            array(
268
                'nameSchema' => $nameSchema,
269
                'urlAliasSchema' => $urlAliasSchema,
270
                'fieldDefinitions' => $this->getFieldDefinitions(),
271
            )
272
        );
273
        $content = new Content(
274
            array(
275
                'internalFields' => $this->getFields(),
276
                'versionInfo' => new VersionInfo(
277
                    array(
278
                        'languageCodes' => array('eng-GB', 'cro-HR'),
279
                    )
280
                ),
281
            )
282
        );
283
284
        return array($content, $contentType);
285
    }
286
287
    /**
288
     * @param object $service
289
     * @param array $configuration
290
     */
291 View Code Duplication
    protected function setConfiguration($service, array $configuration)
292
    {
293
        $refObject = new \ReflectionObject($service);
294
        $refProperty = $refObject->getProperty('settings');
295
        $refProperty->setAccessible(true);
296
        $refProperty->setValue(
297
            $service,
298
            $configuration
299
        );
300
    }
301
}
302