Completed
Push — tolerant_search_service ( 5185ff...1edbf9 )
by André
11:34
created

providerForFindSingleValidatesLocationCriteria()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 10
nc 1
nop 0
dl 0
loc 17
rs 9.4285
c 0
b 0
f 0
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\API\Repository\Values\Content\ContentInfo;
12
use eZ\Publish\Core\Base\Exceptions\NotFoundException;
13
use eZ\Publish\Core\Base\Exceptions\UnauthorizedException;
14
use eZ\Publish\Core\Repository\ContentService;
15
use eZ\Publish\Core\Repository\Helper\DomainMapper;
16
use eZ\Publish\Core\Repository\Tests\Service\Mock\Base as BaseServiceMockTest;
17
use eZ\Publish\Core\Repository\SearchService;
18
use eZ\Publish\Core\Repository\Permission\PermissionCriterionResolver;
19
use eZ\Publish\API\Repository\Values\Content\Query;
20
use eZ\Publish\API\Repository\Values\Content\LocationQuery;
21
use eZ\Publish\API\Repository\Values\Content\Query\Criterion;
22
use eZ\Publish\API\Repository\Values\Content\Query\SortClause;
23
use eZ\Publish\API\Repository\Values\Content\Search\SearchResult;
24
use eZ\Publish\API\Repository\Values\Content\Search\SearchHit;
25
use eZ\Publish\Core\Search\Common\BackgroundIndexer;
26
use eZ\Publish\Core\Search\Common\BackgroundIndexer\NullIndexer;
27
use eZ\Publish\SPI\Persistence\Content\ContentInfo as SPIContentInfo;
28
use eZ\Publish\SPI\Persistence\Content\Location as SPILocation;
29
use eZ\Publish\API\Repository\Exceptions\InvalidArgumentException;
30
use Exception;
31
32
/**
33
 * Mock test case for Search service.
34
 */
