|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace GeneratedHydratorTest\ClosureGenerator\FileCache; |
|
6
|
|
|
|
|
7
|
|
|
use GeneratedHydrator\ClosureGenerator\FileCache\GenerateFileCacheHydrator; |
|
8
|
|
|
use GeneratedHydratorTestAsset\BaseClass; |
|
9
|
|
|
use GeneratedHydratorTestAsset\ClassWithPrivatePropertiesAndParents; |
|
10
|
|
|
use PHPUnit\Framework\TestCase; |
|
11
|
|
|
use ReflectionClass; |
|
12
|
|
|
use ReflectionException; |
|
13
|
|
|
|
|
14
|
|
|
class GenerateHydratorTest extends TestCase |
|
15
|
|
|
{ |
|
16
|
|
|
/** @var GenerateFileCacheHydrator */ |
|
17
|
|
|
private $generateHydrator; |
|
18
|
|
|
|
|
19
|
|
|
public function setUp() : void |
|
20
|
|
|
{ |
|
21
|
|
|
parent::setUp(); |
|
22
|
|
|
$this->generateHydrator = new GenerateFileCacheHydrator("build/"); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @throws ReflectionException |
|
27
|
|
|
*/ |
|
28
|
|
|
public function testDefaultBaseClass() : void |
|
29
|
|
|
{ |
|
30
|
|
|
$hydrate = ($this->generateHydrator)(BaseClass::class); |
|
31
|
|
|
$object = new BaseClass(); |
|
32
|
|
|
|
|
33
|
|
|
$result = $hydrate( |
|
34
|
|
|
[ |
|
35
|
|
|
'publicProperty' => 'publicPropertyNew', |
|
36
|
|
|
'protectedProperty' => 'protectedPropertyNew', |
|
37
|
|
|
'privateProperty' => 'privatePropertyNew', |
|
38
|
|
|
], |
|
39
|
|
|
$object |
|
40
|
|
|
); |
|
41
|
|
|
|
|
42
|
|
|
self::assertSame( |
|
43
|
|
|
[ |
|
44
|
|
|
'publicProperty' => 'publicPropertyNew', |
|
45
|
|
|
'protectedProperty' => 'protectedPropertyNew', |
|
46
|
|
|
'privateProperty' => 'privatePropertyNew', |
|
47
|
|
|
], |
|
48
|
|
|
$this->getProperties($result) |
|
49
|
|
|
); |
|
50
|
|
|
} |
|
51
|
|
|
/** |
|
52
|
|
|
* @throws ReflectionException |
|
53
|
|
|
*/ |
|
54
|
|
|
public function testClassWithParents() : void |
|
55
|
|
|
{ |
|
56
|
|
|
$object = new ClassWithPrivatePropertiesAndParents(); |
|
57
|
|
|
$hydrate = ($this->generateHydrator)(get_class($object)); |
|
58
|
|
|
|
|
59
|
|
|
$hydrate( |
|
60
|
|
|
[ |
|
61
|
|
|
'property0' => 'property0_new', |
|
62
|
|
|
'property1' => 'property1_new', |
|
63
|
|
|
'property2' => 'property2_new', |
|
64
|
|
|
'property3' => 'property3_new', |
|
65
|
|
|
'property4' => 'property4_new', |
|
66
|
|
|
'property5' => 'property5_new', |
|
67
|
|
|
'property6' => 'property6_new', |
|
68
|
|
|
'property7' => 'property7_new', |
|
69
|
|
|
'property8' => 'property8_new', |
|
70
|
|
|
'property9' => 'property9_new', |
|
71
|
|
|
'property20' => 'property20_new', |
|
72
|
|
|
'property21' => 'property21_new', |
|
73
|
|
|
'property22' => 'property22_new', |
|
74
|
|
|
'property30' => 'property30_new', |
|
75
|
|
|
'property31' => 'property31_new', |
|
76
|
|
|
'property32' => 'property32_new', |
|
77
|
|
|
], |
|
78
|
|
|
$object |
|
79
|
|
|
); |
|
80
|
|
|
self::assertSame( |
|
81
|
|
|
[ |
|
82
|
|
|
'property0' => 'property0_new', |
|
83
|
|
|
'property1' => 'property1_new', |
|
84
|
|
|
'property2' => 'property2_new', |
|
85
|
|
|
'property3' => 'property3_new', |
|
86
|
|
|
'property4' => 'property4_new', |
|
87
|
|
|
'property5' => 'property5_new', |
|
88
|
|
|
'property6' => 'property6_new', |
|
89
|
|
|
'property7' => 'property7_new', |
|
90
|
|
|
'property8' => 'property8_new', |
|
91
|
|
|
'property9' => 'property9_new', |
|
92
|
|
|
'property20' => 'property20_new', |
|
93
|
|
|
'property21' => 'property21_new', |
|
94
|
|
|
'property22' => 'property22_new', |
|
95
|
|
|
'property30' => 'property30_new', |
|
96
|
|
|
'property31' => 'property31_new', |
|
97
|
|
|
'property32' => 'property32_new', |
|
98
|
|
|
], |
|
99
|
|
|
$this->getProperties($object) |
|
|
|
|
|
|
100
|
|
|
); |
|
101
|
|
|
} |
|
102
|
|
|
/** |
|
103
|
|
|
* @return mixed[] |
|
104
|
|
|
* |
|
105
|
|
|
* @throws ReflectionException |
|
106
|
|
|
*/ |
|
107
|
|
|
private function getProperties(object $object) : array |
|
108
|
|
|
{ |
|
109
|
|
|
$reflectionClass = new ReflectionClass($object); |
|
110
|
|
|
|
|
111
|
|
|
return $this->getPropertiesWithReflection($object, $reflectionClass); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @return mixed[] |
|
116
|
|
|
*/ |
|
117
|
|
|
private function getPropertiesWithReflection(object $object, ReflectionClass $reflectionClass) : array |
|
118
|
|
|
{ |
|
119
|
|
|
$properties = $reflectionClass->getParentClass() |
|
120
|
|
|
? $this->getPropertiesWithReflection($object, $reflectionClass->getParentClass()) |
|
121
|
|
|
: []; |
|
122
|
|
|
foreach ($reflectionClass->getProperties() as $reflectionProperty) { |
|
123
|
|
|
if ($reflectionProperty->isStatic()) { |
|
124
|
|
|
continue; |
|
125
|
|
|
} |
|
126
|
|
|
if ($reflectionProperty->isPrivate() || $reflectionProperty->isProtected()) { |
|
127
|
|
|
$reflectionProperty->setAccessible(true); |
|
128
|
|
|
} |
|
129
|
|
|
$properties[$reflectionProperty->getName()] = $reflectionProperty->getValue($object); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
return $properties; |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: