1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DeepCopyTest\Reflection; |
4
|
|
|
|
5
|
|
|
use DeepCopy\Reflection\ReflectionHelper; |
6
|
|
|
use PHPUnit_Framework_TestCase; |
7
|
|
|
use ReflectionClass; |
8
|
|
|
use ReflectionProperty; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @covers \DeepCopy\Reflection\ReflectionHelper |
12
|
|
|
*/ |
13
|
|
|
class ReflectionHelperTest extends PHPUnit_Framework_TestCase |
14
|
|
|
{ |
15
|
|
|
public function test_it_retrieves_the_object_properties() |
16
|
|
|
{ |
17
|
|
|
$child = new ReflectionHelperTestChild(); |
18
|
|
|
$childReflectionClass = new ReflectionClass($child); |
19
|
|
|
|
20
|
|
|
$expectedProps = array( |
21
|
|
|
'a1', |
22
|
|
|
'a2', |
23
|
|
|
'a3', |
24
|
|
|
'a10', |
25
|
|
|
'a11', |
26
|
|
|
'a12', |
27
|
|
|
'a20', |
28
|
|
|
'a21', |
29
|
|
|
'a22', |
30
|
|
|
'a100', |
31
|
|
|
'a101', |
32
|
|
|
'a102', |
33
|
|
|
); |
34
|
|
|
|
35
|
|
|
$actualProps = ReflectionHelper::getProperties($childReflectionClass); |
36
|
|
|
|
37
|
|
|
$this->assertSame($expectedProps, array_keys($actualProps)); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @dataProvider provideProperties |
42
|
|
|
*/ |
43
|
|
|
public function test_it_can_retrieve_an_object_property($name) |
44
|
|
|
{ |
45
|
|
|
$object = new ReflectionHelperTestChild(); |
46
|
|
|
|
47
|
|
|
$property = ReflectionHelper::getProperty($object, $name); |
48
|
|
|
|
49
|
|
|
$this->assertInstanceOf(ReflectionProperty::class, $property); |
50
|
|
|
|
51
|
|
|
$this->assertSame($name, $property->getName()); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function provideProperties() |
55
|
|
|
{ |
56
|
|
|
return [ |
57
|
|
|
'public property' => ['a10'], |
58
|
|
|
'private property' => ['a102'], |
59
|
|
|
'private property of ancestor' => ['a3'] |
60
|
|
|
]; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @expectedException \DeepCopy\Exception\PropertyException |
65
|
|
|
*/ |
66
|
|
|
public function test_it_cannot_retrieve_a_non_existent_prperty() |
67
|
|
|
{ |
68
|
|
|
$object = new ReflectionHelperTestChild(); |
69
|
|
|
|
70
|
|
|
ReflectionHelper::getProperty($object, 'non existent property'); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
class ReflectionHelperTestParent |
75
|
|
|
{ |
76
|
|
|
public $a1; |
77
|
|
|
protected $a2; |
78
|
|
|
private $a3; |
|
|
|
|
79
|
|
|
|
80
|
|
|
public $a10; |
81
|
|
|
protected $a11; |
82
|
|
|
private $a12; |
|
|
|
|
83
|
|
|
|
84
|
|
|
public static $a20; |
85
|
|
|
protected static $a21; |
86
|
|
|
private static $a22; |
|
|
|
|
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
class ReflectionHelperTestChild extends ReflectionHelperTestParent |
90
|
|
|
{ |
91
|
|
|
public $a1; |
92
|
|
|
protected $a2; |
93
|
|
|
private $a3; |
|
|
|
|
94
|
|
|
|
95
|
|
|
public $a100; |
96
|
|
|
protected $a101; |
97
|
|
|
private $a102; |
|
|
|
|
98
|
|
|
} |
99
|
|
|
|
This check marks private properties in classes that are never used. Those properties can be removed.