Completed
Push — ezp26381-content_view_default_... ( 113831...1416f7 )
by
unknown
77:41 queued 49:16
created

SudoMainLocationLoaderTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
dl 0
loc 116
rs 10
c 0
b 0
f 0
wmc 8
lcom 1
cbo 5

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 4 1
A testLoadLocationNoMainLocation() 0 10 1
A testLoadLocation() 0 23 1
A getRepositoryMock() 0 21 2
A getLocationServiceMock() 0 12 2
B getPermissionResolverMock() 0 24 1
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\Helper\ContentInfoLocationLoader\SudoMainLocationLoader;
10
use eZ\Publish\Core\Repository\Values\Content\Location;
11
use PHPUnit_Framework_TestCase;
12
13
class SudoMainLocationLoaderTest extends PHPUnit_Framework_TestCase
14
{
15
    /**
16
     * @var \eZ\Publish\Core\Helper\ContentInfoLocationLoader\SudoMainLocationLoader
17
     */
18
    private $loader;
19
20
    public function setUp()
21
    {
22
        $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...
23
    }
24
25
    public function testLoadLocationNoMainLocation()
26
    {
27
        $contentInfo = new ContentInfo();
28
29
        $this->getLocationServiceMock()
30
            ->expects($this->never())
31
            ->method('loadLocation');
32
33
        $this->assertNull($this->loader->loadLocation($contentInfo));
34
    }
35
36
    public function testLoadLocation()
37
    {
38
        $contentInfo = new ContentInfo(['mainLocationId' => 42]);
39
        $location = new Location();
40
41
        $this->getRepositoryMock()
42
            ->expects($this->any())
43
            ->method('getPermissionResolver')
44
            ->will($this->returnValue($this->getPermissionResolverMock()));
45
46
        $this->getRepositoryMock()
47
            ->expects($this->any())
48
            ->method('getLocationService')
49
            ->will($this->returnValue($this->getLocationServiceMock()));
50
51
        $this->getLocationServiceMock()
52
            ->expects($this->once())
53
            ->method('loadLocation')
54
            ->with(42)
55
            ->will($this->returnValue($location));
56
57
        $this->assertSame($location, $this->loader->loadLocation($contentInfo));
58
    }
59
60
    /**
61
     * @return \eZ\Publish\Core\Repository\Repository|\PHPUnit_Framework_MockObject_MockObject
62
     */
63
    private function getRepositoryMock()
64
    {
65
        static $repositoryMock;
66
67
        if ($repositoryMock === null) {
68
            $repositoryClass = 'eZ\Publish\Core\Repository\Repository';
69
70
            $repositoryMock = $this
71
                ->getMockBuilder($repositoryClass)
72
                ->disableOriginalConstructor()
73
                ->setMethods(
74
                    array_diff(
75
                        get_class_methods($repositoryClass),
76
                        array('sudo')
77
                    )
78
                )
79
                ->getMock();
80
        }
81
82
        return $repositoryMock;
83
    }
84
85
    /**
86
     * @return \eZ\Publish\API\Repository\LocationService|\PHPUnit_Framework_MockObject_MockObject
87
     */
88
    private function getLocationServiceMock()
89
    {
90
        static $mock;
91
92
        if ($mock === null) {
93
            $mock = $this
94
                ->getMockBuilder('eZ\Publish\API\Repository\LocationService')
95
                ->getMock();
96
        }
97
98
        return $mock;
99
    }
100
101
    /**
102
     * @return \eZ\Publish\Core\Repository\Permission\PermissionResolver|\PHPUnit_Framework_MockObject_MockObject
103
     */
104
    private function getPermissionResolverMock()
105
    {
106
        return $this
107
            ->getMockBuilder('eZ\Publish\Core\Repository\Permission\PermissionResolver')
108
            ->setMethods(null)
109
            ->setConstructorArgs(
110
                [
111
                    $this
112
                        ->getMockBuilder('eZ\Publish\Core\Repository\Helper\RoleDomainMapper')
113
                        ->disableOriginalConstructor()
114
                        ->getMock(),
115
                    $this
116
                        ->getMockBuilder('eZ\Publish\Core\Repository\Helper\LimitationService')
117
                        ->getMock(),
118
                    $this
119
                        ->getMockBuilder('eZ\Publish\SPI\Persistence\User\Handler')
120
                        ->getMock(),
121
                    $this
122
                        ->getMockBuilder('eZ\Publish\API\Repository\Values\User\UserReference')
123
                        ->getMock(),
124
                ]
125
            )
126
            ->getMock();
127
    }
128
}
129