Completed
Push — master ( 65f512...fe0390 )
by Łukasz
26:26
created

SearchTest::testFindSingle()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 61

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 61
rs 8.8509
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Repository\ContentService;
12
use eZ\Publish\Core\Repository\Helper\DomainMapper;
13
use eZ\Publish\Core\Repository\Tests\Service\Mock\Base as BaseServiceMockTest;
14
use eZ\Publish\Core\Repository\SearchService;
15
use eZ\Publish\Core\Repository\Permission\PermissionCriterionResolver;
16
use eZ\Publish\API\Repository\Values\Content\Content;
17
use eZ\Publish\API\Repository\Values\Content\Location;
18
use eZ\Publish\API\Repository\Values\Content\Query;
19
use eZ\Publish\API\Repository\Values\Content\LocationQuery;
20
use eZ\Publish\API\Repository\Values\Content\Query\Criterion;
21
use eZ\Publish\API\Repository\Values\Content\Query\SortClause;
22
use eZ\Publish\API\Repository\Values\Content\Search\SearchResult;
23
use eZ\Publish\API\Repository\Values\Content\Search\SearchHit;
24
use eZ\Publish\Core\Search\Common\BackgroundIndexer;
25
use eZ\Publish\Core\Search\Common\BackgroundIndexer\NullIndexer;
26
use eZ\Publish\SPI\Persistence\Content\ContentInfo as SPIContentInfo;
27
use eZ\Publish\SPI\Persistence\Content\Location as SPILocation;
28
use eZ\Publish\API\Repository\Exceptions\InvalidArgumentException;
29
use Exception;
30
31
/**
32
 * Mock test case for Search service.
33
 */
