Completed
Push — ezp_30300 ( ef176f...d1dcf2 )
by
unknown
26:24
created

DomainMapperTest   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 223
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Importance

Changes 0
Metric Value
dl 0
loc 223
rs 10
c 0
b 0
f 0
wmc 14
lcom 1
cbo 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A testBuildVersionInfo() 0 16 2
A testBuildLocationWithContentForRootLocation() 0 15 1
A testBuildLocationWithContentThrowsInvalidArgumentException() 0 9 1
A testBuildLocationWithContentIsAlignedWithBuildLocation() 0 9 1
B providerForBuildVersionInfo() 0 59 1
A providerForBuildLocationDomainObjectsOnSearchResult() 0 13 1
A testBuildLocationDomainObjectsOnSearchResult() 0 33 3
A getDomainMapper() 0 10 1
A getContentHandlerMock() 0 4 1
A getLanguageHandlerMock() 0 4 1
A getTypeHandlerMock() 0 4 1
1
<?php
2
3
/**
4
 * File contains: eZ\Publish\Core\Repository\Tests\Service\Mock\DomainMapperTest 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\Mock;
10
11
use eZ\Publish\API\Repository\Exceptions\InvalidArgumentException;
12
use eZ\Publish\API\Repository\Values\Content\Search\SearchHit;
13
use eZ\Publish\API\Repository\Values\Content\Search\SearchResult;
14
use eZ\Publish\API\Repository\Values\Content\VersionInfo as APIVersionInfo;
15
use eZ\Publish\Core\Repository\Tests\Service\Mock\Base as BaseServiceMockTest;
16
use eZ\Publish\Core\Repository\Helper\DomainMapper;
17
use eZ\Publish\Core\Repository\Values\Content\Content;
18
use eZ\Publish\SPI\Persistence\Content\ContentInfo;
19
use eZ\Publish\SPI\Persistence\Content\Location;
20
use eZ\Publish\API\Repository\Values\Content\Location as APILocation;
21
use eZ\Publish\SPI\Persistence\Content\VersionInfo as SPIVersionInfo;
22
use eZ\Publish\SPI\Persistence\Content\ContentInfo as SPIContentInfo;
23
24
/**
25
 * Mock test case for internal DomainMapper.
26
 */
