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\Core\Repository\ContentService; |
12
|
|
|
use eZ\Publish\Core\Repository\ContentTypeService; |
13
|
|
|
use eZ\Publish\Core\Repository\FieldTypeService; |
14
|
|
|
use eZ\Publish\Core\Repository\LocationService; |
15
|
|
|
use eZ\Publish\Core\Repository\UserService; |
16
|
|
|
use eZ\Publish\Core\REST\Server\Input\Parser\UserGroupUpdate; |
17
|
|
|
use eZ\Publish\API\Repository\Values\Content\ContentInfo; |
18
|
|
|
use eZ\Publish\Core\Repository\Values\Content\Location; |
19
|
|
|
use eZ\Publish\API\Repository\Values\Content\ContentMetadataUpdateStruct; |
20
|
|
|
use eZ\Publish\API\Repository\Values\User\UserGroupUpdateStruct; |
21
|
|
|
use eZ\Publish\Core\Repository\Values\Content\ContentUpdateStruct; |
22
|
|
|
use eZ\Publish\Core\REST\Common\Input\FieldTypeParser; |
23
|
|
|
use eZ\Publish\Core\REST\Server\Values\RestUserGroupUpdateStruct; |
24
|
|
|
|
25
|
|
|
class UserGroupUpdateTest extends BaseTest |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* Tests the UserGroupUpdate parser. |
29
|
|
|
*/ |
30
|
|
|
public function testParse() |
31
|
|
|
{ |
32
|
|
|
$inputArray = array( |
33
|
|
|
'mainLanguageCode' => 'eng-US', |
34
|
|
|
'Section' => array( |
35
|
|
|
'_href' => '/content/sections/1', |
36
|
|
|
), |
37
|
|
|
'remoteId' => 'remoteId123456', |
38
|
|
|
'fields' => array( |
39
|
|
|
'field' => array( |
40
|
|
|
array( |
41
|
|
|
'fieldDefinitionIdentifier' => 'name', |
42
|
|
|
'fieldValue' => array(), |
43
|
|
|
), |
44
|
|
|
), |
45
|
|
|
), |
46
|
|
|
'__url' => '/user/groups/1/5', |
47
|
|
|
); |
48
|
|
|
|
49
|
|
|
$userGroupUpdate = $this->getParser(); |
50
|
|
|
$result = $userGroupUpdate->parse($inputArray, $this->getParsingDispatcherMock()); |
51
|
|
|
|
52
|
|
|
$this->assertInstanceOf( |
53
|
|
|
RestUserGroupUpdateStruct::class, |
54
|
|
|
$result, |
55
|
|
|
'UserGroupUpdate not created correctly.' |
56
|
|
|
); |
57
|
|
|
|
58
|
|
|
$this->assertInstanceOf( |
59
|
|
|
ContentUpdateStruct::class, |
60
|
|
|
$result->userGroupUpdateStruct->contentUpdateStruct, |
61
|
|
|
'UserGroupUpdate not created correctly.' |
62
|
|
|
); |
63
|
|
|
|
64
|
|
|
$this->assertInstanceOf( |
65
|
|
|
ContentMetadataUpdateStruct::class, |
66
|
|
|
$result->userGroupUpdateStruct->contentMetadataUpdateStruct, |
67
|
|
|
'UserGroupUpdate not created correctly.' |
68
|
|
|
); |
69
|
|
|
|
70
|
|
|
$this->assertEquals( |
71
|
|
|
1, |
72
|
|
|
$result->sectionId, |
73
|
|
|
'sectionId not created correctly' |
74
|
|
|
); |
75
|
|
|
|
76
|
|
|
$this->assertEquals( |
77
|
|
|
'eng-US', |
78
|
|
|
$result->userGroupUpdateStruct->contentMetadataUpdateStruct->mainLanguageCode, |
79
|
|
|
'mainLanguageCode not created correctly' |
80
|
|
|
); |
81
|
|
|
|
82
|
|
|
$this->assertEquals( |
83
|
|
|
'remoteId123456', |
84
|
|
|
$result->userGroupUpdateStruct->contentMetadataUpdateStruct->remoteId, |
85
|
|
|
'remoteId not created correctly' |
86
|
|
|
); |
87
|
|
|
|
88
|
|
|
foreach ($result->userGroupUpdateStruct->contentUpdateStruct->fields as $field) { |
89
|
|
|
$this->assertEquals( |
90
|
|
|
'foo', |
91
|
|
|
$field->value, |
92
|
|
|
'field value not created correctly' |
93
|
|
|
); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Test UserGroupUpdate parser throwing exception on missing Section href. |
99
|
|
|
* |
100
|
|
|
* @expectedException \eZ\Publish\Core\REST\Common\Exceptions\Parser |
101
|
|
|
* @expectedExceptionMessage Missing '_href' attribute for Section element in UserGroupUpdate. |
102
|
|
|
*/ |
103
|
|
|
public function testParseExceptionOnMissingSectionHref() |
104
|
|
|
{ |
105
|
|
|
$inputArray = array( |
106
|
|
|
'mainLanguageCode' => 'eng-US', |
107
|
|
|
'Section' => array(), |
108
|
|
|
'remoteId' => 'remoteId123456', |
109
|
|
|
'fields' => array( |
110
|
|
|
'field' => array( |
111
|
|
|
array( |
112
|
|
|
'fieldDefinitionIdentifier' => 'name', |
113
|
|
|
'fieldValue' => array(), |
114
|
|
|
), |
115
|
|
|
), |
116
|
|
|
), |
117
|
|
|
'__url' => '/user/groups/1/5', |
118
|
|
|
); |
119
|
|
|
|
120
|
|
|
$userGroupUpdate = $this->getParser(); |
121
|
|
|
$userGroupUpdate->parse($inputArray, $this->getParsingDispatcherMock()); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Test UserGroupUpdate parser throwing exception on invalid fields data. |
126
|
|
|
* |
127
|
|
|
* @expectedException \eZ\Publish\Core\REST\Common\Exceptions\Parser |
128
|
|
|
* @expectedExceptionMessage Invalid 'fields' element for UserGroupUpdate. |
129
|
|
|
*/ |
130
|
|
View Code Duplication |
public function testParseExceptionOnInvalidFields() |
131
|
|
|
{ |
132
|
|
|
$inputArray = array( |
133
|
|
|
'mainLanguageCode' => 'eng-US', |
134
|
|
|
'Section' => array( |
135
|
|
|
'_href' => '/content/sections/1', |
136
|
|
|
), |
137
|
|
|
'remoteId' => 'remoteId123456', |
138
|
|
|
'fields' => array(), |
139
|
|
|
'__url' => '/user/groups/1/5', |
140
|
|
|
); |
141
|
|
|
|
142
|
|
|
$userGroupUpdate = $this->getParser(); |
143
|
|
|
$userGroupUpdate->parse($inputArray, $this->getParsingDispatcherMock()); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Test UserGroupUpdate parser throwing exception on missing field definition identifier. |
148
|
|
|
* |
149
|
|
|
* @expectedException \eZ\Publish\Core\REST\Common\Exceptions\Parser |
150
|
|
|
* @expectedExceptionMessage Missing 'fieldDefinitionIdentifier' element in field data for UserGroupUpdate. |
151
|
|
|
*/ |
152
|
|
|
public function testParseExceptionOnMissingFieldDefinitionIdentifier() |
153
|
|
|
{ |
154
|
|
|
$inputArray = array( |
155
|
|
|
'mainLanguageCode' => 'eng-US', |
156
|
|
|
'Section' => array( |
157
|
|
|
'_href' => '/content/sections/1', |
158
|
|
|
), |
159
|
|
|
'remoteId' => 'remoteId123456', |
160
|
|
|
'fields' => array( |
161
|
|
|
'field' => array( |
162
|
|
|
array( |
163
|
|
|
'fieldValue' => array(), |
164
|
|
|
), |
165
|
|
|
), |
166
|
|
|
), |
167
|
|
|
'__url' => '/user/groups/1/5', |
168
|
|
|
); |
169
|
|
|
|
170
|
|
|
$userGroupUpdate = $this->getParser(); |
171
|
|
|
$userGroupUpdate->parse($inputArray, $this->getParsingDispatcherMock()); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Test UserGroupUpdate parser throwing exception on missing field value. |
176
|
|
|
* |
177
|
|
|
* @expectedException \eZ\Publish\Core\REST\Common\Exceptions\Parser |
178
|
|
|
* @expectedExceptionMessage Missing 'fieldValue' element for 'name' identifier in UserGroupUpdate. |
179
|
|
|
*/ |
180
|
|
|
public function testParseExceptionOnMissingFieldValue() |
181
|
|
|
{ |
182
|
|
|
$inputArray = array( |
183
|
|
|
'mainLanguageCode' => 'eng-US', |
184
|
|
|
'Section' => array( |
185
|
|
|
'_href' => '/content/sections/1', |
186
|
|
|
), |
187
|
|
|
'remoteId' => 'remoteId123456', |
188
|
|
|
'fields' => array( |
189
|
|
|
'field' => array( |
190
|
|
|
array( |
191
|
|
|
'fieldDefinitionIdentifier' => 'name', |
192
|
|
|
), |
193
|
|
|
), |
194
|
|
|
), |
195
|
|
|
'__url' => '/user/groups/1/5', |
196
|
|
|
); |
197
|
|
|
|
198
|
|
|
$userGroupUpdate = $this->getParser(); |
199
|
|
|
$userGroupUpdate->parse($inputArray, $this->getParsingDispatcherMock()); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Returns the UserGroupUpdate parser. |
204
|
|
|
* |
205
|
|
|
* @return \eZ\Publish\Core\REST\Server\Input\Parser\UserGroupUpdate |
206
|
|
|
*/ |
207
|
|
|
protected function internalGetParser() |
208
|
|
|
{ |
209
|
|
|
return new UserGroupUpdate( |
|
|
|
|
210
|
|
|
$this->getUserServiceMock(), |
211
|
|
|
$this->getContentServiceMock(), |
212
|
|
|
$this->getLocationServiceMock(), |
213
|
|
|
$this->getFieldTypeParserMock() |
214
|
|
|
); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* Get the field type parser mock object. |
219
|
|
|
* |
220
|
|
|
* @return \eZ\Publish\Core\REST\Common\Input\FieldTypeParser; |
|
|
|
|
221
|
|
|
*/ |
222
|
|
View Code Duplication |
private function getFieldTypeParserMock() |
223
|
|
|
{ |
224
|
|
|
$fieldTypeParserMock = $this->getMockBuilder(FieldTypeParser::class) |
225
|
|
|
->disableOriginalConstructor() |
226
|
|
|
->setMethods(array()) |
227
|
|
|
->setConstructorArgs( |
228
|
|
|
array( |
229
|
|
|
$this->getContentServiceMock(), |
230
|
|
|
$this->createMock(ContentTypeService::class), |
231
|
|
|
$this->createMock(FieldTypeService::class), |
232
|
|
|
) |
233
|
|
|
) |
234
|
|
|
->getMock(); |
235
|
|
|
|
236
|
|
|
$fieldTypeParserMock->expects($this->any()) |
237
|
|
|
->method('parseFieldValue') |
238
|
|
|
->with(4, 'name', array()) |
239
|
|
|
->will($this->returnValue('foo')); |
240
|
|
|
|
241
|
|
|
return $fieldTypeParserMock; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* Get the user service mock object. |
246
|
|
|
* |
247
|
|
|
* @return \eZ\Publish\API\Repository\UserService |
248
|
|
|
*/ |
249
|
|
|
protected function getUserServiceMock() |
250
|
|
|
{ |
251
|
|
|
$userServiceMock = $this->createMock(UserService::class); |
252
|
|
|
|
253
|
|
|
$userServiceMock->expects($this->any()) |
254
|
|
|
->method('newUserGroupUpdateStruct') |
255
|
|
|
->will( |
256
|
|
|
$this->returnValue(new UserGroupUpdateStruct()) |
257
|
|
|
); |
258
|
|
|
|
259
|
|
|
return $userServiceMock; |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* Get the location service mock object. |
264
|
|
|
* |
265
|
|
|
* @return \eZ\Publish\API\Repository\LocationService |
266
|
|
|
*/ |
267
|
|
View Code Duplication |
protected function getLocationServiceMock() |
268
|
|
|
{ |
269
|
|
|
$userServiceMock = $this->createMock(LocationService::class); |
270
|
|
|
|
271
|
|
|
$userServiceMock->expects($this->any()) |
272
|
|
|
->method('loadLocation') |
273
|
|
|
->with($this->equalTo(5)) |
274
|
|
|
->will( |
275
|
|
|
$this->returnValue( |
276
|
|
|
new Location( |
277
|
|
|
array( |
278
|
|
|
'contentInfo' => new ContentInfo( |
279
|
|
|
array( |
280
|
|
|
'id' => 4, |
281
|
|
|
) |
282
|
|
|
), |
283
|
|
|
) |
284
|
|
|
) |
285
|
|
|
) |
286
|
|
|
); |
287
|
|
|
|
288
|
|
|
return $userServiceMock; |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* Get the content service mock object. |
293
|
|
|
* |
294
|
|
|
* @return \eZ\Publish\API\Repository\ContentService |
295
|
|
|
*/ |
296
|
|
View Code Duplication |
protected function getContentServiceMock() |
297
|
|
|
{ |
298
|
|
|
$contentServiceMock = $this->createMock(ContentService::class); |
299
|
|
|
|
300
|
|
|
$contentServiceMock->expects($this->any()) |
301
|
|
|
->method('newContentUpdateStruct') |
302
|
|
|
->will( |
303
|
|
|
$this->returnValue(new ContentUpdateStruct()) |
304
|
|
|
); |
305
|
|
|
|
306
|
|
|
$contentServiceMock->expects($this->any()) |
307
|
|
|
->method('newContentMetadataUpdateStruct') |
308
|
|
|
->will( |
309
|
|
|
$this->returnValue(new ContentMetadataUpdateStruct()) |
310
|
|
|
); |
311
|
|
|
|
312
|
|
|
return $contentServiceMock; |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
public function getParseHrefExpectationsMap() |
316
|
|
|
{ |
317
|
|
|
return array( |
318
|
|
|
array('/content/sections/1', 'sectionId', 1), |
319
|
|
|
array('/user/groups/1/5', 'groupPath', '1/5'), |
320
|
|
|
); |
321
|
|
|
} |
322
|
|
|
} |
323
|
|
|
|
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:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.