Completed
Push — 7.0 ( 720769...dd7a06 )
by André
150:06 queued 119:32
created

SearchTest   B

Complexity

Total Complexity 27

Size/Duplication

Total Lines 1015
Duplicated Lines 16.16 %

Coupling/Cohesion

Components 1
Dependencies 18

Importance

Changes 0
Metric Value
wmc 27
lcom 1
cbo 18
dl 164
loc 1015
rs 7.3399
c 0
b 0
f 0

22 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
A providerForFindContentWhenContentLoadThrowsException() 0 13 1
B testFindContentWhenContentLoadThrowsException() 0 43 2
A testFindContentNoPermissionsFilter() 0 68 1
B testFindContentWithPermission() 0 77 1
B testFindContentWithNoPermission() 0 36 1
A testFindContentWithDefaultQueryValues() 0 74 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
    public function providerForFindContentWhenContentLoadThrowsException()
245
    {
246
        return [
247
            [
248
                new NotFoundException('content', 'id = 33'),
249
                true,
250
            ],
251
            [
252
                new UnauthorizedException('content', 'read', ['id' => 33]),
253
                false,
254
            ],
255
        ];
256
    }
257
258
    /**
259
     * Test for the findContent() method when search is out of sync with persistence.
260
     *
261
     * @dataProvider providerForFindContentWhenContentLoadThrowsException
262
     * @covers \eZ\Publish\Core\Repository\SearchService::findContent
263
     */
264
    public function testFindContentWhenContentLoadThrowsException($e, $index = true)
265
    {
266
        $indexer = $this->createMock(BackgroundIndexer::class);
267
        if ($index) {
268
            $indexer->expects($this->once())
269
                ->method('registerContent')
270
                ->with($this->isInstanceOf(SPIContentInfo::class));
271
        } else {
272
            $indexer->expects($this->never())->method($this->anything());
273
        }
274
275
        $service = $this->getMockBuilder(SearchService::class)
276
            ->setConstructorArgs([
277
                $repo = $this->getRepositoryMock(),
278
                $this->getSPIMockHandler('Search\\Handler'),
279
                $this->getDomainMapperMock(),
280
                $this->getPermissionCriterionResolverMock(),
281
                $indexer,
282
            ])->setMethods(['internalFindContentInfo'])
283
            ->getMock();
284
285
        $info = new SPIContentInfo(['id' => 33]);
286
        $result = new SearchResult(['searchHits' => [new SearchHit(['valueObject' => $info])], 'totalCount' => 2]);
287
        $service->expects($this->once())
288
            ->method('internalFindContentInfo')
289
            ->with($this->isInstanceOf(Query::class))
290
            ->willReturn($result);
291
292
        $contentService = $this->createMock(ContentService::class);
293
        $contentService->expects($this->once())
294
            ->method('internalLoadContent')
295
            ->with(33)
296
            ->willThrowException($e);
297
298
        $repo->expects($this->once())
299
            ->method('getContentService')
300
            ->willReturn($contentService);
301
302
        $finalResult = $service->findContent(new Query());
303
304
        $this->assertEmpty($finalResult->searchHits, 'Expected search hits to be empty');
305
        $this->assertEquals(1, $finalResult->totalCount, 'Expected total count to be 1');
306
    }
307
308
    /**
309
     * Test for the findContent() method.
310
     *
311
     * @covers \eZ\Publish\Core\Repository\SearchService::addPermissionsCriterion
312
     * @covers \eZ\Publish\Core\Repository\SearchService::findContent
313
     */
314
    public function testFindContentNoPermissionsFilter()
315
    {
316
        $repositoryMock = $this->getRepositoryMock();
317
        /** @var \eZ\Publish\SPI\Search\Handler $searchHandlerMock */
318
        $searchHandlerMock = $this->getSPIMockHandler('Search\\Handler');
319
        $domainMapperMock = $this->getDomainMapperMock();
320
        $permissionsCriterionResolverMock = $this->getPermissionCriterionResolverMock();
321
        $service = new SearchService(
322
            $repositoryMock,
0 ignored issues
show
Bug introduced by
It seems like $repositoryMock defined by $this->getRepositoryMock() on line 316 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...
323
            $searchHandlerMock,
324
            $domainMapperMock,
0 ignored issues
show
Bug introduced by
It seems like $domainMapperMock defined by $this->getDomainMapperMock() on line 319 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...
325
            $permissionsCriterionResolverMock,
0 ignored issues
show
Bug introduced by
It seems like $permissionsCriterionResolverMock defined by $this->getPermissionCriterionResolverMock() on line 320 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...
326
            new NullIndexer(),
327
            array()
328
        );
329
330
        $repositoryMock->expects($this->never())->method('hasAccess');
331
332
        $repositoryMock
333
            ->expects($this->once())
334
            ->method('getContentService')
335
            ->will(
336
                $this->returnValue(
337
                    $contentServiceMock = $this
338
                        ->getMockBuilder(ContentService::class)
339
                        ->disableOriginalConstructor()
340
                        ->getMock()
341
                )
342
            );
343
344
        $serviceQuery = new Query();
345
        $handlerQuery = new Query(array('filter' => new Criterion\MatchAll(), 'limit' => 25));
346
        $languageFilter = array();
347
        $spiContentInfo = new SPIContentInfo();
348
        $contentMock = $this->getMockForAbstractClass(Content::class);
349
350
        /* @var \PHPUnit_Framework_MockObject_MockObject $searchHandlerMock */
351
        $searchHandlerMock->expects($this->once())
352
            ->method('findContent')
353
            ->with($this->equalTo($handlerQuery), $this->equalTo($languageFilter))
354
            ->will(
355
                $this->returnValue(
356
                    new SearchResult(
357
                        array(
358
                            'searchHits' => array(new SearchHit(array('valueObject' => $spiContentInfo))),
359
                            'totalCount' => 1,
360
                        )
361
                    )
362
                )
363
            );
364
365
        $contentServiceMock
366
            ->expects($this->once())
367
            ->method('internalLoadContent')
368
            ->will($this->returnValue($contentMock));
369
370
        $result = $service->findContent($serviceQuery, $languageFilter, false);
371
372
        $this->assertEquals(
373
            new SearchResult(
374
                array(
375
                    'searchHits' => array(new SearchHit(array('valueObject' => $contentMock))),
376
                    'totalCount' => 1,
377
                )
378
            ),
379
            $result
380
        );
381
    }
382
383
    /**
384
     * Test for the findContent() method.
385
     *
386
     * @covers \eZ\Publish\Core\Repository\SearchService::addPermissionsCriterion
387
     * @covers \eZ\Publish\Core\Repository\SearchService::findContent
388
     */
389
    public function testFindContentWithPermission()
390
    {
391
        $repositoryMock = $this->getRepositoryMock();
392
        /** @var \eZ\Publish\SPI\Search\Handler $searchHandlerMock */
393
        $searchHandlerMock = $this->getSPIMockHandler('Search\\Handler');
394
        $domainMapperMock = $this->getDomainMapperMock();
395
        $permissionsCriterionResolverMock = $this->getPermissionCriterionResolverMock();
396
        $service = new SearchService(
397
            $repositoryMock,
0 ignored issues
show
Bug introduced by
It seems like $repositoryMock defined by $this->getRepositoryMock() on line 391 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...
398
            $searchHandlerMock,
399
            $domainMapperMock,
0 ignored issues
show
Bug introduced by
It seems like $domainMapperMock defined by $this->getDomainMapperMock() on line 394 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...
400
            $permissionsCriterionResolverMock,
0 ignored issues
show
Bug introduced by
It seems like $permissionsCriterionResolverMock defined by $this->getPermissionCriterionResolverMock() on line 395 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...
401
            new NullIndexer(),
402
            array()
403
        );
404
405
        $repositoryMock
406
            ->expects($this->once())
407
            ->method('getContentService')
408
            ->will(
409
                $this->returnValue(
410
                    $contentServiceMock = $this
411
                        ->getMockBuilder(ContentService::class)
412
                        ->disableOriginalConstructor()
413
                        ->getMock()
414
                )
415
            );
416
417
        $criterionMock = $this
418
            ->getMockBuilder(Criterion::class)
419
            ->disableOriginalConstructor()
420
            ->getMock();
421
        $query = new Query(array('filter' => $criterionMock, 'limit' => 10));
422
        $languageFilter = array();
423
        $spiContentInfo = new SPIContentInfo();
424
        $contentMock = $this->getMockForAbstractClass(Content::class);
425
426
        /* @var \PHPUnit_Framework_MockObject_MockObject $searchHandlerMock */
427
        $searchHandlerMock->expects($this->once())
428
            ->method('findContent')
429
            ->with($this->equalTo($query), $this->equalTo($languageFilter))
430
            ->will(
431
                $this->returnValue(
432
                    new SearchResult(
433
                        array(
434
                            'searchHits' => array(new SearchHit(array('valueObject' => $spiContentInfo))),
435
                            'totalCount' => 1,
436
                        )
437
                    )
438
                )
439
            );
440
441
        $domainMapperMock->expects($this->never())
442
            ->method($this->anything());
443
444
        $contentServiceMock
445
            ->expects($this->once())
446
            ->method('internalLoadContent')
447
            ->will($this->returnValue($contentMock));
448
449
        $permissionsCriterionResolverMock->expects($this->once())
450
            ->method('getPermissionsCriterion')
451
            ->with('content', 'read')
452
            ->will($this->returnValue(true));
453
454
        $result = $service->findContent($query, $languageFilter, true);
455
456
        $this->assertEquals(
457
            new SearchResult(
458
                array(
459
                    'searchHits' => array(new SearchHit(array('valueObject' => $contentMock))),
460
                    'totalCount' => 1,
461
                )
462
            ),
463
            $result
464
        );
465
    }
