Completed
Push — master ( 2d5a91...5e6a60 )
by André
72:09 queued 53:20
created

SearchTest::getPermissionsCriterionHandlerMock()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 0
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * File contains: eZ\Publish\Core\Repository\Tests\Service\Mock\SearchTest class.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 */
9
namespace eZ\Publish\Core\Repository\Tests\Service\Mock;
10
11
use eZ\Publish\Core\Repository\Tests\Service\Mock\Base as BaseServiceMockTest;
12
use eZ\Publish\Core\Repository\SearchService;
13
use eZ\Publish\Core\Repository\Permission\PermissionCriterionResolver;
14
use eZ\Publish\API\Repository\Values\Content\Query;
15
use eZ\Publish\API\Repository\Values\Content\LocationQuery;
16
use eZ\Publish\API\Repository\Values\Content\Query\Criterion;
17
use eZ\Publish\API\Repository\Values\Content\Query\SortClause;
18
use eZ\Publish\API\Repository\Values\Content\Search\SearchResult;
19
use eZ\Publish\API\Repository\Values\Content\Search\SearchHit;
20
use eZ\Publish\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 $permissionsCriterionResolverMock;
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
        $permissionsCriterionResolverMock = $this->getPermissionCriterionResolverMock();
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
            $permissionsCriterionResolverMock,
0 ignored issues
show
Bug introduced by
It seems like $permissionsCriterionResolverMock defined by $this->getPermissionCriterionResolverMock() 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\API\Re...ssionCriterionResolver>, maybe add an additional type check?

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

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

    return array();
}

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

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

Loading history...
55
            $settings
56
        );
57
58
        $this->assertAttributeSame(
59
            $repositoryMock,
60
            'repository',
61
            $service
62
        );
63
64
        $this->assertAttributeSame(
65
            $searchHandlerMock,
66
            'searchHandler',
67
            $service
68
        );
69
70
        $this->assertAttributeSame(
71
            $domainMapperMock,
72
            'domainMapper',
73
            $service
74
        );
75
76
        $this->assertAttributeSame(
77
            $permissionsCriterionResolverMock,
78
            'permissionCriterionResolver',
79
            $service
80
        );
81
82
        $this->assertAttributeSame(
83
            $settings,
84
            'settings',
85
            $service
86
        );
87
    }
88
89
    public function providerForFindContentValidatesLocationCriteriaAndSortClauses()
90
    {
91
        return array(
92
            array(
93
                new Query(array('filter' => new Criterion\Location\Depth(Criterion\Operator::LT, 2))),
94
                "Argument '\$query' is invalid: Location criterions cannot be used in Content search",
95
            ),
96
            array(
97
                new Query(array('query' => new Criterion\Location\Depth(Criterion\Operator::LT, 2))),
98
                "Argument '\$query' is invalid: Location criterions cannot be used in Content search",
99
            ),
100
            array(
101
                new Query(
102
                    array(
103
                        'query' => new Criterion\LogicalAnd(
104
                            array(
105
                                new Criterion\Location\Depth(Criterion\Operator::LT, 2),
106
                            )
107
                        ),
108
                    )
109
                ),
110
                "Argument '\$query' is invalid: Location criterions cannot be used in Content search",
111
            ),
112
            array(
113
                new Query(array('sortClauses' => array(new SortClause\Location\Id()))),
114
                "Argument '\$query' is invalid: Location sort clauses cannot be used in Content search",
115
            ),
116
        );
117
    }
118
119
    /**
120
     * @dataProvider providerForFindContentValidatesLocationCriteriaAndSortClauses
121
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
122
     */
123 View Code Duplication
    public function testFindContentValidatesLocationCriteriaAndSortClauses($query, $exceptionMessage)
124
    {
125
        $repositoryMock = $this->getRepositoryMock();
126
        /** @var \eZ\Publish\SPI\Search\Handler $searchHandlerMock */
127
        $searchHandlerMock = $this->getSPIMockHandler('Search\\Handler');
128
        $permissionsCriterionResolverMock = $this->getPermissionCriterionResolverMock();
129
130
        $service = new SearchService(
131
            $repositoryMock,
0 ignored issues
show
Bug introduced by
It seems like $repositoryMock defined by $this->getRepositoryMock() on line 125 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...
132
            $searchHandlerMock,
133
            $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...
134
            $permissionsCriterionResolverMock,
0 ignored issues
show
Bug introduced by
It seems like $permissionsCriterionResolverMock defined by $this->getPermissionCriterionResolverMock() on line 128 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, eZ\Publish\Core\Reposito...hService::__construct() does only seem to accept object<eZ\Publish\API\Re...ssionCriterionResolver>, maybe add an additional type check?

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

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

    return array();
}

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

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

Loading history...
135
            array()
136
        );
