Completed
Push — 7.5 ( b0871e...08bd2d )
by Łukasz
18:14
created

testExpectNotFoundExceptionWhenMainLocationIdIsNull()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
5
 * @license For full copyright and license information view LICENSE file distributed with this source code.
6
 */
7
declare(strict_types=1);
8
9
namespace eZ\Publish\Core\Repository\Tests\LocationResolver;
10
11
use eZ\Publish\API\Repository\Exceptions\BadStateException;
12
use eZ\Publish\API\Repository\Exceptions\NotFoundException;
13
use eZ\Publish\API\Repository\Exceptions\UnauthorizedException;
14
use eZ\Publish\API\Repository\LocationService;
15
use eZ\Publish\API\Repository\Values\Content\ContentInfo;
16
use eZ\Publish\Core\Repository\LocationResolver\PermissionAwareLocationResolver;
17
use eZ\Publish\Core\Repository\Values\Content\Location;
18
use PHPUnit\Framework\TestCase;
19
20
final class PermissionAwareLocationResolverTest extends TestCase
21
{
22
    /** @var \eZ\Publish\API\Repository\LocationService */
23
    private $locationService;
24
25
    /** @var \eZ\Publish\Core\Repository\LocationResolver\LocationResolver */
26
    private $locationResolver;
27
28
    public function setUp(): void
29
    {
30
        $this->locationService = $this->createMock(LocationService::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(\eZ\Pu...LocationService::class) of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<eZ\Publish\API\Repository\LocationService> of property $locationService.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
31
32
        $this->locationResolver = new PermissionAwareLocationResolver($this->locationService);
33
    }
34
35
    /**
36
     * Test for the resolveLocation() method.
37
     *
38
     * @covers \eZ\Publish\Core\Repository\LocationResolver\PermissionAwareLocationResolver::resolveLocation
39
     */
40
    public function testResolveMainLocation(): void
41
    {
42
        $contentInfo = new ContentInfo(['mainLocationId' => 42]);
43
        $location = new Location(['id' => 42]);
44
45
        // User has access to the main Location
46
        $this->locationService
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<eZ\Publish\API\Repository\LocationService>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
47
            ->method('loadLocation')
48
            ->willReturn($location);
49
50
        $this->assertSame($location, $this->locationResolver->resolveLocation($contentInfo));
51
    }
52
53
    /**
54
     * Test for the resolveLocation() method.
55
     *
56
     * @covers \eZ\Publish\Core\Repository\LocationResolver\PermissionAwareLocationResolver::resolveLocation
57
     */
58
    public function testResolveSecondaryLocation(): void
59
    {
60
        $contentInfo = new ContentInfo(['mainLocationId' => 42]);
61
        $location1 = new Location(['id' => 43]);
62
        $location2 = new Location(['id' => 44]);
63
64
        // User doesn't have access to main location but to the third Content's location
65
        $this->locationService
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<eZ\Publish\API\Repository\LocationService>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
66
            ->method('loadLocation')
67
            ->willThrowException($this->createMock(UnauthorizedException::class));
68
69
        $this->locationService
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<eZ\Publish\API\Repository\LocationService>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
70
            ->method('loadLocations')
71
            ->willReturn([$location1, $location2]);
72
73
        $this->assertSame($location1, $this->locationResolver->resolveLocation($contentInfo));
74
    }
75
76
    /**
77
     * Test for the resolveLocation() method when Locations don't exist.
78
     *
79
     * @covers \eZ\Publish\Core\Repository\LocationResolver\PermissionAwareLocationResolver::resolveLocation
80
     */
81
    public function testExpectNotFoundExceptionWhenLocationDoesNotExist(): void
82
    {
83
        $contentInfo = new ContentInfo(['mainLocationId' => 42]);
84
85
        $this->locationService
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<eZ\Publish\API\Repository\LocationService>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
86
            ->method('loadLocation')
87
            ->willThrowException($this->createMock(NotFoundException::class));
88
89
        $this->locationService
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<eZ\Publish\API\Repository\LocationService>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
90
            ->method('loadLocations')
91
            ->willReturn([]);
92
93
        $this->expectException(NotFoundException::class);
94
95
        $this->locationResolver->resolveLocation($contentInfo);
96
    }
97
98
    /**
99
     * Test for the resolveLocation() method when ContentInfo's mainLocationId is null.
100
     *
101
     * @covers \eZ\Publish\Core\Repository\LocationResolver\PermissionAwareLocationResolver::resolveLocation
102
     */
103
    public function testExpectNotFoundExceptionWhenMainLocationIdIsNull(): void
104
    {
105
        $contentInfo = new ContentInfo(['mainLocationId' => null]);
106
107
        $this->expectException(NotFoundException::class);
108
109
        $this->locationResolver->resolveLocation($contentInfo);
110
    }
111
112
    /**
113
     * Test for the resolveLocation() method when Location is not yet published.
114
     *
115
     * @covers \eZ\Publish\Core\Repository\LocationResolver\PermissionAwareLocationResolver::resolveLocation
116
     */
117
    public function testExpectBadStateExceptionWhenContentNotYetPublished(): void
118
    {
119
        $contentInfo = new ContentInfo(['mainLocationId' => 42, 'status' => ContentInfo::STATUS_DRAFT]);
120
121
        $this->locationService
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<eZ\Publish\API\Repository\LocationService>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
122
            ->method('loadLocation')
123
            ->willThrowException($this->createMock(NotFoundException::class));
124
125
        $this->locationService
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<eZ\Publish\API\Repository\LocationService>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
126
            ->method('loadLocations')
127
            ->willThrowException($this->createMock(BadStateException::class));
128
129
        $this->expectException(BadStateException::class);
130
131
        $this->locationResolver->resolveLocation($contentInfo);
132
    }
133
}
134