466
467
    /**
468
     * Test for the findContent() method.
469
     *
470
     * @covers \eZ\Publish\Core\Repository\SearchService::addPermissionsCriterion
471
     * @covers \eZ\Publish\Core\Repository\SearchService::findContent
472
     */
473
    public function testFindContentWithNoPermission()
474
    {
475
        $repositoryMock = $this->getRepositoryMock();
476
        /** @var \eZ\Publish\SPI\Search\Handler $searchHandlerMock */
477
        $searchHandlerMock = $this->getSPIMockHandler('Search\\Handler');
478
        $permissionsCriterionResolverMock = $this->getPermissionCriterionResolverMock();
479
        $service = new SearchService(
480
            $repositoryMock,
0 ignored issues
show
Bug introduced by
It seems like $repositoryMock defined by $this->getRepositoryMock() on line 475 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...
481
            $searchHandlerMock,
482
            $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...
483
            $permissionsCriterionResolverMock,
0 ignored issues
show
Bug introduced by
It seems like $permissionsCriterionResolverMock defined by $this->getPermissionCriterionResolverMock() on line 478 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...
484
            new NullIndexer(),
485
            array()
486
        );
487
488
        /* @var \PHPUnit_Framework_MockObject_MockObject $searchHandlerMock */
489
        $searchHandlerMock->expects($this->never())->method('findContent');
490
491
        $criterionMock = $this
492
            ->getMockBuilder(Criterion::class)
493
            ->disableOriginalConstructor()
494
            ->getMock();
495
        $query = new Query(array('filter' => $criterionMock));
496
497
        $permissionsCriterionResolverMock->expects($this->once())
498
            ->method('getPermissionsCriterion')
499
            ->with('content', 'read')
500
            ->will($this->returnValue(false));
501
502
        $result = $service->findContent($query, array(), true);
503
504
        $this->assertEquals(
505
            new SearchResult(array('time' => 0, 'totalCount' => 0)),
506
            $result
507
        );
508
    }
509
510
    /**
511
     * Test for the findContent() method.
512
     */
513
    public function testFindContentWithDefaultQueryValues()