137
138
        try {
139
            $service->findContent($query);
140
        } catch (InvalidArgumentException $e) {
141
            $this->assertEquals($exceptionMessage, $e->getMessage());
142
            throw $e;
143
        }
144
145
        $this->fail('Expected exception was not thrown');
146
    }
147
148
    public function providerForFindSingleValidatesLocationCriteria()
149
    {
150
        return array(
151
            array(
152
                new Criterion\Location\Depth(Criterion\Operator::LT, 2),
153
                "Argument '\$filter' is invalid: Location criterions cannot be used in Content search",
154
            ),
155
            array(
156
                new Criterion\LogicalAnd(
157
                    array(
158
                        new Criterion\Location\Depth(Criterion\Operator::LT, 2),
159
                    )
160
                ),
161
                "Argument '\$filter' is invalid: Location criterions cannot be used in Content search",
162
            ),
163
        );
164
    }
165
166
    /**
167
     * @dataProvider providerForFindSingleValidatesLocationCriteria
168
     * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
169
     */
170 View Code Duplication
    public function testFindSingleValidatesLocationCriteria($criterion, $exceptionMessage)
171
    {
172
        $repositoryMock = $this->getRepositoryMock();
173
        /** @var \eZ\Publish\SPI\Search\Handler $searchHandlerMock */
174
        $searchHandlerMock = $this->getSPIMockHandler('Search\\Handler');
175
        $permissionsCriterionResolverMock = $this->getPermissionCriterionResolverMock();
176
        $service = new SearchService(
177
            $repositoryMock,
0 ignored issues
show
Bug introduced by
It seems like $repositoryMock defined by $this->getRepositoryMock() on line 172 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...
178
            $searchHandlerMock,
179
            $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...
180
            $permissionsCriterionResolverMock,
0 ignored issues
show
Bug introduced by
It seems like $permissionsCriterionResolverMock defined by $this->getPermissionCriterionResolverMock() on line 175 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, eZ\Publish\Core\Reposito...hService::__construct() does only seem to accept object<eZ\Publish\API\Re...ssionCriterionResolver>, maybe add an additional type check?

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

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

    return array();
}

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

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

Loading history...
181
            array()
182
        );
183
184
        try {
185
            $service->findSingle($criterion);
186
        } catch (InvalidArgumentException $e) {
187
            $this->assertEquals($exceptionMessage, $e->getMessage());
188
            throw $e;
189
        }
190
191
        $this->fail('Expected exception was not thrown');
192
    }
193
194
    /**
195
     * Test for the findContent() method.
196
     *
197
     * @covers \eZ\Publish\Core\Repository\SearchService::addPermissionsCriterion
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
        $permissionsCriterionResolverMock = $this->getPermissionCriterionResolverMock();
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
            $permissionsCriterionResolverMock,
0 ignored issues
show
Bug introduced by
It seems like $permissionsCriterionResolverMock defined by $this->getPermissionCriterionResolverMock() 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\API\Re...ssionCriterionResolver>, maybe add an additional type check?

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

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

    return array();
}

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

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

Loading history...
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
        $permissionsCriterionResolverMock->expects($this->once())
225
            ->method('getPermissionsCriterion')
226
            ->with('content', 'read')
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\SearchService::addPermissionsCriterion
236
     * @covers \eZ\Publish\Core\Repository\SearchService::findContent
237
     */
238
    public function testFindContentNoPermissionsFilter()
239
    {
240
        $repositoryMock = $this->getRepositoryMock();
241
        /** @var \eZ\Publish\SPI\Search\Handler $searchHandlerMock */
242
        $searchHandlerMock = $this->getSPIMockHandler('Search\\Handler');
243
        $domainMapperMock = $this->getDomainMapperMock();
244
        $permissionsCriterionResolverMock = $this->getPermissionCriterionResolverMock();
245
        $service = new SearchService(
246
            $repositoryMock,
0 ignored issues
show
Bug introduced by
It seems like $repositoryMock defined by $this->getRepositoryMock() on line 240 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...
247
            $searchHandlerMock,
248
            $domainMapperMock,
0 ignored issues
show
Bug introduced by
It seems like $domainMapperMock defined by $this->getDomainMapperMock() on line 243 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...
249
            $permissionsCriterionResolverMock,
0 ignored issues
show
Bug introduced by
It seems like $permissionsCriterionResolverMock defined by $this->getPermissionCriterionResolverMock() 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\API\Re...ssionCriterionResolver>, maybe add an additional type check?

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

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

    return array();
}

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

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

Loading history...
250
            array()
251
        );
252
253
        $repositoryMock->expects($this->never())->method('hasAccess');
254
255
        $repositoryMock
256
            ->expects($this->once())
257
            ->method('getContentService')
