Completed
Push — 6.7 ( d8e596...bdbdd3 )
by André
23:22
created

MapperTest   B

Complexity

Total Complexity 40

Size/Duplication

Total Lines 639
Duplicated Lines 1.88 %

Coupling/Cohesion

Components 1
Dependencies 13

Importance

Changes 0
Metric Value
dl 12
loc 639
rs 8.1676
c 0
b 0
f 0
wmc 40
lcom 1
cbo 13

29 Methods

Rating   Name   Duplication   Size   Complexity  
A testCtor() 0 12 1
B getCreateStructFixture() 0 24 1
B testCreateVersionInfoForContent() 0 27 1
A getFullContentFixture() 0 23 1
B testConvertToStorageValue() 0 30 1
B testExtractContentFromRows() 0 43 1
B testExtractContentFromRowsMultipleVersions() 0 46 1
A testCreateCreateStructFromContent() 0 23 1
A testCreateCreateStructFromContentBasicProperties() 0 16 1
A testCreateCreateStructFromContentParentLocationsEmpty() 0 7 1
A testCreateCreateStructFromContentFieldCount() 0 7 1
A testCreateCreateStructFromContentFieldsNoId() 0 6 2
A testExtractRelationsFromRows() 0 13 1
A testCreateCreateStructFromContentWithPreserveOriginalLanguage() 0 19 1
A testExtractContentInfoFromRow() 0 9 1
A extractContentInfoFromRowProvider() 0 16 3
A testCreateRelationFromCreateStruct() 0 12 2
A extractVersionInfoFromRowProvider() 0 20 2
A getContentExtractFixture() 0 4 1
A getNamesExtractFixture() 0 4 1
A getContentExtractReference() 0 4 1
A getMultipleVersionsExtractFixture() 0 4 1
A getMultipleVersionsNamesExtractFixture() 0 4 1
A getRelationExtractFixture() 0 4 1
A getRelationExtractReference() 0 4 1
A getMapper() 0 7 1
A getValueConverterRegistryMock() 0 12 2
A getRelationCreateStructFixture() 12 12 1
B getLanguageHandler() 0 51 6

How to fix   Duplicated Code    Complexity   

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:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like MapperTest often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use MapperTest, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
/**
4
 * File contains: eZ\Publish\Core\Persistence\Legacy\Tests\Content\MapperTest 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;
10
11
use eZ\Publish\Core\Persistence\Legacy\Content\Mapper;
12
use eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\ConverterRegistry as Registry;
13
use eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldValue;
14
use eZ\Publish\API\Repository\Values\Content\Relation as RelationValue;
15
use eZ\Publish\SPI\Persistence\Content;
16
use eZ\Publish\SPI\Persistence\Content\ContentInfo;
17
use eZ\Publish\SPI\Persistence\Content\VersionInfo;
18
use eZ\Publish\SPI\Persistence\Content\Field;
19
use eZ\Publish\SPI\Persistence\Content\FieldValue;
20
use eZ\Publish\SPI\Persistence\Content\Language;
21
use eZ\Publish\SPI\Persistence\Content\CreateStruct;
22
use eZ\Publish\SPI\Persistence\Content\Location\CreateStruct as LocationCreateStruct;
23
use eZ\Publish\SPI\Persistence\Content\Relation\CreateStruct as RelationCreateStruct;
24
25
/**
26
 * Test case for Mapper.
27
 */
