Completed
Push — master ( 949e77...a1f0e2 )
by
unknown
120:09 queued 86:45
created

getLocationServiceMock()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 0
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
4
 * @license For full copyright and license information view LICENSE file distributed with this source code.
5
 */
6
namespace eZ\Publish\Core\Helper\Tests\ContentInfoLocationLoader;
7
8
use eZ\Publish\API\Repository\Values\Content\ContentInfo;
9
use eZ\Publish\Core\Base\Exceptions\NotFoundException;
10
use eZ\Publish\Core\Helper\ContentInfoLocationLoader\SudoMainLocationLoader;
11
use eZ\Publish\Core\Repository\Values\Content\Location;
12
use PHPUnit_Framework_TestCase;
13
14
class SudoMainLocationLoaderTest extends PHPUnit_Framework_TestCase
15
{
16
    /**
17
     * @var \eZ\Publish\Core\Helper\ContentInfoLocationLoader\SudoMainLocationLoader
18
     */
19
    private $loader;
20
21
    public function setUp()
22
    {
23
        $this->loader = new SudoMainLocationLoader($this->getRepositoryMock());
0 ignored issues
show
Bug introduced by
It seems like $this->getRepositoryMock() targeting eZ\Publish\Core\Helper\T...st::getRepositoryMock() can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, eZ\Publish\Core\Helper\C...onLoader::__construct() does only seem to accept object<eZ\Publish\API\Repository\Repository>, 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...
24
    }
25
26
    /**
27
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
28
     */
29
    public function testLoadLocationNoMainLocation()
30
    {
31
        $contentInfo = new ContentInfo();
32
33
        $this->getLocationServiceMock()
34
            ->expects($this->never())
35
            ->method('loadLocation');
36
37
        $this->loader->loadLocation($contentInfo);
38
    }
39
40 View Code Duplication
    public function testLoadLocation()
41
    {
42
        $contentInfo = new ContentInfo(['mainLocationId' => 42]);
43
        $location = new Location();
44
45
        $this->getRepositoryMock()
46
            ->expects($this->any())
47
            ->method('getPermissionResolver')
48
            ->will($this->returnValue($this->getPermissionResolverMock()));
49
50
        $this->getRepositoryMock()
51
            ->expects($this->any())
52
            ->method('getLocationService')
53
            ->will($this->returnValue($this->getLocationServiceMock()));
54
55
        $this->getLocationServiceMock()
56
            ->expects($this->once())
57
            ->method('loadLocation')
58
            ->with(42)
59
            ->will($this->returnValue($location));
60
61
        $this->assertSame($location, $this->loader->loadLocation($contentInfo));
62
    }
63
64
    /**
65
     * @expectedException \eZ\Publish\API\Repository\Exceptions\NotFoundException
66
     */
67 View Code Duplication
    public function testLoadLocationError()
68
    {
69
        $contentInfo = new ContentInfo(['mainLocationId' => 42]);
70
        $location = new Location();
71
72
        $this->getRepositoryMock()
73
            ->expects($this->any())
74
            ->method('getPermissionResolver')
75
            ->will($this->returnValue($this->getPermissionResolverMock()));
76
77
        $this->getRepositoryMock()
78
            ->expects($this->any())
79
            ->method('getLocationService')
80
            ->will($this->returnValue($this->getLocationServiceMock()));
81
82
        $this->getLocationServiceMock()
83
            ->expects($this->once())
84
            ->method('loadLocation')
85
            ->with(42)
86
            ->will(
87
                $this->throwException(new NotFoundException('main location of content', 42))
88
            );
89
90
        $this->assertSame($location, $this->loader->loadLocation($contentInfo));
91
    }
92
93
    /**
94
     * @return \eZ\Publish\Core\Repository\Repository|\PHPUnit_Framework_MockObject_MockObject
95
     */
96
    private function getRepositoryMock()
97
    {
98
        static $repositoryMock;
99
100
        if ($repositoryMock === null) {
101
            $repositoryClass = 'eZ\Publish\Core\Repository\Repository';
102
103
            $repositoryMock = $this
104
                ->getMockBuilder($repositoryClass)
105
                ->disableOriginalConstructor()
106
                ->setMethods(
107
                    array_diff(
108
                        get_class_methods($repositoryClass),
109
                        array('sudo')
110
                    )
111
                )
112
                ->getMock();
113
        }
114
115
        return $repositoryMock;
116
    }
117
118
    /**
119
     * @return \eZ\Publish\API\Repository\LocationService|\PHPUnit_Framework_MockObject_MockObject
120
     */
121
    private function getLocationServiceMock()
122
    {
123
        static $mock;
124
125
        if ($mock === null) {
126
            $mock = $this
127
                ->getMockBuilder('eZ\Publish\API\Repository\LocationService')
128
                ->getMock();
129
        }
130
131
        return $mock;
132
    }
133
134
    /**
135
     * @return \eZ\Publish\Core\Repository\Permission\PermissionResolver|\PHPUnit_Framework_MockObject_MockObject
136
     */
137
    private function getPermissionResolverMock()
138
    {
139
        return $this
140
            ->getMockBuilder('eZ\Publish\Core\Repository\Permission\PermissionResolver')
141
            ->setMethods(null)
142
            ->setConstructorArgs(
143
                [
144
                    $this
145
                        ->getMockBuilder('eZ\Publish\Core\Repository\Helper\RoleDomainMapper')
146
                        ->disableOriginalConstructor()
147
                        ->getMock(),
148
                    $this
149
                        ->getMockBuilder('eZ\Publish\Core\Repository\Helper\LimitationService')
150
                        ->getMock(),
151
                    $this
152
                        ->getMockBuilder('eZ\Publish\SPI\Persistence\User\Handler')
153
                        ->getMock(),
154
                    $this
155
                        ->getMockBuilder('eZ\Publish\API\Repository\Values\User\UserReference')
156
                        ->getMock(),
157
                ]
158
            )
159
            ->getMock();
160
    }
161
}
162