258
            ->will(
259
                $this->returnValue(
260
                    $contentServiceMock = $this
261
                        ->getMockBuilder('eZ\\Publish\\Core\\Repository\\ContentService')
262
                        ->disableOriginalConstructor()
263
                        ->getMock()
264
                )
265
            );
266
267
        $serviceQuery = new Query();
268
        $handlerQuery = new Query(array('filter' => new Criterion\MatchAll(), 'limit' => 25));
269
        $languageFilter = array();
270
        $spiContentInfo = new SPIContentInfo();
271
        $contentMock = $this->getMockForAbstractClass('eZ\\Publish\\API\\Repository\\Values\\Content\\Content');
272
273
        /* @var \PHPUnit_Framework_MockObject_MockObject $searchHandlerMock */
274
        $searchHandlerMock->expects($this->once())
275
            ->method('findContent')
276
            ->with($this->equalTo($handlerQuery), $this->equalTo($languageFilter))
277
            ->will(
278
                $this->returnValue(
279
                    new SearchResult(
280
                        array(
281
                            'searchHits' => array(new SearchHit(array('valueObject' => $spiContentInfo))),
282
                            'totalCount' => 1,
283
                        )
284
                    )
285
                )
286
            );
287
288
        $contentServiceMock
289
            ->expects($this->once())
290
            ->method('internalLoadContent')
291
            ->will($this->returnValue($contentMock));
292
293
        $result = $service->findContent($serviceQuery, $languageFilter, false);
294
295
        $this->assertEquals(
296
            new SearchResult(
297
                array(
298
                    'searchHits' => array(new SearchHit(array('valueObject' => $contentMock))),
299
                    'totalCount' => 1,
300
                )
301
            ),
302
            $result
303
        );
304
    }
305
306
    /**
307
     * Test for the findContent() method.
308
     *
309
     * @covers \eZ\Publish\Core\Repository\SearchService::addPermissionsCriterion
310
     * @covers \eZ\Publish\Core\Repository\SearchService::findContent
311
     */
312
    public function testFindContentWithPermission()
313
    {
314
        $repositoryMock = $this->getRepositoryMock();
315
        /** @var \eZ\Publish\SPI\Search\Handler $searchHandlerMock */
316
        $searchHandlerMock = $this->getSPIMockHandler('Search\\Handler');
317
        $domainMapperMock = $this->getDomainMapperMock();
318
        $permissionsCriterionResolverMock = $this->getPermissionCriterionResolverMock();
319
        $service = new SearchService(
320
            $repositoryMock,
0 ignored issues
show
Bug introduced by
It seems like $repositoryMock defined by $this->getRepositoryMock() on line 314 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...
321
            $searchHandlerMock,
322
            $domainMapperMock,
0 ignored issues
show
Bug introduced by
It seems like $domainMapperMock defined by $this->getDomainMapperMock() on line 317 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...
323
            $permissionsCriterionResolverMock,
0 ignored issues
show
Bug introduced by
It seems like $permissionsCriterionResolverMock defined by $this->getPermissionCriterionResolverMock() on line 318 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, eZ\Publish\Core\Reposito...hService::__construct() does only seem to accept object<eZ\Publish\API\Re...ssionCriterionResolver>, maybe add an additional type check?

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

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

    return array();
}

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

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

Loading history...
324
            array()
325
        );
326
327
        $repositoryMock
328
            ->expects($this->once())
329
            ->method('getContentService')
330
            ->will(
331
                $this->returnValue(
332
                    $contentServiceMock = $this
333
                        ->getMockBuilder('eZ\\Publish\\Core\\Repository\\ContentService')
334
                        ->disableOriginalConstructor()
335
                        ->getMock()
336
                )
337
            );
338
339
        $criterionMock = $this
340
            ->getMockBuilder('eZ\\Publish\\API\\Repository\\Values\\Content\\Query\\Criterion')
341
            ->disableOriginalConstructor()
342
            ->getMock();
343
        $query = new Query(array('filter' => $criterionMock, 'limit' => 10));
344
        $languageFilter = array();
345
        $spiContentInfo = new SPIContentInfo();
346
        $contentMock = $this->getMockForAbstractClass('eZ\\Publish\\API\\Repository\\Values\\Content\\Content');
347
348
        /* @var \PHPUnit_Framework_MockObject_MockObject $searchHandlerMock */
349
        $searchHandlerMock->expects($this->once())
350
            ->method('findContent')
351
            ->with($this->equalTo($query), $this->equalTo($languageFilter))
352
            ->will(
353
                $this->returnValue(
354
                    new SearchResult(
355
                        array(
356
                            'searchHits' => array(new SearchHit(array('valueObject' => $spiContentInfo))),
357
                            'totalCount' => 1,
358
                        )
359
                    )
360
                )
361
            );
362
363
        $domainMapperMock->expects($this->never())
364
            ->method($this->anything());
365
366
        $contentServiceMock