28
class MapperTest extends LanguageAwareTestCase
29
{
30
    /**
31
     * Value converter registry mock.
32
     *
33
     * @var \eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\ConverterRegistry
34
     */
35
    protected $valueConverterRegistryMock;
36
37
    /**
38
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Mapper::__construct
39
     */
40
    public function testCtor()
41
    {
42
        $regMock = $this->getValueConverterRegistryMock();
43
44
        $mapper = $this->getMapper();
45
46
        $this->assertAttributeSame(
47
            $regMock,
48
            'converterRegistry',
49
            $mapper
50
        );
51
    }
52
53
    /**
54
     * Returns a eZ\Publish\SPI\Persistence\Content\CreateStruct fixture.
55
     *
56
     * @return \eZ\Publish\SPI\Persistence\Content\CreateStruct
57
     */
58
    protected function getCreateStructFixture()
59
    {
60
        $struct = new CreateStruct();
61
62
        $struct->name = 'Content name';
0 ignored issues
show
Documentation Bug introduced by
It seems like 'Content name' of type string is incompatible with the declared type array<integer,string> of property $name.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
63
        $struct->typeId = 23;
64
        $struct->sectionId = 42;
65
        $struct->ownerId = 13;
66
        $struct->initialLanguageId = 2;
67
        $struct->locations = array(
68
            new LocationCreateStruct(
69
                array('parentId' => 2)
70
            ),
71
            new LocationCreateStruct(
72
                array('parentId' => 3)
73
            ),
74
            new LocationCreateStruct(
75
                array('parentId' => 4)
76
            ),
77
        );
78
        $struct->fields = array(new Field());
79
80
        return $struct;
81
    }
82
83
    /**
84
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Mapper::createVersionInfoForContent
85
     */
86
    public function testCreateVersionInfoForContent()
87
    {
88
        $content = $this->getFullContentFixture();
89
        $time = time();
90
91
        $mapper = $this->getMapper();
92
93
        $versionInfo = $mapper->createVersionInfoForContent(
94
            $content,
95
            1,
96
            14
97
        );
98
99
        $this->assertPropertiesCorrect(
100
            array(
101
                'id' => null,
102
                'versionNo' => 1,
103
                'creatorId' => 14,
104
                'status' => 0,
105
                'initialLanguageCode' => 'eng-GB',
106
                'languageIds' => array(4),
107
            ),
108
            $versionInfo
109
        );
110
        $this->assertGreaterThanOrEqual($time, $versionInfo->creationDate);
111
        $this->assertGreaterThanOrEqual($time, $versionInfo->modificationDate);
112
    }
113
114
    /**
115
     * Returns a Content fixture.
116
     *
117
     * @return Content
118
     */
119
    protected function getFullContentFixture()
120
    {
121
        $content = new Content();
122
123
        $content->fields = array(
124
            new Field(array('languageCode' => 'eng-GB')),
125
        );
126
        $content->versionInfo = new VersionInfo(
127
            array(
128
                'versionNo' => 1,
129
                'initialLanguageCode' => 'eng-GB',
130
                'languageIds' => array(4),
131
            )
132
        );
133
134
        $content->versionInfo->contentInfo = new ContentInfo();
135
        $content->versionInfo->contentInfo->id = 2342;
136
        $content->versionInfo->contentInfo->contentTypeId = 23;
137
        $content->versionInfo->contentInfo->sectionId = 42;
138
        $content->versionInfo->contentInfo->ownerId = 13;
139
140
        return $content;
141
    }
142
143
    /**
144
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Mapper::convertToStorageValue
145
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldValue
146
     */
147
    public function testConvertToStorageValue()
148
    {
149
        $convMock = $this->getMock(
150
            'eZ\\Publish\\Core\\Persistence\\Legacy\\Content\\FieldValue\\Converter'
151
        );
152
        $convMock->expects($this->once())
153
            ->method('toStorageValue')
154
            ->with(
155
                $this->isInstanceOf(
156
                    'eZ\\Publish\\SPI\\Persistence\\Content\\FieldValue'
157
                ),
158
                $this->isInstanceOf(
159
                    'eZ\\Publish\\Core\\Persistence\\Legacy\\Content\\StorageFieldValue'
160
                )
161
            )->will($this->returnValue(new StorageFieldValue()));
162
163
        $reg = new Registry(array('some-type' => $convMock));
164
165
        $field = new Field();
166
        $field->type = 'some-type';
167
        $field->value = new FieldValue();
168
169
        $mapper = new Mapper($reg, $this->getLanguageHandler());
170
        $res = $mapper->convertToStorageValue($field);
171
172
        $this->assertInstanceOf(
173
            'eZ\\Publish\\Core\\Persistence\\Legacy\\Content\\StorageFieldValue',
174
            $res
175
        );
176
    }
177
178
    /**
179
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Mapper::extractContentFromRows
180
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Mapper::extractFieldFromRow
181
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Mapper::extractFieldValueFromRow
182
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldValue
183
     */
184
    public function testExtractContentFromRows()
185
    {
186
        $convMock = $this->getMock(
187
            'eZ\\Publish\\Core\\Persistence\\Legacy\\Content\\FieldValue\\Converter'
188
        );
189
        $convMock->expects($this->exactly(13))
190
            ->method('toFieldValue')
191
            ->with(
192
                $this->isInstanceOf(
193
                    'eZ\\Publish\\Core\\Persistence\\Legacy\\Content\\StorageFieldValue'
194
                )
195
            )->will(
196
                $this->returnValue(
197
                    new FieldValue()
198
                )
199
            );
200
201
        $reg = new Registry(
202
            array(
203
                'ezauthor' => $convMock,
204
                'ezstring' => $convMock,
205
                'ezrichtext' => $convMock,
206
                'ezboolean' => $convMock,
207
                'ezimage' => $convMock,
208
                'ezdatetime' => $convMock,
209
                'ezkeyword' => $convMock,
210
                'ezsrrating' => $convMock,
211
            )
212
        );
213
214
        $rowsFixture = $this->getContentExtractFixture();
215
        $nameRowsFixture = $this->getNamesExtractFixture();
216
217
        $mapper = new Mapper($reg, $this->getLanguageHandler());
218
        $result = $mapper->extractContentFromRows($rowsFixture, $nameRowsFixture);
219
220
        $this->assertEquals(
221
            array(
222
                $this->getContentExtractReference(),
223
            ),
224
            $result
225
        );
226
    }
227
228
    /**
229
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Mapper::extractContentFromRows
230
     */
231
    public function testExtractContentFromRowsMultipleVersions()
232
    {
233
        $convMock = $this->getMock(
234
            'eZ\\Publish\\Core\\Persistence\\Legacy\\Content\\FieldValue\\Converter'
235
        );
236
        $convMock->expects($this->any())
237
            ->method('toFieldValue')
238
            ->will($this->returnValue(new FieldValue()));
239
240
        $reg = new Registry(
241
            array(
242
                'ezstring' => $convMock,
243
                'ezrichtext' => $convMock,
244
                'ezdatetime' => $convMock,
245
            )
246
        );
247
248
        $rowsFixture = $this->getMultipleVersionsExtractFixture();
249
        $nameRowsFixture = $this->getMultipleVersionsNamesExtractFixture();
250
251
        $mapper = new Mapper($reg, $this->getLanguageHandler());
252
        $result = $mapper->extractContentFromRows($rowsFixture, $nameRowsFixture);
253
254
        $this->assertEquals(
255
            2,
256
            count($result)
257
        );
258
259
        $this->assertEquals(
260
            11,
261
            $result[0]->versionInfo->contentInfo->id
262
        );
263
        $this->assertEquals(
264
            11,
265
            $result[1]->versionInfo->contentInfo->id
266
        );
267
268
        $this->assertEquals(
269
            1,
270
            $result[0]->versionInfo->versionNo
271
        );
272
        $this->assertEquals(
273
            2,
274
            $result[1]->versionInfo->versionNo
275
        );
276
    }
277
278
    /**
279
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Mapper::createCreateStructFromContent
280
     */
281
    public function testCreateCreateStructFromContent()
282
    {
283
        $time = time();
284
        $mapper = $this->getMapper();
285
286
        $content = $this->getContentExtractReference();
287
288
        $struct = $mapper->createCreateStructFromContent($content);
289
290
        $this->assertInstanceOf(
291
            'eZ\\Publish\\SPI\\Persistence\\Content\\CreateStruct',
292
            $struct
293
        );
294
295
        return array(
296
            'original' => $content,
297
            'result' => $struct,
298
            'time' => $time,
299
        );
300
301
        // parentLocations
302
        // fields
303
    }
304
305
    /**
306
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Mapper::createCreateStructFromContent
307
     * @depends testCreateCreateStructFromContent
308
     */
309
    public function testCreateCreateStructFromContentBasicProperties($data)
310
    {
311
        $content = $data['original'];
312
        $struct = $data['result'];
313
        $time = $data['time'];
314
        $this->assertStructsEqual(
315
            $content->versionInfo->contentInfo,
316
            $struct,
317
            array('sectionId', 'ownerId')
318
        );
319
        self::assertNotEquals($content->versionInfo->contentInfo->remoteId, $struct->remoteId);
320
        self::assertSame($content->versionInfo->contentInfo->contentTypeId, $struct->typeId);
321
        self::assertSame(2, $struct->initialLanguageId);
322
        self::assertSame($content->versionInfo->contentInfo->alwaysAvailable, $struct->alwaysAvailable);
323
        self::assertGreaterThanOrEqual($time, $struct->modified);
324
    }
325
326
    /**
327
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Mapper::createCreateStructFromContent
328
     * @depends testCreateCreateStructFromContent
329
     */
330
    public function testCreateCreateStructFromContentParentLocationsEmpty($data)
331
    {
332
        $this->assertEquals(
333
            array(),
334
            $data['result']->locations
335
        );
336
    }
337
338
    /**
339
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Mapper::createCreateStructFromContent
340
     * @depends testCreateCreateStructFromContent
341
     */
342
    public function testCreateCreateStructFromContentFieldCount($data)
343
    {
344
        $this->assertEquals(
345
            count($data['original']->fields),
346
            count($data['result']->fields)
347
        );
348
    }
349
350
    /**
351
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Mapper::createCreateStructFromContent
352
     * @depends testCreateCreateStructFromContent
353
     */
354
    public function testCreateCreateStructFromContentFieldsNoId($data)
355
    {
356
        foreach ($data['result']->fields as $field) {
357
            $this->assertNull($field->id);
358
        }
359
    }
360
361
    public function testExtractRelationsFromRows()
362
    {
363
        $mapper = $this->getMapper();
364
365
        $rows = $this->getRelationExtractFixture();
366
367
        $res = $mapper->extractRelationsFromRows($rows);
368
369
        $this->assertEquals(
370
            $this->getRelationExtractReference(),
371
            $res
372
        );
373
    }
374
375
    /**
376
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Mapper::createCreateStructFromContent
377
     */
378
    public function testCreateCreateStructFromContentWithPreserveOriginalLanguage()
379
    {
380
        $time = time();
381
        $mapper = $this->getMapper();
382
383
        $content = $this->getContentExtractReference();
384
        $content->versionInfo->contentInfo->mainLanguageCode = 'eng-GB';
385
386
        $struct = $mapper->createCreateStructFromContent($content, true);
387
388
        $this->assertInstanceOf(CreateStruct::class, $struct);
389
        $this->assertStructsEqual($content->versionInfo->contentInfo, $struct, ['sectionId', 'ownerId']);
390
        self::assertNotEquals($content->versionInfo->contentInfo->remoteId, $struct->remoteId);
391
        self::assertSame($content->versionInfo->contentInfo->contentTypeId, $struct->typeId);
392
        self::assertSame(2, $struct->initialLanguageId);
393
        self::assertSame(4, $struct->mainLanguageId);
394
        self::assertSame($content->versionInfo->contentInfo->alwaysAvailable, $struct->alwaysAvailable);
395
        self::assertGreaterThanOrEqual($time, $struct->modified);
396
    }
397
398
    /**
399
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Mapper::extractContentInfoFromRow
400
     * @dataProvider extractContentInfoFromRowProvider
401
     *
402
     * @param array $fixtures
403
     * @param string $prefix
404
     */
405
    public function testExtractContentInfoFromRow(array $fixtures, $prefix)
406
    {
407
        $contentInfoReference = $this->getContentExtractReference()->versionInfo->contentInfo;
408
        $mapper = new Mapper(
409
            $this->getValueConverterRegistryMock(),
410
            $this->getLanguageHandler()
411
        );
412
        self::assertEquals($contentInfoReference, $mapper->extractContentInfoFromRow($fixtures, $prefix));
413
    }
414
415
    /**
416
     * Returns test data for {@link testExtractContentInfoFromRow()}.
417
     *
418
     * @return array
419
     */
420
    public function extractContentInfoFromRowProvider()
421
    {
422
        $fixtures = $this->getContentExtractFixture();
423
        $fixturesNoPrefix = array();
424
        foreach ($fixtures[0] as $key => $value) {
425
            $keyNoPrefix = $key === 'ezcontentobject_tree_main_node_id'
426
                ? $key
427
                : str_replace('ezcontentobject_', '', $key);
428
            $fixturesNoPrefix[$keyNoPrefix] = $value;
429
        }
430
431
        return array(
432
            array($fixtures[0], 'ezcontentobject_'),
433
            array($fixturesNoPrefix, ''),
434
        );
435
    }
436
437
    /**
438
     * @covers \eZ\Publish\Core\Persistence\Legacy\Content\Mapper::createRelationFromCreateStruct
439
     */
440
    public function testCreateRelationFromCreateStruct()
441
    {
442
        $struct = $this->getRelationCreateStructFixture();
443
444
        $mapper = $this->getMapper();
445
        $relation = $mapper->createRelationFromCreateStruct($struct);
446
447
        self::assertInstanceOf('eZ\\Publish\\SPI\\Persistence\\Content\\Relation', $relation);
448
        foreach ($struct as $property => $value) {
0 ignored issues
show
Bug introduced by
The expression $struct of type object<eZ\Publish\SPI\Pe...\Relation\CreateStruct> is not traversable.
Loading history...
449
            self::assertSame($value, $relation->$property);
450
        }
451
    }
452
453
    /**
454
     * Returns test data for {@link testExtractVersionInfoFromRow()}.
455
     *
456
     * @return array
457
     */
458
    public function extractVersionInfoFromRowProvider()
459
    {
460
        $fixturesAll = $this->getContentExtractFixture();
461
        $fixtures = $fixturesAll[0];
462
        $fixtures['ezcontentobject_version_names'] = array(
463
            array('content_translation' => 'eng-US', 'name' => 'Something'),
464
        );
465
        $fixtures['ezcontentobject_version_languages'] = array(2);
466
        $fixtures['ezcontentobject_version_initial_language_code'] = 'eng-US';
467
        $fixturesNoPrefix = array();
468
        foreach ($fixtures as $key => $value) {
469
            $keyNoPrefix = str_replace('ezcontentobject_version_', '', $key);
470
            $fixturesNoPrefix[$keyNoPrefix] = $value;
471
        }
472
473
        return array(
474
            array($fixtures, 'ezcontentobject_version_'),
475
            array($fixturesNoPrefix, ''),
476
        );
477
    }
478
479
    /**
480
     * Returns a fixture of database rows for content extraction.
481
     *
482
     * Fixture is stored in _fixtures/extract_content_from_rows.php
483
     *
484
     * @return array
485
     */
486
    protected function getContentExtractFixture()
487
    {
488
        return require __DIR__ . '/_fixtures/extract_content_from_rows.php';
489
    }
490
491
    /**
492
     * Returns a fixture of database rows for content names extraction.
493
     *
494
     * Fixture is stored in _fixtures/extract_names_from_rows.php
495
     *
496
     * @return array
497
     */
498
    protected function getNamesExtractFixture()
499
    {
500
        return require __DIR__ . '/_fixtures/extract_names_from_rows.php';
501
    }
502
503
    /**
504
     * Returns a reference result for content extraction.
505
     *
506
     * Fixture is stored in _fixtures/extract_content_from_rows_result.php
507
     *
508
     * @return Content
509
     */
510
    protected function getContentExtractReference()
511
    {
512
        return require __DIR__ . '/_fixtures/extract_content_from_rows_result.php';
513
    }
514
515
    /**
516
     * Returns a fixture for mapping multiple versions of a content object.
517
     *
518
     * @return string[][]
519
     */
520
    protected function getMultipleVersionsExtractFixture()
521
    {
522
        return require __DIR__ . '/_fixtures/extract_content_from_rows_multiple_versions.php';
523
    }
524
525
    /**
526
     * Returns a fixture of database rows for content names extraction across multiple versions.
527
     *
528
     * Fixture is stored in _fixtures/extract_names_from_rows_multiple_versions.php
529
     *
530
     * @return array
531
     */
532
    protected function getMultipleVersionsNamesExtractFixture()
533
    {
534
        return require __DIR__ . '/_fixtures/extract_names_from_rows_multiple_versions.php';
535
    }
536
537
    /**
538
     * Returns a fixture of database rows for relations extraction.
539
     *
540
     * Fixture is stored in _fixtures/relations.php
541
     *
542
     * @return array
543
     */
544
    protected function getRelationExtractFixture()
545
    {
546
        return require __DIR__ . '/_fixtures/relations_rows.php';
547
    }
548
549
    /**
550
     * Returns a reference result for content extraction.
551
     *
552
     * Fixture is stored in _fixtures/relations_results.php
553
     *
554
     * @return Content
555
     */
556
    protected function getRelationExtractReference()
557
    {
558
        return require __DIR__ . '/_fixtures/relations_results.php';
559
    }
560
561
    /**
562
     * Returns a Mapper.
563
     *
564
     * @return \eZ\Publish\Core\Persistence\Legacy\Content\Mapper
565
     */
566
    protected function getMapper($valueConverter = null)
0 ignored issues
show
Unused Code introduced by
The parameter $valueConverter is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
567
    {
568
        return new Mapper(
569
            $this->getValueConverterRegistryMock(),
570
            $this->getLanguageHandler()
571
        );
572
    }
573
574
    /**
575
     * Returns a FieldValue converter registry mock.
576
     *
577
     * @return \eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\ConverterRegistry
578
     */
579
    protected function getValueConverterRegistryMock()
580
    {
581
        if (!isset($this->valueConverterRegistryMock)) {
582
            $this->valueConverterRegistryMock = $this->getMock(
583
                'eZ\\Publish\\Core\\Persistence\\Legacy\\Content\\FieldValue\\ConverterRegistry',
584
                array(),
585
                array(array())
586
            );
587
        }
588
589
        return $this->valueConverterRegistryMock;
590
    }
591
592
    /**
593
     * Returns a eZ\Publish\SPI\Persistence\Content\CreateStruct fixture.
594
     *
595
     * @return \eZ\Publish\SPI\Persistence\Content\Relation\CreateStruct
596
     */
597 View Code Duplication
    protected function getRelationCreateStructFixture()
598
    {
599
        $struct = new RelationCreateStruct();
600
601
        $struct->destinationContentId = 0;
602
        $struct->sourceContentId = 0;
603
        $struct->sourceContentVersionNo = 1;
604
        $struct->sourceFieldDefinitionId = 1;
605
        $struct->type = RelationValue::COMMON;
606
607
        return $struct;
608
    }
609
610
    /**
611
     * Returns a language handler mock.
612
     *
613
     * @return \eZ\Publish\Core\Persistence\Legacy\Content\Language\Handler
614
     */
615
    protected function getLanguageHandler()
616
    {
617
        $languages = array(
618
            new Language(
619
                array(
620
                    'id' => 2,
621
                    'languageCode' => 'eng-US',
622
                    'name' => 'US english',
623
                )
624
            ),
625
            new Language(
626
                array(
627
                    'id' => 4,
628
                    'languageCode' => 'eng-GB',
629
                    'name' => 'British english',
630
                )
631
            ),
632
        );
633
634
        if (!isset($this->languageHandler)) {
635
            $this->languageHandler = $this->getMock('eZ\\Publish\\SPI\\Persistence\\Content\\Language\\Handler');
636
            $this->languageHandler->expects($this->any())
637
                ->method('load')
638
                ->will(
639
                    $this->returnCallback(
640
                        function ($id) use ($languages) {
641
                            foreach ($languages as $language) {
642
                                if ($language->id == $id) {
643
                                    return $language;
644
                                }
645
                            }
646
                        }
647
                    )
648
                );
649
            $this->languageHandler->expects($this->any())
650
                ->method('loadByLanguageCode')
651
                ->will(
652
                    $this->returnCallback(
653
                        function ($languageCode) use ($languages) {
654
                            foreach ($languages as $language) {
655
                                if ($language->languageCode == $languageCode) {
656
                                    return $language;
657
                                }
658
                            }
659
                        }
660
                    )
661
                );
662
        }
663
664
        return $this->languageHandler;
665
    }
666
}
667