27
class DomainMapperTest extends BaseServiceMockTest
28
{
29
    /**
30
     * @covers \eZ\Publish\Core\Repository\Helper\DomainMapper::buildVersionInfoDomainObject
31
     * @dataProvider providerForBuildVersionInfo
32
     */
33
    public function testBuildVersionInfo(SPIVersionInfo $spiVersionInfo, array $languages, array $expected)
0 ignored issues
show
Unused Code introduced by
The parameter $languages 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...
34
    {
35
        $languageHandlerMock = $this->getLanguageHandlerMock();
36
        $languageHandlerMock->expects($this->never())->method('load');
37
38
        $versionInfo = $this->getDomainMapper()->buildVersionInfoDomainObject($spiVersionInfo);
39
        $this->assertInstanceOf(APIVersionInfo::class, $versionInfo);
40
41
        foreach ($expected as $expectedProperty => $expectedValue) {
42
            $this->assertAttributeSame(
43
                $expectedValue,
44
                $expectedProperty,
45
                $versionInfo
46
            );
47
        }
48
    }
49
50
    /**
51
     * @covers \eZ\Publish\Core\Repository\Helper\DomainMapper::buildLocationWithContent
52
     */
53
    public function testBuildLocationWithContentForRootLocation()
54
    {
55
        $spiRootLocation = new Location(['id' => 1]);
56
        $apiRootLocation = $this->getDomainMapper()->buildLocationWithContent($spiRootLocation, null);
57
58
        $expectedContentInfo = new ContentInfo([
59
            'id' => 0,
60
        ]);
61
        $expectedContent = new Content();
62
63
        $this->assertInstanceOf(APILocation::class, $apiRootLocation);
64
        $this->assertEquals($spiRootLocation->id, $apiRootLocation->id);
65
        $this->assertEquals($expectedContentInfo->id, $apiRootLocation->getContentInfo()->id);
66
        $this->assertEquals($expectedContent, $apiRootLocation->getContent());
67
    }
68
69
    /**
70
     * @covers \eZ\Publish\Core\Repository\Helper\DomainMapper::buildLocationWithContent
71
     */
72
    public function testBuildLocationWithContentThrowsInvalidArgumentException()
73
    {
74
        $this->expectException(InvalidArgumentException::class);
75
        $this->expectExceptionMessage('Argument \'$content\' is invalid: Location 2 has missing Content');
76
77
        $nonRootLocation = new Location(['id' => 2]);
78
79
        $this->getDomainMapper()->buildLocationWithContent($nonRootLocation, null);
80
    }
81
82
    public function testBuildLocationWithContentIsAlignedWithBuildLocation()
83
    {
84
        $spiRootLocation = new Location(['id' => 1]);
85
86
        $this->assertEquals(
87
            $this->getDomainMapper()->buildLocationWithContent($spiRootLocation, null),
88
            $this->getDomainMapper()->buildLocation($spiRootLocation)
89
        );
90
    }
91
92
    public function providerForBuildVersionInfo()
93
    {
94
        return array(
95
            array(
96
                new SPIVersionInfo(
97
                    array(
98
                        'status' => 44,
99
                        'contentInfo' => new SPIContentInfo(),
100
                    )
101
                ),
102
                array(),
103
                array('status' => APIVersionInfo::STATUS_DRAFT),
104
            ),
105
            array(
106
                new SPIVersionInfo(
107
                    array(
108
                        'status' => SPIVersionInfo::STATUS_DRAFT,
109
                        'contentInfo' => new SPIContentInfo(),
110
                    )
111
                ),
112
                array(),
113
                array('status' => APIVersionInfo::STATUS_DRAFT),
114
            ),
115
            array(
116
                new SPIVersionInfo(
117
                    array(
118
                        'status' => SPIVersionInfo::STATUS_PENDING,
119
                        'contentInfo' => new SPIContentInfo(),
120
                    )
121
                ),
122
                array(),
123
                array('status' => APIVersionInfo::STATUS_DRAFT),
124
            ),
125
            array(
126
                new SPIVersionInfo(
127
                    array(
128
                        'status' => SPIVersionInfo::STATUS_ARCHIVED,
129
                        'contentInfo' => new SPIContentInfo(),
130
                        'languageCodes' => array('eng-GB', 'nor-NB', 'fre-FR'),
131
                    )
132
                ),
133
                array(1 => 'eng-GB', 3 => 'nor-NB', 5 => 'fre-FR'),
134
                array(
135
                    'status' => APIVersionInfo::STATUS_ARCHIVED,
136
                    'languageCodes' => array('eng-GB', 'nor-NB', 'fre-FR'),
137
                ),
138
            ),
139
            array(
140
                new SPIVersionInfo(
141
                    array(
142
                        'status' => SPIVersionInfo::STATUS_PUBLISHED,
143
                        'contentInfo' => new SPIContentInfo(),
144
                    )
145
                ),
146
                array(),
147
                array('status' => APIVersionInfo::STATUS_PUBLISHED),
148
            ),
149
        );
150
    }
151
152
    public function providerForBuildLocationDomainObjectsOnSearchResult()
153
    {
154
        $locationHits = [
155
            new Location(['id' => 21, 'contentId' => 32]),
156
            new Location(['id' => 22, 'contentId' => 33]),
157
        ];
158
159
        return [
160
            [$locationHits, [32, 33], [], [32 => new ContentInfo(['id' => 32]), 33 => new ContentInfo(['id' => 33])], 0],
161
            [$locationHits, [32, 33], ['languages' => ['eng-GB']], [32 => new ContentInfo(['id' => 32])], 1],
162
            [$locationHits, [32, 33], ['languages' => ['eng-GB']], [], 2],
163
        ];
164
    }
165
166
    /**
167
     * @covers \eZ\Publish\Core\Repository\Helper\DomainMapper::buildLocationDomainObjectsOnSearchResult
168
     * @dataProvider providerForBuildLocationDomainObjectsOnSearchResult
169
     *
170
     * @param array $locationHits
171
     * @param array $contentIds
172
     * @param array $languageFilter
173
     * @param array $contentInfoList
174
     * @param int $missing
175
     */
176
    public function testBuildLocationDomainObjectsOnSearchResult(
177
        array $locationHits,
178
        array $contentIds,
179
        array $languageFilter,
180
        array $contentInfoList,
181
        int $missing
182
    ) {
183
        $contentHandlerMock = $this->getContentHandlerMock();
184
        $contentHandlerMock
185
            ->expects($this->once())
186
            ->method('loadContentInfoList')
187
            ->with($contentIds)
188
            ->willReturn($contentInfoList);
189
190
        $result = new SearchResult(['totalCount' => 10]);
191
        foreach ($locationHits as $locationHit) {
192
            $result->searchHits[] = new SearchHit(['valueObject' => $locationHit]);
193
        }
194
195
        $spiResult = clone $result;
196
        $missingLocations = $this->getDomainMapper()->buildLocationDomainObjectsOnSearchResult($result, $languageFilter);
197
        $this->assertInternalType('array', $missingLocations);
198
199
        if (!$missing) {
200
            $this->assertEmpty($missingLocations);
201
        } else {
202
            $this->assertNotEmpty($missingLocations);
203
        }
204
205
        $this->assertCount($missing, $missingLocations);
206
        $this->assertEquals($spiResult->totalCount - $missing, $result->totalCount);
207
        $this->assertCount(count($spiResult->searchHits) - $missing, $result->searchHits);
208
    }
209
210
    /**
211
     * Returns DomainMapper.
212
     *
213
     * @return \eZ\Publish\Core\Repository\Helper\DomainMapper
214
     */
215
    protected function getDomainMapper()
216
    {
217
        return new DomainMapper(
218
            $this->getContentHandlerMock(),
0 ignored issues
show
Bug introduced by
It seems like $this->getContentHandlerMock() targeting eZ\Publish\Core\Reposito...getContentHandlerMock() can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, eZ\Publish\Core\Reposito...inMapper::__construct() does only seem to accept object<eZ\Publish\SPI\Pe...stence\Content\Handler>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
219
            $this->getPersistenceMockHandler('Content\\Location\\Handler'),
220
            $this->getTypeHandlerMock(),
0 ignored issues
show
Bug introduced by
It seems like $this->getTypeHandlerMock() targeting eZ\Publish\Core\Reposito...t::getTypeHandlerMock() can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, eZ\Publish\Core\Reposito...inMapper::__construct() does only seem to accept object<eZ\Publish\SPI\Pe...e\Content\Type\Handler>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
221
            $this->getLanguageHandlerMock(),
0 ignored issues
show
Bug introduced by
It seems like $this->getLanguageHandlerMock() targeting eZ\Publish\Core\Reposito...etLanguageHandlerMock() can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, eZ\Publish\Core\Reposito...inMapper::__construct() does only seem to accept object<eZ\Publish\SPI\Pe...ntent\Language\Handler>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
222
            $this->getFieldTypeRegistryMock()
0 ignored issues
show
Bug introduced by
It seems like $this->getFieldTypeRegistryMock() targeting eZ\Publish\Core\Reposito...FieldTypeRegistryMock() can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, eZ\Publish\Core\Reposito...inMapper::__construct() does only seem to accept object<eZ\Publish\Core\R...lper\FieldTypeRegistry>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
223
        );
224
    }
225
226
    /**
227
     * @return \eZ\Publish\SPI\Persistence\Content\Handler|\PHPUnit\Framework\MockObject\MockObject
228
     */
229
    protected function getContentHandlerMock()
230
    {
231
        return $this->getPersistenceMockHandler('Content\\Handler');
232
    }
233
234
    /**
235
     * @return \eZ\Publish\SPI\Persistence\Content\Language\Handler|\PHPUnit\Framework\MockObject\MockObject
236
     */
237
    protected function getLanguageHandlerMock()
238
    {
239
        return $this->getPersistenceMockHandler('Content\\Language\\Handler');
240
    }
241
242
    /**
243
     * @return \eZ\Publish\SPI\Persistence\Content\Type\Handler|\PHPUnit\Framework\MockObject\MockObject
244
     */
245
    protected function getTypeHandlerMock()
246
    {
247
        return $this->getPersistenceMockHandler('Content\\Type\\Handler');
248
    }
249
}
250