514
    {
515
        $repositoryMock = $this->getRepositoryMock();
516
        /** @var \eZ\Publish\SPI\Search\Handler $searchHandlerMock */
517
        $searchHandlerMock = $this->getSPIMockHandler('Search\\Handler');
518
        $domainMapperMock = $this->getDomainMapperMock();
519
        $service = new SearchService(
520
            $repositoryMock,
0 ignored issues
show
Bug introduced by
It seems like $repositoryMock defined by $this->getRepositoryMock() on line 515 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...
521
            $searchHandlerMock,
522
            $domainMapperMock,
0 ignored issues
show
Bug introduced by
It seems like $domainMapperMock defined by $this->getDomainMapperMock() on line 518 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...
523
            $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...
524
            new NullIndexer(),
525
            array()
526
        );
527
528
        $repositoryMock
529
            ->expects($this->once())
530
            ->method('getContentService')
531
            ->will(
532
                $this->returnValue(
533
                    $contentServiceMock = $this
534
                        ->getMockBuilder(ContentService::class)
535
                        ->disableOriginalConstructor()
536
                        ->getMock()
537
                )
538
            );
539
540
        $languageFilter = array();
541
        $spiContentInfo = new SPIContentInfo();
542
        $contentMock = $this->getMockForAbstractClass(Content::class);
543
        $domainMapperMock->expects($this->never())
544
            ->method($this->anything());
545
546
        $contentServiceMock
547
            ->expects($this->once())
548
            ->method('internalLoadContent')
549
            ->will($this->returnValue($contentMock));
550
551
        /* @var \PHPUnit_Framework_MockObject_MockObject $searchHandlerMock */
552
        $searchHandlerMock
553
            ->expects($this->once())
554
            ->method('findContent')
555
            ->with(
556
                new Query(
557
                    array(
558
                        'filter' => new Criterion\MatchAll(),
559
                        'limit' => 25,
560
                    )
561
                ),
562
                array()
563
            )
564
            ->will(
565
                $this->returnValue(
566
                    new SearchResult(
567
                        array(
568
                            'searchHits' => array(new SearchHit(array('valueObject' => $spiContentInfo))),
569
                            'totalCount' => 1,
570
                        )
571
                    )
572
                )
573
            );
574
575
        $result = $service->findContent(new Query(), $languageFilter, false);
576
577
        $this->assertEquals(
578
            new SearchResult(
579
                array(
580
                    'searchHits' => array(new SearchHit(array('valueObject' => $contentMock))),
581
                    'totalCount' => 1,
582
                )
583
            ),
584
            $result
585
        );
586
    }
587
588
    /**
589
     * Test for the findSingle() method.
590
     *
591
     * @covers \eZ\Publish\Core\Repository\SearchService::addPermissionsCriterion
592
     * @covers \eZ\Publish\Core\Repository\SearchService::findSingle
593
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
594
     */
595 View Code Duplication
    public function testFindSingleThrowsNotFoundException()
596
    {
597
        $repositoryMock = $this->getRepositoryMock();
598
        /** @var \eZ\Publish\SPI\Search\Handler $searchHandlerMock */
599
        $searchHandlerMock = $this->getSPIMockHandler('Search\\Handler');
600
        $service = new SearchService(
601
            $repositoryMock,
0 ignored issues
show
Bug introduced by
It seems like $repositoryMock defined by $this->getRepositoryMock() on line 597 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...
602
            $searchHandlerMock,
603
            $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...
604
            $permissionsCriterionResolverMock = $this->getPermissionCriterionResolverMock(),
605
            new NullIndexer(),
606
            array()
607
        );
608
609
        /** @var \eZ\Publish\API\Repository\Values\Content\Query\Criterion $criterionMock */
610
        $criterionMock = $this
611
            ->getMockBuilder(Criterion::class)
612
            ->disableOriginalConstructor()
613
            ->getMock();
614
615
        $permissionsCriterionResolverMock->expects($this->once())
616
            ->method('getPermissionsCriterion')
617
            ->with('content', 'read')
618
            ->willReturn(false);
619
620
        $service->findSingle($criterionMock, array(), true);
621
    }
622
623
    /**
624
     * Test for the findSingle() method.
625
     *
626
     * @covers \eZ\Publish\Core\Repository\SearchService::addPermissionsCriterion
627
     * @covers \eZ\Publish\Core\Repository\SearchService::findSingle
628
     * @expectedException \Exception
629
     * @expectedExceptionMessage Handler threw an exception
630
     */
631 View Code Duplication
    public function testFindSingleThrowsHandlerException()
632
    {
633
        $repositoryMock = $this->getRepositoryMock();
634
        /** @var \eZ\Publish\SPI\Search\Handler $searchHandlerMock */
635
        $searchHandlerMock = $this->getSPIMockHandler('Search\\Handler');
636
        $permissionsCriterionResolverMock = $this->getPermissionCriterionResolverMock();
637
        $service = new SearchService(
638
            $repositoryMock,
0 ignored issues
show
Bug introduced by
It seems like $repositoryMock defined by $this->getRepositoryMock() on line 633 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...
639
            $searchHandlerMock,
640
            $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...
641
            $permissionsCriterionResolverMock,
0 ignored issues
show
Bug introduced by
It seems like $permissionsCriterionResolverMock defined by $this->getPermissionCriterionResolverMock() on line 636 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...
642
            new NullIndexer(),
643
            array()
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->throwException(new Exception('Handler threw an exception')));
656
657
        $service->findSingle($criterionMock, array(), true);
658
    }
