|
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\Criterion; |
|
12
|
|
|
|
|
13
|
|
|
use eZ\Publish\API\Repository\Values\Content\Query\Criterion\UserMetadata as UserMetadataCriterion; |
|
14
|
|
|
use eZ\Publish\Core\REST\Server\Input\Parser\Criterion\UserMetadata; |
|
15
|
|
|
use eZ\Publish\Core\REST\Server\Tests\Input\Parser\BaseTest; |
|
16
|
|
|
|
|
17
|
|
|
class UserMetadataTest extends BaseTest |
|
18
|
|
|
{ |
|
19
|
|
|
public function testParseProvider() |
|
20
|
|
|
{ |
|
21
|
|
|
return [ |
|
22
|
|
|
[ |
|
23
|
|
|
['UserMetadataCriterion' => ['Target' => 'owner', 'Value' => 14]], |
|
24
|
|
|
new UserMetadataCriterion('owner', null, [14]), |
|
25
|
|
|
], |
|
26
|
|
|
[ |
|
27
|
|
|
['UserMetadataCriterion' => ['Target' => 'owner', 'Value' => '14,15,42']], |
|
28
|
|
|
new UserMetadataCriterion('owner', null, [14, 15, 42]), |
|
29
|
|
|
], |
|
30
|
|
|
[ |
|
31
|
|
|
['UserMetadataCriterion' => ['Target' => 'owner', 'Value' => [14, 15, 42]]], |
|
32
|
|
|
new UserMetadataCriterion('owner', null, [14, 15, 42]), |
|
33
|
|
|
], |
|
34
|
|
|
]; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Tests the UserMetadata parser. |
|
39
|
|
|
* |
|
40
|
|
|
* @dataProvider testParseProvider |
|
41
|
|
|
*/ |
|
42
|
|
|
public function testParse($data, $expected) |
|
43
|
|
|
{ |
|
44
|
|
|
$userMetadata = $this->getParser(); |
|
45
|
|
|
$result = $userMetadata->parse($data, $this->getParsingDispatcherMock()); |
|
46
|
|
|
|
|
47
|
|
|
$this->assertEquals( |
|
48
|
|
|
$expected, |
|
49
|
|
|
$result, |
|
50
|
|
|
'UserMetadata parser not created correctly.' |
|
51
|
|
|
); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Test UserMetadata parser throwing exception on invalid UserMetadataCriterion format. |
|
56
|
|
|
* |
|
57
|
|
|
* @expectedException \eZ\Publish\Core\REST\Common\Exceptions\Parser |
|
58
|
|
|
* @expectedExceptionMessage Invalid <UserMetadataCriterion> format |
|
59
|
|
|
*/ |
|
60
|
|
|
public function testParseExceptionOnInvalidCriterionFormat() |
|
61
|
|
|
{ |
|
62
|
|
|
$inputArray = [ |
|
63
|
|
|
'foo' => 'Michael learns to mock', |
|
64
|
|
|
]; |
|
65
|
|
|
|
|
66
|
|
|
$dataKeyValueObjectClass = $this->getParser(); |
|
67
|
|
|
$dataKeyValueObjectClass->parse($inputArray, $this->getParsingDispatcherMock()); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Test UserMetadata parser throwing exception on invalid target format. |
|
72
|
|
|
* |
|
73
|
|
|
* @expectedException \eZ\Publish\Core\REST\Common\Exceptions\Parser |
|
74
|
|
|
* @expectedExceptionMessage Invalid <Target> format |
|
75
|
|
|
*/ |
|
76
|
|
View Code Duplication |
public function testParseExceptionOnInvalidTargetFormat() |
|
77
|
|
|
{ |
|
78
|
|
|
$inputArray = [ |
|
79
|
|
|
'UserMetadataCriterion' => [ |
|
80
|
|
|
'foo' => 'Mock around the clock', |
|
81
|
|
|
'Value' => 42, |
|
82
|
|
|
], |
|
83
|
|
|
]; |
|
84
|
|
|
|
|
85
|
|
|
$dataKeyValueObjectClass = $this->getParser(); |
|
86
|
|
|
$dataKeyValueObjectClass->parse($inputArray, $this->getParsingDispatcherMock()); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Test UserMetadata parser throwing exception on invalid value format. |
|
91
|
|
|
* |
|
92
|
|
|
* @expectedException \eZ\Publish\Core\REST\Common\Exceptions\Parser |
|
93
|
|
|
* @expectedExceptionMessage Invalid <Value> format |
|
94
|
|
|
*/ |
|
95
|
|
View Code Duplication |
public function testParseExceptionOnInvalidValueFormat() |
|
96
|
|
|
{ |
|
97
|
|
|
$inputArray = [ |
|
98
|
|
|
'UserMetadataCriterion' => [ |
|
99
|
|
|
'Target' => 'Moxette', |
|
100
|
|
|
'foo' => 42, |
|
101
|
|
|
], |
|
102
|
|
|
]; |
|
103
|
|
|
|
|
104
|
|
|
$dataKeyValueObjectClass = $this->getParser(); |
|
105
|
|
|
$dataKeyValueObjectClass->parse($inputArray, $this->getParsingDispatcherMock()); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Test UserMetadata parser throwing exception on wrong type of value format. |
|
110
|
|
|
* |
|
111
|
|
|
* @expectedException \eZ\Publish\Core\REST\Common\Exceptions\Parser |
|
112
|
|
|
* @expectedExceptionMessage Invalid <Value> format |
|
113
|
|
|
*/ |
|
114
|
|
View Code Duplication |
public function testParseExceptionOnWrongValueType() |
|
115
|
|
|
{ |
|
116
|
|
|
$inputArray = [ |
|
117
|
|
|
'UserMetadataCriterion' => [ |
|
118
|
|
|
'Target' => 'We will mock you', |
|
119
|
|
|
'Value' => new \stdClass(), |
|
120
|
|
|
], |
|
121
|
|
|
]; |
|
122
|
|
|
|
|
123
|
|
|
$dataKeyValueObjectClass = $this->getParser(); |
|
124
|
|
|
$dataKeyValueObjectClass->parse($inputArray, $this->getParsingDispatcherMock()); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Returns the UserMetadata criterion parser. |
|
129
|
|
|
* |
|
130
|
|
|
* @return \eZ\Publish\Core\REST\Server\Input\Parser\Criterion\UserMetadata |
|
131
|
|
|
*/ |
|
132
|
|
|
protected function internalGetParser() |
|
133
|
|
|
{ |
|
134
|
|
|
return new UserMetadata(); |
|
|
|
|
|
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|
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_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.