1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the ONGR package. |
5
|
|
|
* |
6
|
|
|
* (c) NFQ Technologies UAB <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace ONGR\ElasticsearchBundle\Test; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* A helper for testing setters/getters. |
16
|
|
|
* |
17
|
|
|
* Trait EncapsulationTestAwareTrait |
18
|
|
|
*/ |
19
|
|
|
trait EncapsulationTestAwareTrait |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var string[] List of fields that should not be checked for tests. |
23
|
|
|
*/ |
24
|
|
|
private $ignoredFields = []; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var mixed Stub object to test. |
28
|
|
|
*/ |
29
|
|
|
private $stub = null; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @return mixed |
33
|
|
|
*/ |
34
|
|
|
public function getStub() |
35
|
|
|
{ |
36
|
|
|
return $this->stub; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param mixed $stub |
41
|
|
|
*/ |
42
|
|
|
public function setStub($stub) |
43
|
|
|
{ |
44
|
|
|
$this->stub = $stub; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Returns list of fields to test. Works as data provider. |
49
|
|
|
* |
50
|
|
|
* @return array |
51
|
|
|
*/ |
52
|
|
|
abstract public function getFieldsData(); |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Returns entity class name. |
56
|
|
|
* |
57
|
|
|
* @return string |
58
|
|
|
*/ |
59
|
|
|
abstract public function getClassName(); |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Returns list of fields that should not be checked for tests. |
63
|
|
|
* |
64
|
|
|
* @return array |
65
|
|
|
*/ |
66
|
|
|
protected function getIgnoredFields() |
67
|
|
|
{ |
68
|
|
|
return $this->ignoredFields; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Set list of fields that should not be checked for tests. |
73
|
|
|
* |
74
|
|
|
* @param string[] $fields |
75
|
|
|
*/ |
76
|
|
|
protected function setIgnoredFields(array $fields) |
77
|
|
|
{ |
78
|
|
|
$this->ignoredFields = $fields; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Set list of fields that should not be checked for tests. |
83
|
|
|
* |
84
|
|
|
* @param string $field |
85
|
|
|
*/ |
86
|
|
|
protected function addIgnoredField($field) |
87
|
|
|
{ |
88
|
|
|
$this->ignoredFields[] = $field; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Tests field setter and getter. |
93
|
|
|
* |
94
|
|
|
* @param string $field |
95
|
|
|
* @param null|string $type |
96
|
|
|
* @param null|string $addMethod |
97
|
|
|
* @param null|string $removeMethod |
98
|
|
|
* @param null|string[] $additionalSetter |
99
|
|
|
* |
100
|
|
|
* @dataProvider getFieldsData() |
101
|
|
|
*/ |
102
|
|
|
public function testSetterGetter( |
103
|
|
|
$field, |
104
|
|
|
$type = null, |
105
|
|
|
$addMethod = null, |
106
|
|
|
$removeMethod = null, |
107
|
|
|
$additionalSetter = null |
108
|
|
|
) { |
109
|
|
|
/** @var \PHPUnit_Framework_TestCase|EncapsulationTestAwareTrait $this */ |
110
|
|
|
|
111
|
|
|
$objectClass = $this->getClassName(); |
112
|
|
|
|
113
|
|
|
$setter = 'set' . ucfirst($field); |
114
|
|
|
$getter = 'get' . ucfirst($field); |
115
|
|
|
|
116
|
|
|
if ($type === 'boolean') { |
117
|
|
|
$getter = 'is' . ucfirst($field); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
$stub = $this->getStub(); |
121
|
|
|
|
122
|
|
|
if ($stub === null) { |
123
|
|
|
$stub = $this->getMockForAbstractClass($objectClass); |
|
|
|
|
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
$this->validate($stub, $getter, $setter, $addMethod, $removeMethod, $additionalSetter); |
127
|
|
|
|
128
|
|
|
$expectedObject = $this->getExpectedVariable($type); |
129
|
|
|
|
130
|
|
|
if ($type && class_exists($type)) { |
|
|
|
|
131
|
|
|
$hash = spl_object_hash($expectedObject); |
132
|
|
|
|
133
|
|
|
if ($addMethod) { |
|
|
|
|
134
|
|
|
$stub->$addMethod($expectedObject); |
135
|
|
|
|
136
|
|
|
foreach ($stub->$getter() as $collectionObject) { |
137
|
|
|
$this->assertEquals($hash, spl_object_hash($collectionObject)); |
|
|
|
|
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
if ($removeMethod) { |
|
|
|
|
142
|
|
|
$stub->$removeMethod($expectedObject); |
143
|
|
|
$this->assertEquals(0, count($stub->$getter())); |
|
|
|
|
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
$stub->$setter($expectedObject); |
147
|
|
|
$this->assertEquals($hash, spl_object_hash($stub->$getter())); |
|
|
|
|
148
|
|
|
} else { |
149
|
|
|
$stub->$setter($expectedObject); |
150
|
|
|
$this->assertEquals($expectedObject, $stub->$getter()); |
|
|
|
|
151
|
|
|
|
152
|
|
|
if ($addMethod) { |
|
|
|
|
153
|
|
|
$stub->$addMethod($this->getExpectedVariable(null)); |
154
|
|
|
$this->assertEquals(2, count($stub->$getter())); |
|
|
|
|
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
if ($additionalSetter) { |
159
|
|
|
$stub = $this->getMockForAbstractClass($objectClass); |
|
|
|
|
160
|
|
|
$setter = key($additionalSetter); |
161
|
|
|
$stub->$setter($additionalSetter[$setter][0]); |
162
|
|
|
$this->assertEquals($additionalSetter[$setter][1], $stub->$getter()); |
|
|
|
|
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Tests if all entity fields are registered. |
168
|
|
|
*/ |
169
|
|
|
public function testAllEntityFieldsRegistered() |
170
|
|
|
{ |
171
|
|
|
/** @var \PHPUnit_Framework_TestCase|EncapsulationTestAwareTrait $this */ |
172
|
|
|
|
173
|
|
|
$reflect = new \ReflectionClass($this->getClassName()); |
174
|
|
|
$properties = $reflect->getProperties(); |
175
|
|
|
|
176
|
|
|
$fields = []; |
177
|
|
|
|
178
|
|
|
/** @var \ReflectionProperty $property */ |
179
|
|
|
foreach ($properties as $property) { |
180
|
|
|
$fields[] = $property->getName(); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
$parentClass = $reflect->getParentClass(); |
184
|
|
|
if ($parentClass) { |
185
|
|
|
$parentClassProperties = $parentClass->getProperties(); |
186
|
|
|
/** @var \ReflectionProperty $property */ |
187
|
|
|
foreach ($parentClassProperties as $property) { |
188
|
|
|
$this->addIgnoredField($property->getName()); |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
$traits = $reflect->getTraits(); |
193
|
|
|
if ($traits) { |
|
|
|
|
194
|
|
|
foreach ($traits as $trait) { |
195
|
|
|
$traitProperties = $trait->getProperties(); |
196
|
|
|
/** @var \ReflectionProperty $property */ |
197
|
|
|
foreach ($traitProperties as $property) { |
198
|
|
|
$this->addIgnoredField($property->getName()); |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
$registeredFields = []; |
204
|
|
|
|
205
|
|
|
foreach ($this->getFieldsData() as $data) { |
206
|
|
|
$registeredFields[] = $data[0]; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
$diff = array_diff($fields, $registeredFields, $this->getIgnoredFields()); |
210
|
|
|
|
211
|
|
|
if (count($diff) !== 0) { |
212
|
|
|
$this->fail( |
|
|
|
|
213
|
|
|
sprintf( |
214
|
|
|
'All entity fields must be registered in test. Please check field(s) "%s".', |
215
|
|
|
implode('", "', $diff) |
216
|
|
|
) |
217
|
|
|
); |
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Return expected variable for compare. |
223
|
|
|
* |
224
|
|
|
* @param null|string $type |
225
|
|
|
* |
226
|
|
|
* @return object |
227
|
|
|
*/ |
228
|
|
|
protected function getExpectedVariable($type) |
229
|
|
|
{ |
230
|
|
|
/** @var \PHPUnit_Framework_TestCase|EncapsulationTestAwareTrait $this */ |
231
|
|
|
|
232
|
|
|
if ($type === null || $type == 'boolean') { |
233
|
|
|
return rand(0, 9999); |
234
|
|
|
} elseif ($type == 'string') { |
235
|
|
|
return 'string' . rand(0, 9999); |
236
|
|
|
} elseif ($type == 'array') { |
237
|
|
|
return [rand(0, 9999)]; |
238
|
|
|
} elseif ($type == '\DateTime') { |
239
|
|
|
return new \DateTime(); |
240
|
|
|
} elseif (class_exists($type) || interface_exists($type)) { |
241
|
|
|
return $this->getMockForAbstractClass($type); |
|
|
|
|
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
return null; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* Validate class before test. |
249
|
|
|
* |
250
|
|
|
* @param \PHPUnit_Framework_MockObject_MockObject $stub |
251
|
|
|
* @param null|string $getter |
252
|
|
|
* @param null|string $setter |
253
|
|
|
* @param null|string $addMethod |
254
|
|
|
* @param null|string $removeMethod |
255
|
|
|
* @param null|string[] $additionalSetter |
256
|
|
|
*/ |
257
|
|
|
protected function validate($stub, $getter, $setter, $addMethod, $removeMethod, $additionalSetter) |
258
|
|
|
{ |
259
|
|
|
/** @var \PHPUnit_Framework_TestCase|EncapsulationTestAwareTrait $this */ |
260
|
|
|
|
261
|
|
|
$this->assertTrue(method_exists($stub, $setter), "Method ${setter}() not found!"); |
|
|
|
|
262
|
|
|
$this->assertTrue(method_exists($stub, $getter), "Method ${getter}() not found!"); |
|
|
|
|
263
|
|
|
|
264
|
|
|
if ($addMethod) { |
|
|
|
|
265
|
|
|
$this->assertTrue(method_exists($stub, $addMethod), "Method ${addMethod}() not found!"); |
|
|
|
|
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
if ($removeMethod) { |
|
|
|
|
269
|
|
|
$this->assertTrue(method_exists($stub, $removeMethod), "Method ${removeMethod}() not found!"); |
|
|
|
|
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
if ($additionalSetter) { |
273
|
|
|
$setter = key($additionalSetter); |
274
|
|
|
$this->assertTrue(method_exists($stub, $setter), "Method ${setter}() not found!"); |
|
|
|
|
275
|
|
|
} |
276
|
|
|
} |
277
|
|
|
} |
278
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.