659
660
    /**
661
     * Test for the findSingle() method.
662
     *
663
     * @covers \eZ\Publish\Core\Repository\SearchService::addPermissionsCriterion
664
     * @covers \eZ\Publish\Core\Repository\SearchService::findSingle
665
     */
666
    public function testFindSingle()
667
    {
668
        $repositoryMock = $this->getRepositoryMock();
669
        /** @var \eZ\Publish\SPI\Search\Handler $searchHandlerMock */
670
        $searchHandlerMock = $this->getSPIMockHandler('Search\\Handler');
671
        $domainMapperMock = $this->getDomainMapperMock();
672
        $permissionsCriterionResolverMock = $this->getPermissionCriterionResolverMock();
673
        $service = new SearchService(
674
            $repositoryMock,
0 ignored issues
show
Bug introduced by
It seems like $repositoryMock defined by $this->getRepositoryMock() on line 668 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...
675
            $searchHandlerMock,
676
            $domainMapperMock,
0 ignored issues
show
Bug introduced by
It seems like $domainMapperMock defined by $this->getDomainMapperMock() on line 671 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...
677
            $permissionsCriterionResolverMock,
0 ignored issues
show
Bug introduced by
It seems like $permissionsCriterionResolverMock defined by $this->getPermissionCriterionResolverMock() on line 672 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...
678
            new NullIndexer(),
679
            array()
680
        );
681
682
        $repositoryMock
683
            ->expects($this->once())
684
            ->method('getContentService')
685
            ->will(
686
                $this->returnValue(
687
                    $contentServiceMock = $this
688
                        ->getMockBuilder(ContentService::class)
689
                        ->disableOriginalConstructor()
690
                        ->getMock()
691
                )
692
            );
693
694
        /** @var \eZ\Publish\API\Repository\Values\Content\Query\Criterion $criterionMock */
695
        $criterionMock = $this
696
            ->getMockBuilder(Criterion::class)
697
            ->disableOriginalConstructor()
698
            ->getMock();
699
700
        $permissionsCriterionResolverMock->expects($this->once())
701
            ->method('getPermissionsCriterion')
702
            ->with('content', 'read')
703
            ->will($this->returnValue(true));
704
705
        $languageFilter = array();
706
        $spiContentInfo = new SPIContentInfo();
707
        $contentMock = $this->getMockForAbstractClass(Content::class);
708
709
        /* @var \PHPUnit_Framework_MockObject_MockObject $searchHandlerMock */
710
        $searchHandlerMock->expects($this->once())
711
            ->method('findSingle')
712
            ->with($this->equalTo($criterionMock), $this->equalTo($languageFilter))
713
            ->will($this->returnValue($spiContentInfo));
714
715
        $domainMapperMock->expects($this->never())
716
            ->method($this->anything());
717
718
        $contentServiceMock
719
            ->expects($this->once())
720
            ->method('internalLoadContent')
721
            ->will($this->returnValue($contentMock));
722
723
        $result = $service->findSingle($criterionMock, $languageFilter, true);
724
725
        $this->assertEquals($contentMock, $result);
726
    }
727
728
    /**
729
     * Test for the findLocations() method.
730
     */
731
    public function testFindLocationsWithPermission()