367
            ->expects($this->once())
368
            ->method('internalLoadContent')
369
            ->will($this->returnValue($contentMock));
370
371
        $permissionsCriterionResolverMock->expects($this->once())
372
            ->method('getPermissionsCriterion')
373
            ->with('content', 'read')
374
            ->will($this->returnValue(true));
375
376
        $result = $service->findContent($query, $languageFilter, true);
377
378
        $this->assertEquals(
379
            new SearchResult(
380
                array(
381
                    'searchHits' => array(new SearchHit(array('valueObject' => $contentMock))),
382
                    'totalCount' => 1,
383
                )
384
            ),
385
            $result
386
        );
387
    }
388
389
    /**
390
     * Test for the findContent() method.
391
     *
392
     * @covers \eZ\Publish\Core\Repository\SearchService::addPermissionsCriterion
393
     * @covers \eZ\Publish\Core\Repository\SearchService::findContent
394
     */
395
    public function testFindContentWithNoPermission()
396
    {
397
        $repositoryMock = $this->getRepositoryMock();
398
        /** @var \eZ\Publish\SPI\Search\Handler $searchHandlerMock */
399
        $searchHandlerMock = $this->getSPIMockHandler('Search\\Handler');
400
        $permissionsCriterionResolverMock = $this->getPermissionCriterionResolverMock();
401
        $service = new SearchService(
402
            $repositoryMock,
0 ignored issues
show
Bug introduced by
It seems like $repositoryMock defined by $this->getRepositoryMock() on line 397 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, eZ\Publish\Core\Reposito...hService::__construct() does only seem to accept object<eZ\Publish\API\Repository\Repository>, maybe add an additional type check?

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

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

    return array();
}

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

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

Loading history...
403
            $searchHandlerMock,
404
            $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...
405
            $permissionsCriterionResolverMock,
0 ignored issues
show
Bug introduced by
It seems like $permissionsCriterionResolverMock defined by $this->getPermissionCriterionResolverMock() 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\Re...ssionCriterionResolver>, maybe add an additional type check?

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

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

    return array();
}

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

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

Loading history...
406
            array()
407
        );
408
409
        /* @var \PHPUnit_Framework_MockObject_MockObject $searchHandlerMock */
410
        $searchHandlerMock->expects($this->never())->method('findContent');
411
412
        $criterionMock = $this
413
            ->getMockBuilder('eZ\\Publish\\API\\Repository\\Values\\Content\\Query\\Criterion')
414
            ->disableOriginalConstructor()
415
            ->getMock();
416
        $query = new Query(array('filter' => $criterionMock));
417
418
        $permissionsCriterionResolverMock->expects($this->once())
419
            ->method('getPermissionsCriterion')
420
            ->with('content', 'read')
421
            ->will($this->returnValue(false));
422
423
        $result = $service->findContent($query, array(), true);
424
425
        $this->assertEquals(
426
            new SearchResult(array('time' => 0, 'totalCount' => 0)),
427
            $result
428
        );
429
    }
430
431
    /**
432
     * Test for the findContent() method.
433
     */
434
    public function testFindContentWithDefaultQueryValues()
435
    {
436
        $repositoryMock = $this->getRepositoryMock();
437
        /** @var \eZ\Publish\SPI\Search\Handler $searchHandlerMock */
438
        $searchHandlerMock = $this->getSPIMockHandler('Search\\Handler');
439
        $domainMapperMock = $this->getDomainMapperMock();
440
        $service = new SearchService(
441
            $repositoryMock,
0 ignored issues
show
Bug introduced by
It seems like $repositoryMock defined by $this->getRepositoryMock() on line 436 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...
442
            $searchHandlerMock,
443
            $domainMapperMock,
0 ignored issues
show
Bug introduced by
It seems like $domainMapperMock defined by $this->getDomainMapperMock() 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\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...
444
            $this->getPermissionCriterionResolverMock(),
0 ignored issues
show
Bug introduced by
It seems like $this->getPermissionCriterionResolverMock() targeting eZ\Publish\Core\Reposito...CriterionResolverMock() can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, eZ\Publish\Core\Reposito...hService::__construct() does only seem to accept object<eZ\Publish\API\Re...ssionCriterionResolver>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
445
            array()
446
        );
447
448
        $repositoryMock
449
            ->expects($this->once())
450
            ->method('getContentService')
451
            ->will(
452
                $this->returnValue(
453
                    $contentServiceMock = $this
454
                        ->getMockBuilder('eZ\\Publish\\Core\\Repository\\ContentService')
455
                        ->disableOriginalConstructor()
456
                        ->getMock()
457
                )
458
            );
459
460
        $languageFilter = array();
461
        $spiContentInfo = new SPIContentInfo();
462
        $contentMock = $this->getMockForAbstractClass('eZ\\Publish\\API\\Repository\\Values\\Content\\Content');
463
        $domainMapperMock->expects($this->never())
464
            ->method($this->anything());
465
466
        $contentServiceMock
467
            ->expects($this->once())
468
            ->method('internalLoadContent')
469
            ->will($this->returnValue($contentMock));
470
471
        /* @var \PHPUnit_Framework_MockObject_MockObject $searchHandlerMock */
472
        $searchHandlerMock
473
            ->expects($this->once())
474
            ->method('findContent')
475
            ->with(
476
                new Query(
477
                    array(
478
                        'filter' => new Criterion\MatchAll(),
479
                        'limit' => 25,
480
                    )
481
                ),
482
                array()
483
            )
484
            ->will(
485
                $this->returnValue(
486
                    new SearchResult(
487
                        array(
488
                            'searchHits' => array(new SearchHit(array('valueObject' => $spiContentInfo))),
489
                            'totalCount' => 1,
490
                        )
491
                    )
492
                )
493
            );
494
495
        $result = $service->findContent(new Query(), $languageFilter, false);
496
497
        $this->assertEquals(
498
            new SearchResult(
499
                array(
500
                    'searchHits' => array(new SearchHit(array('valueObject' => $contentMock))),
501
                    'totalCount' => 1,
502
                )
503
            ),
504
            $result
505
        );
506
    }