35
class SearchTest extends BaseServiceMockTest
36
{
37
    protected $repositoryMock;
38
39
    protected $domainMapperMock;
40
41
    protected $permissionsCriterionResolverMock;
42
43
    /**
44
     * Test for the __construct() method.
45
     *
46
     * @covers \eZ\Publish\Core\Repository\SearchService::__construct
47
     */
48
    public function testConstructor()
49
    {
50
        $repositoryMock = $this->getRepositoryMock();
51
        /** @var \eZ\Publish\SPI\Search\Handler $searchHandlerMock */
52
        $searchHandlerMock = $this->getSPIMockHandler('Search\\Handler');
53
        $domainMapperMock = $this->getDomainMapperMock();
54
        $permissionsCriterionResolverMock = $this->getPermissionCriterionResolverMock();
55
        $settings = array('teh setting');
56
57
        $service = new SearchService(
58
            $repositoryMock,
0 ignored issues
show
Bug introduced by
It seems like $repositoryMock defined by $this->getRepositoryMock() on line 50 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...
59
            $searchHandlerMock,
60
            $domainMapperMock,
0 ignored issues
show
Bug introduced by
It seems like $domainMapperMock defined by $this->getDomainMapperMock() 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\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...
61
            $permissionsCriterionResolverMock,
0 ignored issues
show
Bug introduced by
It seems like $permissionsCriterionResolverMock defined by $this->getPermissionCriterionResolverMock() 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\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...
62
            new NullIndexer(),
63
            $settings
64
        );
65
66
        $this->assertAttributeSame(
67
            $repositoryMock,
68
            'repository',
69
            $service
70
        );
71
72
        $this->assertAttributeSame(
73
            $searchHandlerMock,
74
            'searchHandler',
75
            $service
76
        );
77
78
        $this->assertAttributeSame(
79
            $domainMapperMock,
80
            'domainMapper',
81
            $service
82
        );
83
84
        $this->assertAttributeSame(
85
            $permissionsCriterionResolverMock,
86
            'permissionCriterionResolver',
87
            $service
88
        );
89
90
        $this->assertAttributeSame(
91
            $settings,
92
            'settings',
93
            $service
94
        );
95
    }
96
97
    public function providerForFindContentValidatesLocationCriteriaAndSortClauses()
98
    {
99
        return array(
100
            array(
101
                new Query(array('filter' => new Criterion\Location\Depth(Criterion\Operator::LT, 2))),
102
                "Argument '\$query' is invalid: Location criterions cannot be used in Content search",
103
            ),
104
            array(
105
                new Query(array('query' => new Criterion\Location\Depth(Criterion\Operator::LT, 2))),
106
                "Argument '\$query' is invalid: Location criterions cannot be used in Content search",
107
            ),
108
            array(
109
                new Query(
110
                    array(
111
                        'query' => new Criterion\LogicalAnd(
112
                            array(
113
                                new Criterion\Location\Depth(Criterion\Operator::LT, 2),
114
                            )
115
                        ),
116
                    )
117
                ),
118
                "Argument '\$query' is invalid: Location criterions cannot be used in Content search",
119
            ),
120
            array(
121
                new Query(array('sortClauses' => array(new SortClause\Location\Id()))),
122
                "Argument '\$query' is invalid: Location sort clauses cannot be used in Content search",
123
            ),
124
        );
125
    }
126
127
    /**
128
     * @dataProvider providerForFindContentValidatesLocationCriteriaAndSortClauses
129
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
130
     */
131 View Code Duplication
    public function testFindContentValidatesLocationCriteriaAndSortClauses($query, $exceptionMessage)
132
    {
133
        $repositoryMock = $this->getRepositoryMock();
134
        /** @var \eZ\Publish\SPI\Search\Handler $searchHandlerMock */
135
        $searchHandlerMock = $this->getSPIMockHandler('Search\\Handler');
136
        $permissionsCriterionResolverMock = $this->getPermissionCriterionResolverMock();
137
138
        $service = new SearchService(
139
            $repositoryMock,
0 ignored issues
show
Bug introduced by
It seems like $repositoryMock defined by $this->getRepositoryMock() on line 133 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...
140
            $searchHandlerMock,
141
            $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...
142
            $permissionsCriterionResolverMock,
0 ignored issues
show
Bug introduced by
It seems like $permissionsCriterionResolverMock defined by $this->getPermissionCriterionResolverMock() on line 136 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...
143
            new NullIndexer(),
144
            array()
145
        );
146
147
        try {
148
            $service->findContent($query);
149
        } catch (InvalidArgumentException $e) {
150
            $this->assertEquals($exceptionMessage, $e->getMessage());
151
            throw $e;
152
        }
153
154
        $this->fail('Expected exception was not thrown');
155
    }
156
157
    public function providerForFindSingleValidatesLocationCriteria()
158
    {
159
        return array(
160
            array(
161
                new Criterion\Location\Depth(Criterion\Operator::LT, 2),
162
                "Argument '\$filter' is invalid: Location criterions cannot be used in Content search",
163
            ),
164
            array(
165
                new Criterion\LogicalAnd(
166
                    array(
167
                        new Criterion\Location\Depth(Criterion\Operator::LT, 2),
168
                    )
169
                ),
170
                "Argument '\$filter' is invalid: Location criterions cannot be used in Content search",
171
            ),
172
        );
173
    }
174
175
    /**
176
     * @dataProvider providerForFindSingleValidatesLocationCriteria
177
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
178
     */
179 View Code Duplication
    public function testFindSingleValidatesLocationCriteria($criterion, $exceptionMessage)
180
    {
181
        $repositoryMock = $this->getRepositoryMock();
182
        /** @var \eZ\Publish\SPI\Search\Handler $searchHandlerMock */
183
        $searchHandlerMock = $this->getSPIMockHandler('Search\\Handler');
184
        $permissionsCriterionResolverMock = $this->getPermissionCriterionResolverMock();
185
        $service = new SearchService(
186
            $repositoryMock,
0 ignored issues
show
Bug introduced by
It seems like $repositoryMock defined by $this->getRepositoryMock() on line 181 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...
187
            $searchHandlerMock,
188
            $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...
189
            $permissionsCriterionResolverMock,
0 ignored issues
show
Bug introduced by
It seems like $permissionsCriterionResolverMock defined by $this->getPermissionCriterionResolverMock() on line 184 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...
190
            new NullIndexer(),
191
            array()
192
        );
193
194
        try {
195
            $service->findSingle($criterion);
196
        } catch (InvalidArgumentException $e) {
197
            $this->assertEquals($exceptionMessage, $e->getMessage());
198
            throw $e;
199
        }
200
201
        $this->fail('Expected exception was not thrown');
202
    }
203
204
    /**
205
     * Test for the findContent() method.
206
     *
207
     * @covers \eZ\Publish\Core\Repository\SearchService::addPermissionsCriterion
208
     * @covers \eZ\Publish\Core\Repository\SearchService::findContent
209
     * @expectedException \Exception
210
     * @expectedExceptionMessage Handler threw an exception
211
     */
212 View Code Duplication
    public function testFindContentThrowsHandlerException()
213
    {
214
        $repositoryMock = $this->getRepositoryMock();
215
        /** @var \eZ\Publish\SPI\Search\Handler $searchHandlerMock */
216
        $searchHandlerMock = $this->getSPIMockHandler('Search\\Handler');
217
        $permissionsCriterionResolverMock = $this->getPermissionCriterionResolverMock();
218
219
        $service = new SearchService(
220
            $repositoryMock,
0 ignored issues
show
Bug introduced by
It seems like $repositoryMock defined by $this->getRepositoryMock() on line 214 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...
221
            $searchHandlerMock,
222
            $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...
223
            $permissionsCriterionResolverMock,
0 ignored issues
show
Bug introduced by
It seems like $permissionsCriterionResolverMock defined by $this->getPermissionCriterionResolverMock() on line 217 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...
224
            new NullIndexer(),
225
            array()
226
        );
227
228
        /** @var \eZ\Publish\API\Repository\Values\Content\Query\Criterion $criterionMock */
229
        $criterionMock = $this
230
            ->getMockBuilder('eZ\\Publish\\API\\Repository\\Values\\Content\\Query\\Criterion')
231
            ->disableOriginalConstructor()
232
            ->getMock();
233
        $query = new Query(array('filter' => $criterionMock));
234
235
        $permissionsCriterionResolverMock->expects($this->once())
236
            ->method('getPermissionsCriterion')
237
            ->with('content', 'read')
238
            ->will($this->throwException(new Exception('Handler threw an exception')));
239
240
        $service->findContent($query, array(), true);
241
    }
242
243 View Code Duplication
    public function providerForFindContentWhenContentLoadThrowsException()
244
    {
245
        return [
246
            [
247
                new NotFoundException('content', 'id = 33'),
248
                true,
249
            ],
250
            [
251
                new UnauthorizedException('content', 'read', ['id' => 33]),
252
                false,
253
            ],
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->getMockBuilder(BackgroundIndexer::class)
267
            ->disableOriginalConstructor()
268
            ->getMock();
269
270 View Code Duplication
        if ($index) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
271
            $indexer->expects($this->once())
272
                ->method('registerContent')
273
                ->with($this->isInstanceOf(SPIContentInfo::class));
274
        } else {
275
            $indexer->expects($this->never())->method($this->anything());
276
        }
277
278
        $service = $this->getMockBuilder(SearchService::class)
279
            ->setConstructorArgs([
280
                $repo = $this->getRepositoryMock(),
281
                $this->getSPIMockHandler('Search\\Handler'),
282
                $this->getDomainMapperMock(),
283
                $this->getPermissionCriterionResolverMock(),
284
                $indexer
285
            ])->setMethods(['internalFindContentInfo'])
286
            ->getMock();
287
288
        $info = new SPIContentInfo(['id' => 33]);
289
        $result = new SearchResult(['searchHits' => [new SearchHit(['valueObject' => $info])], 'totalCount' => 2]);
290
        $service->expects($this->once())
291
            ->method('internalFindContentInfo')
292
            ->with($this->isInstanceOf(Query::class))
293
            ->willReturn($result);
294
295
        $contentService = $this->getMockBuilder(ContentService::class)
296
            ->disableOriginalConstructor()
297
            ->getMock();
298
299
        $contentService->expects($this->once())
300
            ->method('internalLoadContent')
301
            ->with(33)
302
            ->willThrowException($e);
303
304
        $repo->expects($this->once())
305
            ->method('getContentService')
306
            ->willReturn($contentService);
307
308
        $finalResult =  $service->findContent(new Query());
309
310
        $this->assertEmpty($finalResult->searchHits, 'Expected search hits to be empty');
311
        $this->assertEquals(1, $finalResult->totalCount, 'Expected total count to be 1');
312
    }
313
314
    /**
315
     * Test for the findContent() method.
316
     *
317
     * @covers \eZ\Publish\Core\Repository\SearchService::addPermissionsCriterion
318
     * @covers \eZ\Publish\Core\Repository\SearchService::findContent
319
     */
320
    public function testFindContentNoPermissionsFilter()
321
    {
322
        $repositoryMock = $this->getRepositoryMock();
323
        /** @var \eZ\Publish\SPI\Search\Handler $searchHandlerMock */
324
        $searchHandlerMock = $this->getSPIMockHandler('Search\\Handler');
325
        $domainMapperMock = $this->getDomainMapperMock();
326
        $permissionsCriterionResolverMock = $this->getPermissionCriterionResolverMock();
327
        $service = new SearchService(
328
            $repositoryMock,
0 ignored issues
show
Bug introduced by
It seems like $repositoryMock defined by $this->getRepositoryMock() on line 322 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...
329
            $searchHandlerMock,
330
            $domainMapperMock,
0 ignored issues
show
Bug introduced by
It seems like $domainMapperMock defined by $this->getDomainMapperMock() on line 325 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...
331
            $permissionsCriterionResolverMock,
0 ignored issues
show
Bug introduced by
It seems like $permissionsCriterionResolverMock defined by $this->getPermissionCriterionResolverMock() on line 326 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...
332
            new NullIndexer(),
333
            array()
334
        );
335
336
        $repositoryMock->expects($this->never())->method('hasAccess');
337
338
        $repositoryMock
339
            ->expects($this->once())
340
            ->method('getContentService')
341
            ->will(
342
                $this->returnValue(
343
                    $contentServiceMock = $this
344
                        ->getMockBuilder('eZ\\Publish\\Core\\Repository\\ContentService')
345
                        ->disableOriginalConstructor()
346
                        ->getMock()
347
                )
348
            );
349
350
        $serviceQuery = new Query();
351
        $handlerQuery = new Query(array('filter' => new Criterion\MatchAll(), 'limit' => 25));
352
        $languageFilter = array();
353
        $spiContentInfo = new SPIContentInfo();
354
        $contentMock = $this->getMockForAbstractClass('eZ\\Publish\\API\\Repository\\Values\\Content\\Content');
355
356
        /* @var \PHPUnit_Framework_MockObject_MockObject $searchHandlerMock */
357
        $searchHandlerMock->expects($this->once())
358
            ->method('findContent')
359
            ->with($this->equalTo($handlerQuery), $this->equalTo($languageFilter))
360
            ->will(
361
                $this->returnValue(
362
                    new SearchResult(
363
                        array(
364
                            'searchHits' => array(new SearchHit(array('valueObject' => $spiContentInfo))),
365
                            'totalCount' => 1,
366
                        )
367
                    )
368
                )
369
            );
370
371
        $contentServiceMock
372
            ->expects($this->once())
373
            ->method('internalLoadContent')
374
            ->will($this->returnValue($contentMock));
375
376
        $result = $service->findContent($serviceQuery, $languageFilter, false);
377
378
        $this->assertEquals(
379
            new SearchResult(
380
                array(
381
                    'searchHits' => array(new SearchHit(array('valueObject' => $contentMock))),
382
                    'totalCount' => 1,
383
                )
384
            ),
385
            $result
386
        );
387
    }
388
389
    /**
390
     * Test for the findContent() method.
391
     *
392
     * @covers \eZ\Publish\Core\Repository\SearchService::addPermissionsCriterion
393
     * @covers \eZ\Publish\Core\Repository\SearchService::findContent
394
     */
395
    public function testFindContentWithPermission()
396
    {
397
        $repositoryMock = $this->getRepositoryMock();
398
        /** @var \eZ\Publish\SPI\Search\Handler $searchHandlerMock */
399
        $searchHandlerMock = $this->getSPIMockHandler('Search\\Handler');
400
        $domainMapperMock = $this->getDomainMapperMock();
401
        $permissionsCriterionResolverMock = $this->getPermissionCriterionResolverMock();
402
        $service = new SearchService(
403
            $repositoryMock,
0 ignored issues
show
Bug introduced by
It seems like $repositoryMock defined by $this->getRepositoryMock() on line 397 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...
404
            $searchHandlerMock,
405
            $domainMapperMock,
0 ignored issues
show
Bug introduced by
It seems like $domainMapperMock defined by $this->getDomainMapperMock() on line 400 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...
406
            $permissionsCriterionResolverMock,
0 ignored issues
show
Bug introduced by
It seems like $permissionsCriterionResolverMock defined by $this->getPermissionCriterionResolverMock() on line 401 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...
407
            new NullIndexer(),
408
            array()
409
        );
410
411
        $repositoryMock
412
            ->expects($this->once())
413
            ->method('getContentService')
414
            ->will(
415
                $this->returnValue(
416
                    $contentServiceMock = $this
417
                        ->getMockBuilder('eZ\\Publish\\Core\\Repository\\ContentService')
418
                        ->disableOriginalConstructor()
419
                        ->getMock()
420
                )
421
            );
422
423
        $criterionMock = $this
424
            ->getMockBuilder('eZ\\Publish\\API\\Repository\\Values\\Content\\Query\\Criterion')
425
            ->disableOriginalConstructor()
426
            ->getMock();
427
        $query = new Query(array('filter' => $criterionMock, 'limit' => 10));
428
        $languageFilter = array();
429
        $spiContentInfo = new SPIContentInfo();
430
        $contentMock = $this->getMockForAbstractClass('eZ\\Publish\\API\\Repository\\Values\\Content\\Content');
431
432
        /* @var \PHPUnit_Framework_MockObject_MockObject $searchHandlerMock */
433
        $searchHandlerMock->expects($this->once())
434
            ->method('findContent')
435
            ->with($this->equalTo($query), $this->equalTo($languageFilter))
436
            ->will(
437
                $this->returnValue(
438
                    new SearchResult(
439
                        array(
440
                            'searchHits' => array(new SearchHit(array('valueObject' => $spiContentInfo))),
441
                            'totalCount' => 1,
442
                        )
443
                    )
444
                )
445
            );
446
447
        $domainMapperMock->expects($this->never())
448
            ->method($this->anything());
449
450
        $contentServiceMock
451
            ->expects($this->once())
452
            ->method('internalLoadContent')
453
            ->will($this->returnValue($contentMock));
454
455
        $permissionsCriterionResolverMock->expects($this->once())
456
            ->method('getPermissionsCriterion')
457
            ->with('content', 'read')
458
            ->will($this->returnValue(true));
459
460
        $result = $service->findContent($query, $languageFilter, true);
461
462
        $this->assertEquals(
463
            new SearchResult(
464
                array(
465
                    'searchHits' => array(new SearchHit(array('valueObject' => $contentMock))),
466
                    'totalCount' => 1,
467
                )
468
            ),
469
            $result
470
        );
471
    }
472
473
    /**
474
     * Test for the findContent() method.
475
     *
476
     * @covers \eZ\Publish\Core\Repository\SearchService::addPermissionsCriterion
477
     * @covers \eZ\Publish\Core\Repository\SearchService::findContent
478
     */
479
    public function testFindContentWithNoPermission()
480
    {
481
        $repositoryMock = $this->getRepositoryMock();
482
        /** @var \eZ\Publish\SPI\Search\Handler $searchHandlerMock */
483
        $searchHandlerMock = $this->getSPIMockHandler('Search\\Handler');
484
        $permissionsCriterionResolverMock = $this->getPermissionCriterionResolverMock();
485
        $service = new SearchService(
486
            $repositoryMock,
0 ignored issues
show
Bug introduced by
It seems like $repositoryMock defined by $this->getRepositoryMock() on line 481 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, eZ\Publish\Core\Reposito...hService::__construct() does only seem to accept object<eZ\Publish\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...
487
            $searchHandlerMock,
488
            $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...
489
            $permissionsCriterionResolverMock,
0 ignored issues
show
Bug introduced by
It seems like $permissionsCriterionResolverMock defined by $this->getPermissionCriterionResolverMock() on line 484 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...
490
            new NullIndexer(),
491
            array()
492
        );
493
494
        /* @var \PHPUnit_Framework_MockObject_MockObject $searchHandlerMock */
495
        $searchHandlerMock->expects($this->never())->method('findContent');
496
497
        $criterionMock = $this
498
            ->getMockBuilder('eZ\\Publish\\API\\Repository\\Values\\Content\\Query\\Criterion')
499
            ->disableOriginalConstructor()
500
            ->getMock();
501
        $query = new Query(array('filter' => $criterionMock));
502
503
        $permissionsCriterionResolverMock->expects($this->once())
504
            ->method('getPermissionsCriterion')
505
            ->with('content', 'read')
506
            ->will($this->returnValue(false));
507
508
        $result = $service->findContent($query, array(), true);
509
510
        $this->assertEquals(
511
            new SearchResult(array('time' => 0, 'totalCount' => 0)),
512
            $result
513
        );
514
    }
515
516
    /**
517
     * Test for the findContent() method.
518
     */
519
    public function testFindContentWithDefaultQueryValues()
520
    {
521
        $repositoryMock = $this->getRepositoryMock();
522
        /** @var \eZ\Publish\SPI\Search\Handler $searchHandlerMock */
523
        $searchHandlerMock = $this->getSPIMockHandler('Search\\Handler');
524
        $domainMapperMock = $this->getDomainMapperMock();
525
        $service = new SearchService(
526
            $repositoryMock,
0 ignored issues
show
Bug introduced by
It seems like $repositoryMock defined by $this->getRepositoryMock() on line 521 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...
527
            $searchHandlerMock,
528
            $domainMapperMock,
0 ignored issues
show
Bug introduced by
It seems like $domainMapperMock defined by $this->getDomainMapperMock() on line 524 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...
529
            $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...
530
            new NullIndexer(),
531
            array()
532
        );
533
534
        $repositoryMock
535
            ->expects($this->once())
536
            ->method('getContentService')
537
            ->will(
538
                $this->returnValue(
539
                    $contentServiceMock = $this
540
                        ->getMockBuilder('eZ\\Publish\\Core\\Repository\\ContentService')
541
                        ->disableOriginalConstructor()
542
                        ->getMock()
543
                )
544
            );
545
546
        $languageFilter = array();
547
        $spiContentInfo = new SPIContentInfo();
548
        $contentMock = $this->getMockForAbstractClass('eZ\\Publish\\API\\Repository\\Values\\Content\\Content');
549
        $domainMapperMock->expects($this->never())
550
            ->method($this->anything());
551
552
        $contentServiceMock
553
            ->expects($this->once())
554
            ->method('internalLoadContent')
555
            ->will($this->returnValue($contentMock));
556
557
        /* @var \PHPUnit_Framework_MockObject_MockObject $searchHandlerMock */
558
        $searchHandlerMock
559
            ->expects($this->once())
560
            ->method('findContent')
561
            ->with(
562
                new Query(
563
                    array(
564
                        'filter' => new Criterion\MatchAll(),
565
                        'limit' => 25,
566
                    )
567
                ),
568
                array()
569
            )
570
            ->will(
571
                $this->returnValue(
572
                    new SearchResult(
573
                        array(
574
                            'searchHits' => array(new SearchHit(array('valueObject' => $spiContentInfo))),
575
                            'totalCount' => 1,
576
                        )
577
                    )
578
                )
579
            );
580
581
        $result = $service->findContent(new Query(), $languageFilter, false);
582
583
        $this->assertEquals(
584
            new SearchResult(
585
                array(
586
                    'searchHits' => array(new SearchHit(array('valueObject' => $contentMock))),
587
                    'totalCount' => 1,
588
                )
589
            ),
590
            $result
591
        );
592
    }
593
594
    /**
595
     * Test for the findSingle() method.
596
     *
597
     * @covers \eZ\Publish\Core\Repository\SearchService::addPermissionsCriterion
598
     * @covers \eZ\Publish\Core\Repository\SearchService::findSingle
599
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
600
     */
601 View Code Duplication
    public function testFindSingleThrowsNotFoundException()
602
    {
603
        $repositoryMock = $this->getRepositoryMock();
604
        /** @var \eZ\Publish\SPI\Search\Handler $searchHandlerMock */
605
        $searchHandlerMock = $this->getSPIMockHandler('Search\\Handler');
606
        $service = new SearchService(
607
            $repositoryMock,
0 ignored issues
show
Bug introduced by
It seems like $repositoryMock defined by $this->getRepositoryMock() on line 603 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...
608
            $searchHandlerMock,
609
            $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...
610
            $permissionsCriterionResolverMock = $this->getPermissionCriterionResolverMock(),
611
            new NullIndexer(),
612
            array()
613
        );
614
615
        /** @var \eZ\Publish\API\Repository\Values\Content\Query\Criterion $criterionMock */
616
        $criterionMock = $this
617
            ->getMockBuilder('eZ\\Publish\\API\\Repository\\Values\\Content\\Query\\Criterion')
618
            ->disableOriginalConstructor()
619
            ->getMock();
620
621
        $permissionsCriterionResolverMock->expects($this->once())
622
            ->method('getPermissionsCriterion')
623
            ->with('content', 'read')
624
            ->willReturn(false);
625
626
        $service->findSingle($criterionMock, array(), true);
627
    }
628
629
    /**
630
     * Test for the findSingle() method.
631
     *
632
     * @covers \eZ\Publish\Core\Repository\SearchService::addPermissionsCriterion
633
     * @covers \eZ\Publish\Core\Repository\SearchService::findSingle
634
     * @expectedException \Exception
635
     * @expectedExceptionMessage Handler threw an exception
636
     */
637 View Code Duplication
    public function testFindSingleThrowsHandlerException()
638
    {
639
        $repositoryMock = $this->getRepositoryMock();
640
        /** @var \eZ\Publish\SPI\Search\Handler $searchHandlerMock */
641
        $searchHandlerMock = $this->getSPIMockHandler('Search\\Handler');
642
        $permissionsCriterionResolverMock = $this->getPermissionCriterionResolverMock();
643
        $service = new SearchService(
644
            $repositoryMock,
0 ignored issues
show
Bug introduced by
It seems like $repositoryMock defined by $this->getRepositoryMock() on line 639 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...
645
            $searchHandlerMock,
646
            $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...
647
            $permissionsCriterionResolverMock,
0 ignored issues
show
Bug introduced by
It seems like $permissionsCriterionResolverMock defined by $this->getPermissionCriterionResolverMock() on line 642 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...
648
            new NullIndexer(),
649
            array()
650
        );
651
652
        /** @var \eZ\Publish\API\Repository\Values\Content\Query\Criterion $criterionMock */
653
        $criterionMock = $this
654
            ->getMockBuilder('eZ\\Publish\\API\\Repository\\Values\\Content\\Query\\Criterion')
655
            ->disableOriginalConstructor()
656
            ->getMock();
657
658
        $permissionsCriterionResolverMock->expects($this->once())
659
            ->method('getPermissionsCriterion')
660
            ->with('content', 'read')
661
            ->will($this->throwException(new Exception('Handler threw an exception')));
662
663
        $service->findSingle($criterionMock, array(), true);
664
    }
665
666
    /**
667
     * Test for the findSingle() method.
668
     *
669
     * @covers \eZ\Publish\Core\Repository\SearchService::addPermissionsCriterion
670
     * @covers \eZ\Publish\Core\Repository\SearchService::findSingle
671
     */
672
    public function testFindSingle()
673
    {
674
        $repositoryMock = $this->getRepositoryMock();
675
        /** @var \eZ\Publish\SPI\Search\Handler $searchHandlerMock */
676
        $searchHandlerMock = $this->getSPIMockHandler('Search\\Handler');
677
        $domainMapperMock = $this->getDomainMapperMock();
678
        $permissionsCriterionResolverMock = $this->getPermissionCriterionResolverMock();
679
        $service = new SearchService(
680
            $repositoryMock,
0 ignored issues
show
Bug introduced by
It seems like $repositoryMock defined by $this->getRepositoryMock() on line 674 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...
681
            $searchHandlerMock,
682
            $domainMapperMock,
0 ignored issues
show
Bug introduced by
It seems like $domainMapperMock defined by $this->getDomainMapperMock() on line 677 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...
683
            $permissionsCriterionResolverMock,
0 ignored issues
show
Bug introduced by
It seems like $permissionsCriterionResolverMock defined by $this->getPermissionCriterionResolverMock() on line 678 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...
684
            new NullIndexer(),
685
            array()
686
        );
687
688
        $repositoryMock
689
            ->expects($this->once())
690
            ->method('getContentService')
691
            ->will(
692
                $this->returnValue(
693
                    $contentServiceMock = $this
694
                        ->getMockBuilder('eZ\\Publish\\Core\\Repository\\ContentService')
695
                        ->disableOriginalConstructor()
696
                        ->getMock()
697
                )
698
            );
699
700
        /** @var \eZ\Publish\API\Repository\Values\Content\Query\Criterion $criterionMock */
701
        $criterionMock = $this
702
            ->getMockBuilder('eZ\\Publish\\API\\Repository\\Values\\Content\\Query\\Criterion')
703
            ->disableOriginalConstructor()
704
            ->getMock();
705
706
        $permissionsCriterionResolverMock->expects($this->once())
707
            ->method('getPermissionsCriterion')
708
            ->with('content', 'read')
709
            ->will($this->returnValue(true));
710
711
        $languageFilter = array();
712
        $spiContentInfo = new SPIContentInfo();
713
        $contentMock = $this->getMockForAbstractClass('eZ\\Publish\\API\\Repository\\Values\\Content\\Content');
714
715
        /* @var \PHPUnit_Framework_MockObject_MockObject $searchHandlerMock */
716
        $searchHandlerMock->expects($this->once())
717
            ->method('findSingle')
718
            ->with($this->equalTo($criterionMock), $this->equalTo($languageFilter))
719
            ->will($this->returnValue($spiContentInfo));
720
721
        $domainMapperMock->expects($this->never())
722
            ->method($this->anything());
723
724
        $contentServiceMock
725
            ->expects($this->once())
726
            ->method('internalLoadContent')
727
            ->will($this->returnValue($contentMock));
728
729
        $result = $service->findSingle($criterionMock, $languageFilter, true);
730
731
        $this->assertEquals($contentMock, $result);
732
    }
733
734
    /**
735
     * Test for the findLocations() method.
736
     */
737
    public function testFindLocationsWithPermission()
738
    {
739
        $repositoryMock = $this->getRepositoryMock();
740
        /** @var \eZ\Publish\SPI\Search\Handler $searchHandlerMock */
741
        $searchHandlerMock = $this->getSPIMockHandler('Search\\Handler');
742
        $domainMapperMock = $this->getDomainMapperMock();
743
        $permissionsCriterionResolverMock = $this->getPermissionCriterionResolverMock();
744
        $service = new SearchService(
745
            $repositoryMock,
0 ignored issues
show
Bug introduced by
It seems like $repositoryMock defined by $this->getRepositoryMock() on line 739 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...
746
            $searchHandlerMock,
747
            $domainMapperMock,
0 ignored issues
show
Bug introduced by
It seems like $domainMapperMock defined by $this->getDomainMapperMock() on line 742 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...
748
            $permissionsCriterionResolverMock,
0 ignored issues
show
Bug introduced by
It seems like $permissionsCriterionResolverMock defined by $this->getPermissionCriterionResolverMock() on line 743 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...
749
            new NullIndexer(),
750
            array()
751
        );
752
753
        $criterionMock = $this
754
            ->getMockBuilder('eZ\\Publish\\API\\Repository\\Values\\Content\\Query\\Criterion')
755
            ->disableOriginalConstructor()
756
            ->getMock();
757
        $query = new LocationQuery(array('filter' => $criterionMock, 'limit' => 10));
758
        $spiLocation = new SPILocation();
759
        $locationMock = $this->getMockForAbstractClass('eZ\\Publish\\API\\Repository\\Values\\Content\\Location');
760
761
        /* @var \PHPUnit_Framework_MockObject_MockObject $searchHandlerMock */
762
        $searchHandlerMock->expects($this->once())
763
            ->method('findLocations')
764
            ->with($this->equalTo($query))
765
            ->will(
766
                $this->returnValue(
767
                    new SearchResult(
768
                        array(
769
                            'searchHits' => array(new SearchHit(array('valueObject' => $spiLocation))),
770
                            'totalCount' => 1,
771
                        )
772
                    )
773
                )
774
            );
775
776
        $domainMapperMock->expects($this->once())
777
            ->method('buildLocationDomainObject')
778
            ->with($this->equalTo($spiLocation))
779
            ->will($this->returnValue($locationMock));
780
781
        $permissionsCriterionResolverMock->expects($this->once())
782
            ->method('getPermissionsCriterion')
783
            ->with('content', 'read')
784
            ->will($this->returnValue(true));
785
786
        $result = $service->findLocations($query, array(), true);
787
788
        $this->assertEquals(
789
            new SearchResult(
790
                array(
791
                    'searchHits' => array(new SearchHit(array('valueObject' => $locationMock))),
792
                    'totalCount' => 1,
793
                )
794
            ),
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('eZ\\Publish\\API\\Repository\\Values\\Content\\Location');
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
                    new SearchResult(
832
                        array(
833
                            'searchHits' => array(new SearchHit(array('valueObject' => $spiLocation))),
834
                            'totalCount' => 1,
835
                        )
836
                    )
837
                )
838
            );
839
840
        $domainMapperMock->expects($this->once())
841
            ->method('buildLocationDomainObject')
842
            ->with($this->equalTo($spiLocation))
843
            ->will($this->returnValue($locationMock));
844
845
        $result = $service->findLocations($serviceQuery, array(), false);
846
847
        $this->assertEquals(
848
            new SearchResult(
849
                array(
850
                    'searchHits' => array(new SearchHit(array('valueObject' => $locationMock))),
851
                    'totalCount' => 1,
852
                )
853
            ),
854
            $result
855
        );
856
    }