732
    {
733
        $repositoryMock = $this->getRepositoryMock();
734
        /** @var \eZ\Publish\SPI\Search\Handler $searchHandlerMock */
735
        $searchHandlerMock = $this->getSPIMockHandler('Search\\Handler');
736
        $domainMapperMock = $this->getDomainMapperMock();
737
        $permissionsCriterionResolverMock = $this->getPermissionCriterionResolverMock();
738
        $service = new SearchService(
739
            $repositoryMock,
0 ignored issues
show
Bug introduced by
It seems like $repositoryMock defined by $this->getRepositoryMock() on line 733 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...
740
            $searchHandlerMock,
741
            $domainMapperMock,
0 ignored issues
show
Bug introduced by
It seems like $domainMapperMock defined by $this->getDomainMapperMock() on line 736 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...
742
            $permissionsCriterionResolverMock,
0 ignored issues
show
Bug introduced by
It seems like $permissionsCriterionResolverMock defined by $this->getPermissionCriterionResolverMock() on line 737 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...
743
            new NullIndexer(),
744
            array()
745
        );
746
747
        $criterionMock = $this
748
            ->getMockBuilder(Criterion::class)
749
            ->disableOriginalConstructor()
750
            ->getMock();
751
        $query = new LocationQuery(array('filter' => $criterionMock, 'limit' => 10));
752
        $spiLocation = new SPILocation();
753
        $locationMock = $this->getMockForAbstractClass(Location::class);
754
755
        /* @var \PHPUnit_Framework_MockObject_MockObject $searchHandlerMock */
756
        $searchHandlerMock->expects($this->once())
757
            ->method('findLocations')
758
            ->with($this->equalTo($query))
759
            ->will(
760
                $this->returnValue(
761
                    $spiResult = new SearchResult(
762
                        array(
763
                            'searchHits' => array(new SearchHit(array('valueObject' => $spiLocation))),
764
                            'totalCount' => 1,
765
                        )
766
                    )
767
                )
768
            );
769
770
        $permissionsCriterionResolverMock->expects($this->once())
771
            ->method('getPermissionsCriterion')
772
            ->with('content', 'read')
773
            ->will($this->returnValue(true));
774
775
        $endResult = new SearchResult(
776
            [
777
                'searchHits' => [new SearchHit(['valueObject' => $locationMock])],
778
                'totalCount' => 1,
779
            ]
780
        );
781
782
        $domainMapperMock->expects($this->once())
783
            ->method('buildLocationDomainObjectsOnSearchResult')
784
            ->with($this->equalTo($spiResult))
785
            ->willReturnCallback(function (SearchResult $spiResult) use ($endResult) {
786
                $spiResult->searchHits[0] = $endResult->searchHits[0];
787
788
                return [];
789
            });
790
791
        $result = $service->findLocations($query, array(), true);
792
793
        $this->assertEquals(
794
            $endResult,
795
            $result
796
        );
797
    }
798
799
    /**
800
     * Test for the findLocations() method.
801
     */
802
    public function testFindLocationsWithNoPermissionsFilter()
803
    {
804
        $repositoryMock = $this->getRepositoryMock();
805
        /** @var \eZ\Publish\SPI\Search\Handler $searchHandlerMock */
806
        $searchHandlerMock = $this->getSPIMockHandler('Search\\Handler');
807
        $domainMapperMock = $this->getDomainMapperMock();
808
        $permissionsCriterionResolverMock = $this->getPermissionCriterionResolverMock();
809
        $service = new SearchService(
810
            $repositoryMock,
0 ignored issues
show
Bug introduced by
It seems like $repositoryMock defined by $this->getRepositoryMock() on line 804 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...
811
            $searchHandlerMock,
812
            $domainMapperMock,
0 ignored issues
show
Bug introduced by
It seems like $domainMapperMock defined by $this->getDomainMapperMock() on line 807 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...
813
            $permissionsCriterionResolverMock,
0 ignored issues
show
Bug introduced by
It seems like $permissionsCriterionResolverMock defined by $this->getPermissionCriterionResolverMock() on line 808 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...
814
            new NullIndexer(),
815
            array()
816
        );
817
818
        $repositoryMock->expects($this->never())->method('hasAccess');
819
820
        $serviceQuery = new LocationQuery();
821
        $handlerQuery = new LocationQuery(array('filter' => new Criterion\MatchAll(), 'limit' => 25));
822
        $spiLocation = new SPILocation();
823
        $locationMock = $this->getMockForAbstractClass(Location::class);
824
825
        /* @var \PHPUnit_Framework_MockObject_MockObject $searchHandlerMock */
826
        $searchHandlerMock->expects($this->once())
827
            ->method('findLocations')
828
            ->with($this->equalTo($handlerQuery))
829
            ->will(
830
                $this->returnValue(
831
                    $spiResult = new SearchResult(
832
                        array(
833
                            'searchHits' => array(new SearchHit(array('valueObject' => $spiLocation))),
834
                            'totalCount' => 1,
835
                        )
836
                    )
837
                )
838
            );
839
840
        $permissionsCriterionResolverMock->expects($this->never())->method($this->anything());
841
842
        $endResult = new SearchResult(
843
            [
844
                'searchHits' => [new SearchHit(['valueObject' => $locationMock])],
845
                'totalCount' => 1,
846
            ]
847
        );
848
849
        $domainMapperMock->expects($this->once())
850
            ->method('buildLocationDomainObjectsOnSearchResult')
851
            ->with($this->equalTo($spiResult))
852
            ->willReturnCallback(function (SearchResult $spiResult) use ($endResult) {
853
                $spiResult->searchHits[0] = $endResult->searchHits[0];
854
855
                return [];
856
            });
857
858
        $result = $service->findLocations($serviceQuery, array(), false);
859
860
        $this->assertEquals(
861
            $endResult,
862
            $result
863
        );
864
    }