507
508
    /**
509
     * Test for the findSingle() method.
510
     *
511
     * @covers \eZ\Publish\Core\Repository\SearchService::addPermissionsCriterion
512
     * @covers \eZ\Publish\Core\Repository\SearchService::findSingle
513
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
514
     */
515 View Code Duplication
    public function testFindSingleThrowsNotFoundException()
516
    {
517
        $repositoryMock = $this->getRepositoryMock();
518
        /** @var \eZ\Publish\SPI\Search\Handler $searchHandlerMock */
519
        $searchHandlerMock = $this->getSPIMockHandler('Search\\Handler');
520
        $service = new SearchService(
521
            $repositoryMock,
0 ignored issues
show
Bug introduced by
It seems like $repositoryMock defined by $this->getRepositoryMock() on line 517 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...
522
            $searchHandlerMock,
523
            $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...
524
            $permissionsCriterionResolverMock = $this->getPermissionCriterionResolverMock(),
525
            array()
526
        );
527
528
        /** @var \eZ\Publish\API\Repository\Values\Content\Query\Criterion $criterionMock */
529
        $criterionMock = $this
530
            ->getMockBuilder('eZ\\Publish\\API\\Repository\\Values\\Content\\Query\\Criterion')
531
            ->disableOriginalConstructor()
532
            ->getMock();
533
534
        $permissionsCriterionResolverMock->expects($this->once())
535
            ->method('getPermissionsCriterion')
536
            ->with('content', 'read')
537
            ->willReturn(false);
538
539
        $service->findSingle($criterionMock, array(), true);
540
    }
541
542
    /**
543
     * Test for the findSingle() method.
544
     *
545
     * @covers \eZ\Publish\Core\Repository\SearchService::addPermissionsCriterion
546
     * @covers \eZ\Publish\Core\Repository\SearchService::findSingle
547
     * @expectedException \Exception
548
     * @expectedExceptionMessage Handler threw an exception
549
     */
550 View Code Duplication
    public function testFindSingleThrowsHandlerException()
551
    {
552
        $repositoryMock = $this->getRepositoryMock();
553
        /** @var \eZ\Publish\SPI\Search\Handler $searchHandlerMock */
554
        $searchHandlerMock = $this->getSPIMockHandler('Search\\Handler');
555
        $permissionsCriterionResolverMock = $this->getPermissionCriterionResolverMock();
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
            $permissionsCriterionResolverMock,
0 ignored issues
show
Bug introduced by
It seems like $permissionsCriterionResolverMock defined by $this->getPermissionCriterionResolverMock() 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\API\Re...ssionCriterionResolver>, maybe add an additional type check?

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

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

    return array();
}

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

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

Loading history...
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
        $permissionsCriterionResolverMock->expects($this->once())
571
            ->method('getPermissionsCriterion')
572
            ->with('content', 'read')
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\SearchService::addPermissionsCriterion
582
     * @covers \eZ\Publish\Core\Repository\SearchService::findSingle
583
     */
584
    public function testFindSingle()