857
858 View Code Duplication
    public function providerForFindLocationsWhenDomainMapperThrowsException()
859
    {
860
        return [
861
            [
862
                new NotFoundException('content', 'id = 33'),
863
                true,
864
            ],
865
            [
866
                new UnauthorizedException('content', 'read', ['id' => 33]),
867
                false,
868
            ],
869
870
        ];
871
    }
872
873
    /**
874
     * Test for the findLocations() method when search is out of sync with persistence.
875
     *
876
     * @dataProvider providerForFindLocationsWhenDomainMapperThrowsException
877
     * @covers \eZ\Publish\Core\Repository\SearchService::findLocations
878
     */
879
    public function testFindLocationsBackgroundIndexerWhenDomainMapperThrowsException($e, $index = true)
880
    {
881
        $indexer = $this->getMockBuilder(BackgroundIndexer::class)
882
            ->disableOriginalConstructor()
883
            ->getMock();
884
885 View Code Duplication
        if ($index) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
886
            $indexer->expects($this->once())
887
                ->method('registerLocation')
888
                ->with($this->isInstanceOf(SPILocation::class));
889
        } else {
890
            $indexer->expects($this->never())->method($this->anything());
891
        }
892
893
        $service = $this->getMockBuilder(SearchService::class)
894
            ->setConstructorArgs([
895
                $this->getRepositoryMock(),
896
                $searchHandler = $this->getSPIMockHandler('Search\\Handler'),
897
                $mapper = $this->getDomainMapperMock(),
898
                $this->getPermissionCriterionResolverMock(),
899
                $indexer
900
            ])->setMethods(['addPermissionsCriterion'])
901
            ->getMock();
902
903
        $location = new SPILocation(['id' => 44]);
904
        $service->expects($this->once())
905
            ->method('addPermissionsCriterion')
906
            ->with($this->isInstanceOf(Criterion::class))
907
            ->willReturn(true);
908
909
        $result = new SearchResult(['searchHits' => [new SearchHit(['valueObject' => $location])], 'totalCount' => 2]);
910
        $searchHandler->expects($this->once())
911
            ->method('findLocations')
912
            ->with($this->isInstanceOf(LocationQuery::class), $this->isType('array'))
913
            ->willReturn($result);
914
915
        $mapper->expects($this->once())
916
            ->method('buildLocationDomainObject')
917
            ->with($this->isInstanceOf(SPILocation::class))
918
            ->willThrowException($e);
919
920
        $finalResult = $service->findLocations(new LocationQuery());
921
922
        $this->assertEmpty($finalResult->searchHits, 'Expected search hits to be empty');
923
        $this->assertEquals(1, $finalResult->totalCount, 'Expected total count to be 1');
924
    }
925
926
    /**
927
     * Test for the findLocations() method.
928
     *
929
     * @expectedException \Exception
930
     * @expectedExceptionMessage Handler threw an exception
931
     */
932 View Code Duplication
    public function testFindLocationsThrowsHandlerException()