865
866
    /**
867
     * Test for the findLocations() method when search is out of sync with persistence.
868
     *
869
     * @covers \eZ\Publish\Core\Repository\SearchService::findLocations
870
     */
871
    public function testFindLocationsBackgroundIndexerWhenDomainMapperThrowsException()
872
    {
873
        $indexer = $this->createMock(BackgroundIndexer::class);
874
        $indexer->expects($this->once())
875
            ->method('registerLocation')
876
            ->with($this->isInstanceOf(SPILocation::class));
877
878
        $service = $this->getMockBuilder(SearchService::class)
879
            ->setConstructorArgs([
880
                $this->getRepositoryMock(),
881
                $searchHandler = $this->getSPIMockHandler('Search\\Handler'),
882
                $mapper = $this->getDomainMapperMock(),
883
                $this->getPermissionCriterionResolverMock(),
884
                $indexer,
885
            ])->setMethods(['addPermissionsCriterion'])
886
            ->getMock();
887
888
        $location = new SPILocation(['id' => 44]);
889
        $service->expects($this->once())
890
            ->method('addPermissionsCriterion')
891
            ->with($this->isInstanceOf(Criterion::class))
892
            ->willReturn(true);
893
894
        $result = new SearchResult(['searchHits' => [new SearchHit(['valueObject' => $location])], 'totalCount' => 2]);
895
        $searchHandler->expects($this->once())
896
            ->method('findLocations')
897
            ->with($this->isInstanceOf(LocationQuery::class), $this->isType('array'))
898
            ->willReturn($result);
899
900
        $mapper->expects($this->once())
901
            ->method('buildLocationDomainObjectsOnSearchResult')
902
            ->with($this->equalTo($result))
903
            ->willReturnCallback(function (SearchResult $spiResult) use ($location) {
904
                unset($spiResult->searchHits[0]);
905
                --$spiResult->totalCount;
906
907
                return [$location];
908
            });
909
910
        $finalResult = $service->findLocations(new LocationQuery());
911
912
        $this->assertEmpty($finalResult->searchHits, 'Expected search hits to be empty');
913
        $this->assertEquals(1, $finalResult->totalCount, 'Expected total count to be 1');
914
    }
915
916
    /**
917
     * Test for the findLocations() method.
918
     *
919
     * @expectedException \Exception
920
     * @expectedExceptionMessage Handler threw an exception
921
     */
922 View Code Duplication
    public function testFindLocationsThrowsHandlerException()
923
    {
924
        $repositoryMock = $this->getRepositoryMock();
925
        /** @var \eZ\Publish\SPI\Search\Handler $searchHandlerMock */
926
        $searchHandlerMock = $this->getSPIMockHandler('Search\\Handler');
927
        $permissionsCriterionResolverMock = $this->getPermissionCriterionResolverMock();
928
929
        $service = new SearchService(
930
            $repositoryMock,
0 ignored issues
show
Bug introduced by
It seems like $repositoryMock defined by $this->getRepositoryMock() on line 924 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...
931
            $searchHandlerMock,
932
            $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...
933
            $permissionsCriterionResolverMock,
0 ignored issues
show
Bug introduced by
It seems like $permissionsCriterionResolverMock defined by $this->getPermissionCriterionResolverMock() on line 927 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...
934
            new NullIndexer(),
935
            array()
936
        );
937
938
        /** @var \eZ\Publish\API\Repository\Values\Content\Query\Criterion $criterionMock */
939
        $criterionMock = $this
940
            ->getMockBuilder(Criterion::class)
941
            ->disableOriginalConstructor()
942
            ->getMock();
943
        $query = new LocationQuery(array('filter' => $criterionMock));
944
945
        $permissionsCriterionResolverMock->expects($this->once())
946
            ->method('getPermissionsCriterion')
947
            ->with('content', 'read')
948
            ->will($this->throwException(new Exception('Handler threw an exception')));
949
950
        $service->findLocations($query, array(), true);
951
    }