585
    {
586
        $repositoryMock = $this->getRepositoryMock();
587
        /** @var \eZ\Publish\SPI\Search\Handler $searchHandlerMock */
588
        $searchHandlerMock = $this->getSPIMockHandler('Search\\Handler');
589
        $domainMapperMock = $this->getDomainMapperMock();
590
        $permissionsCriterionResolverMock = $this->getPermissionCriterionResolverMock();
591
        $service = new SearchService(
592
            $repositoryMock,
0 ignored issues
show
Bug introduced by
It seems like $repositoryMock defined by $this->getRepositoryMock() on line 586 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...
593
            $searchHandlerMock,
594
            $domainMapperMock,
0 ignored issues
show
Bug introduced by
It seems like $domainMapperMock defined by $this->getDomainMapperMock() on line 589 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...
595
            $permissionsCriterionResolverMock,
0 ignored issues
show
Bug introduced by
It seems like $permissionsCriterionResolverMock defined by $this->getPermissionCriterionResolverMock() 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\API\Re...ssionCriterionResolver>, maybe add an additional type check?

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

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

    return array();
}

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

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

Loading history...
596
            array()
597
        );
598
599
        $repositoryMock
600
            ->expects($this->once())
601
            ->method('getContentService')
602
            ->will(
603
                $this->returnValue(
604
                    $contentServiceMock = $this
605
                        ->getMockBuilder('eZ\\Publish\\Core\\Repository\\ContentService')
606
                        ->disableOriginalConstructor()
607
                        ->getMock()
608
                )
609
            );
610
611
        /** @var \eZ\Publish\API\Repository\Values\Content\Query\Criterion $criterionMock */
612
        $criterionMock = $this
613
            ->getMockBuilder('eZ\\Publish\\API\\Repository\\Values\\Content\\Query\\Criterion')
614
            ->disableOriginalConstructor()
615
            ->getMock();
616
617
        $permissionsCriterionResolverMock->expects($this->once())
618
            ->method('getPermissionsCriterion')
619
            ->with('content', 'read')
620
            ->will($this->returnValue(true));
621
622
        $languageFilter = array();
623
        $spiContentInfo = new SPIContentInfo();
624
        $contentMock = $this->getMockForAbstractClass('eZ\\Publish\\API\\Repository\\Values\\Content\\Content');
625
626
        /* @var \PHPUnit_Framework_MockObject_MockObject $searchHandlerMock */
627
        $searchHandlerMock->expects($this->once())
628
            ->method('findSingle')
629
            ->with($this->equalTo($criterionMock), $this->equalTo($languageFilter))
630
            ->will($this->returnValue($spiContentInfo));
631
632
        $domainMapperMock->expects($this->never())
633
            ->method($this->anything());
634
635
        $contentServiceMock
636
            ->expects($this->once())
637
            ->method('internalLoadContent')
638
            ->will($this->returnValue($contentMock));
639
640
        $result = $service->findSingle($criterionMock, $languageFilter, true);
641
642
        $this->assertEquals($contentMock, $result);
643
    }
644
645
    /**
646
     * Test for the findLocations() method.
647
     */
648
    public function testFindLocationsWithPermission()
649
    {
650
        $repositoryMock = $this->getRepositoryMock();
651
        /** @var \eZ\Publish\SPI\Search\Handler $searchHandlerMock */
652
        $searchHandlerMock = $this->getSPIMockHandler('Search\\Handler');
653
        $domainMapperMock = $this->getDomainMapperMock();
654
        $permissionsCriterionResolverMock = $this->getPermissionCriterionResolverMock();
655
        $service = new SearchService(
656
            $repositoryMock,
0 ignored issues
show
Bug introduced by
It seems like $repositoryMock defined by $this->getRepositoryMock() on line 650 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...
657
            $searchHandlerMock,
658
            $domainMapperMock,
0 ignored issues
show
Bug introduced by
It seems like $domainMapperMock defined by $this->getDomainMapperMock() on line 653 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...
659
            $permissionsCriterionResolverMock,
0 ignored issues
show
Bug introduced by
It seems like $permissionsCriterionResolverMock defined by $this->getPermissionCriterionResolverMock() 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\API\Re...ssionCriterionResolver>, maybe add an additional type check?

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

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

    return array();
}

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

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

Loading history...
660
            array()
661
        );
662
663
        $criterionMock = $this
664
            ->getMockBuilder('eZ\\Publish\\API\\Repository\\Values\\Content\\Query\\Criterion')
665
            ->disableOriginalConstructor()
666
            ->getMock();
667
        $query = new LocationQuery(array('filter' => $criterionMock, 'limit' => 10));
668
        $spiLocation = new SPILocation();
669
        $locationMock = $this->getMockForAbstractClass('eZ\\Publish\\API\\Repository\\Values\\Content\\Location');
670
671
        /* @var \PHPUnit_Framework_MockObject_MockObject $searchHandlerMock */
672
        $searchHandlerMock->expects($this->once())
673
            ->method('findLocations')
674
            ->with($this->equalTo($query))
675
            ->will(
676
                $this->returnValue(
677
                    new SearchResult(
678
                        array(
679
                            'searchHits' => array(new SearchHit(array('valueObject' => $spiLocation))),
680
                            'totalCount' => 1,
681
                        )
682
                    )
683
                )
684
            );