34
class SearchTest extends BaseServiceMockTest
35
{
36
    protected $repositoryMock;
37
38
    protected $domainMapperMock;
39
40
    protected $permissionsCriterionResolverMock;
41
42
    /**
43
     * Test for the __construct() method.
44
     *
45
     * @covers \eZ\Publish\Core\Repository\SearchService::__construct
46
     */
47
    public function testConstructor()
48
    {
49
        $repositoryMock = $this->getRepositoryMock();
50
        /** @var \eZ\Publish\SPI\Search\Handler $searchHandlerMock */
51
        $searchHandlerMock = $this->getSPIMockHandler('Search\\Handler');
52
        $domainMapperMock = $this->getDomainMapperMock();
53
        $permissionsCriterionResolverMock = $this->getPermissionCriterionResolverMock();
54
        $settings = array('teh setting');
55
56
        $service = new SearchService(
57
            $repositoryMock,
0 ignored issues
show
Bug introduced by
It seems like $repositoryMock defined by $this->getRepositoryMock() on line 49 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...
58
            $searchHandlerMock,
59
            $domainMapperMock,
0 ignored issues
show
Bug introduced by
It seems like $domainMapperMock defined by $this->getDomainMapperMock() on line 52 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...
60
            $permissionsCriterionResolverMock,
0 ignored issues
show
Bug introduced by
It seems like $permissionsCriterionResolverMock defined by $this->getPermissionCriterionResolverMock() on line 53 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...
61
            new NullIndexer(),
62
            $settings
63
        );
64
65
        $this->assertAttributeSame(
66
            $repositoryMock,
67
            'repository',
68
            $service
69
        );
70
71
        $this->assertAttributeSame(
72
            $searchHandlerMock,
73
            'searchHandler',
74
            $service
75
        );
76
77
        $this->assertAttributeSame(
78
            $domainMapperMock,
79
            'domainMapper',
80
            $service
81
        );
82
83
        $this->assertAttributeSame(
84
            $permissionsCriterionResolverMock,
85
            'permissionCriterionResolver',
86
            $service
87
        );
88
89
        $this->assertAttributeSame(
90
            $settings,
91
            'settings',
92
            $service
93
        );
94
    }
95
96
    public function providerForFindContentValidatesLocationCriteriaAndSortClauses()
97
    {
98
        return array(
99
            array(
100
                new Query(array('filter' => new Criterion\Location\Depth(Criterion\Operator::LT, 2))),
101
                "Argument '\$query' is invalid: Location criterions cannot be used in Content search",
102
            ),
103
            array(
104
                new Query(array('query' => new Criterion\Location\Depth(Criterion\Operator::LT, 2))),
105
                "Argument '\$query' is invalid: Location criterions cannot be used in Content search",
106
            ),
107
            array(
108
                new Query(
109
                    array(
110
                        'query' => new Criterion\LogicalAnd(
111
                            array(
112
                                new Criterion\Location\Depth(Criterion\Operator::LT, 2),
113
                            )
114
                        ),
115
                    )
116
                ),
117
                "Argument '\$query' is invalid: Location criterions cannot be used in Content search",
118
            ),
119
            array(
120
                new Query(array('sortClauses' => array(new SortClause\Location\Id()))),
121
                "Argument '\$query' is invalid: Location sort clauses cannot be used in Content search",
122
            ),
123
        );
124
    }
125
126
    /**
127
     * @dataProvider providerForFindContentValidatesLocationCriteriaAndSortClauses
128
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
129
     */
130 View Code Duplication
    public function testFindContentValidatesLocationCriteriaAndSortClauses($query, $exceptionMessage)
131
    {
132
        $repositoryMock = $this->getRepositoryMock();
133
        /** @var \eZ\Publish\SPI\Search\Handler $searchHandlerMock */
134
        $searchHandlerMock = $this->getSPIMockHandler('Search\\Handler');
135
        $permissionsCriterionResolverMock = $this->getPermissionCriterionResolverMock();
136
137
        $service = new SearchService(
138
            $repositoryMock,
0 ignored issues
show
Bug introduced by
It seems like $repositoryMock defined by $this->getRepositoryMock() on line 132 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...
139
            $searchHandlerMock,
140
            $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...
141
            $permissionsCriterionResolverMock,
0 ignored issues
show
Bug introduced by
It seems like $permissionsCriterionResolverMock defined by $this->getPermissionCriterionResolverMock() on line 135 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...
142
            new NullIndexer(),
143
            array()
144
        );
145
146
        try {
147
            $service->findContent($query);
148
        } catch (InvalidArgumentException $e) {
149
            $this->assertEquals($exceptionMessage, $e->getMessage());
150
            throw $e;
151
        }
152
153
        $this->fail('Expected exception was not thrown');
154
    }
155
156
    public function providerForFindSingleValidatesLocationCriteria()
157
    {
158
        return array(
159
            array(
160
                new Criterion\Location\Depth(Criterion\Operator::LT, 2),
161
                "Argument '\$filter' is invalid: Location criterions cannot be used in Content search",
162
            ),
163
            array(
164
                new Criterion\LogicalAnd(
165
                    array(
166
                        new Criterion\Location\Depth(Criterion\Operator::LT, 2),
167
                    )
168
                ),
169
                "Argument '\$filter' is invalid: Location criterions cannot be used in Content search",
170
            ),
171
        );
172
    }
173
174
    /**
175
     * @dataProvider providerForFindSingleValidatesLocationCriteria
176
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
177
     */
178 View Code Duplication
    public function testFindSingleValidatesLocationCriteria($criterion, $exceptionMessage)
179
    {
180
        $repositoryMock = $this->getRepositoryMock();
181
        /** @var \eZ\Publish\SPI\Search\Handler $searchHandlerMock */
182
        $searchHandlerMock = $this->getSPIMockHandler('Search\\Handler');
183
        $permissionsCriterionResolverMock = $this->getPermissionCriterionResolverMock();
184
        $service = new SearchService(
185
            $repositoryMock,
0 ignored issues
show
Bug introduced by
It seems like $repositoryMock defined by $this->getRepositoryMock() on line 180 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...
186
            $searchHandlerMock,
187
            $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...
188
            $permissionsCriterionResolverMock,
0 ignored issues
show
Bug introduced by
It seems like $permissionsCriterionResolverMock defined by $this->getPermissionCriterionResolverMock() on line 183 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...
189
            new NullIndexer(),
190
            array()
191
        );
192
193
        try {
194
            $service->findSingle($criterion);
195
        } catch (InvalidArgumentException $e) {
196
            $this->assertEquals($exceptionMessage, $e->getMessage());
197
            throw $e;
198
        }
199
200
        $this->fail('Expected exception was not thrown');
201
    }
202
203
    /**
204
     * Test for the findContent() method.
205
     *
206
     * @covers \eZ\Publish\Core\Repository\SearchService::addPermissionsCriterion
207
     * @covers \eZ\Publish\Core\Repository\SearchService::findContent
208
     * @expectedException \Exception
209
     * @expectedExceptionMessage Handler threw an exception
210
     */
211 View Code Duplication
    public function testFindContentThrowsHandlerException()
212
    {
213
        $repositoryMock = $this->getRepositoryMock();
214
        /** @var \eZ\Publish\SPI\Search\Handler $searchHandlerMock */
215
        $searchHandlerMock = $this->getSPIMockHandler('Search\\Handler');
216
        $permissionsCriterionResolverMock = $this->getPermissionCriterionResolverMock();
217
218
        $service = new SearchService(
219
            $repositoryMock,
0 ignored issues
show
Bug introduced by
It seems like $repositoryMock defined by $this->getRepositoryMock() on line 213 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...
220
            $searchHandlerMock,
221
            $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...
222
            $permissionsCriterionResolverMock,
0 ignored issues
show
Bug introduced by
It seems like $permissionsCriterionResolverMock defined by $this->getPermissionCriterionResolverMock() on line 216 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...
223
            new NullIndexer(),
224
            array()
225
        );
226
227
        /** @var \eZ\Publish\API\Repository\Values\Content\Query\Criterion $criterionMock */
228
        $criterionMock = $this
229
            ->getMockBuilder(Criterion::class)
230
            ->disableOriginalConstructor()
231
            ->getMock();
232
        $query = new Query(array('filter' => $criterionMock));
233
234
        $permissionsCriterionResolverMock->expects($this->once())
235
            ->method('getPermissionsCriterion')
236
            ->with('content', 'read')
237
            ->will($this->throwException(new Exception('Handler threw an exception')));
238
239
        $service->findContent($query, array(), true);
240
    }
241
242
    /**
243
     * Test for the findContent() method when search is out of sync with persistence.
244
     *
245
     * @covers \eZ\Publish\Core\Repository\SearchService::findContent
246
     */
247
    public function testFindContentWhenDomainMapperThrowsException()
248
    {
249
        $indexer = $this->createMock(BackgroundIndexer::class);
250
        $indexer->expects($this->once())
251
            ->method('registerContent')
252
            ->with($this->isInstanceOf(SPIContentInfo::class));
253
254
        $service = $this->getMockBuilder(SearchService::class)
255
            ->setConstructorArgs([
256
                $this->getRepositoryMock(),
257
                $this->getSPIMockHandler('Search\\Handler'),
258
                $mapper = $this->getDomainMapperMock(),
259
                $this->getPermissionCriterionResolverMock(),
260
                $indexer,
261
            ])->setMethods(['internalFindContentInfo'])
262
            ->getMock();
263
264
        $info = new SPIContentInfo(['id' => 33]);
265
        $result = new SearchResult(['searchHits' => [new SearchHit(['valueObject' => $info])], 'totalCount' => 2]);
266
        $service->expects($this->once())
267
            ->method('internalFindContentInfo')
268
            ->with($this->isInstanceOf(Query::class))
269
            ->willReturn($result);
270
271
        $mapper->expects($this->once())
272
            ->method('buildContentDomainObjectsOnSearchResult')
273
            ->with($this->equalTo($result), $this->equalTo([]))
274
            ->willReturnCallback(function (SearchResult $spiResult) use ($info) {
275
                unset($spiResult->searchHits[0]);
276
                --$spiResult->totalCount;
277
278
                return [$info];
279
            });
280
281
        $finalResult = $service->findContent(new Query());
282
283
        $this->assertEmpty($finalResult->searchHits, 'Expected search hits to be empty');
284
        $this->assertEquals(1, $finalResult->totalCount, 'Expected total count to be 1');
285
    }
286
287
    /**
288
     * Test for the findContent() method.
289
     *
290
     * @covers \eZ\Publish\Core\Repository\SearchService::addPermissionsCriterion
291
     * @covers \eZ\Publish\Core\Repository\SearchService::findContent
292
     */
293
    public function testFindContentNoPermissionsFilter()
294
    {
295
        /** @var \eZ\Publish\SPI\Search\Handler $searchHandlerMock */
296
        $searchHandlerMock = $this->getSPIMockHandler('Search\\Handler');
297
        $repositoryMock = $this->getRepositoryMock();
298
        $service = new SearchService(
299
            $repositoryMock,
0 ignored issues
show
Bug introduced by
It seems like $repositoryMock defined by $this->getRepositoryMock() on line 297 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...
300
            $searchHandlerMock,
301
            $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...
302
            $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...
303
            new NullIndexer(),
304
            array()
305
        );
306
307
        $repositoryMock->expects($this->never())->method('hasAccess');
308
309
        $serviceQuery = new Query();
310
        $handlerQuery = new Query(array('filter' => new Criterion\MatchAll(), 'limit' => 25));
311
        $languageFilter = array();
312
        $spiContentInfo = new SPIContentInfo();
313
        $contentMock = $this->getMockForAbstractClass(Content::class);
314
315
        /* @var \PHPUnit\Framework\MockObject\MockObject $searchHandlerMock */
316
        $searchHandlerMock->expects($this->once())
317
            ->method('findContent')
318
            ->with($this->equalTo($handlerQuery), $this->equalTo($languageFilter))
319
            ->will(
320
                $this->returnValue(
321
                    new SearchResult(
322
                        array(
323
                            'searchHits' => array(new SearchHit(array('valueObject' => $spiContentInfo))),
324
                            'totalCount' => 1,
325
                        )
326
                    )
327
                )
328
            );
329
330
        $mapper->expects($this->once())
331
            ->method('buildContentDomainObjectsOnSearchResult')
332
            ->with($this->isInstanceOf(SearchResult::class), $this->equalTo([]))
333
            ->willReturnCallback(function (SearchResult $spiResult) use ($contentMock) {
334
                $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...
335
336
                return [];
337
            });
338
339
        $result = $service->findContent($serviceQuery, $languageFilter, false);
340
341
        $this->assertEquals(
342
            new SearchResult(
343
                array(
344
                    'searchHits' => array(new SearchHit(array('valueObject' => $contentMock))),
345
                    'totalCount' => 1,
346
                )
347
            ),
348
            $result
349
        );
350
    }
351
352
    /**
353
     * Test for the findContent() method.
354
     *
355
     * @covers \eZ\Publish\Core\Repository\SearchService::addPermissionsCriterion
356
     * @covers \eZ\Publish\Core\Repository\SearchService::findContent
357
     */
358
    public function testFindContentWithPermission()
359
    {
360
        /** @var \eZ\Publish\SPI\Search\Handler $searchHandlerMock */
361
        $searchHandlerMock = $this->getSPIMockHandler('Search\\Handler');
362
        $domainMapperMock = $this->getDomainMapperMock();
363
        $permissionsCriterionResolverMock = $this->getPermissionCriterionResolverMock();
364
        $service = new SearchService(
365
            $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...
366
            $searchHandlerMock,
367
            $domainMapperMock,
0 ignored issues
show
Bug introduced by
It seems like $domainMapperMock defined by $this->getDomainMapperMock() on line 362 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...
368
            $permissionsCriterionResolverMock,
0 ignored issues
show
Bug introduced by
It seems like $permissionsCriterionResolverMock defined by $this->getPermissionCriterionResolverMock() on line 363 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...
369
            new NullIndexer(),
370
            array()
371
        );
372
373
        $criterionMock = $this
374
            ->getMockBuilder(Criterion::class)
375
            ->disableOriginalConstructor()
376
            ->getMock();
377
        $query = new Query(array('filter' => $criterionMock, 'limit' => 10));
378
        $languageFilter = array();
379
        $spiContentInfo = new SPIContentInfo();
380
        $contentMock = $this->getMockForAbstractClass(Content::class);
381
382
        $permissionsCriterionResolverMock->expects($this->once())
383
            ->method('getPermissionsCriterion')
384
            ->with('content', 'read')
385
            ->will($this->returnValue(true));
386
387
        /* @var \PHPUnit\Framework\MockObject\MockObject $searchHandlerMock */
388
        $searchHandlerMock->expects($this->once())
389
            ->method('findContent')
390
            ->with($this->equalTo($query), $this->equalTo($languageFilter))
391
            ->will(
392
                $this->returnValue(
393
                    new SearchResult(
394
                        array(
395
                            'searchHits' => array(new SearchHit(array('valueObject' => $spiContentInfo))),
396
                            'totalCount' => 1,
397
                        )
398
                    )
399
                )
400
            );
401
402
        $domainMapperMock
403
            ->expects($this->once())
404
            ->method('buildContentDomainObjectsOnSearchResult')
405
            ->with($this->isInstanceOf(SearchResult::class), $this->equalTo([]))
406
            ->willReturnCallback(function (SearchResult $spiResult) use ($contentMock) {
407
                $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...
408
409
                return [];
410
            });
411
412
        $result = $service->findContent($query, $languageFilter, true);
413
414
        $this->assertEquals(
415
            new SearchResult(
416
                array(
417
                    'searchHits' => array(new SearchHit(array('valueObject' => $contentMock))),
418
                    'totalCount' => 1,
419
                )
420
            ),
421
            $result
422
        );
423
    }
424
425
    /**
426
     * Test for the findContent() method.
427
     *
428
     * @covers \eZ\Publish\Core\Repository\SearchService::addPermissionsCriterion
429
     * @covers \eZ\Publish\Core\Repository\SearchService::findContent
430
     */
431
    public function testFindContentWithNoPermission()
432
    {
433
        /** @var \eZ\Publish\SPI\Search\Handler $searchHandlerMock */
434
        $searchHandlerMock = $this->getSPIMockHandler('Search\\Handler');
435
        $permissionsCriterionResolverMock = $this->getPermissionCriterionResolverMock();
436
        $service = new SearchService(
437
            $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...
438
            $searchHandlerMock,
439
            $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...
440
            $permissionsCriterionResolverMock,
0 ignored issues
show
Bug introduced by
It seems like $permissionsCriterionResolverMock defined by $this->getPermissionCriterionResolverMock() on line 435 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...
441
            new NullIndexer(),
442
            array()
443
        );
444
445
        /* @var \PHPUnit\Framework\MockObject\MockObject $searchHandlerMock */
446
        $searchHandlerMock->expects($this->never())->method('findContent');
447
448
        $criterionMock = $this
449
            ->getMockBuilder(Criterion::class)
450
            ->disableOriginalConstructor()
451
            ->getMock();
452
        $query = new Query(array('filter' => $criterionMock));
453
454
        $permissionsCriterionResolverMock->expects($this->once())
455
            ->method('getPermissionsCriterion')
456
            ->with('content', 'read')
457
            ->will($this->returnValue(false));
458
459
        $mapper->expects($this->once())
460
            ->method('buildContentDomainObjectsOnSearchResult')
461
            ->with($this->isInstanceOf(SearchResult::class), $this->equalTo([]))
462
            ->willReturn([]);
463
464
        $result = $service->findContent($query, array(), true);
465
466
        $this->assertEquals(
467
            new SearchResult(array('time' => 0, 'totalCount' => 0)),
468
            $result
469
        );
470
    }
471
472
    /**
473
     * Test for the findContent() method.
474
     */
475
    public function testFindContentWithDefaultQueryValues()
476
    {
477
        /** @var \eZ\Publish\SPI\Search\Handler $searchHandlerMock */
478
        $searchHandlerMock = $this->getSPIMockHandler('Search\\Handler');
479
        $domainMapperMock = $this->getDomainMapperMock();
480
        $service = new SearchService(
481
            $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...
482
            $searchHandlerMock,
483
            $domainMapperMock,
0 ignored issues
show
Bug introduced by
It seems like $domainMapperMock defined by $this->getDomainMapperMock() on line 479 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...
484
            $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...
485
            new NullIndexer(),
486
            array()
487
        );
488
489
        $languageFilter = array();
490
        $spiContentInfo = new SPIContentInfo();
491
        $contentMock = $this->getMockForAbstractClass(Content::class);
492
493
        /* @var \PHPUnit\Framework\MockObject\MockObject $searchHandlerMock */
494
        $searchHandlerMock
495
            ->expects($this->once())
496
            ->method('findContent')
497
            ->with(
498
                new Query(
499
                    array(
500
                        'filter' => new Criterion\MatchAll(),
501
                        'limit' => 25,
502
                    )
503
                ),
504
                array()
505
            )
506
            ->will(
507
                $this->returnValue(
508
                    new SearchResult(
509
                        array(
510
                            'searchHits' => array(new SearchHit(array('valueObject' => $spiContentInfo))),
511
                            'totalCount' => 1,
512
                        )
513
                    )
514
                )
515
            );
516
517
        $domainMapperMock
518
            ->expects($this->once())
519
            ->method('buildContentDomainObjectsOnSearchResult')
520
            ->with($this->isInstanceOf(SearchResult::class), $this->equalTo([]))
521
            ->willReturnCallback(function (SearchResult $spiResult) use ($contentMock) {
522
                $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...
523
524
                return [];
525
            });
526
527
        $result = $service->findContent(new Query(), $languageFilter, false);
528
529
        $this->assertEquals(
530
            new SearchResult(
531
                array(
532
                    'searchHits' => array(new SearchHit(array('valueObject' => $contentMock))),
533
                    'totalCount' => 1,
534
                )
535
            ),
536
            $result
537
        );
538
    }
539
540
    /**
541
     * Test for the findSingle() method.
542
     *
543
     * @covers \eZ\Publish\Core\Repository\SearchService::addPermissionsCriterion
544
     * @covers \eZ\Publish\Core\Repository\SearchService::findSingle
545
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
546
     */
547 View Code Duplication
    public function testFindSingleThrowsNotFoundException()
548
    {
549
        $repositoryMock = $this->getRepositoryMock();
550
        /** @var \eZ\Publish\SPI\Search\Handler $searchHandlerMock */
551
        $searchHandlerMock = $this->getSPIMockHandler('Search\\Handler');
552
        $service = new SearchService(
553
            $repositoryMock,
0 ignored issues
show
Bug introduced by
It seems like $repositoryMock defined by $this->getRepositoryMock() on line 549 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...
554
            $searchHandlerMock,
555
            $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...
556
            $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...
557
            new NullIndexer(),
558
            array()
559
        );
560
561
        /** @var \eZ\Publish\API\Repository\Values\Content\Query\Criterion $criterionMock */
562
        $criterionMock = $this
563
            ->getMockBuilder(Criterion::class)
564
            ->disableOriginalConstructor()
565
            ->getMock();
566
567
        $permissionsCriterionResolverMock->expects($this->once())
568
            ->method('getPermissionsCriterion')
569
            ->with('content', 'read')
570
            ->willReturn(false);
571
572
        $service->findSingle($criterionMock, array(), true);
573
    }
574
575
    /**
576
     * Test for the findSingle() method.
577
     *
578
     * @covers \eZ\Publish\Core\Repository\SearchService::addPermissionsCriterion
579
     * @covers \eZ\Publish\Core\Repository\SearchService::findSingle
580
     * @expectedException \Exception
581
     * @expectedExceptionMessage Handler threw an exception
582
     */
583 View Code Duplication
    public function testFindSingleThrowsHandlerException()
584
    {
585
        $repositoryMock = $this->getRepositoryMock();
586
        /** @var \eZ\Publish\SPI\Search\Handler $searchHandlerMock */
587
        $searchHandlerMock = $this->getSPIMockHandler('Search\\Handler');
588
        $permissionsCriterionResolverMock = $this->getPermissionCriterionResolverMock();
589
        $service = new SearchService(
590
            $repositoryMock,
0 ignored issues
show
Bug introduced by
It seems like $repositoryMock defined by $this->getRepositoryMock() on line 585 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...
591
            $searchHandlerMock,
592
            $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...
593
            $permissionsCriterionResolverMock,
0 ignored issues
show
Bug introduced by
It seems like $permissionsCriterionResolverMock defined by $this->getPermissionCriterionResolverMock() on line 588 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...
594
            new NullIndexer(),
595
            array()
596
        );
597
598
        /** @var \eZ\Publish\API\Repository\Values\Content\Query\Criterion $criterionMock */
599
        $criterionMock = $this
600
            ->getMockBuilder(Criterion::class)
601
            ->disableOriginalConstructor()
602
            ->getMock();
603
604
        $permissionsCriterionResolverMock->expects($this->once())
605
            ->method('getPermissionsCriterion')
606
            ->with('content', 'read')
607
            ->will($this->throwException(new Exception('Handler threw an exception')));
608
609
        $service->findSingle($criterionMock, array(), true);
610
    }
611
612
    /**
613
     * Test for the findSingle() method.
614
     *
615
     * @covers \eZ\Publish\Core\Repository\SearchService::addPermissionsCriterion
616
     * @covers \eZ\Publish\Core\Repository\SearchService::findSingle
617
     */
618
    public function testFindSingle()
619
    {
620
        $repositoryMock = $this->getRepositoryMock();
621
        /** @var \eZ\Publish\SPI\Search\Handler $searchHandlerMock */
622
        $searchHandlerMock = $this->getSPIMockHandler('Search\\Handler');
623
        $domainMapperMock = $this->getDomainMapperMock();
624
        $permissionsCriterionResolverMock = $this->getPermissionCriterionResolverMock();
625
        $service = new SearchService(
626
            $repositoryMock,
0 ignored issues
show
Bug introduced by
It seems like $repositoryMock defined by $this->getRepositoryMock() on line 620 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...
627
            $searchHandlerMock,
628
            $domainMapperMock,
0 ignored issues
show
Bug introduced by
It seems like $domainMapperMock defined by $this->getDomainMapperMock() on line 623 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...
629
            $permissionsCriterionResolverMock,
0 ignored issues
show
Bug introduced by
It seems like $permissionsCriterionResolverMock defined by $this->getPermissionCriterionResolverMock() on line 624 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...
630
            new NullIndexer(),
631
            array()
632
        );
633
634
        $repositoryMock
635
            ->expects($this->once())
636
            ->method('getContentService')
637
            ->will(
638
                $this->returnValue(
639
                    $contentServiceMock = $this
640
                        ->getMockBuilder(ContentService::class)
641
                        ->disableOriginalConstructor()
642
                        ->getMock()
643
                )
644
            );
645
646
        /** @var \eZ\Publish\API\Repository\Values\Content\Query\Criterion $criterionMock */
647
        $criterionMock = $this
648
            ->getMockBuilder(Criterion::class)
649
            ->disableOriginalConstructor()
650
            ->getMock();
651
652
        $permissionsCriterionResolverMock->expects($this->once())
653
            ->method('getPermissionsCriterion')
654
            ->with('content', 'read')
655
            ->will($this->returnValue(true));
656
657
        $languageFilter = array();
658
        $spiContentInfo = new SPIContentInfo();
659
        $contentMock = $this->getMockForAbstractClass(Content::class);
660
661
        /* @var \PHPUnit\Framework\MockObject\MockObject $searchHandlerMock */
662
        $searchHandlerMock->expects($this->once())
663
            ->method('findSingle')
664
            ->with($this->equalTo($criterionMock), $this->equalTo($languageFilter))
665
            ->will($this->returnValue($spiContentInfo));
666
667
        $domainMapperMock->expects($this->never())
668
            ->method($this->anything());
669
670
        $contentServiceMock
671
            ->expects($this->once())
672
            ->method('internalLoadContent')
673
            ->will($this->returnValue($contentMock));
674
675
        $result = $service->findSingle($criterionMock, $languageFilter, true);
676
677
        $this->assertEquals($contentMock, $result);
678
    }
679
680
    /**
681
     * Test for the findLocations() method.
682
     */
683
    public function testFindLocationsWithPermission()
684
    {
685
        $repositoryMock = $this->getRepositoryMock();
686
        /** @var \eZ\Publish\SPI\Search\Handler $searchHandlerMock */
687
        $searchHandlerMock = $this->getSPIMockHandler('Search\\Handler');
688
        $domainMapperMock = $this->getDomainMapperMock();
689
        $permissionsCriterionResolverMock = $this->getPermissionCriterionResolverMock();
690
        $service = new SearchService(
691
            $repositoryMock,
0 ignored issues
show
Bug introduced by
It seems like $repositoryMock defined by $this->getRepositoryMock() on line 685 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...
692
            $searchHandlerMock,
693
            $domainMapperMock,
0 ignored issues
show
Bug introduced by
It seems like $domainMapperMock defined by $this->getDomainMapperMock() on line 688 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...
694
            $permissionsCriterionResolverMock,
0 ignored issues
show
Bug introduced by
It seems like $permissionsCriterionResolverMock defined by $this->getPermissionCriterionResolverMock() on line 689 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...
695
            new NullIndexer(),
696
            array()
697
        );
698
699
        $criterionMock = $this
700
            ->getMockBuilder(Criterion::class)
701
            ->disableOriginalConstructor()
702
            ->getMock();
703
        $query = new LocationQuery(array('filter' => $criterionMock, 'limit' => 10));
704
        $spiLocation = new SPILocation();
705
        $locationMock = $this->getMockForAbstractClass(Location::class);
706
707
        /* @var \PHPUnit\Framework\MockObject\MockObject $searchHandlerMock */
708
        $searchHandlerMock->expects($this->once())
709
            ->method('findLocations')
710
            ->with($this->equalTo($query))
711
            ->will(
712
                $this->returnValue(
713
                    $spiResult = new SearchResult(
714
                        array(
715
                            'searchHits' => array(new SearchHit(array('valueObject' => $spiLocation))),
716
                            'totalCount' => 1,
717
                        )
718
                    )
719
                )
720
            );
721
722
        $permissionsCriterionResolverMock->expects($this->once())
723
            ->method('getPermissionsCriterion')
724
            ->with('content', 'read')
725
            ->will($this->returnValue(true));
726
727
        $endResult = new SearchResult(
728
            [
729
                'searchHits' => [new SearchHit(['valueObject' => $locationMock])],
730
                'totalCount' => 1,
731
            ]
732
        );
733
734
        $domainMapperMock->expects($this->once())
735
            ->method('buildLocationDomainObjectsOnSearchResult')
736
            ->with($this->equalTo($spiResult))
737
            ->willReturnCallback(function (SearchResult $spiResult) use ($endResult) {
738
                $spiResult->searchHits[0] = $endResult->searchHits[0];
739
740
                return [];
741
            });
742
743
        $result = $service->findLocations($query, array(), true);
744
745
        $this->assertEquals(
746
            $endResult,
747
            $result
748
        );
749
    }
750
751
    /**
752
     * Test for the findLocations() method.
753
     */
754
    public function testFindLocationsWithNoPermissionsFilter()
755
    {
756
        $repositoryMock = $this->getRepositoryMock();
757
        /** @var \eZ\Publish\SPI\Search\Handler $searchHandlerMock */
758
        $searchHandlerMock = $this->getSPIMockHandler('Search\\Handler');
759
        $domainMapperMock = $this->getDomainMapperMock();
760
        $permissionsCriterionResolverMock = $this->getPermissionCriterionResolverMock();
761
        $service = new SearchService(
762
            $repositoryMock,
0 ignored issues
show
Bug introduced by
It seems like $repositoryMock defined by $this->getRepositoryMock() on line 756 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...
763
            $searchHandlerMock,
764
            $domainMapperMock,
0 ignored issues
show
Bug introduced by
It seems like $domainMapperMock defined by $this->getDomainMapperMock() on line 759 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...
765
            $permissionsCriterionResolverMock,
0 ignored issues
show
Bug introduced by
It seems like $permissionsCriterionResolverMock defined by $this->getPermissionCriterionResolverMock() on line 760 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...
766
            new NullIndexer(),
767
            array()
768
        );
769
770
        $repositoryMock->expects($this->never())->method('hasAccess');
771
772
        $serviceQuery = new LocationQuery();
773
        $handlerQuery = new LocationQuery(array('filter' => new Criterion\MatchAll(), 'limit' => 25));
774
        $spiLocation = new SPILocation();
775
        $locationMock = $this->getMockForAbstractClass(Location::class);
776
777
        /* @var \PHPUnit\Framework\MockObject\MockObject $searchHandlerMock */
778
        $searchHandlerMock->expects($this->once())
779
            ->method('findLocations')
780
            ->with($this->equalTo($handlerQuery))
781
            ->will(
782
                $this->returnValue(
783
                    $spiResult = new SearchResult(
784
                        array(
785
                            'searchHits' => array(new SearchHit(array('valueObject' => $spiLocation))),
786
                            'totalCount' => 1,
787
                        )
788
                    )
789
                )
790
            );
791
792
        $permissionsCriterionResolverMock->expects($this->never())->method($this->anything());
793
794
        $endResult = new SearchResult(
795
            [
796
                'searchHits' => [new SearchHit(['valueObject' => $locationMock])],
797
                'totalCount' => 1,
798
            ]
799
        );
800
801
        $domainMapperMock->expects($this->once())
802
            ->method('buildLocationDomainObjectsOnSearchResult')
803
            ->with($this->equalTo($spiResult))
804
            ->willReturnCallback(function (SearchResult $spiResult) use ($endResult) {
805
                $spiResult->searchHits[0] = $endResult->searchHits[0];
806
807
                return [];
808
            });
809
810
        $result = $service->findLocations($serviceQuery, array(), false);
811
812
        $this->assertEquals(
813
            $endResult,
814
            $result
815
        );
816
    }
817
818
    /**
819
     * Test for the findLocations() method when search is out of sync with persistence.
820
     *
821
     * @covers \eZ\Publish\Core\Repository\SearchService::findLocations
822
     */
823
    public function testFindLocationsBackgroundIndexerWhenDomainMapperThrowsException()
824
    {
825
        $indexer = $this->createMock(BackgroundIndexer::class);
826
        $indexer->expects($this->once())
827
            ->method('registerLocation')
828
            ->with($this->isInstanceOf(SPILocation::class));
829
830
        $service = $this->getMockBuilder(SearchService::class)
831
            ->setConstructorArgs([
832
                $this->getRepositoryMock(),
833
                $searchHandler = $this->getSPIMockHandler('Search\\Handler'),
834
                $mapper = $this->getDomainMapperMock(),
835
                $this->getPermissionCriterionResolverMock(),
836
                $indexer,
837
            ])->setMethods(['addPermissionsCriterion'])
838
            ->getMock();
839
840
        $location = new SPILocation(['id' => 44]);
841
        $service->expects($this->once())
842
            ->method('addPermissionsCriterion')
843
            ->with($this->isInstanceOf(Criterion::class))
844
            ->willReturn(true);
845
846
        $result = new SearchResult(['searchHits' => [new SearchHit(['valueObject' => $location])], 'totalCount' => 2]);
847
        $searchHandler->expects($this->once())
848
            ->method('findLocations')
849
            ->with($this->isInstanceOf(LocationQuery::class), $this->isType('array'))
850
            ->willReturn($result);
851
852
        $mapper->expects($this->once())
853
            ->method('buildLocationDomainObjectsOnSearchResult')
854
            ->with($this->equalTo($result))
855
            ->willReturnCallback(function (SearchResult $spiResult) use ($location) {
856
                unset($spiResult->searchHits[0]);
857
                --$spiResult->totalCount;
858
859
                return [$location];
860
            });
861
862
        $finalResult = $service->findLocations(new LocationQuery());
863
864
        $this->assertEmpty($finalResult->searchHits, 'Expected search hits to be empty');
865
        $this->assertEquals(1, $finalResult->totalCount, 'Expected total count to be 1');
866
    }
867
868
    /**
869
     * Test for the findLocations() method.
870
     *
871
     * @expectedException \Exception
872
     * @expectedExceptionMessage Handler threw an exception
873
     */
874 View Code Duplication
    public function testFindLocationsThrowsHandlerException()
875
    {
876
        $repositoryMock = $this->getRepositoryMock();
877
        /** @var \eZ\Publish\SPI\Search\Handler $searchHandlerMock */
878
        $searchHandlerMock = $this->getSPIMockHandler('Search\\Handler');
879
        $permissionsCriterionResolverMock = $this->getPermissionCriterionResolverMock();
880
881
        $service = new SearchService(
882
            $repositoryMock,
0 ignored issues
show
Bug introduced by
It seems like $repositoryMock defined by $this->getRepositoryMock() on line 876 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...
883
            $searchHandlerMock,
884
            $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...
885
            $permissionsCriterionResolverMock,
0 ignored issues
show
Bug introduced by
It seems like $permissionsCriterionResolverMock defined by $this->getPermissionCriterionResolverMock() on line 879 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...
886
            new NullIndexer(),
887
            array()
888
        );
889
890
        /** @var \eZ\Publish\API\Repository\Values\Content\Query\Criterion $criterionMock */
891
        $criterionMock = $this
892
            ->getMockBuilder(Criterion::class)
893
            ->disableOriginalConstructor()
894
            ->getMock();
895
        $query = new LocationQuery(array('filter' => $criterionMock));
896
897
        $permissionsCriterionResolverMock->expects($this->once())
898
            ->method('getPermissionsCriterion')
899
            ->with('content', 'read')
900
            ->will($this->throwException(new Exception('Handler threw an exception')));
901
902
        $service->findLocations($query, array(), true);
903
    }
904
905
    /**
906
     * Test for the findLocations() method.
907
     */
908
    public function testFindLocationsWithDefaultQueryValues()
909
    {
910
        $repositoryMock = $this->getRepositoryMock();
911
        /** @var \eZ\Publish\SPI\Search\Handler $searchHandlerMock */
912
        $searchHandlerMock = $this->getSPIMockHandler('Search\\Handler');
913
        $domainMapperMock = $this->getDomainMapperMock();
914
        $service = new SearchService(
915
            $repositoryMock,
0 ignored issues
show
Bug introduced by
It seems like $repositoryMock defined by $this->getRepositoryMock() on line 910 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...
916
            $searchHandlerMock,
917
            $domainMapperMock,
0 ignored issues
show
Bug introduced by
It seems like $domainMapperMock defined by $this->getDomainMapperMock() on line 913 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...
918
            $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...
919
            new NullIndexer(),
920
            array()
921
        );
922
923
        $spiLocation = new SPILocation();
924
        $locationMock = $this->getMockForAbstractClass(Location::class);
925
926
        /* @var \PHPUnit\Framework\MockObject\MockObject $searchHandlerMock */
927
        $searchHandlerMock
928
            ->expects($this->once())
929
            ->method('findLocations')
930
            ->with(
931
                new LocationQuery(
932
                    array(
933
                        'filter' => new Criterion\MatchAll(),
934
                        'limit' => 25,
935
                    )
936
                )
937
            )
938
            ->will(
939
                $this->returnValue(
940
                    $spiResult = new SearchResult(
941
                        array(
942
                            'searchHits' => array(new SearchHit(array('valueObject' => $spiLocation))),
943
                            'totalCount' => 1,
944
                        )
945
                    )
946
                )
947
            );
948
949
        $endResult = new SearchResult(
950
            [
951
                'searchHits' => [new SearchHit(['valueObject' => $locationMock])],
952
                'totalCount' => 1,
953
            ]
954
        );
955
956
        $domainMapperMock->expects($this->once())
957
            ->method('buildLocationDomainObjectsOnSearchResult')
958
            ->with($this->equalTo($spiResult))
959
            ->willReturnCallback(function (SearchResult $spiResult) use ($endResult) {
960
                $spiResult->searchHits[0] = $endResult->searchHits[0];
961
962
                return [];
963
            });
964
965
        $result = $service->findLocations(new LocationQuery(), array(), false);
966
967
        $this->assertEquals(
968
            $endResult,
969
            $result
970
        );
971
    }
972
973
    /**
974
     * @return \PHPUnit\Framework\MockObject\MockObject|\eZ\Publish\Core\Repository\Helper\DomainMapper
975
     */
976
    protected function getDomainMapperMock()
977
    {
978
        if (!isset($this->domainMapperMock)) {
979
            $this->domainMapperMock = $this
980
                ->getMockBuilder(DomainMapper::class)
981
                ->disableOriginalConstructor()
982
                ->getMock();
983
        }
984
985
        return $this->domainMapperMock;
986
    }
987
988
    /**
989
     * @return \PHPUnit\Framework\MockObject\MockObject|\eZ\Publish\API\Repository\PermissionCriterionResolver
990
     */
991
    protected function getPermissionCriterionResolverMock()
992
    {
993
        if (!isset($this->permissionsCriterionResolverMock)) {
994
            $this->permissionsCriterionResolverMock = $this
995
                ->getMockBuilder(PermissionCriterionResolver::class)
996
                ->disableOriginalConstructor()
997
                ->getMock();
998
        }
999
1000
        return $this->permissionsCriterionResolverMock;
1001
    }
1002
}
1003