1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Communibase; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* @package Communibase |
7
|
|
|
* @author Kingsquare ([email protected]) |
8
|
|
|
* @copyright Copyright (c) Kingsquare BV (http://www.kingsquare.nl) |
9
|
|
|
*/ |
10
|
|
|
class GetByRefTest extends \PHPUnit_Framework_TestCase |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @expectedException Exception |
14
|
|
|
* @expectedExceptionMessage Please provide a documentReference object with a type and id |
15
|
|
|
*/ |
16
|
|
|
public function invalid() |
17
|
|
|
{ |
18
|
|
|
$this->getMockConnector()->getByRef([]); |
|
|
|
|
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @test |
23
|
|
|
* @throws Exception |
24
|
|
|
*/ |
25
|
|
View Code Duplication |
public function singlePathToAttribute() |
|
|
|
|
26
|
|
|
{ |
27
|
|
|
$result = $this->getMockConnector()->getByRef([ |
|
|
|
|
28
|
|
|
'rootDocumentEntityType' => 'Test', |
29
|
|
|
'rootDocumentId' => 'X', |
30
|
|
|
'path' => [ |
31
|
|
|
[ |
32
|
|
|
'field' => 'singlePathToAttribute' |
33
|
|
|
] |
34
|
|
|
] |
35
|
|
|
]); |
36
|
|
|
|
37
|
|
|
$this->assertSame('value', $result); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @test |
42
|
|
|
* @throws Exception |
43
|
|
|
*/ |
44
|
|
View Code Duplication |
public function multiplePathToAttribute() |
|
|
|
|
45
|
|
|
{ |
46
|
|
|
$result = $this->getMockConnector()->getByRef([ |
|
|
|
|
47
|
|
|
'rootDocumentEntityType' => 'Test', |
48
|
|
|
'rootDocumentId' => 'X', |
49
|
|
|
'path' => [ |
50
|
|
|
[ |
51
|
|
|
'field' => 'multiplePathToAttribute' |
52
|
|
|
], |
53
|
|
|
[ |
54
|
|
|
'field' => 'field' |
55
|
|
|
] |
56
|
|
|
] |
57
|
|
|
]); |
58
|
|
|
|
59
|
|
|
$this->assertSame('value', $result); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @test |
64
|
|
|
* @throws Exception |
65
|
|
|
*/ |
66
|
|
View Code Duplication |
public function multiplePathToAttributeDepth() |
|
|
|
|
67
|
|
|
{ |
68
|
|
|
$result = $this->getMockConnector()->getByRef([ |
|
|
|
|
69
|
|
|
'rootDocumentEntityType' => 'Test', |
70
|
|
|
'rootDocumentId' => 'X', |
71
|
|
|
'path' => [ |
72
|
|
|
[ |
73
|
|
|
'field' => 'multiplePathToAttributeDepth' |
74
|
|
|
], |
75
|
|
|
[ |
76
|
|
|
'field' => 'field' |
77
|
|
|
], |
78
|
|
|
[ |
79
|
|
|
'field' => 'field' |
80
|
|
|
] |
81
|
|
|
] |
82
|
|
|
]); |
83
|
|
|
|
84
|
|
|
$this->assertSame('value', $result); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @test |
89
|
|
|
* @throws Exception |
90
|
|
|
*/ |
91
|
|
|
public function singlePathToArrayObject() |
92
|
|
|
{ |
93
|
|
|
$result = $this->getMockConnector()->getByRef([ |
|
|
|
|
94
|
|
|
'rootDocumentEntityType' => 'Test', |
95
|
|
|
'rootDocumentId' => 'X', |
96
|
|
|
'path' => [ |
97
|
|
|
[ |
98
|
|
|
'field' => 'singlePathToArrayObject', |
99
|
|
|
'objectId' => 'value' |
100
|
|
|
] |
101
|
|
|
] |
102
|
|
|
]); |
103
|
|
|
|
104
|
|
|
$this->assertSame(['_id' => 'value'], $result); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @test |
109
|
|
|
* @throws Exception |
110
|
|
|
*/ |
111
|
|
View Code Duplication |
public function multiplePathToArrayObject() |
|
|
|
|
112
|
|
|
{ |
113
|
|
|
$result = $this->getMockConnector()->getByRef([ |
|
|
|
|
114
|
|
|
'rootDocumentEntityType' => 'Test', |
115
|
|
|
'rootDocumentId' => 'X', |
116
|
|
|
'path' => [ |
117
|
|
|
[ |
118
|
|
|
'field' => 'multiplePathToArrayObject', |
119
|
|
|
], |
120
|
|
|
[ |
121
|
|
|
'field' => 'field', |
122
|
|
|
'objectId' => 'value' |
123
|
|
|
] |
124
|
|
|
] |
125
|
|
|
]); |
126
|
|
|
|
127
|
|
|
$this->assertSame(['_id' => 'value'], $result); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|Connector |
132
|
|
|
*/ |
133
|
|
|
protected function getMockConnector() |
134
|
|
|
{ |
135
|
|
|
$mock = $this->getMockBuilder(Connector::class) |
136
|
|
|
->setMethods(['getById']) |
137
|
|
|
->disableOriginalConstructor() |
138
|
|
|
->getMock(); |
139
|
|
|
|
140
|
|
|
$mock->method('getById') |
141
|
|
|
->willReturn([ |
142
|
|
|
'_id' => '1234', |
143
|
|
|
'singlePathToAttribute' => 'value', |
144
|
|
|
'multiplePathToAttribute' => [ |
145
|
|
|
'field' => 'value' |
146
|
|
|
], |
147
|
|
|
'multiplePathToAttributeDepth' => [ |
148
|
|
|
'field' => [ |
149
|
|
|
'field' => 'value' |
150
|
|
|
], |
151
|
|
|
], |
152
|
|
|
'singlePathToArrayObject' => [ |
153
|
|
|
[ |
154
|
|
|
'_id' => 'value' |
155
|
|
|
] |
156
|
|
|
], |
157
|
|
|
'multiplePathToArrayObject' => [ |
158
|
|
|
'field' => [ |
159
|
|
|
[ |
160
|
|
|
'_id' => 'value' |
161
|
|
|
] |
162
|
|
|
] |
163
|
|
|
], |
164
|
|
|
'multiplePathToArrayObjectDepth' => [ |
165
|
|
|
'field' => [ |
166
|
|
|
'field' => [ |
167
|
|
|
[ |
168
|
|
|
'_id' => 'value' |
169
|
|
|
] |
170
|
|
|
] |
171
|
|
|
] |
172
|
|
|
], |
173
|
|
|
]); |
174
|
|
|
|
175
|
|
|
return $mock; |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: