Completed
Push — tolerant_search_service ( 768e04...a664cf )
by André
13:22
created

testFindSingleValidatesLocationCriteria()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 24
Code Lines 17

Duplication

Lines 24
Ratio 100 %

Importance

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