685
686
        $domainMapperMock->expects($this->once())
687
            ->method('buildLocationDomainObject')
688
            ->with($this->equalTo($spiLocation))
689
            ->will($this->returnValue($locationMock));
690
691
        $permissionsCriterionResolverMock->expects($this->once())
692
            ->method('getPermissionsCriterion')
693
            ->with('content', 'read')
694
            ->will($this->returnValue(true));
695
696
        $result = $service->findLocations($query, array(), true);
697
698
        $this->assertEquals(
699
            new SearchResult(
700
                array(
701
                    'searchHits' => array(new SearchHit(array('valueObject' => $locationMock))),
702
                    'totalCount' => 1,
703
                )
704
            ),
705
            $result
706
        );
707
    }
708
709
    /**
710
     * Test for the findLocations() method.
711
     */
712
    public function testFindLocationsWithNoPermissionsFilter()
713
    {
714
        $repositoryMock = $this->getRepositoryMock();
715
        /** @var \eZ\Publish\SPI\Search\Handler $searchHandlerMock */
716
        $searchHandlerMock = $this->getSPIMockHandler('Search\\Handler');
717
        $domainMapperMock = $this->getDomainMapperMock();
718
        $permissionsCriterionResolverMock = $this->getPermissionCriterionResolverMock();
719
        $service = new SearchService(
720
            $repositoryMock,
0 ignored issues
show
Bug introduced by
It seems like $repositoryMock defined by $this->getRepositoryMock() on line 714 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...
721
            $searchHandlerMock,
722
            $domainMapperMock,
0 ignored issues
show
Bug introduced by
It seems like $domainMapperMock defined by $this->getDomainMapperMock() on line 717 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...
723
            $permissionsCriterionResolverMock,
0 ignored issues
show
Bug introduced by
It seems like $permissionsCriterionResolverMock defined by $this->getPermissionCriterionResolverMock() 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\API\Re...ssionCriterionResolver>, maybe add an additional type check?

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

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

    return array();
}

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

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

Loading history...
724
            array()
725
        );
726
727
        $repositoryMock->expects($this->never())->method('hasAccess');
728
729
        $serviceQuery = new LocationQuery();
730
        $handlerQuery = new LocationQuery(array('filter' => new Criterion\MatchAll(), 'limit' => 25));
731
        $spiLocation = new SPILocation();
732
        $locationMock = $this->getMockForAbstractClass('eZ\\Publish\\API\\Repository\\Values\\Content\\Location');
733
734
        /* @var \PHPUnit_Framework_MockObject_MockObject $searchHandlerMock */
735
        $searchHandlerMock->expects($this->once())
736
            ->method('findLocations')
737
            ->with($this->equalTo($handlerQuery))
738
            ->will(
739
                $this->returnValue(
740
                    new SearchResult(
741
                        array(
742
                            'searchHits' => array(new SearchHit(array('valueObject' => $spiLocation))),
743
                            'totalCount' => 1,
744
                        )
745
                    )
746
                )
747
            );
748
749
        $domainMapperMock->expects($this->once())
750
            ->method('buildLocationDomainObject')
751
            ->with($this->equalTo($spiLocation))
752
            ->will($this->returnValue($locationMock));
753
754
        $result = $service->findLocations($serviceQuery, array(), false);
755
756
        $this->assertEquals(
757
            new SearchResult(
758
                array(
759
                    'searchHits' => array(new SearchHit(array('valueObject' => $locationMock))),
760
                    'totalCount' => 1,
761
                )
762
            ),
763
            $result
764
        );
765
    }
766
767
    /**
768
     * Test for the findLocations() method.
769
     *
770
     * @expectedException \Exception
771
     * @expectedExceptionMessage Handler threw an exception
772
     */
773 View Code Duplication
    public function testFindLocationsThrowsHandlerException()
774
    {
775
        $repositoryMock = $this->getRepositoryMock();
776
        /** @var \eZ\Publish\SPI\Search\Handler $searchHandlerMock */
777
        $searchHandlerMock = $this->getSPIMockHandler('Search\\Handler');
778
        $permissionsCriterionResolverMock = $this->getPermissionCriterionResolverMock();
779
780
        $service = new SearchService(
781
            $repositoryMock,
0 ignored issues
show
Bug introduced by
It seems like $repositoryMock defined by $this->getRepositoryMock() on line 775 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...
782
            $searchHandlerMock,
783
            $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...
784
            $permissionsCriterionResolverMock,
0 ignored issues
show
Bug introduced by
It seems like $permissionsCriterionResolverMock defined by $this->getPermissionCriterionResolverMock() on line 778 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, eZ\Publish\Core\Reposito...hService::__construct() does only seem to accept object<eZ\Publish\API\Re...ssionCriterionResolver>, maybe add an additional type check?

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

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

    return array();
}

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

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

Loading history...
785
            array()
786
        );
