Completed
Push — location_content_property ( b180d5 )
by André
16:28
created

SearchTest   B

Complexity

Total Complexity 25

Size/Duplication

Total Lines 969
Duplicated Lines 16.92 %

Coupling/Cohesion

Components 1
Dependencies 17

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 164
loc 969
rs 7.8722
c 1
b 0
f 0
wmc 25
lcom 1
cbo 17

21 Methods

Rating   Name   Duplication   Size   Complexity  
A testConstructor() 0 48 1
B providerForFindContentValidatesLocationCriteriaAndSortClauses() 0 29 1
B testFindContentValidatesLocationCriteriaAndSortClauses() 25 25 2
A providerForFindSingleValidatesLocationCriteria() 0 17 1
B testFindSingleValidatesLocationCriteria() 24 24 2
B testFindContentThrowsHandlerException() 30 30 1
B testFindContentWhenDomainMapperThrowsException() 0 39 1
A testFindContentNoPermissionsFilter() 0 58 1
A testFindContentWithPermission() 0 66 1
B testFindContentWithNoPermission() 0 40 1
A testFindContentWithDefaultQueryValues() 0 64 1
B testFindSingleThrowsNotFoundException() 27 27 1
B testFindSingleThrowsHandlerException() 28 28 1
A testFindSingle() 0 61 1
A testFindLocationsWithPermission() 0 67 1
A testFindLocationsWithNoPermissionsFilter() 0 63 1
B testFindLocationsBackgroundIndexerWhenDomainMapperThrowsException() 0 44 1
B testFindLocationsThrowsHandlerException() 30 30 1
A testFindLocationsWithDefaultQueryValues() 0 64 1
A getDomainMapperMock() 0 11 2
A getPermissionCriterionResolverMock() 0 11 2

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\Mock\SearchTest 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\Core\Base\Exceptions\NotFoundException;
12
use eZ\Publish\Core\Base\Exceptions\UnauthorizedException;
13
use eZ\Publish\Core\Repository\ContentService;
14
use eZ\Publish\Core\Repository\Helper\DomainMapper;
15
use eZ\Publish\Core\Repository\Tests\Service\Mock\Base as BaseServiceMockTest;
16
use eZ\Publish\Core\Repository\SearchService;
17
use eZ\Publish\Core\Repository\Permission\PermissionCriterionResolver;
18
use eZ\Publish\API\Repository\Values\Content\Content;
19
use eZ\Publish\API\Repository\Values\Content\Location;
20
use eZ\Publish\API\Repository\Values\Content\Query;
21
use eZ\Publish\API\Repository\Values\Content\LocationQuery;
22
use eZ\Publish\API\Repository\Values\Content\Query\Criterion;
23
use eZ\Publish\API\Repository\Values\Content\Query\SortClause;
24
use eZ\Publish\API\Repository\Values\Content\Search\SearchResult;
25
use eZ\Publish\API\Repository\Values\Content\Search\SearchHit;
26
use eZ\Publish\Core\Search\Common\BackgroundIndexer;
27
use eZ\Publish\Core\Search\Common\BackgroundIndexer\NullIndexer;
28
use eZ\Publish\SPI\Persistence\Content\ContentInfo as SPIContentInfo;
29
use eZ\Publish\SPI\Persistence\Content\Location as SPILocation;
30
use eZ\Publish\API\Repository\Exceptions\InvalidArgumentException;
31
use Exception;
32
33
/**
34
 * Mock test case for Search service.
35
 */
