1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace RoaveTest\BackwardCompatibility\DetectChanges\BCBreak\PropertyBased; |
6
|
|
|
|
7
|
|
|
use PHPUnit\Framework\TestCase; |
8
|
|
|
use Roave\BackwardCompatibility\Change; |
9
|
|
|
use Roave\BackwardCompatibility\DetectChanges\BCBreak\PropertyBased\PropertyVisibilityReduced; |
10
|
|
|
use Roave\BetterReflection\BetterReflection; |
11
|
|
|
use Roave\BetterReflection\Reflection\ReflectionProperty; |
12
|
|
|
use Roave\BetterReflection\Reflector\ClassReflector; |
13
|
|
|
use Roave\BetterReflection\SourceLocator\Type\StringSourceLocator; |
14
|
|
|
use RoaveTest\BackwardCompatibility\TypeRestriction; |
15
|
|
|
use function array_combine; |
16
|
|
|
use function array_keys; |
17
|
|
|
use function array_map; |
18
|
|
|
use function iterator_to_array; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @covers \Roave\BackwardCompatibility\DetectChanges\BCBreak\PropertyBased\PropertyVisibilityReduced |
22
|
|
|
*/ |
23
|
|
|
final class PropertyVisibilityReducedTest extends TestCase |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @param string[] $expectedMessages |
27
|
|
|
* |
28
|
|
|
* @dataProvider propertiesToBeTested |
29
|
|
|
*/ |
30
|
|
|
public function testDiffs( |
31
|
|
|
ReflectionProperty $fromProperty, |
32
|
|
|
ReflectionProperty $toProperty, |
33
|
|
|
array $expectedMessages |
34
|
|
|
) : void { |
35
|
|
|
$changes = (new PropertyVisibilityReduced()) |
36
|
|
|
->__invoke($fromProperty, $toProperty); |
37
|
|
|
|
38
|
|
|
self::assertSame( |
39
|
|
|
$expectedMessages, |
40
|
|
|
array_map(static function (Change $change) : string { |
41
|
|
|
return $change->__toString(); |
42
|
|
|
}, iterator_to_array($changes)) |
43
|
|
|
); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @return array<string, array<int, ReflectionProperty|array<int, string>>> |
48
|
|
|
* |
49
|
|
|
* @psalm-return array<string, array{0: ReflectionProperty, 1: ReflectionProperty, 2: list<string>}> |
50
|
|
|
*/ |
51
|
|
|
public function propertiesToBeTested() : array |
52
|
|
|
{ |
53
|
|
|
$astLocator = (new BetterReflection())->astLocator(); |
54
|
|
|
|
55
|
|
|
$fromLocator = new StringSourceLocator( |
56
|
|
|
<<<'PHP' |
57
|
|
|
<?php |
58
|
|
|
|
59
|
|
|
class TheClass { |
60
|
|
|
public $publicMaintainedPublic; |
61
|
|
|
public $publicReducedToProtected; |
62
|
|
|
public $publicReducedToPrivate; |
63
|
|
|
protected $protectedMaintainedProtected; |
64
|
|
|
protected $protectedReducedToPrivate; |
65
|
|
|
protected $protectedIncreasedToPublic; |
66
|
|
|
private $privateMaintainedPrivate; |
67
|
|
|
private $privateIncreasedToProtected; |
68
|
|
|
private $privateIncreasedToPublic; |
69
|
|
|
} |
70
|
|
|
PHP |
71
|
|
|
, |
72
|
|
|
$astLocator |
73
|
|
|
); |
74
|
|
|
|
75
|
|
|
$toLocator = new StringSourceLocator( |
76
|
|
|
<<<'PHP' |
77
|
|
|
<?php |
78
|
|
|
|
79
|
|
|
class TheClass { |
80
|
|
|
public $publicMaintainedPublic; |
81
|
|
|
protected $publicReducedToProtected; |
82
|
|
|
private $publicReducedToPrivate; |
83
|
|
|
protected $protectedMaintainedProtected; |
84
|
|
|
private $protectedReducedToPrivate; |
85
|
|
|
public $protectedIncreasedToPublic; |
86
|
|
|
private $privateMaintainedPrivate; |
87
|
|
|
protected $privateIncreasedToProtected; |
88
|
|
|
public $privateIncreasedToPublic; |
89
|
|
|
} |
90
|
|
|
PHP |
91
|
|
|
, |
92
|
|
|
$astLocator |
93
|
|
|
); |
94
|
|
|
|
95
|
|
|
$fromClassReflector = new ClassReflector($fromLocator); |
96
|
|
|
$toClassReflector = new ClassReflector($toLocator); |
97
|
|
|
$fromClass = $fromClassReflector->reflect('TheClass'); |
98
|
|
|
$toClass = $toClassReflector->reflect('TheClass'); |
99
|
|
|
|
100
|
|
|
$properties = [ |
101
|
|
|
'publicMaintainedPublic' => [], |
102
|
|
|
'publicReducedToProtected' => ['[BC] CHANGED: Property TheClass#$publicReducedToProtected visibility reduced from public to protected'], |
103
|
|
|
'publicReducedToPrivate' => ['[BC] CHANGED: Property TheClass#$publicReducedToPrivate visibility reduced from public to private'], |
104
|
|
|
'protectedMaintainedProtected' => [], |
105
|
|
|
'protectedReducedToPrivate' => ['[BC] CHANGED: Property TheClass#$protectedReducedToPrivate visibility reduced from protected to private'], |
106
|
|
|
'protectedIncreasedToPublic' => [], |
107
|
|
|
'privateMaintainedPrivate' => [], |
108
|
|
|
'privateIncreasedToProtected' => [], |
109
|
|
|
'privateIncreasedToPublic' => [], |
110
|
|
|
]; |
111
|
|
|
|
112
|
|
|
return TypeRestriction::array(array_combine( |
113
|
|
|
array_keys($properties), |
114
|
|
|
array_map( |
115
|
|
|
/** @psalm-param list<string> $errorMessages https://github.com/vimeo/psalm/issues/2772 */ |
116
|
|
|
static function (string $property, array $errorMessages) use ($fromClass, $toClass) : array { |
117
|
|
|
return [ |
118
|
|
|
TypeRestriction::object($fromClass->getProperty($property)), |
119
|
|
|
TypeRestriction::object($toClass->getProperty($property)), |
120
|
|
|
$errorMessages, |
121
|
|
|
]; |
122
|
|
|
}, |
123
|
|
|
array_keys($properties), |
124
|
|
|
$properties |
125
|
|
|
) |
126
|
|
|
)); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|