933
    {
934
        $repositoryMock = $this->getRepositoryMock();
935
        /** @var \eZ\Publish\SPI\Search\Handler $searchHandlerMock */
936
        $searchHandlerMock = $this->getSPIMockHandler('Search\\Handler');
937
        $permissionsCriterionResolverMock = $this->getPermissionCriterionResolverMock();
938
939
        $service = new SearchService(
940
            $repositoryMock,
0 ignored issues
show
Bug introduced by
It seems like $repositoryMock defined by $this->getRepositoryMock() on line 934 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...
941
            $searchHandlerMock,
942
            $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...
943
            $permissionsCriterionResolverMock,
0 ignored issues
show
Bug introduced by
It seems like $permissionsCriterionResolverMock defined by $this->getPermissionCriterionResolverMock() on line 937 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...
944
            new NullIndexer(),
945
            array()
946
        );
947
948
        /** @var \eZ\Publish\API\Repository\Values\Content\Query\Criterion $criterionMock */
949
        $criterionMock = $this
950
            ->getMockBuilder('eZ\\Publish\\API\\Repository\\Values\\Content\\Query\\Criterion')
951
            ->disableOriginalConstructor()
952
            ->getMock();
953
        $query = new LocationQuery(array('filter' => $criterionMock));
954
955
        $permissionsCriterionResolverMock->expects($this->once())
956
            ->method('getPermissionsCriterion')
957
            ->with('content', 'read')
958
            ->will($this->throwException(new Exception('Handler threw an exception')));
959
960
        $service->findLocations($query, array(), true);
961
    }
