|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace RoaveTest\BackwardCompatibility\DetectChanges\BCBreak\ClassBased; |
|
6
|
|
|
|
|
7
|
|
|
use PHPUnit\Framework\TestCase; |
|
8
|
|
|
use Roave\BackwardCompatibility\Change; |
|
9
|
|
|
use Roave\BackwardCompatibility\Changes; |
|
10
|
|
|
use Roave\BackwardCompatibility\DetectChanges\BCBreak\ClassBased\PropertyChanged; |
|
11
|
|
|
use Roave\BackwardCompatibility\DetectChanges\BCBreak\PropertyBased\PropertyBased; |
|
12
|
|
|
use Roave\BetterReflection\BetterReflection; |
|
13
|
|
|
use Roave\BetterReflection\Reflection\ReflectionProperty; |
|
14
|
|
|
use Roave\BetterReflection\Reflector\ClassReflector; |
|
15
|
|
|
use Roave\BetterReflection\SourceLocator\Type\StringSourceLocator; |
|
16
|
|
|
use RoaveTest\BackwardCompatibility\Assertion; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @covers \Roave\BackwardCompatibility\DetectChanges\BCBreak\ClassBased\PropertyChanged |
|
20
|
|
|
*/ |
|
21
|
|
|
final class PropertyChangedTest extends TestCase |
|
22
|
|
|
{ |
|
23
|
|
|
public function testWillDetectChangesInProperties() : void |
|
24
|
|
|
{ |
|
25
|
|
|
$astLocator = (new BetterReflection())->astLocator(); |
|
26
|
|
|
|
|
27
|
|
|
$fromLocator = new StringSourceLocator( |
|
28
|
|
|
<<<'PHP' |
|
29
|
|
|
<?php |
|
30
|
|
|
|
|
31
|
|
|
class TheClass { |
|
32
|
|
|
public $a; |
|
33
|
|
|
protected $b; |
|
34
|
|
|
private $c; |
|
35
|
|
|
public static $d; |
|
36
|
|
|
public $G; |
|
37
|
|
|
} |
|
38
|
|
|
PHP |
|
39
|
|
|
, |
|
40
|
|
|
$astLocator |
|
41
|
|
|
); |
|
42
|
|
|
|
|
43
|
|
|
$toLocator = new StringSourceLocator( |
|
44
|
|
|
<<<'PHP' |
|
45
|
|
|
<?php |
|
46
|
|
|
|
|
47
|
|
|
class TheClass { |
|
48
|
|
|
protected $b; |
|
49
|
|
|
public static $d; |
|
50
|
|
|
public $e; |
|
51
|
|
|
public $f; |
|
52
|
|
|
public $g; |
|
53
|
|
|
} |
|
54
|
|
|
PHP |
|
55
|
|
|
, |
|
56
|
|
|
$astLocator |
|
57
|
|
|
); |
|
58
|
|
|
|
|
59
|
|
|
$comparator = $this->createMock(PropertyBased::class); |
|
60
|
|
|
|
|
61
|
|
|
$comparator |
|
62
|
|
|
->expects(self::exactly(2)) |
|
63
|
|
|
->method('__invoke') |
|
64
|
|
|
->willReturnCallback(static function (ReflectionProperty $from, ReflectionProperty $to) : Changes { |
|
65
|
|
|
$propertyName = $from->getName(); |
|
66
|
|
|
|
|
67
|
|
|
self::assertSame($propertyName, $to->getName()); |
|
68
|
|
|
|
|
69
|
|
|
return Changes::fromList(Change::added($propertyName, true)); |
|
70
|
|
|
}); |
|
71
|
|
|
|
|
72
|
|
|
Assertion::assertChangesEqual( |
|
73
|
|
|
Changes::fromList( |
|
74
|
|
|
Change::added('b', true), |
|
75
|
|
|
Change::added('d', true) |
|
76
|
|
|
), |
|
77
|
|
|
(new PropertyChanged($comparator))->__invoke( |
|
78
|
|
|
(new ClassReflector($fromLocator))->reflect('TheClass'), |
|
79
|
|
|
(new ClassReflector($toLocator))->reflect('TheClass') |
|
80
|
|
|
) |
|
81
|
|
|
); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @dataProvider finalClassProvider |
|
86
|
|
|
*/ |
|
87
|
|
|
public function testWillIgnoreChangeInFinalClass($fromLocator, $toLocator) : void |
|
88
|
|
|
{ |
|
89
|
|
|
|
|
90
|
|
|
|
|
91
|
|
|
$comparator = $this->createMock(PropertyBased::class); |
|
92
|
|
|
|
|
93
|
|
|
$comparator |
|
94
|
|
|
->expects(self::exactly(2)) |
|
95
|
|
|
->method('__invoke') |
|
96
|
|
|
->willReturnCallback(static function (ReflectionProperty $from, ReflectionProperty $to) : Changes { |
|
97
|
|
|
$propertyName = $from->getName(); |
|
98
|
|
|
|
|
99
|
|
|
self::assertSame($propertyName, $to->getName()); |
|
100
|
|
|
|
|
101
|
|
|
return Changes::fromList(Change::added($propertyName, true)); |
|
102
|
|
|
}); |
|
103
|
|
|
|
|
104
|
|
|
Assertion::assertChangesEqual( |
|
105
|
|
|
Changes::empty(), |
|
106
|
|
|
(new PropertyChanged($comparator))->__invoke( |
|
107
|
|
|
(new ClassReflector($fromLocator))->reflect('TheClass'), |
|
108
|
|
|
(new ClassReflector($toLocator))->reflect('TheClass') |
|
109
|
|
|
) |
|
110
|
|
|
); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
public function finalClassProvider() : array |
|
114
|
|
|
{ |
|
115
|
|
|
$astLocator = (new BetterReflection())->astLocator(); |
|
116
|
|
|
|
|
117
|
|
|
return [ |
|
118
|
|
|
'final class' => [ |
|
119
|
|
|
new StringSourceLocator( |
|
120
|
|
|
<<<'PHP' |
|
121
|
|
|
<?php |
|
122
|
|
|
|
|
123
|
|
|
final class TheClass { |
|
124
|
|
|
protected $b; |
|
125
|
|
|
} |
|
126
|
|
|
PHP |
|
127
|
|
|
, |
|
128
|
|
|
$astLocator |
|
129
|
|
|
), |
|
130
|
|
|
new StringSourceLocator( |
|
131
|
|
|
<<<'PHP' |
|
132
|
|
|
<?php |
|
133
|
|
|
final class TheClass { |
|
134
|
|
|
private $b; |
|
135
|
|
|
} |
|
136
|
|
|
PHP |
|
137
|
|
|
, |
|
138
|
|
|
$astLocator |
|
139
|
|
|
), |
|
140
|
|
|
], |
|
141
|
|
|
'final class extending baseclass' => [ |
|
142
|
|
|
new StringSourceLocator( |
|
143
|
|
|
<<<'PHP' |
|
144
|
|
|
<?php |
|
145
|
|
|
abstract class baseClass {} |
|
146
|
|
|
|
|
147
|
|
|
final class TheClass extends baseClass { |
|
148
|
|
|
protected $b; |
|
149
|
|
|
} |
|
150
|
|
|
PHP |
|
151
|
|
|
, |
|
152
|
|
|
$astLocator |
|
153
|
|
|
), |
|
154
|
|
|
new StringSourceLocator( |
|
155
|
|
|
<<<'PHP' |
|
156
|
|
|
<?php |
|
157
|
|
|
abstract class baseClass {} |
|
158
|
|
|
|
|
159
|
|
|
final class TheClass extends baseClass{ |
|
160
|
|
|
private $b; |
|
161
|
|
|
} |
|
162
|
|
|
PHP |
|
163
|
|
|
, |
|
164
|
|
|
$astLocator |
|
165
|
|
|
), |
|
166
|
|
|
], |
|
167
|
|
|
]; |
|
168
|
|
|
|
|
169
|
|
|
} |
|
170
|
|
|
} |
|
171
|
|
|
|