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

DomainMapperTest::getContentHandlerMock()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
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\Values\Content\Search\SearchHit;
12
use eZ\Publish\API\Repository\Values\Content\Search\SearchResult;
13
use eZ\Publish\API\Repository\Values\Content\VersionInfo as APIVersionInfo;
14
use eZ\Publish\Core\Repository\Tests\Service\Mock\Base as BaseServiceMockTest;
15
use eZ\Publish\Core\Repository\Helper\DomainMapper;
16
use eZ\Publish\SPI\Persistence\Content\ContentInfo;
17
use eZ\Publish\SPI\Persistence\Content\Location;
18
use eZ\Publish\SPI\Persistence\Content\VersionInfo as SPIVersionInfo;
19
use eZ\Publish\SPI\Persistence\Content\ContentInfo as SPIContentInfo;
20
21
/**
22
 * Mock test case for internal DomainMapper.
23
 */
24
class DomainMapperTest extends BaseServiceMockTest
25
{
26
    /**
27
     * @covers \eZ\Publish\Core\Repository\Helper\DomainMapper::buildVersionInfoDomainObject
28
     * @dataProvider providerForBuildVersionInfo
29
     */
30
    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...
31
    {
32
        $languageHandlerMock = $this->getLanguageHandlerMock();
33
        $languageHandlerMock->expects($this->never())->method('load');
34
35
        $versionInfo = $this->getDomainMapper()->buildVersionInfoDomainObject($spiVersionInfo);
36
        $this->assertInstanceOf('eZ\\Publish\\Core\\Repository\\Values\\Content\\VersionInfo', $versionInfo);
37
38
        foreach ($expected as $expectedProperty => $expectedValue) {
39
            $this->assertAttributeSame(
40
                $expectedValue,
41
                $expectedProperty,
42
                $versionInfo
43
            );
44
        }
45
    }
46
47
    public function providerForBuildVersionInfo()
48
    {
49
        return array(
50
            array(
51
                new SPIVersionInfo(
52
                    array(
53
                        'status' => 44,
54
                        'contentInfo' => new SPIContentInfo(),
55
                    )
56
                ),
57
                array(),
58
                array('status' => APIVersionInfo::STATUS_DRAFT),
59
            ),
60
            array(
61
                new SPIVersionInfo(
62
                    array(
63
                        'status' => SPIVersionInfo::STATUS_DRAFT,
64
                        'contentInfo' => new SPIContentInfo(),
65
                    )
66
                ),
67
                array(),
68
                array('status' => APIVersionInfo::STATUS_DRAFT),
69
            ),
70
            array(
71
                new SPIVersionInfo(
72
                    array(
73
                        'status' => SPIVersionInfo::STATUS_PENDING,
74
                        'contentInfo' => new SPIContentInfo(),
75
                    )
76
                ),
77
                array(),
78
                array('status' => APIVersionInfo::STATUS_DRAFT),
79
            ),
80
            array(
81
                new SPIVersionInfo(
82
                    array(
83
                        'status' => SPIVersionInfo::STATUS_ARCHIVED,
84
                        'contentInfo' => new SPIContentInfo(),
85
                        'languageCodes' => array('eng-GB', 'nor-NB', 'fre-FR'),
86
                    )
87
                ),
88
                array(1 => 'eng-GB', 3 => 'nor-NB', 5 => 'fre-FR'),
89
                array(
90
                    'status' => APIVersionInfo::STATUS_ARCHIVED,
91
                    'languageCodes' => array('eng-GB', 'nor-NB', 'fre-FR'),
92
                ),
93
            ),
94
            array(
95
                new SPIVersionInfo(
96
                    array(
97
                        'status' => SPIVersionInfo::STATUS_PUBLISHED,
98
                        'contentInfo' => new SPIContentInfo(),
99
                    )
100
                ),
101
                array(),
102
                array('status' => APIVersionInfo::STATUS_PUBLISHED),
103
            ),
104
        );
105
    }
106
107
    public function providerForBuildLocationDomainObjectsOnSearchResult()
108
    {
109
        $locationHits = [
110
            new Location(['id' => 21, 'contentId' => 32]),
111
            new Location(['id' => 22, 'contentId' => 33]),
112
        ];
113
114
        return [
115
            [$locationHits, [32, 33], [32 => new ContentInfo(['id' => 32]), 33 => new ContentInfo(['id' => 33])], 0],
116
            [$locationHits, [32, 33], [32 => new ContentInfo(['id' => 32])], 1],
117
            [$locationHits, [32, 33], [], 2],
118
        ];
119
    }
120
121
    /**
122
     * @covers \eZ\Publish\Core\Repository\Helper\DomainMapper::buildLocationDomainObjectsOnSearchResult
123
     * @dataProvider providerForBuildLocationDomainObjectsOnSearchResult
124
     *
125
     * @param array $locationHits
126
     * @param array $contentIds
127
     * @param array $contentInfoList
128
     * @param int $missing
129
     */
130
    public function testBuildLocationDomainObjectsOnSearchResult(
131
        array $locationHits,
132
        array $contentIds,
133
        array $contentInfoList,
134
        int $missing
135
    ) {
136
        $contentHandlerMock = $this->getContentHandlerMock();
137
        $contentHandlerMock
138
            ->expects($this->once())
139
            ->method('loadContentInfoList')
140
            ->with($contentIds)
141
            ->willReturn($contentInfoList);
142
143
        $result = new SearchResult(['totalCount' => 10]);
144
        foreach ($locationHits as $locationHit) {
145
            $result->searchHits[] = new SearchHit(['valueObject' => $locationHit]);
146
        }
147
148
        $spiResult = clone $result;
149
        $missingLocations = $this->getDomainMapper()->buildLocationDomainObjectsOnSearchResult($result);
150
        $this->assertInternalType('array', $missingLocations);
151
152
        if (!$missing) {
153
            $this->assertEmpty($missingLocations);
154
        } else {
155
            $this->assertNotEmpty($missingLocations);
156
        }
157
158
        $this->assertCount($missing, $missingLocations);
159
        $this->assertEquals($spiResult->totalCount - $missing, $result->totalCount);
160
        $this->assertCount(count($spiResult->searchHits) - $missing, $result->searchHits);
161
    }
162
163
    /**
164
     * Returns DomainMapper.
165
     *
166
     * @return \eZ\Publish\Core\Repository\Helper\DomainMapper
167
     */
168
    protected function getDomainMapper()
169
    {
170
        return new DomainMapper(
171
            $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...
172
            $this->getPersistenceMockHandler('Content\\Location\\Handler'),
173
            $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...
174
            $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...
175
            $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...
176
        );
177
    }
178
179
    /**
180
     * @return \eZ\Publish\SPI\Persistence\Content\Handler|\PHPUnit_Framework_MockObject_MockObject
181
     */
182
    protected function getContentHandlerMock()
183
    {
184
        return $this->getPersistenceMockHandler('Content\\Handler');
185
    }
186
187
    /**
188
     * @return \eZ\Publish\SPI\Persistence\Content\Language\Handler|\PHPUnit_Framework_MockObject_MockObject
189
     */
190
    protected function getLanguageHandlerMock()
191
    {
192
        return $this->getPersistenceMockHandler('Content\\Language\\Handler');
193
    }
194
195
    /**
196
     * @return \eZ\Publish\SPI\Persistence\Content\Type\Handler|\PHPUnit_Framework_MockObject_MockObject
197
     */
198
    protected function getTypeHandlerMock()
199
    {
200
        return $this->getPersistenceMockHandler('Content\\Type\\Handler');
201
    }
202
}
203