Completed
Push — master ( a682b4...c5625a )
by André
14:36
created

SearchTest::testFindLocationsWithPermission()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 60
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 42
nc 1
nop 0
dl 0
loc 60
rs 9.5555
c 1
b 0
f 0

How to fix   Long Method   

Long Method

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

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

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * File contains: eZ\Publish\Core\Repository\Tests\Service\Mock\SearchTest class.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 */
9
namespace eZ\Publish\Core\Repository\Tests\Service\Mock;
10
11
use eZ\Publish\Core\Repository\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\SPI\Persistence\Content\ContentInfo as SPIContentInfo;
20
use eZ\Publish\SPI\Persistence\Content\Location as SPILocation;
21
use eZ\Publish\API\Repository\Exceptions\InvalidArgumentException;
22
use Exception;
23
24
/**
25
 * Mock test case for Search service.
26
 */
27
class SearchTest extends BaseServiceMockTest
28
{
29
    protected $repositoryMock;
30
31
    protected $domainMapperMock;
32
33
    protected $permissionsCriterionHandlerMock;
34
35
    /**
36
     * Test for the __construct() method.
37
     *
38
     * @covers \eZ\Publish\Core\Repository\SearchService::__construct
39
     */
40
    public function testConstructor()
41
    {
42
        $repositoryMock = $this->getRepositoryMock();
43
        /** @var \eZ\Publish\SPI\Search\Handler $searchHandlerMock */
44
        $searchHandlerMock = $this->getSPIMockHandler('Search\\Handler');
45
        $domainMapperMock = $this->getDomainMapperMock();
46
        $permissionsCriterionHandlerMock = $this->getPermissionsCriterionHandlerMock();
47
        $settings = array('teh setting');
48
49
        $service = new SearchService(
50
            $repositoryMock,
0 ignored issues
show
Bug introduced by
It seems like $repositoryMock defined by $this->getRepositoryMock() on line 42 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...
51
            $searchHandlerMock,
52
            $domainMapperMock,
0 ignored issues
show
Bug introduced by
It seems like $domainMapperMock defined by $this->getDomainMapperMock() on line 45 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...
53
            $permissionsCriterionHandlerMock,
0 ignored issues
show
Bug introduced by
It seems like $permissionsCriterionHandlerMock defined by $this->getPermissionsCriterionHandlerMock() 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...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...
54
            $settings
55
        );
56
57
        $this->assertAttributeSame(
58
            $repositoryMock,
59
            'repository',
60
            $service
61
        );
62
63
        $this->assertAttributeSame(
64
            $searchHandlerMock,
65
            'searchHandler',
66
            $service
67
        );
68
69
        $this->assertAttributeSame(
70
            $domainMapperMock,
71
            'domainMapper',
72
            $service
73
        );
74
75
        $this->assertAttributeSame(
76
            $permissionsCriterionHandlerMock,
77
            'permissionsCriterionHandler',
78
            $service
79
        );
80
81
        $this->assertAttributeSame(
82
            $settings,
83
            'settings',
84
            $service
85
        );
86
    }
87
88
    public function providerForFindContentValidatesLocationCriteriaAndSortClauses()
89
    {
90
        return array(
91
            array(
92
                new Query(array('filter' => new Criterion\Location\Depth(Criterion\Operator::LT, 2))),
93
                "Argument '\$query' is invalid: Location criterions cannot be used in Content search",
94
            ),
95
            array(
96
                new Query(array('query' => new Criterion\Location\Depth(Criterion\Operator::LT, 2))),
97
                "Argument '\$query' is invalid: Location criterions cannot be used in Content search",
98
            ),
99
            array(
100
                new Query(
101
                    array(
102
                        'query' => new Criterion\LogicalAnd(
103
                            array(
104
                                new Criterion\Location\Depth(Criterion\Operator::LT, 2),
105
                            )
106
                        ),
107
                    )
108
                ),
109
                "Argument '\$query' is invalid: Location criterions cannot be used in Content search",
110
            ),
111
            array(
112
                new Query(array('sortClauses' => array(new SortClause\Location\Id()))),
113
                "Argument '\$query' is invalid: Location sort clauses cannot be used in Content search",
114
            ),
115
        );
116
    }
117
118
    /**
119
     * @dataProvider providerForFindContentValidatesLocationCriteriaAndSortClauses
120
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
121
     */
122 View Code Duplication
    public function testFindContentValidatesLocationCriteriaAndSortClauses($query, $exceptionMessage)
123
    {
124
        $repositoryMock = $this->getRepositoryMock();
125
        /** @var \eZ\Publish\SPI\Search\Handler $searchHandlerMock */
126
        $searchHandlerMock = $this->getSPIMockHandler('Search\\Handler');
127
        $permissionsCriterionHandlerMock = $this->getPermissionsCriterionHandlerMock();
128
129
        $service = new SearchService(
130
            $repositoryMock,
0 ignored issues
show
Bug introduced by
It seems like $repositoryMock defined by $this->getRepositoryMock() on line 124 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...
131
            $searchHandlerMock,
132
            $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...
133
            $permissionsCriterionHandlerMock,
0 ignored issues
show
Bug introduced by
It seems like $permissionsCriterionHandlerMock defined by $this->getPermissionsCriterionHandlerMock() 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\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...
134
            array()
135
        );
136
137
        try {
138
            $service->findContent($query);
139
        } catch (InvalidArgumentException $e) {
140
            $this->assertEquals($exceptionMessage, $e->getMessage());
141
            throw $e;
142
        }
143
144
        $this->fail('Expected exception was not thrown');
145
    }
146
147
    public function providerForFindSingleValidatesLocationCriteria()
148
    {
149
        return array(
150
            array(
151
                new Criterion\Location\Depth(Criterion\Operator::LT, 2),
152
                "Argument '\$filter' is invalid: Location criterions cannot be used in Content search",
153
            ),
154
            array(
155
                new Criterion\LogicalAnd(
156
                    array(
157
                        new Criterion\Location\Depth(Criterion\Operator::LT, 2),
158
                    )
159
                ),
160
                "Argument '\$filter' is invalid: Location criterions cannot be used in Content search",
161
            ),
162
        );
163
    }
164
165
    /**
166
     * @dataProvider providerForFindSingleValidatesLocationCriteria
167
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
168
     */
169 View Code Duplication
    public function testFindSingleValidatesLocationCriteria($criterion, $exceptionMessage)
170
    {
171
        $repositoryMock = $this->getRepositoryMock();
172
        /** @var \eZ\Publish\SPI\Search\Handler $searchHandlerMock */
173
        $searchHandlerMock = $this->getSPIMockHandler('Search\\Handler');
174
        $permissionsCriterionHandlerMock = $this->getPermissionsCriterionHandlerMock();
175
        $service = new SearchService(
176
            $repositoryMock,
0 ignored issues
show
Bug introduced by
It seems like $repositoryMock defined by $this->getRepositoryMock() on line 171 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...
177
            $searchHandlerMock,
178
            $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...
179
            $permissionsCriterionHandlerMock,
0 ignored issues
show
Bug introduced by
It seems like $permissionsCriterionHandlerMock defined by $this->getPermissionsCriterionHandlerMock() 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\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...
180
            array()
181
        );
182
183
        try {
184
            $service->findSingle($criterion);
185
        } catch (InvalidArgumentException $e) {
186
            $this->assertEquals($exceptionMessage, $e->getMessage());
187
            throw $e;
188
        }
189
190
        $this->fail('Expected exception was not thrown');
191
    }
192
193
    /**
194
     * Test for the findContent() method.
195
     *
196
     * @covers \eZ\Publish\Core\Repository\PermissionsCriterionHandler::addPermissionsCriterion
197
     * @covers \eZ\Publish\Core\Repository\PermissionsCriterionHandler::getPermissionsCriterion
198
     * @covers \eZ\Publish\Core\Repository\SearchService::findContent
199
     * @expectedException \Exception
200
     * @expectedExceptionMessage Handler threw an exception
201
     */
202 View Code Duplication
    public function testFindContentThrowsHandlerException()
203
    {
204
        $repositoryMock = $this->getRepositoryMock();
205
        /** @var \eZ\Publish\SPI\Search\Handler $searchHandlerMock */
206
        $searchHandlerMock = $this->getSPIMockHandler('Search\\Handler');
207
        $permissionsCriterionHandlerMock = $this->getPermissionsCriterionHandlerMock();
208
209
        $service = new SearchService(
210
            $repositoryMock,
0 ignored issues
show
Bug introduced by
It seems like $repositoryMock defined by $this->getRepositoryMock() on line 204 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...
211
            $searchHandlerMock,
212
            $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...
213
            $permissionsCriterionHandlerMock,
0 ignored issues
show
Bug introduced by
It seems like $permissionsCriterionHandlerMock defined by $this->getPermissionsCriterionHandlerMock() on line 207 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...
214
            array()
215
        );
216
217
        /** @var \eZ\Publish\API\Repository\Values\Content\Query\Criterion $criterionMock */
218
        $criterionMock = $this
219
            ->getMockBuilder('eZ\\Publish\\API\\Repository\\Values\\Content\\Query\\Criterion')
220
            ->disableOriginalConstructor()
221
            ->getMock();
222
        $query = new Query(array('filter' => $criterionMock));
223
224
        $permissionsCriterionHandlerMock->expects($this->once())
225
            ->method('addPermissionsCriterion')
226
            ->with($criterionMock)
227
            ->will($this->throwException(new Exception('Handler threw an exception')));
228
229
        $service->findContent($query, array(), true);
230
    }
231
232
    /**
233
     * Test for the findContent() method.
234
     *
235
     * @covers \eZ\Publish\Core\Repository\PermissionsCriterionHandler::addPermissionsCriterion
236
     * @covers \eZ\Publish\Core\Repository\PermissionsCriterionHandler::getPermissionsCriterion
237
     * @covers \eZ\Publish\Core\Repository\SearchService::findContent
238
     */
239
    public function testFindContentNoPermissionsFilter()
240
    {
241
        $repositoryMock = $this->getRepositoryMock();
242
        /** @var \eZ\Publish\SPI\Search\Handler $searchHandlerMock */
243
        $searchHandlerMock = $this->getSPIMockHandler('Search\\Handler');
244
        $domainMapperMock = $this->getDomainMapperMock();
245
        $permissionsCriterionHandlerMock = $this->getPermissionsCriterionHandlerMock();
246
        $service = new SearchService(
247
            $repositoryMock,
0 ignored issues
show
Bug introduced by
It seems like $repositoryMock defined by $this->getRepositoryMock() on line 241 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...
248
            $searchHandlerMock,
249
            $domainMapperMock,
0 ignored issues
show
Bug introduced by
It seems like $domainMapperMock defined by $this->getDomainMapperMock() on line 244 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...
250
            $permissionsCriterionHandlerMock,
0 ignored issues
show
Bug introduced by
It seems like $permissionsCriterionHandlerMock defined by $this->getPermissionsCriterionHandlerMock() 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\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...
251
            array()
252
        );
253
254
        $repositoryMock->expects($this->never())->method('hasAccess');
255
256
        $repositoryMock
257
            ->expects($this->once())
258
            ->method('getContentService')
259
            ->will(
260
                $this->returnValue(
261
                    $contentServiceMock = $this
262
                        ->getMockBuilder('eZ\\Publish\\Core\\Repository\\ContentService')
263
                        ->disableOriginalConstructor()
264
                        ->getMock()
265
                )
266
            );
267
268
        $serviceQuery = new Query();
269
        $handlerQuery = new Query(array('filter' => new Criterion\MatchAll(), 'limit' => 25));
270
        $languageFilter = array();
271
        $spiContentInfo = new SPIContentInfo();
272
        $contentMock = $this->getMockForAbstractClass('eZ\\Publish\\API\\Repository\\Values\\Content\\Content');
273
274
        /* @var \PHPUnit_Framework_MockObject_MockObject $searchHandlerMock */
275
        $searchHandlerMock->expects($this->once())
276
            ->method('findContent')
277
            ->with($this->equalTo($handlerQuery), $this->equalTo($languageFilter))
278
            ->will(
279
                $this->returnValue(
280
                    new SearchResult(
281
                        array(
282
                            'searchHits' => array(new SearchHit(array('valueObject' => $spiContentInfo))),
283
                            'totalCount' => 1,
284
                        )
285
                    )
286
                )
287
            );
288
289
        $contentServiceMock
290
            ->expects($this->once())
291
            ->method('internalLoadContent')
292
            ->will($this->returnValue($contentMock));
293
294
        $result = $service->findContent($serviceQuery, $languageFilter, false);
295
296
        $this->assertEquals(
297
            new SearchResult(
298
                array(
299
                    'searchHits' => array(new SearchHit(array('valueObject' => $contentMock))),
300
                    'totalCount' => 1,
301
                )
302
            ),
303
            $result
304
        );
305
    }
306
307
    /**
308
     * Test for the findContent() method.
309
     *
310
     * @covers \eZ\Publish\Core\Repository\PermissionsCriterionHandler::addPermissionsCriterion
311
     * @covers \eZ\Publish\Core\Repository\PermissionsCriterionHandler::getPermissionsCriterion
312
     * @covers \eZ\Publish\Core\Repository\SearchService::findContent
313
     */
314
    public function testFindContentWithPermission()
315
    {
316
        $repositoryMock = $this->getRepositoryMock();
317
        /** @var \eZ\Publish\SPI\Search\Handler $searchHandlerMock */
318
        $searchHandlerMock = $this->getSPIMockHandler('Search\\Handler');
319
        $domainMapperMock = $this->getDomainMapperMock();
320
        $permissionsCriterionHandlerMock = $this->getPermissionsCriterionHandlerMock();
321
        $service = new SearchService(
322
            $repositoryMock,
0 ignored issues
show
Bug introduced by
It seems like $repositoryMock defined by $this->getRepositoryMock() on line 316 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, eZ\Publish\Core\Reposito...hService::__construct() does only seem to accept object<eZ\Publish\API\Repository\Repository>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
323
            $searchHandlerMock,
324
            $domainMapperMock,
0 ignored issues
show
Bug introduced by
It seems like $domainMapperMock defined by $this->getDomainMapperMock() on line 319 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, eZ\Publish\Core\Reposito...hService::__construct() does only seem to accept object<eZ\Publish\Core\R...ry\Helper\DomainMapper>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
325
            $permissionsCriterionHandlerMock,
0 ignored issues
show
Bug introduced by
It seems like $permissionsCriterionHandlerMock defined by $this->getPermissionsCriterionHandlerMock() 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\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...
326
            array()
327
        );
328
329
        $repositoryMock
330
            ->expects($this->once())
331
            ->method('getContentService')
332
            ->will(
333
                $this->returnValue(
334
                    $contentServiceMock = $this
335
                        ->getMockBuilder('eZ\\Publish\\Core\\Repository\\ContentService')
336
                        ->disableOriginalConstructor()
337
                        ->getMock()
338
                )
339
            );
340
341
        $criterionMock = $this
342
            ->getMockBuilder('eZ\\Publish\\API\\Repository\\Values\\Content\\Query\\Criterion')
343
            ->disableOriginalConstructor()
344
            ->getMock();
345
        $query = new Query(array('filter' => $criterionMock, 'limit' => 10));
346
        $languageFilter = array();
347
        $spiContentInfo = new SPIContentInfo();
348
        $contentMock = $this->getMockForAbstractClass('eZ\\Publish\\API\\Repository\\Values\\Content\\Content');
349
350
        /* @var \PHPUnit_Framework_MockObject_MockObject $searchHandlerMock */
351
        $searchHandlerMock->expects($this->once())
352
            ->method('findContent')
353
            ->with($this->equalTo($query), $this->equalTo($languageFilter))
354
            ->will(
355
                $this->returnValue(
356
                    new SearchResult(
357
                        array(
358
                            'searchHits' => array(new SearchHit(array('valueObject' => $spiContentInfo))),
359
                            'totalCount' => 1,
360
                        )
361
                    )
362
                )
363
            );
364
365
        $domainMapperMock->expects($this->never())
366
            ->method($this->anything());
367
368
        $contentServiceMock
369
            ->expects($this->once())
370
            ->method('internalLoadContent')
371
            ->will($this->returnValue($contentMock));
372
373
        $permissionsCriterionHandlerMock->expects($this->once())
374
            ->method('addPermissionsCriterion')
375
            ->with($criterionMock)
376
            ->will($this->returnValue(true));
377
378
        $result = $service->findContent($query, $languageFilter, true);
379
380
        $this->assertEquals(
381
            new SearchResult(
382
                array(
383
                    'searchHits' => array(new SearchHit(array('valueObject' => $contentMock))),
384
                    'totalCount' => 1,
385
                )
386
            ),
387
            $result
388
        );
389
    }
390
391
    /**
392
     * Test for the findContent() method.
393
     *
394
     * @covers \eZ\Publish\Core\Repository\PermissionsCriterionHandler::addPermissionsCriterion
395
     * @covers \eZ\Publish\Core\Repository\PermissionsCriterionHandler::getPermissionsCriterion
396
     * @covers \eZ\Publish\Core\Repository\SearchService::findContent
397
     */
398
    public function testFindContentWithNoPermission()
399
    {
400
        $repositoryMock = $this->getRepositoryMock();
401
        /** @var \eZ\Publish\SPI\Search\Handler $searchHandlerMock */
402
        $searchHandlerMock = $this->getSPIMockHandler('Search\\Handler');
403
        $permissionsCriterionHandlerMock = $this->getPermissionsCriterionHandlerMock();
404
        $service = new SearchService(
405
            $repositoryMock,
0 ignored issues
show
Bug introduced by
It seems like $repositoryMock defined by $this->getRepositoryMock() on line 400 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, eZ\Publish\Core\Reposito...hService::__construct() does only seem to accept object<eZ\Publish\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...
406
            $searchHandlerMock,
407
            $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...
408
            $permissionsCriterionHandlerMock,
0 ignored issues
show
Bug introduced by
It seems like $permissionsCriterionHandlerMock defined by $this->getPermissionsCriterionHandlerMock() on line 403 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...
409
            array()
410
        );
411
412
        /* @var \PHPUnit_Framework_MockObject_MockObject $searchHandlerMock */
413
        $searchHandlerMock->expects($this->never())->method('findContent');
414
415
        $criterionMock = $this
416
            ->getMockBuilder('eZ\\Publish\\API\\Repository\\Values\\Content\\Query\\Criterion')
417
            ->disableOriginalConstructor()
418
            ->getMock();
419
        $query = new Query(array('filter' => $criterionMock));
420
421
        $permissionsCriterionHandlerMock->expects($this->once())
422
            ->method('addPermissionsCriterion')
423
            ->with($criterionMock)
424
            ->will($this->returnValue(false));
425
426
        $result = $service->findContent($query, array(), true);
427
428
        $this->assertEquals(
429
            new SearchResult(array('time' => 0, 'totalCount' => 0)),
430
            $result
431
        );
432
    }
433
434
    /**
435
     * Test for the findContent() method.
436
     */
437
    public function testFindContentWithDefaultQueryValues()
438
    {
439
        $repositoryMock = $this->getRepositoryMock();
440
        /** @var \eZ\Publish\SPI\Search\Handler $searchHandlerMock */
441
        $searchHandlerMock = $this->getSPIMockHandler('Search\\Handler');
442
        $domainMapperMock = $this->getDomainMapperMock();
443
        $service = new SearchService(
444
            $repositoryMock,
0 ignored issues
show
Bug introduced by
It seems like $repositoryMock defined by $this->getRepositoryMock() on line 439 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...
445
            $searchHandlerMock,
446
            $domainMapperMock,
0 ignored issues
show
Bug introduced by
It seems like $domainMapperMock defined by $this->getDomainMapperMock() on line 442 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...
447
            $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...
448
            array()
449
        );
450
451
        $repositoryMock
452
            ->expects($this->once())
453
            ->method('getContentService')
454
            ->will(
455
                $this->returnValue(
456
                    $contentServiceMock = $this
457
                        ->getMockBuilder('eZ\\Publish\\Core\\Repository\\ContentService')
458
                        ->disableOriginalConstructor()
459
                        ->getMock()
460
                )
461
            );
462
463
        $languageFilter = array();
464
        $spiContentInfo = new SPIContentInfo();
465
        $contentMock = $this->getMockForAbstractClass('eZ\\Publish\\API\\Repository\\Values\\Content\\Content');
466
        $domainMapperMock->expects($this->never())
467
            ->method($this->anything());
468
469
        $contentServiceMock
470
            ->expects($this->once())
471
            ->method('internalLoadContent')
472
            ->will($this->returnValue($contentMock));
473
474
        /* @var \PHPUnit_Framework_MockObject_MockObject $searchHandlerMock */
475
        $searchHandlerMock
476
            ->expects($this->once())
477
            ->method('findContent')
478
            ->with(
479
                new Query(
480
                    array(
481
                        'filter' => new Criterion\MatchAll(),
482
                        'limit' => 25,
483
                    )
484
                ),
485
                array()
486
            )
487
            ->will(
488
                $this->returnValue(
489
                    new SearchResult(
490
                        array(
491
                            'searchHits' => array(new SearchHit(array('valueObject' => $spiContentInfo))),
492
                            'totalCount' => 1,
493
                        )
494
                    )
495
                )
496
            );
497
498
        $result = $service->findContent(new Query(), $languageFilter, false);
499
500
        $this->assertEquals(
501
            new SearchResult(
502
                array(
503
                    'searchHits' => array(new SearchHit(array('valueObject' => $contentMock))),
504
                    'totalCount' => 1,
505
                )
506
            ),
507
            $result
508
        );
509
    }
510
511
    /**
512
     * Test for the findSingle() method.
513
     *
514
     * @covers \eZ\Publish\Core\Repository\PermissionsCriterionHandler::addPermissionsCriterion
515
     * @covers \eZ\Publish\Core\Repository\PermissionsCriterionHandler::getPermissionsCriterion
516
     * @covers \eZ\Publish\Core\Repository\SearchService::findSingle
517
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
518
     */
519
    public function testFindSingleThrowsNotFoundException()
520
    {
521
        $repositoryMock = $this->getRepositoryMock();
522
        /** @var \eZ\Publish\SPI\Search\Handler $searchHandlerMock */
523
        $searchHandlerMock = $this->getSPIMockHandler('Search\\Handler');
524
        $service = new SearchService(
525
            $repositoryMock,
0 ignored issues
show
Bug introduced by
It seems like $repositoryMock defined by $this->getRepositoryMock() on line 521 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, eZ\Publish\Core\Reposito...hService::__construct() does only seem to accept object<eZ\Publish\API\Repository\Repository>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
526
            $searchHandlerMock,
527
            $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...
528
            $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...
529
            array()
530
        );
531
532
        /** @var \eZ\Publish\API\Repository\Values\Content\Query\Criterion $criterionMock */
533
        $criterionMock = $this
534
            ->getMockBuilder('eZ\\Publish\\API\\Repository\\Values\\Content\\Query\\Criterion')
535
            ->disableOriginalConstructor()
536
            ->getMock();
537
538
        $service->findSingle($criterionMock, array(), true);
539
    }
540
541
    /**
542
     * Test for the findSingle() method.
543
     *
544
     * @covers \eZ\Publish\Core\Repository\PermissionsCriterionHandler::addPermissionsCriterion
545
     * @covers \eZ\Publish\Core\Repository\PermissionsCriterionHandler::getPermissionsCriterion
546
     * @covers \eZ\Publish\Core\Repository\SearchService::findSingle
547
     * @expectedException \Exception
548
     * @expectedExceptionMessage Handler threw an exception
549
     */
550
    public function testFindSingleThrowsHandlerException()
551
    {
552
        $repositoryMock = $this->getRepositoryMock();
553
        /** @var \eZ\Publish\SPI\Search\Handler $searchHandlerMock */
554
        $searchHandlerMock = $this->getSPIMockHandler('Search\\Handler');
555
        $permissionsCriterionHandlerMock = $this->getPermissionsCriterionHandlerMock();
556
        $service = new SearchService(
557
            $repositoryMock,
0 ignored issues
show
Bug introduced by
It seems like $repositoryMock defined by $this->getRepositoryMock() on line 552 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...
558
            $searchHandlerMock,
559
            $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...
560
            $permissionsCriterionHandlerMock,
0 ignored issues
show
Bug introduced by
It seems like $permissionsCriterionHandlerMock defined by $this->getPermissionsCriterionHandlerMock() on line 555 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...
561
            array()
562
        );
563
564
        /** @var \eZ\Publish\API\Repository\Values\Content\Query\Criterion $criterionMock */
565
        $criterionMock = $this
566
            ->getMockBuilder('eZ\\Publish\\API\\Repository\\Values\\Content\\Query\\Criterion')
567
            ->disableOriginalConstructor()
568
            ->getMock();
569
570
        $permissionsCriterionHandlerMock->expects($this->once())
571
            ->method('addPermissionsCriterion')
572
            ->with($criterionMock)
573
            ->will($this->throwException(new Exception('Handler threw an exception')));
574
575
        $service->findSingle($criterionMock, array(), true);
576
    }
577
578
    /**
579
     * Test for the findSingle() method.
580
     *
581
     * @covers \eZ\Publish\Core\Repository\PermissionsCriterionHandler::addPermissionsCriterion
582
     * @covers \eZ\Publish\Core\Repository\PermissionsCriterionHandler::getPermissionsCriterion
583
     * @covers \eZ\Publish\Core\Repository\SearchService::findSingle
584
     */
585
    public function testFindSingle()
586
    {
587
        $repositoryMock = $this->getRepositoryMock();
588
        /** @var \eZ\Publish\SPI\Search\Handler $searchHandlerMock */
589
        $searchHandlerMock = $this->getSPIMockHandler('Search\\Handler');
590
        $domainMapperMock = $this->getDomainMapperMock();
591
        $permissionsCriterionHandlerMock = $this->getPermissionsCriterionHandlerMock();
592
        $service = new SearchService(
593
            $repositoryMock,
0 ignored issues
show
Bug introduced by
It seems like $repositoryMock defined by $this->getRepositoryMock() on line 587 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...
594
            $searchHandlerMock,
595
            $domainMapperMock,
0 ignored issues
show
Bug introduced by
It seems like $domainMapperMock defined by $this->getDomainMapperMock() on line 590 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...
596
            $permissionsCriterionHandlerMock,
0 ignored issues
show
Bug introduced by
It seems like $permissionsCriterionHandlerMock defined by $this->getPermissionsCriterionHandlerMock() on line 591 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...
597
            array()
598
        );
599
600
        $repositoryMock
601
            ->expects($this->once())
602
            ->method('getContentService')
603
            ->will(
604
                $this->returnValue(
605
                    $contentServiceMock = $this
606
                        ->getMockBuilder('eZ\\Publish\\Core\\Repository\\ContentService')
607
                        ->disableOriginalConstructor()
608
                        ->getMock()
609
                )
610
            );
611
612
        /** @var \eZ\Publish\API\Repository\Values\Content\Query\Criterion $criterionMock */
613
        $criterionMock = $this
614
            ->getMockBuilder('eZ\\Publish\\API\\Repository\\Values\\Content\\Query\\Criterion')
615
            ->disableOriginalConstructor()
616
            ->getMock();
617
618
        $permissionsCriterionHandlerMock->expects($this->once())
619
            ->method('addPermissionsCriterion')
620
            ->with($criterionMock)
621
            ->will($this->returnValue(true));
622
623
        $languageFilter = array();
624
        $spiContentInfo = new SPIContentInfo();
625
        $contentMock = $this->getMockForAbstractClass('eZ\\Publish\\API\\Repository\\Values\\Content\\Content');
626
627
        /* @var \PHPUnit_Framework_MockObject_MockObject $searchHandlerMock */
628
        $searchHandlerMock->expects($this->once())
629
            ->method('findSingle')
630
            ->with($this->equalTo($criterionMock), $this->equalTo($languageFilter))
631
            ->will($this->returnValue($spiContentInfo));
632
633
        $domainMapperMock->expects($this->never())
634
            ->method($this->anything());
635
636
        $contentServiceMock
637
            ->expects($this->once())
638
            ->method('internalLoadContent')
639
            ->will($this->returnValue($contentMock));
640
641
        $result = $service->findSingle($criterionMock, $languageFilter, true);
642
643
        $this->assertEquals($contentMock, $result);
644
    }
645
646
    /**
647
     * Test for the findLocations() method.
648
     */
649
    public function testFindLocationsWithPermission()
650
    {
651
        $repositoryMock = $this->getRepositoryMock();
652
        /** @var \eZ\Publish\SPI\Search\Handler $searchHandlerMock */
653
        $searchHandlerMock = $this->getSPIMockHandler('Search\\Handler');
654
        $domainMapperMock = $this->getDomainMapperMock();
655
        $permissionsCriterionHandlerMock = $this->getPermissionsCriterionHandlerMock();
656
        $service = new SearchService(
657
            $repositoryMock,
0 ignored issues
show
Bug introduced by
It seems like $repositoryMock defined by $this->getRepositoryMock() on line 651 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...
658
            $searchHandlerMock,
659
            $domainMapperMock,
0 ignored issues
show
Bug introduced by
It seems like $domainMapperMock defined by $this->getDomainMapperMock() on line 654 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...
660
            $permissionsCriterionHandlerMock,
0 ignored issues
show
Bug introduced by
It seems like $permissionsCriterionHandlerMock defined by $this->getPermissionsCriterionHandlerMock() on line 655 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...
661
            array()
662
        );
663
664
        $criterionMock = $this
665
            ->getMockBuilder('eZ\\Publish\\API\\Repository\\Values\\Content\\Query\\Criterion')
666
            ->disableOriginalConstructor()
667
            ->getMock();
668
        $query = new LocationQuery(array('filter' => $criterionMock, 'limit' => 10));
669
        $spiLocation = new SPILocation();
670
        $locationMock = $this->getMockForAbstractClass('eZ\\Publish\\API\\Repository\\Values\\Content\\Location');
671
672
        /* @var \PHPUnit_Framework_MockObject_MockObject $searchHandlerMock */
673
        $searchHandlerMock->expects($this->once())
674
            ->method('findLocations')
675
            ->with($this->equalTo($query))
676
            ->will(
677
                $this->returnValue(
678
                    new SearchResult(
679
                        array(
680
                            'searchHits' => array(new SearchHit(array('valueObject' => $spiLocation))),
681
                            'totalCount' => 1,
682
                        )
683
                    )
684
                )
685
            );
686
687
        $domainMapperMock->expects($this->once())
688
            ->method('buildLocationDomainObject')
689
            ->with($this->equalTo($spiLocation))
690
            ->will($this->returnValue($locationMock));
691
692
        $permissionsCriterionHandlerMock->expects($this->once())
693
            ->method('addPermissionsCriterion')
694
            ->with($criterionMock)
695
            ->will($this->returnValue(true));
696
697
        $result = $service->findLocations($query, array(), true);
698
699
        $this->assertEquals(
700
            new SearchResult(
701
                array(
702
                    'searchHits' => array(new SearchHit(array('valueObject' => $locationMock))),
703
                    'totalCount' => 1,
704
                )
705
            ),
706
            $result
707
        );
708
    }
709
710
    /**
711
     * Test for the findLocations() method.
712
     */
713
    public function testFindLocationsWithNoPermissionsFilter()
714
    {
715
        $repositoryMock = $this->getRepositoryMock();
716
        /** @var \eZ\Publish\SPI\Search\Handler $searchHandlerMock */
717
        $searchHandlerMock = $this->getSPIMockHandler('Search\\Handler');
718
        $domainMapperMock = $this->getDomainMapperMock();
719
        $permissionsCriterionHandlerMock = $this->getPermissionsCriterionHandlerMock();
720
        $service = new SearchService(
721
            $repositoryMock,
0 ignored issues
show
Bug introduced by
It seems like $repositoryMock defined by $this->getRepositoryMock() on line 715 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...
722
            $searchHandlerMock,
723
            $domainMapperMock,
0 ignored issues
show
Bug introduced by
It seems like $domainMapperMock defined by $this->getDomainMapperMock() on line 718 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...
724
            $permissionsCriterionHandlerMock,
0 ignored issues
show
Bug introduced by
It seems like $permissionsCriterionHandlerMock defined by $this->getPermissionsCriterionHandlerMock() on line 719 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...
725
            array()
726
        );
727
728
        $repositoryMock->expects($this->never())->method('hasAccess');
729
730
        $serviceQuery = new LocationQuery();
731
        $handlerQuery = new LocationQuery(array('filter' => new Criterion\MatchAll(), 'limit' => 25));
732
        $spiLocation = new SPILocation();
733
        $locationMock = $this->getMockForAbstractClass('eZ\\Publish\\API\\Repository\\Values\\Content\\Location');
734
735
        /* @var \PHPUnit_Framework_MockObject_MockObject $searchHandlerMock */
736
        $searchHandlerMock->expects($this->once())
737
            ->method('findLocations')
738
            ->with($this->equalTo($handlerQuery))
739
            ->will(
740
                $this->returnValue(
741
                    new SearchResult(
742
                        array(
743
                            'searchHits' => array(new SearchHit(array('valueObject' => $spiLocation))),
744
                            'totalCount' => 1,
745
                        )
746
                    )
747
                )
748
            );
749
750
        $domainMapperMock->expects($this->once())
751
            ->method('buildLocationDomainObject')
752
            ->with($this->equalTo($spiLocation))
753
            ->will($this->returnValue($locationMock));
754
755
        $result = $service->findLocations($serviceQuery, array(), false);
756
757
        $this->assertEquals(
758
            new SearchResult(
759
                array(
760
                    'searchHits' => array(new SearchHit(array('valueObject' => $locationMock))),
761
                    'totalCount' => 1,
762
                )
763
            ),
764
            $result
765
        );
766
    }
767
768
    /**
769
     * Test for the findLocations() method.
770
     *
771
     * @expectedException \Exception
772
     * @expectedExceptionMessage Handler threw an exception
773
     */
774 View Code Duplication
    public function testFindLocationsThrowsHandlerException()
775
    {
776
        $repositoryMock = $this->getRepositoryMock();
777
        /** @var \eZ\Publish\SPI\Search\Handler $searchHandlerMock */
778
        $searchHandlerMock = $this->getSPIMockHandler('Search\\Handler');
779
        $permissionsCriterionHandlerMock = $this->getPermissionsCriterionHandlerMock();
780
781
        $service = new SearchService(
782
            $repositoryMock,
0 ignored issues
show
Bug introduced by
It seems like $repositoryMock defined by $this->getRepositoryMock() on line 776 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...
783
            $searchHandlerMock,
784
            $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...
785
            $permissionsCriterionHandlerMock,
0 ignored issues
show
Bug introduced by
It seems like $permissionsCriterionHandlerMock defined by $this->getPermissionsCriterionHandlerMock() on line 779 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...
786
            array()
787
        );
788
789
        /** @var \eZ\Publish\API\Repository\Values\Content\Query\Criterion $criterionMock */
790
        $criterionMock = $this
791
            ->getMockBuilder('eZ\\Publish\\API\\Repository\\Values\\Content\\Query\\Criterion')
792
            ->disableOriginalConstructor()
793
            ->getMock();
794
        $query = new LocationQuery(array('filter' => $criterionMock));
795
796
        $permissionsCriterionHandlerMock->expects($this->once())
797
            ->method('addPermissionsCriterion')
798
            ->with($criterionMock)
799
            ->will($this->throwException(new Exception('Handler threw an exception')));
800
801
        $service->findLocations($query, array(), true);
802
    }
803
804
    /**
805
     * Test for the findLocations() method.
806
     */
807
808
    /**
809
     * Test for the findContent() method.
810
     */
811
    public function testFindLocationsWithDefaultQueryValues()
812
    {
813
        $repositoryMock = $this->getRepositoryMock();
814
        /** @var \eZ\Publish\SPI\Search\Handler $searchHandlerMock */
815
        $searchHandlerMock = $this->getSPIMockHandler('Search\\Handler');
816
        $domainMapperMock = $this->getDomainMapperMock();
817
        $service = new SearchService(
818
            $repositoryMock,
0 ignored issues
show
Bug introduced by
It seems like $repositoryMock defined by $this->getRepositoryMock() on line 813 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...
819
            $searchHandlerMock,
820
            $domainMapperMock,
0 ignored issues
show
Bug introduced by
It seems like $domainMapperMock defined by $this->getDomainMapperMock() on line 816 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...
821
            $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...
822
            array()
823
        );
824
825
        $spiLocation = new SPILocation();
826
        $locationMock = $this->getMockForAbstractClass('eZ\\Publish\\API\\Repository\\Values\\Content\\Location');
827
        $domainMapperMock->expects($this->once())
828
            ->method('buildLocationDomainObject')
829
            ->with($this->equalTo($spiLocation))
830
            ->will($this->returnValue($locationMock));
831
832
        /* @var \PHPUnit_Framework_MockObject_MockObject $searchHandlerMock */
833
        $searchHandlerMock
834
            ->expects($this->once())
835
            ->method('findLocations')
836
            ->with(
837
                new LocationQuery(
838
                    array(
839
                        'filter' => new Criterion\MatchAll(),
840
                        'limit' => 25,
841
                    )
842
                )
843
            )
844
            ->will(
845
                $this->returnValue(
846
                    new SearchResult(
847
                        array(
848
                            'searchHits' => array(new SearchHit(array('valueObject' => $spiLocation))),
849
                            'totalCount' => 1,
850
                        )
851
                    )
852
                )
853
            );
854
855
        $result = $service->findLocations(new LocationQuery(), array(), false);
856
857
        $this->assertEquals(
858
            new SearchResult(
859
                array(
860
                    'searchHits' => array(new SearchHit(array('valueObject' => $locationMock))),
861
                    'totalCount' => 1,
862
                )
863
            ),
864
            $result
865
        );
866
    }
867
868
    /**
869
     * @return \PHPUnit_Framework_MockObject_MockObject|\eZ\Publish\Core\Repository\Helper\DomainMapper
870
     */
871 View Code Duplication
    protected function getDomainMapperMock()
872
    {
873
        if (!isset($this->domainMapperMock)) {
874
            $this->domainMapperMock = $this
875
                ->getMockBuilder('eZ\\Publish\\Core\\Repository\\Helper\\DomainMapper')
876
                ->disableOriginalConstructor()
877
                ->getMock();
878
        }
879
880
        return $this->domainMapperMock;
881
    }
882
883
    /**
884
     * @return \PHPUnit_Framework_MockObject_MockObject|\eZ\Publish\Core\Repository\PermissionsCriterionHandler
885
     */
886
    protected function getPermissionsCriterionHandlerMock()
887
    {
888
        if (!isset($this->permissionsCriterionHandlerMock)) {
889
            $this->permissionsCriterionHandlerMock = $this
890
                ->getMockBuilder('eZ\\Publish\\Core\\Repository\\PermissionsCriterionHandler')
891
                ->disableOriginalConstructor()
892
                ->getMock();
893
        }
894
895
        return $this->permissionsCriterionHandlerMock;
896
    }
897
}
898