|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Tests\Koded\Http; |
|
4
|
|
|
|
|
5
|
|
|
trait AssertionTestSupportTrait |
|
6
|
|
|
{ |
|
7
|
|
|
/** |
|
8
|
|
|
* @param object|string $objectOrString |
|
9
|
|
|
* @param string $propertyName |
|
10
|
|
|
* |
|
11
|
|
|
* @return mixed |
|
12
|
|
|
* @throws \ReflectionException |
|
13
|
|
|
*/ |
|
14
|
|
|
private function getObjectProperty( |
|
15
|
|
|
object|string $objectOrString, |
|
16
|
|
|
string $propertyName): mixed |
|
17
|
|
|
{ |
|
18
|
|
|
try { |
|
19
|
|
|
$proto = new \ReflectionClass($objectOrString); |
|
20
|
|
|
$property = $proto->getProperty($propertyName); |
|
21
|
|
|
$property->setAccessible(true); |
|
22
|
|
|
return $property->getValue($objectOrString); |
|
|
|
|
|
|
23
|
|
|
} catch (\ReflectionException $e) { |
|
24
|
|
|
$this->markTestSkipped('[Reflection Error: ' . $e->getMessage()); |
|
|
|
|
|
|
25
|
|
|
} |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @param object|string $objectOrString |
|
30
|
|
|
* @param array $propertyNames List of property names, or empty for all properties |
|
31
|
|
|
* |
|
32
|
|
|
* @return array |
|
33
|
|
|
*/ |
|
34
|
|
|
private function getObjectProperties( |
|
35
|
|
|
object|string $objectOrString, |
|
36
|
|
|
array $propertyNames = []): array |
|
37
|
|
|
{ |
|
38
|
|
|
try { |
|
39
|
|
|
$properties = (new \ReflectionClass($objectOrString))->getProperties(); |
|
40
|
|
|
|
|
41
|
|
|
if (count($propertyNames) > 0) { |
|
42
|
|
|
$properties = array_filter($properties, function(\ReflectionProperty $property) use ($propertyNames) { |
|
43
|
|
|
return in_array($property->getName(), $propertyNames); |
|
44
|
|
|
}); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
$propertyKeys = array_map(function(\ReflectionProperty $property) { |
|
48
|
|
|
return $property->getName(); |
|
49
|
|
|
}, $properties); |
|
50
|
|
|
|
|
51
|
|
|
return array_combine($propertyKeys, array_map(function(\ReflectionProperty $property) use($objectOrString) { |
|
52
|
|
|
$property->setAccessible(true); |
|
53
|
|
|
return $property->getValue($objectOrString); |
|
|
|
|
|
|
54
|
|
|
}, $properties)); |
|
55
|
|
|
|
|
56
|
|
|
} catch (\ReflectionException $e) { |
|
57
|
|
|
$this->markTestSkipped('[Reflection Error: ' . $e->getMessage()); |
|
|
|
|
|
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|