|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace GeneratedHydratorTest\ClosureGenerator\Runtime; |
|
6
|
|
|
|
|
7
|
|
|
use GeneratedHydrator\ClosureGenerator\GenerateHydrator; |
|
8
|
|
|
use GeneratedHydrator\ClosureGenerator\Runtime\GenerateRuntimeExtractor; |
|
9
|
|
|
use GeneratedHydratorTestAsset\BaseClass; |
|
10
|
|
|
use GeneratedHydratorTestAsset\ClassWithPrivatePropertiesAndParents; |
|
11
|
|
|
use GeneratedHydratorTestAsset\ClassWithStaticProperties; |
|
12
|
|
|
use PHPUnit\Framework\TestCase; |
|
13
|
|
|
use ReflectionException; |
|
14
|
|
|
use stdClass; |
|
15
|
|
|
use function get_class; |
|
16
|
|
|
|
|
17
|
|
|
class GenerateExtractorTest extends TestCase |
|
18
|
|
|
{ |
|
19
|
|
|
/** @var GenerateHydrator */ |
|
20
|
|
|
private $generateExtractor; |
|
21
|
|
|
|
|
22
|
|
|
public function setUp() : void |
|
23
|
|
|
{ |
|
24
|
|
|
parent::setUp(); |
|
25
|
|
|
$this->generateExtractor = new GenerateRuntimeExtractor(); |
|
|
|
|
|
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @throws ReflectionException |
|
30
|
|
|
*/ |
|
31
|
|
|
public function testDefaultBaseClass() : void |
|
32
|
|
|
{ |
|
33
|
|
|
$object = new BaseClass(); |
|
34
|
|
|
$extractor = ($this->generateExtractor)(get_class($object)); |
|
35
|
|
|
|
|
36
|
|
|
$result = $extractor($object); |
|
37
|
|
|
|
|
38
|
|
|
self::assertEquals( |
|
39
|
|
|
[ |
|
40
|
|
|
'publicProperty' => 'publicPropertyDefault', |
|
41
|
|
|
'protectedProperty' => 'protectedPropertyDefault', |
|
42
|
|
|
'privateProperty' => 'privatePropertyDefault', |
|
43
|
|
|
], |
|
44
|
|
|
$result |
|
45
|
|
|
); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @throws ReflectionException |
|
50
|
|
|
*/ |
|
51
|
|
|
public function testClassWithParents() : void |
|
52
|
|
|
{ |
|
53
|
|
|
$object = new ClassWithPrivatePropertiesAndParents(); |
|
54
|
|
|
$extractor = ($this->generateExtractor)(get_class($object)); |
|
55
|
|
|
|
|
56
|
|
|
$result = $extractor($object); |
|
57
|
|
|
|
|
58
|
|
|
self::assertEquals( |
|
59
|
|
|
[ |
|
60
|
|
|
'property0' => 'property0_fromChild', |
|
61
|
|
|
'property1' => 'property1_fromChild', |
|
62
|
|
|
'property2' => 'property2', |
|
63
|
|
|
'property3' => 'property3', |
|
64
|
|
|
'property4' => 'property4', |
|
65
|
|
|
'property5' => 'property5', |
|
66
|
|
|
'property6' => 'property6', |
|
67
|
|
|
'property7' => 'property7', |
|
68
|
|
|
'property8' => 'property8', |
|
69
|
|
|
'property9' => 'property9', |
|
70
|
|
|
'property20' => 'property20_fromChild', |
|
71
|
|
|
'property21' => 'property21', |
|
72
|
|
|
'property22' => 'property22', |
|
73
|
|
|
'property30' => 'property30', |
|
74
|
|
|
'property31' => 'property31', |
|
75
|
|
|
'property32' => 'property32', |
|
76
|
|
|
], |
|
77
|
|
|
$result |
|
78
|
|
|
); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @throws ReflectionException |
|
83
|
|
|
*/ |
|
84
|
|
|
public function testStdClass() : void |
|
85
|
|
|
{ |
|
86
|
|
|
$object = new stdClass(); |
|
87
|
|
|
$extractor = ($this->generateExtractor)(stdClass::class); |
|
88
|
|
|
|
|
89
|
|
|
$result = $extractor($object); |
|
90
|
|
|
|
|
91
|
|
|
self::assertEquals([], $result); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @throws ReflectionException |
|
96
|
|
|
*/ |
|
97
|
|
|
public function testClassWithStaticProperties() : void |
|
98
|
|
|
{ |
|
99
|
|
|
$object = new ClassWithStaticProperties(); |
|
100
|
|
|
$extractor = ($this->generateExtractor)(ClassWithStaticProperties::class); |
|
101
|
|
|
|
|
102
|
|
|
$result = $extractor($object); |
|
103
|
|
|
|
|
104
|
|
|
self::assertEquals( |
|
105
|
|
|
[ |
|
106
|
|
|
'private' => null, |
|
107
|
|
|
'protected' => null, |
|
108
|
|
|
'public' => null, |
|
109
|
|
|
], |
|
110
|
|
|
$result |
|
111
|
|
|
); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @throws ReflectionException |
|
116
|
|
|
*/ |
|
117
|
|
|
public function testCache() : void |
|
118
|
|
|
{ |
|
119
|
|
|
$object = new BaseClass(); |
|
120
|
|
|
$extractor = ($this->generateExtractor)(get_class($object)); |
|
121
|
|
|
|
|
122
|
|
|
self::assertSame($extractor, ($this->generateExtractor)(get_class($object))); |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..