1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
/** |
4
|
|
|
* /src/Utils/Tests/PHPUnitUtil.php |
5
|
|
|
* |
6
|
|
|
* @author TLe, Tarmo Leppänen <[email protected]> |
7
|
|
|
*/ |
8
|
|
|
namespace App\Utils\Tests; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class PHPUnitUtil |
12
|
|
|
* |
13
|
|
|
* @package App\Utils\Tests |
14
|
|
|
* @author TLe, Tarmo Leppänen <[email protected]> |
15
|
|
|
*/ |
16
|
|
|
class PHPUnitUtil |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @codeCoverageIgnore |
20
|
|
|
* |
21
|
|
|
* @param string $folder |
22
|
|
|
* @param string $pattern |
23
|
|
|
* |
24
|
|
|
* @return array |
25
|
|
|
*/ |
26
|
|
|
public static function recursiveFileSearch(string $folder, string $pattern): array |
27
|
|
|
{ |
28
|
|
|
$dir = new \RecursiveDirectoryIterator($folder); |
29
|
|
|
$ite = new \RecursiveIteratorIterator($dir); |
30
|
|
|
|
31
|
|
|
$files = new \RegexIterator($ite, $pattern, \RegexIterator::GET_MATCH); |
32
|
|
|
$fileList = array(); |
33
|
|
|
|
34
|
|
|
foreach ($files as $file) { |
35
|
|
|
$fileList[] = $file[0]; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
return $fileList; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Method to call specified 'protected' or 'private' method on given class. |
43
|
|
|
* |
44
|
|
|
* @param mixed $object The instantiated instance of your class |
45
|
|
|
* @param string $name The name of your private/protected method |
46
|
|
|
* @param array $args Method arguments |
47
|
|
|
* |
48
|
|
|
* @return mixed |
49
|
|
|
*/ |
50
|
|
|
public static function callMethod($object, string $name, array $args) |
51
|
|
|
{ |
52
|
|
|
return self::getMethod($object, $name)->invokeArgs($object, $args); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Get a private or protected method for testing/documentation purposes. |
57
|
|
|
* How to use for MyClass->foo(): |
58
|
|
|
* $cls = new MyClass(); |
59
|
|
|
* $foo = PHPUnitUtil::getPrivateMethod($cls, 'foo'); |
60
|
|
|
* $foo->invoke($cls, $...); |
61
|
|
|
* |
62
|
|
|
* @param mixed $object The instantiated instance of your class |
63
|
|
|
* @param string $name The name of your private/protected method |
64
|
|
|
* |
65
|
|
|
* @return \ReflectionMethod The method you asked for |
66
|
|
|
*/ |
67
|
|
|
public static function getMethod($object, string $name): \ReflectionMethod |
68
|
|
|
{ |
69
|
|
|
// Get reflection and make specified method accessible |
70
|
|
|
$class = new \ReflectionClass($object); |
71
|
|
|
$method = $class->getMethod($name); |
72
|
|
|
$method->setAccessible(true); |
73
|
|
|
|
74
|
|
|
return $method; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Helper method to get any property value from given class. |
79
|
|
|
* |
80
|
|
|
* @param string $property |
81
|
|
|
* @param mixed $object |
82
|
|
|
* |
83
|
|
|
* @return mixed |
84
|
|
|
*/ |
85
|
|
|
public static function getProperty(string $property, $object) |
86
|
|
|
{ |
87
|
|
|
$clazz = new \ReflectionClass(\get_class($object)); |
88
|
|
|
|
89
|
|
|
/** @noinspection CallableParameterUseCaseInTypeContextInspection */ |
90
|
|
|
$property = $clazz->getProperty($property); |
91
|
|
|
$property->setAccessible(true); |
92
|
|
|
|
93
|
|
|
return $property->getValue($object); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param \Doctrine\DBAL\Types\Type|string|null $type |
98
|
|
|
* |
99
|
|
|
* @return string |
100
|
|
|
*/ |
101
|
|
|
public static function getType($type): string |
102
|
|
|
{ |
103
|
|
|
switch ($type) { |
104
|
|
|
case 'integer': |
105
|
|
|
case 'bigint': |
106
|
|
|
$output = 'integer'; |
107
|
|
|
break; |
108
|
|
|
case 'time': |
109
|
|
|
case 'date': |
110
|
|
|
case 'datetime': |
111
|
|
|
$output = \DateTime::class; |
112
|
|
|
break; |
113
|
|
|
case 'text': |
114
|
|
|
case 'string': |
115
|
|
|
case 'EnumLogLogin': |
116
|
|
|
$output = 'string'; |
117
|
|
|
break; |
118
|
|
|
case 'array': |
119
|
|
|
$output = 'array'; |
120
|
|
|
break; |
121
|
|
|
case 'boolean': |
122
|
|
|
$output = 'boolean'; |
123
|
|
|
break; |
124
|
|
|
default: |
125
|
|
|
$message = \sprintf( |
126
|
|
|
"Currently type '%s' is not supported within type normalizer", |
127
|
|
|
$type |
128
|
|
|
); |
129
|
|
|
|
130
|
|
|
throw new \LogicException($message); |
131
|
|
|
break; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return $output; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Helper method to override any property value within given class. |
139
|
|
|
* |
140
|
|
|
* @param string $property |
141
|
|
|
* @param mixed $value |
142
|
|
|
* @param mixed $object |
143
|
|
|
*/ |
144
|
|
|
public static function setProperty(string $property, $value, $object): void |
145
|
|
|
{ |
146
|
|
|
$clazz = new \ReflectionClass(\get_class($object)); |
147
|
|
|
|
148
|
|
|
/** @noinspection CallableParameterUseCaseInTypeContextInspection */ |
149
|
|
|
$property = $clazz->getProperty($property); |
150
|
|
|
$property->setAccessible(true); |
151
|
|
|
$property->setValue($object, $value); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Helper method to get valid value for specified type. |
156
|
|
|
* |
157
|
|
|
* @param string $type |
158
|
|
|
* @param array|null $meta |
159
|
|
|
* |
160
|
|
|
* @return mixed |
161
|
|
|
*/ |
162
|
|
|
public static function getValidValueForType(string $type, array $meta = null) |
163
|
|
|
{ |
164
|
|
|
$meta = $meta ?? []; |
165
|
|
|
|
166
|
|
|
$class = \stdClass::class; |
167
|
|
|
|
168
|
|
|
if (\substr_count($type, '\\') > 1) { |
169
|
|
|
$class = \count($meta) ? $meta['targetEntity'] : $type; |
170
|
|
|
|
171
|
|
|
$type = 'CustomClass'; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
if (\strpos($type, '|') !== false) { |
175
|
|
|
$output = self::getValidValueForType(\explode('|', $type)[0], $meta); |
176
|
|
|
} else { |
177
|
|
|
switch ($type) { |
178
|
|
|
case 'CustomClass': |
179
|
|
|
$output = new $class(); |
180
|
|
|
break; |
181
|
|
|
case 'integer': |
182
|
|
|
$output = 666; |
183
|
|
|
break; |
184
|
|
|
case \DateTime::class: |
185
|
|
|
$output = new \DateTime(); |
186
|
|
|
break; |
187
|
|
|
case 'string': |
188
|
|
|
$output = 'Some text here'; |
189
|
|
|
break; |
190
|
|
|
case 'array': |
191
|
|
|
$output = ['some', 'array', 'here']; |
192
|
|
|
break; |
193
|
|
|
case 'boolean': |
194
|
|
|
$output = true; |
195
|
|
|
break; |
196
|
|
|
default: |
197
|
|
|
$message = \sprintf( |
198
|
|
|
"Cannot create valid value for type '%s'.", |
199
|
|
|
$type |
200
|
|
|
); |
201
|
|
|
|
202
|
|
|
throw new \LogicException($message); |
203
|
|
|
break; |
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
return $output; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Helper method to get invalid value for specified type. |
212
|
|
|
* |
213
|
|
|
* @param string $type |
214
|
|
|
* |
215
|
|
|
* @return mixed |
216
|
|
|
*/ |
217
|
|
|
public static function getInvalidValueForType(string $type) |
218
|
|
|
{ |
219
|
|
|
if ($type !== \stdClass::class && \substr_count($type, '\\') > 1) { |
220
|
|
|
$type = 'CustomClass'; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
if (\strpos($type, '|') !== false) { |
224
|
|
|
$output = self::getInvalidValueForType(\explode('|', $type)[0]); |
225
|
|
|
} else { |
226
|
|
|
switch ($type) { |
227
|
|
|
case \stdClass::class: |
228
|
|
|
$output = new \DateTime(); |
229
|
|
|
break; |
230
|
|
|
case 'CustomClass': |
231
|
|
|
case 'integer': |
232
|
|
|
case \DateTime::class: |
233
|
|
|
case 'string': |
234
|
|
|
case 'array': |
235
|
|
|
case 'boolean': |
236
|
|
|
case 'enumLogLogin': |
237
|
|
|
$output = new \stdClass(); |
238
|
|
|
break; |
239
|
|
|
default: |
240
|
|
|
$message = \sprintf( |
241
|
|
|
"Cannot create invalid value for type '%s'.", |
242
|
|
|
$type |
243
|
|
|
); |
244
|
|
|
|
245
|
|
|
throw new \LogicException($message); |
246
|
|
|
break; |
247
|
|
|
} |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
return $output; |
251
|
|
|
} |
252
|
|
|
} |
253
|
|
|
|