962
963
    /**
964
     * Test for the findLocations() method.
965
     */
966
    public function testFindLocationsWithDefaultQueryValues()
967
    {
968
        $repositoryMock = $this->getRepositoryMock();
969
        /** @var \eZ\Publish\SPI\Search\Handler $searchHandlerMock */
970
        $searchHandlerMock = $this->getSPIMockHandler('Search\\Handler');
971
        $domainMapperMock = $this->getDomainMapperMock();
972
        $service = new SearchService(
973
            $repositoryMock,
0 ignored issues
show
Bug introduced by
It seems like $repositoryMock defined by $this->getRepositoryMock() on line 968 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...
974
            $searchHandlerMock,
975
            $domainMapperMock,
0 ignored issues
show
Bug introduced by
It seems like $domainMapperMock defined by $this->getDomainMapperMock() on line 971 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...
976
            $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...
977
            new NullIndexer(),
978
            array()
979
        );
980
981
        $spiLocation = new SPILocation();
982
        $locationMock = $this->getMockForAbstractClass('eZ\\Publish\\API\\Repository\\Values\\Content\\Location');
983
        $domainMapperMock->expects($this->once())
984
            ->method('buildLocationDomainObject')
985
            ->with($this->equalTo($spiLocation))
986
            ->will($this->returnValue($locationMock));
987
988
        /* @var \PHPUnit_Framework_MockObject_MockObject $searchHandlerMock */
989
        $searchHandlerMock
990
            ->expects($this->once())
991
            ->method('findLocations')
992
            ->with(
993
                new LocationQuery(
994
                    array(
995
                        'filter' => new Criterion\MatchAll(),
996
                        'limit' => 25,
997
                    )
998
                )
999
            )
1000
            ->will(
1001
                $this->returnValue(
1002
                    new SearchResult(
1003
                        array(
1004
                            'searchHits' => array(new SearchHit(array('valueObject' => $spiLocation))),
1005
                            'totalCount' => 1,
1006
                        )
1007
                    )
1008
                )
1009
            );
1010
1011
        $result = $service->findLocations(new LocationQuery(), array(), false);
1012
1013
        $this->assertEquals(
1014
            new SearchResult(
1015
                array(
1016
                    'searchHits' => array(new SearchHit(array('valueObject' => $locationMock))),
1017
                    'totalCount' => 1,
1018
                )
1019
            ),
1020
            $result
1021
        );
1022
    }
1023
1024
    /**
1025
     * @return \PHPUnit_Framework_MockObject_MockObject|\eZ\Publish\Core\Repository\Helper\DomainMapper
1026
     */
1027
    protected function getDomainMapperMock()
1028
    {
1029
        if (!isset($this->domainMapperMock)) {
1030
            $this->domainMapperMock = $this
1031
                ->getMockBuilder(DomainMapper::class)
1032
                ->disableOriginalConstructor()
1033
                ->getMock();
1034
        }
1035
1036
        return $this->domainMapperMock;
1037
    }
1038
1039
    /**
1040
     * @return \PHPUnit_Framework_MockObject_MockObject|\eZ\Publish\API\Repository\PermissionCriterionResolver
1041
     */
1042
    protected function getPermissionCriterionResolverMock()
1043
    {
1044
        if (!isset($this->permissionsCriterionResolverMock)) {
1045
            $this->permissionsCriterionResolverMock = $this
1046
                ->getMockBuilder(PermissionCriterionResolver::class)
1047
                ->disableOriginalConstructor()
1048
                ->getMock();
1049
        }
1050
1051
        return $this->permissionsCriterionResolverMock;
1052
    }
1053
}
1054