Completed
Push — tolerant_search_service ( e24b92...5185ff )
by André
13:13
created

SearchTest::getPermissionsCriterionHandlerMock()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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