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
|
|
|
* @version //autogentag// |
10
|
|
|
*/ |
11
|
|
|
namespace eZ\Publish\Core\REST\Server\Tests\Input\Parser; |
12
|
|
|
|
13
|
|
|
use eZ\Publish\API\Repository\Values\User\Limitation\SectionLimitation; |
14
|
|
|
use eZ\Publish\Core\REST\Server\Input\Parser\Limitation\SectionLimitation as Parser; |
15
|
|
|
|
16
|
|
|
class SectionLimitationTest extends BaseTest |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Tests the SectionInput parser. |
20
|
|
|
*/ |
21
|
|
|
public function testParse() |
22
|
|
|
{ |
23
|
|
|
$inputArray = array( |
24
|
|
|
'_identifier' => 'Section', |
25
|
|
|
'values' => [ |
26
|
|
|
'ref' => [ |
27
|
|
|
[ |
28
|
|
|
'_href' => '/content/section/2', |
29
|
|
|
'_media-type' => "application/vnd.ez.api.Section+json", |
30
|
|
|
] |
31
|
|
|
], |
32
|
|
|
], |
33
|
|
|
); |
34
|
|
|
|
35
|
|
|
$this->getRequestParserMock() |
36
|
|
|
->expects($this->once()) |
37
|
|
|
->method('parseHref') |
38
|
|
|
->with('/content/section/2', 'sectionId') |
39
|
|
|
->will($this->returnValue(2)); |
40
|
|
|
|
41
|
|
|
$result = $this->getParser()->parse($inputArray, $this->getParsingDispatcherMock()); |
42
|
|
|
|
43
|
|
|
$this->assertEquals( |
44
|
|
|
new SectionLimitation(['limitationValues' => [2]]), |
45
|
|
|
$result, |
46
|
|
|
'SectionLimitation not created correctly.' |
47
|
|
|
); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function getParseHrefExpectationsMap() |
51
|
|
|
{ |
52
|
|
|
return [ |
53
|
|
|
['/content/section/2', 'sectionId', 2], |
54
|
|
|
]; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Returns the section input parser. |
59
|
|
|
* |
60
|
|
|
* @return \eZ\Publish\Core\REST\Server\Input\Parser\SectionInput |
61
|
|
|
*/ |
62
|
|
|
protected function internalGetParser() |
63
|
|
|
{ |
64
|
|
|
return new Parser($this->getSectionServiceMock()); |
|
|
|
|
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Get the section service mock object. |
69
|
|
|
* |
70
|
|
|
* @return \eZ\Publish\API\Repository\SectionService |
71
|
|
|
*/ |
72
|
|
View Code Duplication |
protected function getSectionServiceMock() |
73
|
|
|
{ |
74
|
|
|
$sectionServiceMock = $this->getMock( |
75
|
|
|
'eZ\\Publish\\Core\\Repository\\SectionService', |
76
|
|
|
array(), |
77
|
|
|
array(), |
78
|
|
|
'', |
79
|
|
|
false |
80
|
|
|
); |
81
|
|
|
|
82
|
|
|
$sectionServiceMock->expects($this->any()) |
83
|
|
|
->method('newSectionCreateStruct') |
84
|
|
|
->will( |
85
|
|
|
$this->returnValue(new SectionCreateStruct()) |
86
|
|
|
); |
87
|
|
|
|
88
|
|
|
return $sectionServiceMock; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
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.