Completed
Push — master ( 28c8cd...7e44c6 )
by André
18:53
created

PolicyCreateTest::getRoleServiceMock()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 14

Duplication

Lines 23
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 14
nc 1
nop 0
dl 23
loc 23
rs 9.0856
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * File containing a test 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\REST\Server\Tests\Input\Parser;
10
11
use eZ\Publish\API\Repository\Values\User\Limitation;
12
use eZ\Publish\Core\Repository\RoleService;
13
use eZ\Publish\Core\REST\Server\Input\Parser\PolicyCreate;
14
use eZ\Publish\Core\Repository\Values\User\PolicyCreateStruct;
15
16
class PolicyCreateTest extends BaseTest
17
{
18
    /**
19
     * Tests the PolicyCreate parser.
20
     */
21
    public function testParse()
22
    {
23
        $inputArray = array(
24
            'module' => 'content',
25
            'function' => 'delete',
26
            'limitations' => array(
27
                'limitation' => array(
28
                    array(
29
                        '_identifier' => 'Class',
30
                        'values' => array(
31
                            'ref' => array(
32
                                array(
33
                                    '_href' => 1,
34
                                ),
35
                                array(
36
                                    '_href' => 2,
37
                                ),
38
                                array(
39
                                    '_href' => 3,
40
                                ),
41
                            ),
42
                        ),
43
                    ),
44
                ),
45
            ),
46
        );
47
48
        $policyCreate = $this->getParser();
49
        $result = $policyCreate->parse($inputArray, $this->getParsingDispatcherMock());
50
51
        $this->assertInstanceOf(
52
            PolicyCreateStruct::class,
53
            $result,
54
            'PolicyCreateStruct not created correctly.'
55
        );
56
57
        $this->assertEquals(
58
            'content',
59
            $result->module,
60
            'PolicyCreateStruct module property not created correctly.'
61
        );
62
63
        $this->assertEquals(
64
            'delete',
65
            $result->function,
66
            'PolicyCreateStruct function property not created correctly.'
67
        );
68
69
        $parsedLimitations = $result->getLimitations();
70
71
        $this->assertInternalType(
72
            'array',
73
            $parsedLimitations,
74
            'PolicyCreateStruct limitations not created correctly'
75
        );
76
77
        $this->assertCount(
78
            1,
79
            $parsedLimitations,
80
            'PolicyCreateStruct limitations not created correctly'
81
        );
82
83
        $this->assertInstanceOf(
84
            Limitation::class,
85
            $parsedLimitations['Class'],
86
            'Limitation not created correctly.'
87
        );
88
89
        $this->assertEquals(
90
            'Class',
91
            $parsedLimitations['Class']->getIdentifier(),
92
            'Limitation identifier not created correctly.'
93
        );
94
95
        $this->assertEquals(
96
            array(1, 2, 3),
97
            $parsedLimitations['Class']->limitationValues,
98
            'Limitation values not created correctly.'
99
        );
100
    }
101
102
    /**
103
     * Test PolicyCreate parser throwing exception on missing module.
104
     *
105
     * @expectedException \eZ\Publish\Core\REST\Common\Exceptions\Parser
106
     * @expectedExceptionMessage Missing 'module' attribute for PolicyCreate.
107
     */
108
    public function testParseExceptionOnMissingModule()
109
    {
110
        $inputArray = array(
111
            'function' => 'delete',
112
            'limitations' => array(
113
                'limitation' => array(
114
                    array(
115
                        '_identifier' => 'Class',
116
                        'values' => array(
117
                            'ref' => array(
118
                                array(
119
                                    '_href' => 1,
120
                                ),
121
                                array(
122
                                    '_href' => 2,
123
                                ),
124
                                array(
125
                                    '_href' => 3,
126
                                ),
127
                            ),
128
                        ),
129
                    ),
130
                ),
131
            ),
132
        );
133
134
        $policyCreate = $this->getParser();
135
        $policyCreate->parse($inputArray, $this->getParsingDispatcherMock());
136
    }
137
138
    /**
139
     * Test PolicyCreate parser throwing exception on missing function.
140
     *
141
     * @expectedException \eZ\Publish\Core\REST\Common\Exceptions\Parser
142
     * @expectedExceptionMessage Missing 'function' attribute for PolicyCreate.
143
     */
144
    public function testParseExceptionOnMissingFunction()
145
    {
146
        $inputArray = array(
147
            'module' => 'content',
148
            'limitations' => array(
149
                'limitation' => array(
150
                    array(
151
                        '_identifier' => 'Class',
152
                        'values' => array(
153
                            'ref' => array(
154
                                array(
155
                                    '_href' => 1,
156
                                ),
157
                                array(
158
                                    '_href' => 2,
159
                                ),
160
                                array(
161
                                    '_href' => 3,
162
                                ),
163
                            ),
164
                        ),
165
                    ),
166
                ),
167
            ),
168
        );
169
170
        $policyCreate = $this->getParser();
171
        $policyCreate->parse($inputArray, $this->getParsingDispatcherMock());
172
    }
173
174
    /**
175
     * Test PolicyCreate parser throwing exception on missing identifier.
176
     *
177
     * @expectedException \eZ\Publish\Core\REST\Common\Exceptions\Parser
178
     * @expectedExceptionMessage Missing '_identifier' attribute for Limitation.
179
     */
180
    public function testParseExceptionOnMissingLimitationIdentifier()
181
    {
182
        $inputArray = array(
183
            'module' => 'content',
184
            'function' => 'delete',
185
            'limitations' => array(
186
                'limitation' => array(
187
                    array(
188
                        'values' => array(
189
                            'ref' => array(
190
                                array(
191
                                    '_href' => 1,
192
                                ),
193
                                array(
194
                                    '_href' => 2,
195
                                ),
196
                                array(
197
                                    '_href' => 3,
198
                                ),
199
                            ),
200
                        ),
201
                    ),
202
                ),
203
            ),
204
        );
205
206
        $policyCreate = $this->getParser();
207
        $policyCreate->parse($inputArray, $this->getParsingDispatcherMock());
208
    }
209
210
    /**
211
     * Test PolicyCreate parser throwing exception on missing values.
212
     *
213
     * @expectedException \eZ\Publish\Core\REST\Common\Exceptions\Parser
214
     * @expectedExceptionMessage Invalid format for limitation values in Limitation.
215
     */
216
    public function testParseExceptionOnMissingLimitationValues()
217
    {
218
        $inputArray = array(
219
            'module' => 'content',
220
            'function' => 'delete',
221
            'limitations' => array(
222
                'limitation' => array(
223
                    array(
224
                        '_identifier' => 'Class',
225
                    ),
226
                ),
227
            ),
228
        );
229
230
        $policyCreate = $this->getParser();
231
        $policyCreate->parse($inputArray, $this->getParsingDispatcherMock());
232
    }
233
234
    /**
235
     * Returns the PolicyCreateStruct parser.
236
     *
237
     * @return \eZ\Publish\Core\REST\Server\Input\Parser\PolicyCreate
238
     */
239
    protected function internalGetParser()
240
    {
241
        return new PolicyCreate(
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \eZ\Publish\C...his->getParserTools()); (eZ\Publish\Core\REST\Ser...put\Parser\PolicyCreate) is incompatible with the return type declared by the abstract method eZ\Publish\Core\REST\Ser...Test::internalGetParser of type eZ\Publish\Core\REST\Server\Input\Parser\Base.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
242
            $this->getRoleServiceMock(),
243
            $this->getParserTools()
244
        );
245
    }
246
247
    /**
248
     * Get the role service mock object.
249
     *
250
     * @return \eZ\Publish\API\Repository\RoleService
251
     */
252 View Code Duplication
    protected function getRoleServiceMock()
253
    {
254
        $roleServiceMock = $this->createMock(RoleService::class);
255
256
        $roleServiceMock->expects($this->any())
257
            ->method('newPolicyCreateStruct')
258
            ->with(
259
                $this->equalTo('content'),
260
                $this->equalTo('delete')
261
            )
262
            ->will(
263
                $this->returnValue(
264
                    new PolicyCreateStruct(
265
                        array(
266
                            'module' => 'content',
267
                            'function' => 'delete',
268
                        )
269
                    )
270
                )
271
            );
272
273
        return $roleServiceMock;
274
    }
275
}
276