952
953
    /**
954
     * Test for the findLocations() method.
955
     */
956
    public function testFindLocationsWithDefaultQueryValues()
957
    {
958
        $repositoryMock = $this->getRepositoryMock();
959
        /** @var \eZ\Publish\SPI\Search\Handler $searchHandlerMock */
960
        $searchHandlerMock = $this->getSPIMockHandler('Search\\Handler');
961
        $domainMapperMock = $this->getDomainMapperMock();
962
        $service = new SearchService(
963
            $repositoryMock,
0 ignored issues
show
Bug introduced by
It seems like $repositoryMock defined by $this->getRepositoryMock() on line 958 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...
964
            $searchHandlerMock,
965
            $domainMapperMock,
0 ignored issues
show
Bug introduced by
It seems like $domainMapperMock defined by $this->getDomainMapperMock() on line 961 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...
966
            $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...
967
            new NullIndexer(),
968
            array()
969
        );
970
971
        $spiLocation = new SPILocation();
972
        $locationMock = $this->getMockForAbstractClass(Location::class);
973
974
        /* @var \PHPUnit_Framework_MockObject_MockObject $searchHandlerMock */
975
        $searchHandlerMock
976
            ->expects($this->once())
977
            ->method('findLocations')
978
            ->with(
979
                new LocationQuery(
980
                    array(
981
                        'filter' => new Criterion\MatchAll(),
982
                        'limit' => 25,
983
                    )
984
                )
985
            )
986
            ->will(
987
                $this->returnValue(
988
                    $spiResult = new SearchResult(
989
                        array(
990
                            'searchHits' => array(new SearchHit(array('valueObject' => $spiLocation))),
991
                            'totalCount' => 1,
992
                        )
993
                    )
994
                )
995
            );
996
997
        $endResult = new SearchResult(
998
            [
999
                'searchHits' => [new SearchHit(['valueObject' => $locationMock])],
1000
                'totalCount' => 1,
1001
            ]
1002
        );
1003
1004
        $domainMapperMock->expects($this->once())
1005
            ->method('buildLocationDomainObjectsOnSearchResult')
1006
            ->with($this->equalTo($spiResult))
1007
            ->willReturnCallback(function (SearchResult $spiResult) use ($endResult) {
1008
                $spiResult->searchHits[0] = $endResult->searchHits[0];
1009
1010
                return [];
1011
            });
1012
1013
        $result = $service->findLocations(new LocationQuery(), array(), false);
1014
1015
        $this->assertEquals(
1016
            $endResult,
1017
            $result
1018
        );
1019
    }
1020
1021
    /**
1022
     * @return \PHPUnit_Framework_MockObject_MockObject|\eZ\Publish\Core\Repository\Helper\DomainMapper
1023
     */
1024
    protected function getDomainMapperMock()
1025
    {
1026
        if (!isset($this->domainMapperMock)) {
1027
            $this->domainMapperMock = $this
1028
                ->getMockBuilder(DomainMapper::class)
1029
                ->disableOriginalConstructor()
1030
                ->getMock();
1031
        }
1032
1033
        return $this->domainMapperMock;
1034
    }
1035
1036
    /**
1037
     * @return \PHPUnit_Framework_MockObject_MockObject|\eZ\Publish\API\Repository\PermissionCriterionResolver
1038
     */
1039
    protected function getPermissionCriterionResolverMock()
1040
    {
1041
        if (!isset($this->permissionsCriterionResolverMock)) {
1042
            $this->permissionsCriterionResolverMock = $this
1043
                ->getMockBuilder(PermissionCriterionResolver::class)
1044
                ->disableOriginalConstructor()
1045
                ->getMock();
1046
        }
1047
1048
        return $this->permissionsCriterionResolverMock;
1049
    }
1050
}
1051