787
788
        /** @var \eZ\Publish\API\Repository\Values\Content\Query\Criterion $criterionMock */
789
        $criterionMock = $this
790
            ->getMockBuilder('eZ\\Publish\\API\\Repository\\Values\\Content\\Query\\Criterion')
791
            ->disableOriginalConstructor()
792
            ->getMock();
793
        $query = new LocationQuery(array('filter' => $criterionMock));
794
795
        $permissionsCriterionResolverMock->expects($this->once())
796
            ->method('getPermissionsCriterion')
797
            ->with('content', 'read')
798
            ->will($this->throwException(new Exception('Handler threw an exception')));
799
800
        $service->findLocations($query, array(), true);
801
    }
802
803
    /**
804
     * Test for the findLocations() method.
805
     */
806
    public function testFindLocationsWithDefaultQueryValues()
807
    {
808
        $repositoryMock = $this->getRepositoryMock();
809
        /** @var \eZ\Publish\SPI\Search\Handler $searchHandlerMock */
810
        $searchHandlerMock = $this->getSPIMockHandler('Search\\Handler');
811
        $domainMapperMock = $this->getDomainMapperMock();
812
        $service = new SearchService(
813
            $repositoryMock,
0 ignored issues
show
Bug introduced by
It seems like $repositoryMock defined by $this->getRepositoryMock() on line 808 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, eZ\Publish\Core\Reposito...hService::__construct() does only seem to accept object<eZ\Publish\API\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...
814
            $searchHandlerMock,
815
            $domainMapperMock,
0 ignored issues
show
Bug introduced by
It seems like $domainMapperMock defined by $this->getDomainMapperMock() on line 811 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...
816
            $this->getPermissionCriterionResolverMock(),
0 ignored issues
show
Bug introduced by
It seems like $this->getPermissionCriterionResolverMock() targeting eZ\Publish\Core\Reposito...CriterionResolverMock() can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, eZ\Publish\Core\Reposito...hService::__construct() does only seem to accept object<eZ\Publish\API\Re...ssionCriterionResolver>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
817
            array()
818
        );
819
820
        $spiLocation = new SPILocation();
821
        $locationMock = $this->getMockForAbstractClass('eZ\\Publish\\API\\Repository\\Values\\Content\\Location');
822
        $domainMapperMock->expects($this->once())
823
            ->method('buildLocationDomainObject')
824
            ->with($this->equalTo($spiLocation))
825
            ->will($this->returnValue($locationMock));
826
827
        /* @var \PHPUnit_Framework_MockObject_MockObject $searchHandlerMock */
828
        $searchHandlerMock
829
            ->expects($this->once())
830
            ->method('findLocations')
831
            ->with(
832
                new LocationQuery(
833
                    array(
834
                        'filter' => new Criterion\MatchAll(),
835
                        'limit' => 25,
836
                    )
837
                )
838
            )
839
            ->will(
840
                $this->returnValue(
841
                    new SearchResult(
842
                        array(
843
                            'searchHits' => array(new SearchHit(array('valueObject' => $spiLocation))),
844
                            'totalCount' => 1,
845
                        )
846
                    )
847
                )
848
            );
849
850
        $result = $service->findLocations(new LocationQuery(), array(), false);
851
852
        $this->assertEquals(
853
            new SearchResult(
854
                array(
855
                    'searchHits' => array(new SearchHit(array('valueObject' => $locationMock))),
856
                    'totalCount' => 1,
857
                )
858
            ),
859
            $result
860
        );
861
    }
862
863
    /**
864
     * @return \PHPUnit_Framework_MockObject_MockObject|\eZ\Publish\Core\Repository\Helper\DomainMapper
865
     */
866 View Code Duplication
    protected function getDomainMapperMock()
867
    {
868
        if (!isset($this->domainMapperMock)) {
869
            $this->domainMapperMock = $this
870
                ->getMockBuilder('eZ\\Publish\\Core\\Repository\\Helper\\DomainMapper')
871
                ->disableOriginalConstructor()
872
                ->getMock();
873
        }
874
875
        return $this->domainMapperMock;
876
    }
877
878
    /**
879
     * @return \PHPUnit_Framework_MockObject_MockObject|\eZ\Publish\API\Repository\PermissionCriterionResolver
880
     */
881
    protected function getPermissionCriterionResolverMock()
882
    {
883
        if (!isset($this->permissionsCriterionResolverMock)) {
884
            $this->permissionsCriterionResolverMock = $this
885
                ->getMockBuilder(PermissionCriterionResolver::class)
886
                ->disableOriginalConstructor()
887
                ->getMock();
888
        }
889
890
        return $this->permissionsCriterionResolverMock;
891
    }
892
}
893