36
class SearchTest extends BaseServiceMockTest
37
{
38
    protected $repositoryMock;
39
40
    protected $domainMapperMock;
41
42
    protected $permissionsCriterionResolverMock;
43
44
    /**
45
     * Test for the __construct() method.
46
     *
47
     * @covers \eZ\Publish\Core\Repository\SearchService::__construct
48
     */
49
    public function testConstructor()
50
    {
51
        $repositoryMock = $this->getRepositoryMock();
52
        /** @var \eZ\Publish\SPI\Search\Handler $searchHandlerMock */
53
        $searchHandlerMock = $this->getSPIMockHandler('Search\\Handler');
54
        $domainMapperMock = $this->getDomainMapperMock();
55
        $permissionsCriterionResolverMock = $this->getPermissionCriterionResolverMock();
56
        $settings = array('teh setting');
57
58
        $service = new SearchService(
59
            $repositoryMock,
0 ignored issues
show
Bug introduced by
It seems like $repositoryMock defined by $this->getRepositoryMock() on line 51 can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, eZ\Publish\Core\Reposito...hService::__construct() does only seem to accept object<eZ\Publish\API\Repository\Repository>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
60
            $searchHandlerMock,
61
            $domainMapperMock,
0 ignored issues
show
Bug introduced by
It seems like $domainMapperMock defined by $this->getDomainMapperMock() on line 54 can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, eZ\Publish\Core\Reposito...hService::__construct() does only seem to accept object<eZ\Publish\Core\R...ry\Helper\DomainMapper>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
62
            $permissionsCriterionResolverMock,
0 ignored issues
show
Bug introduced by
It seems like $permissionsCriterionResolverMock defined by $this->getPermissionCriterionResolverMock() on line 55 can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, eZ\Publish\Core\Reposito...hService::__construct() does only seem to accept object<eZ\Publish\API\Re...ssionCriterionResolver>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
63
            new NullIndexer(),
64
            $settings
65
        );
66
67
        $this->assertAttributeSame(
68
            $repositoryMock,
69
            'repository',
70
            $service
71
        );
72
73
        $this->assertAttributeSame(
74
            $searchHandlerMock,
75
            'searchHandler',
76
            $service
77
        );
78
79
        $this->assertAttributeSame(
80
            $domainMapperMock,
81
            'domainMapper',
82
            $service
83
        );
84
85
        $this->assertAttributeSame(
86
            $permissionsCriterionResolverMock,
87
            'permissionCriterionResolver',
88
            $service
89
        );
90
91
        $this->assertAttributeSame(
92
            $settings,
93
            'settings',
94
            $service
95
        );
96
    }
97
98
    public function providerForFindContentValidatesLocationCriteriaAndSortClauses()
99
    {
100
        return array(
101
            array(
102
                new Query(array('filter' => new Criterion\Location\Depth(Criterion\Operator::LT, 2))),
103
                "Argument '\$query' is invalid: Location criterions cannot be used in Content search",
104
            ),
105
            array(
106
                new Query(array('query' => new Criterion\Location\Depth(Criterion\Operator::LT, 2))),
107
                "Argument '\$query' is invalid: Location criterions cannot be used in Content search",
108
            ),
109
            array(
110
                new Query(
111
                    array(
112
                        'query' => new Criterion\LogicalAnd(
113
                            array(
114
                                new Criterion\Location\Depth(Criterion\Operator::LT, 2),
115
                            )
116
                        ),
117
                    )
118
                ),
119
                "Argument '\$query' is invalid: Location criterions cannot be used in Content search",
120
            ),
121
            array(
122
                new Query(array('sortClauses' => array(new SortClause\Location\Id()))),
123
                "Argument '\$query' is invalid: Location sort clauses cannot be used in Content search",
124
            ),
125
        );
126
    }
127
128
    /**
129
     * @dataProvider providerForFindContentValidatesLocationCriteriaAndSortClauses
130
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
131
     */
132 View Code Duplication
    public function testFindContentValidatesLocationCriteriaAndSortClauses($query, $exceptionMessage)
133
    {
134
        $repositoryMock = $this->getRepositoryMock();
135
        /** @var \eZ\Publish\SPI\Search\Handler $searchHandlerMock */
136
        $searchHandlerMock = $this->getSPIMockHandler('Search\\Handler');
137
        $permissionsCriterionResolverMock = $this->getPermissionCriterionResolverMock();
138
139
        $service = new SearchService(
140
            $repositoryMock,
0 ignored issues
show
Bug introduced by
It seems like $repositoryMock defined by $this->getRepositoryMock() on line 134 can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, eZ\Publish\Core\Reposito...hService::__construct() does only seem to accept object<eZ\Publish\API\Repository\Repository>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
141
            $searchHandlerMock,
142
            $this->getDomainMapperMock(),
0 ignored issues
show
Bug introduced by
It seems like $this->getDomainMapperMock() targeting eZ\Publish\Core\Reposito...::getDomainMapperMock() can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, eZ\Publish\Core\Reposito...hService::__construct() does only seem to accept object<eZ\Publish\Core\R...ry\Helper\DomainMapper>, 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...
143
            $permissionsCriterionResolverMock,
0 ignored issues
show
Bug introduced by
It seems like $permissionsCriterionResolverMock defined by $this->getPermissionCriterionResolverMock() on line 137 can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, eZ\Publish\Core\Reposito...hService::__construct() does only seem to accept object<eZ\Publish\API\Re...ssionCriterionResolver>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
144
            new NullIndexer(),
145
            array()
146
        );
147
148
        try {
149
            $service->findContent($query);
150
        } catch (InvalidArgumentException $e) {
151
            $this->assertEquals($exceptionMessage, $e->getMessage());
152
            throw $e;
153
        }
154
155
        $this->fail('Expected exception was not thrown');
156
    }
157
158
    public function providerForFindSingleValidatesLocationCriteria()
159
    {
160
        return array(
161
            array(
162
                new Criterion\Location\Depth(Criterion\Operator::LT, 2),
163
                "Argument '\$filter' is invalid: Location criterions cannot be used in Content search",
164
            ),
165
            array(
166
                new Criterion\LogicalAnd(
167
                    array(
168
                        new Criterion\Location\Depth(Criterion\Operator::LT, 2),
169
                    )
170
                ),
171
                "Argument '\$filter' is invalid: Location criterions cannot be used in Content search",
172
            ),
173
        );
174
    }
175
176
    /**
177
     * @dataProvider providerForFindSingleValidatesLocationCriteria
178
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
179
     */
180 View Code Duplication
    public function testFindSingleValidatesLocationCriteria($criterion, $exceptionMessage)
181
    {
182
        $repositoryMock = $this->getRepositoryMock();
183
        /** @var \eZ\Publish\SPI\Search\Handler $searchHandlerMock */
184
        $searchHandlerMock = $this->getSPIMockHandler('Search\\Handler');
185
        $permissionsCriterionResolverMock = $this->getPermissionCriterionResolverMock();
186
        $service = new SearchService(
187
            $repositoryMock,
0 ignored issues
show
Bug introduced by
It seems like $repositoryMock defined by $this->getRepositoryMock() on line 182 can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, eZ\Publish\Core\Reposito...hService::__construct() does only seem to accept object<eZ\Publish\API\Repository\Repository>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
188
            $searchHandlerMock,
189
            $this->getDomainMapperMock(),
0 ignored issues
show
Bug introduced by
It seems like $this->getDomainMapperMock() targeting eZ\Publish\Core\Reposito...::getDomainMapperMock() can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, eZ\Publish\Core\Reposito...hService::__construct() does only seem to accept object<eZ\Publish\Core\R...ry\Helper\DomainMapper>, 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...
190
            $permissionsCriterionResolverMock,
0 ignored issues
show
Bug introduced by
It seems like $permissionsCriterionResolverMock defined by $this->getPermissionCriterionResolverMock() on line 185 can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, eZ\Publish\Core\Reposito...hService::__construct() does only seem to accept object<eZ\Publish\API\Re...ssionCriterionResolver>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
191
            new NullIndexer(),
192
            array()
193
        );
194
195
        try {
196
            $service->findSingle($criterion);
197
        } catch (InvalidArgumentException $e) {
198
            $this->assertEquals($exceptionMessage, $e->getMessage());
199
            throw $e;
200
        }
201
202
        $this->fail('Expected exception was not thrown');
203
    }
204
205
    /**
206
     * Test for the findContent() method.
207
     *
208
     * @covers \eZ\Publish\Core\Repository\SearchService::addPermissionsCriterion
209
     * @covers \eZ\Publish\Core\Repository\SearchService::findContent
210
     * @expectedException \Exception
211
     * @expectedExceptionMessage Handler threw an exception
212
     */
213 View Code Duplication
    public function testFindContentThrowsHandlerException()
214
    {
215
        $repositoryMock = $this->getRepositoryMock();
216
        /** @var \eZ\Publish\SPI\Search\Handler $searchHandlerMock */
217
        $searchHandlerMock = $this->getSPIMockHandler('Search\\Handler');
218
        $permissionsCriterionResolverMock = $this->getPermissionCriterionResolverMock();
219
220
        $service = new SearchService(
221
            $repositoryMock,
0 ignored issues
show
Bug introduced by
It seems like $repositoryMock defined by $this->getRepositoryMock() on line 215 can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, eZ\Publish\Core\Reposito...hService::__construct() does only seem to accept object<eZ\Publish\API\Repository\Repository>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
222
            $searchHandlerMock,
223
            $this->getDomainMapperMock(),
0 ignored issues
show
Bug introduced by
It seems like $this->getDomainMapperMock() targeting eZ\Publish\Core\Reposito...::getDomainMapperMock() can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, eZ\Publish\Core\Reposito...hService::__construct() does only seem to accept object<eZ\Publish\Core\R...ry\Helper\DomainMapper>, 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...
224
            $permissionsCriterionResolverMock,
0 ignored issues
show
Bug introduced by
It seems like $permissionsCriterionResolverMock defined by $this->getPermissionCriterionResolverMock() on line 218 can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, eZ\Publish\Core\Reposito...hService::__construct() does only seem to accept object<eZ\Publish\API\Re...ssionCriterionResolver>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
225
            new NullIndexer(),
226
            array()
227
        );
228
229
        /** @var \eZ\Publish\API\Repository\Values\Content\Query\Criterion $criterionMock */
230
        $criterionMock = $this
231
            ->getMockBuilder(Criterion::class)
232
            ->disableOriginalConstructor()
233
            ->getMock();
234
        $query = new Query(array('filter' => $criterionMock));
235
236
        $permissionsCriterionResolverMock->expects($this->once())
237
            ->method('getPermissionsCriterion')
238
            ->with('content', 'read')
239
            ->will($this->throwException(new Exception('Handler threw an exception')));
240
241
        $service->findContent($query, array(), true);
242
    }
243
244
    /**
245
     * Test for the findContent() method when search is out of sync with persistence.
246
     *
247
     * @covers \eZ\Publish\Core\Repository\SearchService::findContent
248
     */
249
    public function testFindContentWhenDomainMapperThrowsException()
250
    {
251
        $indexer = $this->createMock(BackgroundIndexer::class);
252
        $indexer->expects($this->once())
253
            ->method('registerContent')
254
            ->with($this->isInstanceOf(SPIContentInfo::class));
255
256
        $service = $this->getMockBuilder(SearchService::class)
257
            ->setConstructorArgs([
258
                $this->getRepositoryMock(),
259
                $this->getSPIMockHandler('Search\\Handler'),
260
                $mapper = $this->getDomainMapperMock(),
261
                $this->getPermissionCriterionResolverMock(),
262
                $indexer,
263
            ])->setMethods(['internalFindContentInfo'])
264
            ->getMock();
265
266
        $info = new SPIContentInfo(['id' => 33]);
267
        $result = new SearchResult(['searchHits' => [new SearchHit(['valueObject' => $info])], 'totalCount' => 2]);
268
        $service->expects($this->once())
269
            ->method('internalFindContentInfo')
270
            ->with($this->isInstanceOf(Query::class))
271
            ->willReturn($result);
272
273
        $mapper->expects($this->once())
274
            ->method('buildContentDomainObjectsOnSearchResult')
275
            ->with($this->equalTo($result), $this->equalTo([]))
276
            ->willReturnCallback(function (SearchResult $spiResult) use ($info) {
277
                unset($spiResult->searchHits[0]);
278
                --$spiResult->totalCount;
279
280
                return [$info];
281
            });
282
283
        $finalResult = $service->findContent(new Query());
284
285
        $this->assertEmpty($finalResult->searchHits, 'Expected search hits to be empty');
286
        $this->assertEquals(1, $finalResult->totalCount, 'Expected total count to be 1');
287
    }
288
289
    /**
290
     * Test for the findContent() method.
291
     *
292
     * @covers \eZ\Publish\Core\Repository\SearchService::addPermissionsCriterion
293
     * @covers \eZ\Publish\Core\Repository\SearchService::findContent
294
     */
295
    public function testFindContentNoPermissionsFilter()
296
    {
297
        /** @var \eZ\Publish\SPI\Search\Handler $searchHandlerMock */
298
        $searchHandlerMock = $this->getSPIMockHandler('Search\\Handler');
299
        $repositoryMock = $this->getRepositoryMock();
300
        $service = new SearchService(
301
            $repositoryMock,
0 ignored issues
show
Bug introduced by
It seems like $repositoryMock defined by $this->getRepositoryMock() on line 299 can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, eZ\Publish\Core\Reposito...hService::__construct() does only seem to accept object<eZ\Publish\API\Repository\Repository>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
302
            $searchHandlerMock,
303
            $mapper = $this->getDomainMapperMock(),
0 ignored issues
show
Bug introduced by
It seems like $mapper = $this->getDomainMapperMock() can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, eZ\Publish\Core\Reposito...hService::__construct() does only seem to accept object<eZ\Publish\Core\R...ry\Helper\DomainMapper>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
304
            $permissionsCriterionResolverMock = $this->getPermissionCriterionResolverMock(),
0 ignored issues
show
Bug introduced by
It seems like $permissionsCriterionRes...CriterionResolverMock() can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, eZ\Publish\Core\Reposito...hService::__construct() does only seem to accept object<eZ\Publish\API\Re...ssionCriterionResolver>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
305
            new NullIndexer(),
306
            array()
307
        );
308
309
        $repositoryMock->expects($this->never())->method('hasAccess');
310
311
        $serviceQuery = new Query();
312
        $handlerQuery = new Query(array('filter' => new Criterion\MatchAll(), 'limit' => 25));
313
        $languageFilter = array();
314
        $spiContentInfo = new SPIContentInfo();
315
        $contentMock = $this->getMockForAbstractClass(Content::class);
316
317
        /* @var \PHPUnit\Framework\MockObject\MockObject $searchHandlerMock */
318
        $searchHandlerMock->expects($this->once())
319
            ->method('findContent')
320
            ->with($this->equalTo($handlerQuery), $this->equalTo($languageFilter))
321
            ->will(
322
                $this->returnValue(
323
                    new SearchResult(
324
                        array(
325
                            'searchHits' => array(new SearchHit(array('valueObject' => $spiContentInfo))),
326
                            'totalCount' => 1,
327
                        )
328
                    )
329
                )
330
            );
331
332
        $mapper->expects($this->once())
333
            ->method('buildContentDomainObjectsOnSearchResult')
334
            ->with($this->isInstanceOf(SearchResult::class), $this->equalTo([]))
335
            ->willReturnCallback(function (SearchResult $spiResult) use ($contentMock) {
336
                $spiResult->searchHits[0]->valueObject = $contentMock;
0 ignored issues
show
Documentation Bug introduced by
It seems like $contentMock of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<eZ\Publish\API\Re...ory\Values\ValueObject> of property $valueObject.

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...
337
338
                return [];
339
            });
340
341
        $result = $service->findContent($serviceQuery, $languageFilter, false);
342
343
        $this->assertEquals(
344
            new SearchResult(
345
                array(
346
                    'searchHits' => array(new SearchHit(array('valueObject' => $contentMock))),
347
                    'totalCount' => 1,
348
                )
349
            ),
350
            $result
351
        );
352
    }
353
354
    /**
355
     * Test for the findContent() method.
356
     *
357
     * @covers \eZ\Publish\Core\Repository\SearchService::addPermissionsCriterion
358
     * @covers \eZ\Publish\Core\Repository\SearchService::findContent
359
     */
360
    public function testFindContentWithPermission()
361
    {
362
        /** @var \eZ\Publish\SPI\Search\Handler $searchHandlerMock */
363
        $searchHandlerMock = $this->getSPIMockHandler('Search\\Handler');
364
        $domainMapperMock = $this->getDomainMapperMock();
365
        $permissionsCriterionResolverMock = $this->getPermissionCriterionResolverMock();
366
        $service = new SearchService(
367
            $this->getRepositoryMock(),
0 ignored issues
show
Bug introduced by
It seems like $this->getRepositoryMock() targeting eZ\Publish\Core\Reposito...se::getRepositoryMock() can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, eZ\Publish\Core\Reposito...hService::__construct() does only seem to accept object<eZ\Publish\API\Repository\Repository>, 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...
368
            $searchHandlerMock,
369
            $domainMapperMock,
0 ignored issues
show
Bug introduced by
It seems like $domainMapperMock defined by $this->getDomainMapperMock() on line 364 can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, eZ\Publish\Core\Reposito...hService::__construct() does only seem to accept object<eZ\Publish\Core\R...ry\Helper\DomainMapper>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
370
            $permissionsCriterionResolverMock,
0 ignored issues
show
Bug introduced by
It seems like $permissionsCriterionResolverMock defined by $this->getPermissionCriterionResolverMock() on line 365 can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, eZ\Publish\Core\Reposito...hService::__construct() does only seem to accept object<eZ\Publish\API\Re...ssionCriterionResolver>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
371
            new NullIndexer(),
372
            array()
373
        );
374
375
        $criterionMock = $this
376
            ->getMockBuilder(Criterion::class)
377
            ->disableOriginalConstructor()
378
            ->getMock();
379
        $query = new Query(array('filter' => $criterionMock, 'limit' => 10));
380
        $languageFilter = array();
381
        $spiContentInfo = new SPIContentInfo();
382
        $contentMock = $this->getMockForAbstractClass(Content::class);
383
384
        $permissionsCriterionResolverMock->expects($this->once())
385
            ->method('getPermissionsCriterion')
386
            ->with('content', 'read')
387
            ->will($this->returnValue(true));
388
389
        /* @var \PHPUnit\Framework\MockObject\MockObject $searchHandlerMock */
390
        $searchHandlerMock->expects($this->once())
391
            ->method('findContent')
392
            ->with($this->equalTo($query), $this->equalTo($languageFilter))
393
            ->will(
394
                $this->returnValue(
395
                    new SearchResult(
396
                        array(
397
                            'searchHits' => array(new SearchHit(array('valueObject' => $spiContentInfo))),
398
                            'totalCount' => 1,
399
                        )
400
                    )
401
                )
402
            );
403
404
        $domainMapperMock
405
            ->expects($this->once())
406
            ->method('buildContentDomainObjectsOnSearchResult')
407
            ->with($this->isInstanceOf(SearchResult::class), $this->equalTo([]))
408
            ->willReturnCallback(function (SearchResult $spiResult) use ($contentMock) {
409
                $spiResult->searchHits[0]->valueObject = $contentMock;
0 ignored issues
show
Documentation Bug introduced by
It seems like $contentMock of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<eZ\Publish\API\Re...ory\Values\ValueObject> of property $valueObject.

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...
410
411
                return [];
412
            });
413
414
        $result = $service->findContent($query, $languageFilter, true);
415
416
        $this->assertEquals(
417
            new SearchResult(
418
                array(
419
                    'searchHits' => array(new SearchHit(array('valueObject' => $contentMock))),
420
                    'totalCount' => 1,
421
                )
422
            ),
423
            $result
424
        );
425
    }
426
427
    /**
428
     * Test for the findContent() method.
429
     *
430
     * @covers \eZ\Publish\Core\Repository\SearchService::addPermissionsCriterion
431
     * @covers \eZ\Publish\Core\Repository\SearchService::findContent
432
     */
433
    public function testFindContentWithNoPermission()
434
    {
435
        /** @var \eZ\Publish\SPI\Search\Handler $searchHandlerMock */
436
        $searchHandlerMock = $this->getSPIMockHandler('Search\\Handler');
437
        $permissionsCriterionResolverMock = $this->getPermissionCriterionResolverMock();
438
        $service = new SearchService(
439
            $this->getRepositoryMock(),
0 ignored issues
show
Bug introduced by
It seems like $this->getRepositoryMock() targeting eZ\Publish\Core\Reposito...se::getRepositoryMock() can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, eZ\Publish\Core\Reposito...hService::__construct() does only seem to accept object<eZ\Publish\API\Repository\Repository>, 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...
440
            $searchHandlerMock,
441
            $mapper = $this->getDomainMapperMock(),
0 ignored issues
show
Bug introduced by
It seems like $mapper = $this->getDomainMapperMock() can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, eZ\Publish\Core\Reposito...hService::__construct() does only seem to accept object<eZ\Publish\Core\R...ry\Helper\DomainMapper>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
442
            $permissionsCriterionResolverMock,
0 ignored issues
show
Bug introduced by
It seems like $permissionsCriterionResolverMock defined by $this->getPermissionCriterionResolverMock() on line 437 can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, eZ\Publish\Core\Reposito...hService::__construct() does only seem to accept object<eZ\Publish\API\Re...ssionCriterionResolver>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
443
            new NullIndexer(),
444
            array()
445
        );
446
447
        /* @var \PHPUnit\Framework\MockObject\MockObject $searchHandlerMock */
448
        $searchHandlerMock->expects($this->never())->method('findContent');
449
450
        $criterionMock = $this
451
            ->getMockBuilder(Criterion::class)
452
            ->disableOriginalConstructor()
453
            ->getMock();
454
        $query = new Query(array('filter' => $criterionMock));
455
456
        $permissionsCriterionResolverMock->expects($this->once())
457
            ->method('getPermissionsCriterion')
458
            ->with('content', 'read')
459
            ->will($this->returnValue(false));
460
461
        $mapper->expects($this->once())
462
            ->method('buildContentDomainObjectsOnSearchResult')
463
            ->with($this->isInstanceOf(SearchResult::class), $this->equalTo([]))
464
            ->willReturn([]);
465
466
        $result = $service->findContent($query, array(), true);
467
468
        $this->assertEquals(
469
            new SearchResult(array('time' => 0, 'totalCount' => 0)),
470
            $result
471
        );
472
    }
473
474
    /**
475
     * Test for the findContent() method.
476
     */
477
    public function testFindContentWithDefaultQueryValues()
478
    {
479
        /** @var \eZ\Publish\SPI\Search\Handler $searchHandlerMock */
480
        $searchHandlerMock = $this->getSPIMockHandler('Search\\Handler');
481
        $domainMapperMock = $this->getDomainMapperMock();
482
        $service = new SearchService(
483
            $this->getRepositoryMock(),
0 ignored issues
show
Bug introduced by
It seems like $this->getRepositoryMock() targeting eZ\Publish\Core\Reposito...se::getRepositoryMock() can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, eZ\Publish\Core\Reposito...hService::__construct() does only seem to accept object<eZ\Publish\API\Repository\Repository>, 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...
484
            $searchHandlerMock,
485
            $domainMapperMock,
0 ignored issues
show
Bug introduced by
It seems like $domainMapperMock defined by $this->getDomainMapperMock() on line 481 can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, eZ\Publish\Core\Reposito...hService::__construct() does only seem to accept object<eZ\Publish\Core\R...ry\Helper\DomainMapper>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
486
            $this->getPermissionCriterionResolverMock(),
0 ignored issues
show
Bug introduced by
It seems like $this->getPermissionCriterionResolverMock() targeting eZ\Publish\Core\Reposito...CriterionResolverMock() can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, eZ\Publish\Core\Reposito...hService::__construct() does only seem to accept object<eZ\Publish\API\Re...ssionCriterionResolver>, 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...
487
            new NullIndexer(),
488
            array()
489
        );
490
491
        $languageFilter = array();
492
        $spiContentInfo = new SPIContentInfo();
493
        $contentMock = $this->getMockForAbstractClass(Content::class);
494
495
        /* @var \PHPUnit\Framework\MockObject\MockObject $searchHandlerMock */
496
        $searchHandlerMock
497
            ->expects($this->once())
498
            ->method('findContent')
499
            ->with(
500
                new Query(
501
                    array(
502
                        'filter' => new Criterion\MatchAll(),
503
                        'limit' => 25,
504
                    )
505
                ),
506
                array()
507
            )
508
            ->will(
509
                $this->returnValue(
510
                    new SearchResult(
511
                        array(
512
                            'searchHits' => array(new SearchHit(array('valueObject' => $spiContentInfo))),
513
                            'totalCount' => 1,
514
                        )
515
                    )
516
                )
517
            );
518
519
        $domainMapperMock
520
            ->expects($this->once())
521
            ->method('buildContentDomainObjectsOnSearchResult')
522
            ->with($this->isInstanceOf(SearchResult::class), $this->equalTo([]))
523
            ->willReturnCallback(function (SearchResult $spiResult) use ($contentMock) {
524
                $spiResult->searchHits[0]->valueObject = $contentMock;
0 ignored issues
show
Documentation Bug introduced by
It seems like $contentMock of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<eZ\Publish\API\Re...ory\Values\ValueObject> of property $valueObject.

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...
525
526
                return [];
527
            });
528
529
        $result = $service->findContent(new Query(), $languageFilter, false);
530
531
        $this->assertEquals(
532
            new SearchResult(
533
                array(
534
                    'searchHits' => array(new SearchHit(array('valueObject' => $contentMock))),
535
                    'totalCount' => 1,
536
                )
537
            ),
538
            $result
539
        );
540
    }
541
542
    /**
543
     * Test for the findSingle() method.
544
     *
545
     * @covers \eZ\Publish\Core\Repository\SearchService::addPermissionsCriterion
546
     * @covers \eZ\Publish\Core\Repository\SearchService::findSingle
547
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
548
     */
549 View Code Duplication
    public function testFindSingleThrowsNotFoundException()
550
    {
551
        $repositoryMock = $this->getRepositoryMock();
552
        /** @var \eZ\Publish\SPI\Search\Handler $searchHandlerMock */
553
        $searchHandlerMock = $this->getSPIMockHandler('Search\\Handler');
554
        $service = new SearchService(
555
            $repositoryMock,
0 ignored issues
show
Bug introduced by
It seems like $repositoryMock defined by $this->getRepositoryMock() on line 551 can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, eZ\Publish\Core\Reposito...hService::__construct() does only seem to accept object<eZ\Publish\API\Repository\Repository>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
556
            $searchHandlerMock,
557
            $this->getDomainMapperMock(),
0 ignored issues
show
Bug introduced by
It seems like $this->getDomainMapperMock() targeting eZ\Publish\Core\Reposito...::getDomainMapperMock() can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, eZ\Publish\Core\Reposito...hService::__construct() does only seem to accept object<eZ\Publish\Core\R...ry\Helper\DomainMapper>, 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...
558
            $permissionsCriterionResolverMock = $this->getPermissionCriterionResolverMock(),
0 ignored issues
show
Bug introduced by
It seems like $permissionsCriterionRes...CriterionResolverMock() can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, eZ\Publish\Core\Reposito...hService::__construct() does only seem to accept object<eZ\Publish\API\Re...ssionCriterionResolver>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
559
            new NullIndexer(),
560
            array()
561
        );
562
563
        /** @var \eZ\Publish\API\Repository\Values\Content\Query\Criterion $criterionMock */
564
        $criterionMock = $this
565
            ->getMockBuilder(Criterion::class)
566
            ->disableOriginalConstructor()
567
            ->getMock();
568
569
        $permissionsCriterionResolverMock->expects($this->once())
570
            ->method('getPermissionsCriterion')
571
            ->with('content', 'read')
572
            ->willReturn(false);
573
574
        $service->findSingle($criterionMock, array(), true);
575
    }
576
577
    /**
578
     * Test for the findSingle() method.
579
     *
580
     * @covers \eZ\Publish\Core\Repository\SearchService::addPermissionsCriterion
581
     * @covers \eZ\Publish\Core\Repository\SearchService::findSingle
582
     * @expectedException \Exception
583
     * @expectedExceptionMessage Handler threw an exception
584
     */
585 View Code Duplication
    public function testFindSingleThrowsHandlerException()
586
    {
587
        $repositoryMock = $this->getRepositoryMock();
588
        /** @var \eZ\Publish\SPI\Search\Handler $searchHandlerMock */
589
        $searchHandlerMock = $this->getSPIMockHandler('Search\\Handler');
590
        $permissionsCriterionResolverMock = $this->getPermissionCriterionResolverMock();
591
        $service = new SearchService(
592
            $repositoryMock,
0 ignored issues
show
Bug introduced by
It seems like $repositoryMock defined by $this->getRepositoryMock() on line 587 can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, eZ\Publish\Core\Reposito...hService::__construct() does only seem to accept object<eZ\Publish\API\Repository\Repository>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
593
            $searchHandlerMock,
594
            $this->getDomainMapperMock(),
0 ignored issues
show
Bug introduced by
It seems like $this->getDomainMapperMock() targeting eZ\Publish\Core\Reposito...::getDomainMapperMock() can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, eZ\Publish\Core\Reposito...hService::__construct() does only seem to accept object<eZ\Publish\Core\R...ry\Helper\DomainMapper>, 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...
595
            $permissionsCriterionResolverMock,
0 ignored issues
show
Bug introduced by
It seems like $permissionsCriterionResolverMock defined by $this->getPermissionCriterionResolverMock() on line 590 can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, eZ\Publish\Core\Reposito...hService::__construct() does only seem to accept object<eZ\Publish\API\Re...ssionCriterionResolver>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
596
            new NullIndexer(),
597
            array()
598
        );
599
600
        /** @var \eZ\Publish\API\Repository\Values\Content\Query\Criterion $criterionMock */
601
        $criterionMock = $this
602
            ->getMockBuilder(Criterion::class)
603
            ->disableOriginalConstructor()
604
            ->getMock();
605
606
        $permissionsCriterionResolverMock->expects($this->once())
607
            ->method('getPermissionsCriterion')
608
            ->with('content', 'read')
609
            ->will($this->throwException(new Exception('Handler threw an exception')));
610
611
        $service->findSingle($criterionMock, array(), true);
612
    }
613
614
    /**
615
     * Test for the findSingle() method.
616
     *
617
     * @covers \eZ\Publish\Core\Repository\SearchService::addPermissionsCriterion
618
     * @covers \eZ\Publish\Core\Repository\SearchService::findSingle
619
     */
620
    public function testFindSingle()
621
    {
622
        $repositoryMock = $this->getRepositoryMock();
623
        /** @var \eZ\Publish\SPI\Search\Handler $searchHandlerMock */
624
        $searchHandlerMock = $this->getSPIMockHandler('Search\\Handler');
625
        $domainMapperMock = $this->getDomainMapperMock();
626
        $permissionsCriterionResolverMock = $this->getPermissionCriterionResolverMock();
627
        $service = new SearchService(
628
            $repositoryMock,
0 ignored issues
show
Bug introduced by
It seems like $repositoryMock defined by $this->getRepositoryMock() on line 622 can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, eZ\Publish\Core\Reposito...hService::__construct() does only seem to accept object<eZ\Publish\API\Repository\Repository>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
629
            $searchHandlerMock,
630
            $domainMapperMock,
0 ignored issues
show
Bug introduced by
It seems like $domainMapperMock defined by $this->getDomainMapperMock() on line 625 can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, eZ\Publish\Core\Reposito...hService::__construct() does only seem to accept object<eZ\Publish\Core\R...ry\Helper\DomainMapper>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
631
            $permissionsCriterionResolverMock,
0 ignored issues
show
Bug introduced by
It seems like $permissionsCriterionResolverMock defined by $this->getPermissionCriterionResolverMock() on line 626 can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, eZ\Publish\Core\Reposito...hService::__construct() does only seem to accept object<eZ\Publish\API\Re...ssionCriterionResolver>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
632
            new NullIndexer(),
633
            array()
634
        );
635
636
        $repositoryMock
637
            ->expects($this->once())
638
            ->method('getContentService')
639
            ->will(
640
                $this->returnValue(
641
                    $contentServiceMock = $this
642
                        ->getMockBuilder(ContentService::class)
643
                        ->disableOriginalConstructor()
644
                        ->getMock()
645
                )
646
            );
647
648
        /** @var \eZ\Publish\API\Repository\Values\Content\Query\Criterion $criterionMock */
649
        $criterionMock = $this
650
            ->getMockBuilder(Criterion::class)
651
            ->disableOriginalConstructor()
652
            ->getMock();
653
654
        $permissionsCriterionResolverMock->expects($this->once())
655
            ->method('getPermissionsCriterion')
656
            ->with('content', 'read')
657
            ->will($this->returnValue(true));
658
659
        $languageFilter = array();
660
        $spiContentInfo = new SPIContentInfo();
661
        $contentMock = $this->getMockForAbstractClass(Content::class);
662
663
        /* @var \PHPUnit\Framework\MockObject\MockObject $searchHandlerMock */
664
        $searchHandlerMock->expects($this->once())
665
            ->method('findSingle')
666
            ->with($this->equalTo($criterionMock), $this->equalTo($languageFilter))
667
            ->will($this->returnValue($spiContentInfo));
668
669
        $domainMapperMock->expects($this->never())
670
            ->method($this->anything());
671
672
        $contentServiceMock
673
            ->expects($this->once())
674
            ->method('internalLoadContent')
675
            ->will($this->returnValue($contentMock));
676
677
        $result = $service->findSingle($criterionMock, $languageFilter, true);
678
679
        $this->assertEquals($contentMock, $result);
680
    }
681
682
    /**
683
     * Test for the findLocations() method.
684
     */
685
    public function testFindLocationsWithPermission()
686
    {
687
        $repositoryMock = $this->getRepositoryMock();
688
        /** @var \eZ\Publish\SPI\Search\Handler $searchHandlerMock */
689
        $searchHandlerMock = $this->getSPIMockHandler('Search\\Handler');
690
        $domainMapperMock = $this->getDomainMapperMock();
691
        $permissionsCriterionResolverMock = $this->getPermissionCriterionResolverMock();
692
        $service = new SearchService(
693
            $repositoryMock,
0 ignored issues
show
Bug introduced by
It seems like $repositoryMock defined by $this->getRepositoryMock() on line 687 can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, eZ\Publish\Core\Reposito...hService::__construct() does only seem to accept object<eZ\Publish\API\Repository\Repository>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
694
            $searchHandlerMock,
695
            $domainMapperMock,
0 ignored issues
show
Bug introduced by
It seems like $domainMapperMock defined by $this->getDomainMapperMock() on line 690 can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, eZ\Publish\Core\Reposito...hService::__construct() does only seem to accept object<eZ\Publish\Core\R...ry\Helper\DomainMapper>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
696
            $permissionsCriterionResolverMock,
0 ignored issues
show
Bug introduced by
It seems like $permissionsCriterionResolverMock defined by $this->getPermissionCriterionResolverMock() on line 691 can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, eZ\Publish\Core\Reposito...hService::__construct() does only seem to accept object<eZ\Publish\API\Re...ssionCriterionResolver>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
697
            new NullIndexer(),
698
            array()
699
        );
700
701
        $criterionMock = $this
702
            ->getMockBuilder(Criterion::class)
703
            ->disableOriginalConstructor()
704
            ->getMock();
705
        $query = new LocationQuery(array('filter' => $criterionMock, 'limit' => 10));
706
        $spiLocation = new SPILocation();
707
        $locationMock = $this->getMockForAbstractClass(Location::class);
708
709
        /* @var \PHPUnit\Framework\MockObject\MockObject $searchHandlerMock */
710
        $searchHandlerMock->expects($this->once())
711
            ->method('findLocations')
712
            ->with($this->equalTo($query))
713
            ->will(
714
                $this->returnValue(
715
                    $spiResult = new SearchResult(
716
                        array(
717
                            'searchHits' => array(new SearchHit(array('valueObject' => $spiLocation))),
718
                            'totalCount' => 1,
719
                        )
720
                    )
721
                )
722
            );
723
724
        $permissionsCriterionResolverMock->expects($this->once())
725
            ->method('getPermissionsCriterion')
726
            ->with('content', 'read')
727
            ->will($this->returnValue(true));
728
729
        $endResult = new SearchResult(
730
            [
731
                'searchHits' => [new SearchHit(['valueObject' => $locationMock])],
732
                'totalCount' => 1,
733
            ]
734
        );
735
736
        $domainMapperMock->expects($this->once())
737
            ->method('buildLocationDomainObjectsOnSearchResult')
738
            ->with($this->equalTo($spiResult))
739
            ->willReturnCallback(function (SearchResult $spiResult) use ($endResult) {
740
                $spiResult->searchHits[0] = $endResult->searchHits[0];
741
742
                return [];
743
            });
744
745
        $result = $service->findLocations($query, array(), true);
746
747
        $this->assertEquals(
748
            $endResult,
749
            $result
750
        );
751
    }
752
753
    /**
754
     * Test for the findLocations() method.
755
     */
756
    public function testFindLocationsWithNoPermissionsFilter()
757
    {
758
        $repositoryMock = $this->getRepositoryMock();
759
        /** @var \eZ\Publish\SPI\Search\Handler $searchHandlerMock */
760
        $searchHandlerMock = $this->getSPIMockHandler('Search\\Handler');
761
        $domainMapperMock = $this->getDomainMapperMock();
762
        $permissionsCriterionResolverMock = $this->getPermissionCriterionResolverMock();
763
        $service = new SearchService(
764
            $repositoryMock,
0 ignored issues
show
Bug introduced by
It seems like $repositoryMock defined by $this->getRepositoryMock() on line 758 can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, eZ\Publish\Core\Reposito...hService::__construct() does only seem to accept object<eZ\Publish\API\Repository\Repository>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
765
            $searchHandlerMock,
766
            $domainMapperMock,
0 ignored issues
show
Bug introduced by
It seems like $domainMapperMock defined by $this->getDomainMapperMock() on line 761 can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, eZ\Publish\Core\Reposito...hService::__construct() does only seem to accept object<eZ\Publish\Core\R...ry\Helper\DomainMapper>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
767
            $permissionsCriterionResolverMock,
0 ignored issues
show
Bug introduced by
It seems like $permissionsCriterionResolverMock defined by $this->getPermissionCriterionResolverMock() on line 762 can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, eZ\Publish\Core\Reposito...hService::__construct() does only seem to accept object<eZ\Publish\API\Re...ssionCriterionResolver>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
768
            new NullIndexer(),
769
            array()
770
        );
771
772
        $repositoryMock->expects($this->never())->method('hasAccess');
773
774
        $serviceQuery = new LocationQuery();
775
        $handlerQuery = new LocationQuery(array('filter' => new Criterion\MatchAll(), 'limit' => 25));
776
        $spiLocation = new SPILocation();
777
        $locationMock = $this->getMockForAbstractClass(Location::class);
778
779
        /* @var \PHPUnit\Framework\MockObject\MockObject $searchHandlerMock */
780
        $searchHandlerMock->expects($this->once())
781
            ->method('findLocations')
782
            ->with($this->equalTo($handlerQuery))
783
            ->will(
784
                $this->returnValue(
785
                    $spiResult = new SearchResult(
786
                        array(
787
                            'searchHits' => array(new SearchHit(array('valueObject' => $spiLocation))),
788
                            'totalCount' => 1,
789
                        )
790
                    )
791
                )
792
            );
793
794
        $permissionsCriterionResolverMock->expects($this->never())->method($this->anything());
795
796
        $endResult = new SearchResult(
797
            [
798
                'searchHits' => [new SearchHit(['valueObject' => $locationMock])],
799
                'totalCount' => 1,
800
            ]
801
        );
802
803
        $domainMapperMock->expects($this->once())
804
            ->method('buildLocationDomainObjectsOnSearchResult')
805
            ->with($this->equalTo($spiResult))
806
            ->willReturnCallback(function (SearchResult $spiResult) use ($endResult) {
807
                $spiResult->searchHits[0] = $endResult->searchHits[0];
808
809
                return [];
810
            });
811
812
        $result = $service->findLocations($serviceQuery, array(), false);
813
814
        $this->assertEquals(
815
            $endResult,
816
            $result
817
        );
818
    }
819
820
    /**
821
     * Test for the findLocations() method when search is out of sync with persistence.
822
     *
823
     * @covers \eZ\Publish\Core\Repository\SearchService::findLocations
824
     */
825
    public function testFindLocationsBackgroundIndexerWhenDomainMapperThrowsException()
826
    {
827
        $indexer = $this->createMock(BackgroundIndexer::class);
828
        $indexer->expects($this->once())
829
            ->method('registerLocation')
830
            ->with($this->isInstanceOf(SPILocation::class));
831
832
        $service = $this->getMockBuilder(SearchService::class)
833
            ->setConstructorArgs([
834
                $this->getRepositoryMock(),
835
                $searchHandler = $this->getSPIMockHandler('Search\\Handler'),
836
                $mapper = $this->getDomainMapperMock(),
837
                $this->getPermissionCriterionResolverMock(),
838
                $indexer,
839
            ])->setMethods(['addPermissionsCriterion'])
840
            ->getMock();
841
842
        $location = new SPILocation(['id' => 44]);
843
        $service->expects($this->once())
844
            ->method('addPermissionsCriterion')
845
            ->with($this->isInstanceOf(Criterion::class))
846
            ->willReturn(true);
847
848
        $result = new SearchResult(['searchHits' => [new SearchHit(['valueObject' => $location])], 'totalCount' => 2]);
849
        $searchHandler->expects($this->once())
850
            ->method('findLocations')
851
            ->with($this->isInstanceOf(LocationQuery::class), $this->isType('array'))
852
            ->willReturn($result);
853
854
        $mapper->expects($this->once())
855
            ->method('buildLocationDomainObjectsOnSearchResult')
856
            ->with($this->equalTo($result))
857
            ->willReturnCallback(function (SearchResult $spiResult) use ($location) {
858
                unset($spiResult->searchHits[0]);
859
                --$spiResult->totalCount;
860
861
                return [$location];
862
            });
863
864
        $finalResult = $service->findLocations(new LocationQuery());
865
866
        $this->assertEmpty($finalResult->searchHits, 'Expected search hits to be empty');
867
        $this->assertEquals(1, $finalResult->totalCount, 'Expected total count to be 1');
868
    }
869
870
    /**
871
     * Test for the findLocations() method.
872
     *
873
     * @expectedException \Exception
874
     * @expectedExceptionMessage Handler threw an exception
875
     */
876 View Code Duplication
    public function testFindLocationsThrowsHandlerException()
877
    {
878
        $repositoryMock = $this->getRepositoryMock();
879
        /** @var \eZ\Publish\SPI\Search\Handler $searchHandlerMock */
880
        $searchHandlerMock = $this->getSPIMockHandler('Search\\Handler');
881
        $permissionsCriterionResolverMock = $this->getPermissionCriterionResolverMock();
882
883
        $service = new SearchService(
884
            $repositoryMock,
0 ignored issues
show
Bug introduced by
It seems like $repositoryMock defined by $this->getRepositoryMock() on line 878 can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, eZ\Publish\Core\Reposito...hService::__construct() does only seem to accept object<eZ\Publish\API\Repository\Repository>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
885
            $searchHandlerMock,
886
            $this->getDomainMapperMock(),
0 ignored issues
show
Bug introduced by
It seems like $this->getDomainMapperMock() targeting eZ\Publish\Core\Reposito...::getDomainMapperMock() can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, eZ\Publish\Core\Reposito...hService::__construct() does only seem to accept object<eZ\Publish\Core\R...ry\Helper\DomainMapper>, 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...
887
            $permissionsCriterionResolverMock,
0 ignored issues
show
Bug introduced by
It seems like $permissionsCriterionResolverMock defined by $this->getPermissionCriterionResolverMock() on line 881 can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, eZ\Publish\Core\Reposito...hService::__construct() does only seem to accept object<eZ\Publish\API\Re...ssionCriterionResolver>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
888
            new NullIndexer(),
889
            array()
890
        );
891
892
        /** @var \eZ\Publish\API\Repository\Values\Content\Query\Criterion $criterionMock */
893
        $criterionMock = $this
894
            ->getMockBuilder(Criterion::class)
895
            ->disableOriginalConstructor()
896
            ->getMock();
897
        $query = new LocationQuery(array('filter' => $criterionMock));
898
899
        $permissionsCriterionResolverMock->expects($this->once())
900
            ->method('getPermissionsCriterion')
901
            ->with('content', 'read')
902
            ->will($this->throwException(new Exception('Handler threw an exception')));
903
904
        $service->findLocations($query, array(), true);
905
    }
906
907
    /**
908
     * Test for the findLocations() method.
909
     */
910
    public function testFindLocationsWithDefaultQueryValues()
911
    {
912
        $repositoryMock = $this->getRepositoryMock();
913
        /** @var \eZ\Publish\SPI\Search\Handler $searchHandlerMock */
914
        $searchHandlerMock = $this->getSPIMockHandler('Search\\Handler');
915
        $domainMapperMock = $this->getDomainMapperMock();
916
        $service = new SearchService(
917
            $repositoryMock,
0 ignored issues
show
Bug introduced by
It seems like $repositoryMock defined by $this->getRepositoryMock() on line 912 can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, eZ\Publish\Core\Reposito...hService::__construct() does only seem to accept object<eZ\Publish\API\Repository\Repository>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
918
            $searchHandlerMock,
919
            $domainMapperMock,
0 ignored issues
show
Bug introduced by
It seems like $domainMapperMock defined by $this->getDomainMapperMock() on line 915 can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, eZ\Publish\Core\Reposito...hService::__construct() does only seem to accept object<eZ\Publish\Core\R...ry\Helper\DomainMapper>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
920
            $this->getPermissionCriterionResolverMock(),
0 ignored issues
show
Bug introduced by
It seems like $this->getPermissionCriterionResolverMock() targeting eZ\Publish\Core\Reposito...CriterionResolverMock() can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, eZ\Publish\Core\Reposito...hService::__construct() does only seem to accept object<eZ\Publish\API\Re...ssionCriterionResolver>, 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...
921
            new NullIndexer(),
922
            array()
923
        );
924
925
        $spiLocation = new SPILocation();
926
        $locationMock = $this->getMockForAbstractClass(Location::class);
927
928
        /* @var \PHPUnit\Framework\MockObject\MockObject $searchHandlerMock */
929
        $searchHandlerMock
930
            ->expects($this->once())
931
            ->method('findLocations')
932
            ->with(
933
                new LocationQuery(
934
                    array(
935
                        'filter' => new Criterion\MatchAll(),
936
                        'limit' => 25,
937
                    )
938
                )
939
            )
940
            ->will(
941
                $this->returnValue(
942
                    $spiResult = new SearchResult(
943
                        array(
944
                            'searchHits' => array(new SearchHit(array('valueObject' => $spiLocation))),
945
                            'totalCount' => 1,
946
                        )
947
                    )
948
                )
949
            );
950
951
        $endResult = new SearchResult(
952
            [
953
                'searchHits' => [new SearchHit(['valueObject' => $locationMock])],
954
                'totalCount' => 1,
955
            ]
956
        );
957
958
        $domainMapperMock->expects($this->once())
959
            ->method('buildLocationDomainObjectsOnSearchResult')
960
            ->with($this->equalTo($spiResult))
961
            ->willReturnCallback(function (SearchResult $spiResult) use ($endResult) {
962
                $spiResult->searchHits[0] = $endResult->searchHits[0];
963
964
                return [];
965
            });
966
967
        $result = $service->findLocations(new LocationQuery(), array(), false);
968
969
        $this->assertEquals(
970
            $endResult,
971
            $result
972
        );
973
    }
974
975
    /**
976
     * @return \PHPUnit\Framework\MockObject\MockObject|\eZ\Publish\Core\Repository\Helper\DomainMapper
977
     */
978
    protected function getDomainMapperMock()
979
    {
980
        if (!isset($this->domainMapperMock)) {
981
            $this->domainMapperMock = $this
982
                ->getMockBuilder(DomainMapper::class)
983
                ->disableOriginalConstructor()
984
                ->getMock();
985
        }
986
987
        return $this->domainMapperMock;
988
    }
989
990
    /**
991
     * @return \PHPUnit\Framework\MockObject\MockObject|\eZ\Publish\API\Repository\PermissionCriterionResolver
992
     */
993
    protected function getPermissionCriterionResolverMock()
994
    {
995
        if (!isset($this->permissionsCriterionResolverMock)) {
996
            $this->permissionsCriterionResolverMock = $this
997
                ->getMockBuilder(PermissionCriterionResolver::class)
998
                ->disableOriginalConstructor()
999
                ->getMock();
1000
        }
1001
1002
        return $this->permissionsCriterionResolverMock;
1003
    }
1004
}
1005