Code Duplication    Length = 57-58 lines in 2 locations

eZ/Publish/Core/Repository/Tests/Service/Mock/RoleTest.php 2 locations

@@ 30-87 (lines=58) @@
27
     * @covers \eZ\Publish\Core\Repository\Helper\LimitationService::validateLimitation
28
     * @expectedException \eZ\Publish\API\Repository\Exceptions\LimitationValidationException
29
     */
30
    public function testCreateRoleThrowsLimitationValidationException()
31
    {
32
        $limitationMock = $this->getMock('eZ\\Publish\\API\\Repository\\Values\\User\\Limitation\\RoleLimitation');
33
        $limitationTypeMock = $this->getMock('eZ\\Publish\\SPI\\Limitation\\Type');
34
35
        $limitationMock->expects($this->any())
36
            ->method('getIdentifier')
37
            ->will($this->returnValue('mockIdentifier'));
38
39
        $limitationTypeMock->expects($this->once())
40
            ->method('acceptValue')
41
            ->with($this->equalTo($limitationMock));
42
        $limitationTypeMock->expects($this->once())
43
            ->method('validate')
44
            ->with($this->equalTo($limitationMock))
45
            ->will($this->returnValue([42]));
46
47
        $settings = [
48
            'policyMap' => ['mockModule' => ['mockFunction' => ['mockIdentifier' => true]]],
49
            'limitationTypes' => ['mockIdentifier' => $limitationTypeMock],
50
        ];
51
52
        $roleServiceMock = $this->getPartlyMockedRoleService(['loadRoleByIdentifier'], $settings);
53
54
        $repository = $this->getRepositoryMock();
55
        /** @var \eZ\Publish\API\Repository\Values\User\RoleCreateStruct $roleCreateStructMock */
56
        $roleCreateStructMock = $this->getMock('eZ\\Publish\\API\\Repository\\Values\\User\\RoleCreateStruct');
57
        $policyCreateStructMock = $this->getMock('eZ\\Publish\\API\\Repository\\Values\\User\\PolicyCreateStruct');
58
59
        /* @var \eZ\Publish\API\Repository\Values\User\PolicyCreateStruct $policyCreateStructMock */
60
        $policyCreateStructMock->module = 'mockModule';
61
        $policyCreateStructMock->function = 'mockFunction';
62
        $roleCreateStructMock->identifier = 'mockIdentifier';
63
        $roleServiceMock->expects($this->once())
64
            ->method('loadRoleByIdentifier')
65
            ->with($this->equalTo('mockIdentifier'))
66
            ->will($this->throwException(new NotFoundException('Role', 'mockIdentifier')));
67
68
        /* @var \PHPUnit_Framework_MockObject_MockObject $roleCreateStructMock */
69
        $roleCreateStructMock->expects($this->once())
70
            ->method('getPolicies')
71
            ->will($this->returnValue([$policyCreateStructMock]));
72
73
        /* @var \PHPUnit_Framework_MockObject_MockObject $policyCreateStructMock */
74
        $policyCreateStructMock->expects($this->once())
75
            ->method('getLimitations')
76
            ->will($this->returnValue([$limitationMock]));
77
78
        $repository->expects($this->once())
79
            ->method('hasAccess')
80
            ->with(
81
                $this->equalTo('role'),
82
                $this->equalTo('create')
83
            )->will($this->returnValue(true));
84
85
        /* @var \eZ\Publish\API\Repository\Values\User\RoleCreateStruct $roleCreateStructMock */
86
        $roleServiceMock->createRole($roleCreateStructMock);
87
    }
88
89
    /**
90
     * Test for the addPolicy() method.
@@ 97-153 (lines=57) @@
94
     * @covers \eZ\Publish\Core\Repository\Helper\LimitationService::validateLimitation
95
     * @expectedException \eZ\Publish\API\Repository\Exceptions\LimitationValidationException
96
     */
97
    public function testAddPolicyThrowsLimitationValidationException()
98
    {
99
        $limitationMock = $this->getMock('eZ\\Publish\\API\\Repository\\Values\\User\\Limitation\\RoleLimitation');
100
        $limitationTypeMock = $this->getMock('eZ\\Publish\\SPI\\Limitation\\Type');
101
102
        $limitationTypeMock->expects($this->once())
103
            ->method('acceptValue')
104
            ->with($this->equalTo($limitationMock));
105
        $limitationTypeMock->expects($this->once())
106
            ->method('validate')
107
            ->with($this->equalTo($limitationMock))
108
            ->will($this->returnValue([42]));
109
110
        $limitationMock->expects($this->any())
111
            ->method('getIdentifier')
112
            ->will($this->returnValue('mockIdentifier'));
113
114
        $settings = [
115
            'policyMap' => ['mockModule' => ['mockFunction' => ['mockIdentifier' => true]]],
116
            'limitationTypes' => ['mockIdentifier' => $limitationTypeMock],
117
        ];
118
119
        $roleServiceMock = $this->getPartlyMockedRoleService(['loadRole'], $settings);
120
121
        $repository = $this->getRepositoryMock();
122
        $roleMock = $this->getMock('eZ\\Publish\\API\\Repository\\Values\\User\\Role');
123
        $policyCreateStructMock = $this->getMock('eZ\\Publish\\API\\Repository\\Values\\User\\PolicyCreateStruct');
124
125
        $roleMock->expects($this->any())
126
            ->method('__get')
127
            ->with('id')
128
            ->will($this->returnValue(42));
129
        /* @var \eZ\Publish\API\Repository\Values\User\PolicyCreateStruct $policyCreateStructMock */
130
        $policyCreateStructMock->module = 'mockModule';
131
        $policyCreateStructMock->function = 'mockFunction';
132
133
        $roleServiceMock->expects($this->once())
134
            ->method('loadRole')
135
            ->with($this->equalTo(42))
136
            ->will($this->returnValue($roleMock));
137
138
        /* @var \PHPUnit_Framework_MockObject_MockObject $policyCreateStructMock */
139
        $policyCreateStructMock->expects($this->once())
140
            ->method('getLimitations')
141
            ->will($this->returnValue([$limitationMock]));
142
143
        $repository->expects($this->once())
144
            ->method('hasAccess')
145
            ->with(
146
                $this->equalTo('role'),
147
                $this->equalTo('update')
148
            )->will($this->returnValue(true));
149
150
        /* @var \eZ\Publish\API\Repository\Values\User\Role $roleMock */
151
        /* @var \eZ\Publish\API\Repository\Values\User\PolicyCreateStruct $policyCreateStructMock */
152
        $roleServiceMock->addPolicy($roleMock, $policyCreateStructMock);
153
    }
154
155
    /**
156
     * Test for the updatePolicy() method.