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\DetectChanges\BCBreak\ClassBased\ClassBecameTrait; |
10
|
|
|
use Roave\BetterReflection\BetterReflection; |
11
|
|
|
use Roave\BetterReflection\Reflection\ReflectionClass; |
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\ClassBased\ClassBecameTrait |
22
|
|
|
*/ |
23
|
|
|
final class ClassBecameTraitTest extends TestCase |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @param string[] $expectedMessages |
27
|
|
|
* |
28
|
|
|
* @dataProvider classesToBeTested |
29
|
|
|
*/ |
30
|
|
|
public function testDiffs( |
31
|
|
|
ReflectionClass $fromClass, |
32
|
|
|
ReflectionClass $toClass, |
33
|
|
|
array $expectedMessages |
34
|
|
|
) : void { |
35
|
|
|
$changes = (new ClassBecameTrait()) |
36
|
|
|
->__invoke($fromClass, $toClass); |
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, ReflectionClass|array<int, string>>> |
48
|
|
|
* |
49
|
|
|
* @psalm-return array<string, array{0: ReflectionClass, 1: ReflectionClass, 2: list<string>}> |
50
|
|
|
*/ |
51
|
|
|
public function classesToBeTested() : array |
52
|
|
|
{ |
53
|
|
|
$locator = (new BetterReflection())->astLocator(); |
54
|
|
|
$fromReflector = new ClassReflector(new StringSourceLocator( |
55
|
|
|
<<<'PHP' |
56
|
|
|
<?php |
57
|
|
|
|
58
|
|
|
class ConcreteToAbstract {} |
59
|
|
|
abstract class AbstractToConcrete {} |
60
|
|
|
class ConcreteToConcrete {} |
61
|
|
|
abstract class AbstractToAbstract {} |
62
|
|
|
class ConcreteToInterface {} |
63
|
|
|
interface InterfaceToConcrete {} |
64
|
|
|
interface InterfaceToInterface {} |
65
|
|
|
interface InterfaceToAbstract {} |
66
|
|
|
abstract class AbstractToInterface {} |
67
|
|
|
class ClassToTrait {} |
68
|
|
|
trait TraitToClass {} |
69
|
|
|
trait TraitToTrait {} |
70
|
|
|
PHP |
71
|
|
|
, |
72
|
|
|
$locator |
73
|
|
|
)); |
74
|
|
|
$toReflector = new ClassReflector(new StringSourceLocator( |
75
|
|
|
<<<'PHP' |
76
|
|
|
<?php |
77
|
|
|
|
78
|
|
|
abstract class ConcreteToAbstract {} |
79
|
|
|
class AbstractToConcrete {} |
80
|
|
|
class ConcreteToConcrete {} |
81
|
|
|
abstract class AbstractToAbstract {} |
82
|
|
|
interface ConcreteToInterface {} |
83
|
|
|
class InterfaceToConcrete {} |
84
|
|
|
interface InterfaceToInterface {} |
85
|
|
|
abstract class InterfaceToAbstract {} |
86
|
|
|
interface AbstractToInterface {} |
87
|
|
|
trait ClassToTrait {} |
88
|
|
|
class TraitToClass {} |
89
|
|
|
trait TraitToTrait {} |
90
|
|
|
PHP |
91
|
|
|
, |
92
|
|
|
$locator |
93
|
|
|
)); |
94
|
|
|
|
95
|
|
|
$classes = [ |
96
|
|
|
'ConcreteToAbstract' => [], |
97
|
|
|
'AbstractToConcrete' => [], |
98
|
|
|
'ConcreteToConcrete' => [], |
99
|
|
|
'AbstractToAbstract' => [], |
100
|
|
|
'ConcreteToInterface' => [], |
101
|
|
|
'InterfaceToConcrete' => [], |
102
|
|
|
'InterfaceToInterface' => [], |
103
|
|
|
'InterfaceToAbstract' => [], |
104
|
|
|
'AbstractToInterface' => [], |
105
|
|
|
'ClassToTrait' => ['[BC] CHANGED: Class ClassToTrait became a trait'], |
106
|
|
|
'TraitToClass' => [], |
107
|
|
|
'TraitToTrait' => [], |
108
|
|
|
]; |
109
|
|
|
|
110
|
|
|
return TypeRestriction::array(array_combine( |
111
|
|
|
array_keys($classes), |
112
|
|
|
array_map( |
113
|
|
|
/** @psalm-param list<string> $errors https://github.com/vimeo/psalm/issues/2772 */ |
114
|
|
|
static function (string $className, array $errors) use ($fromReflector, $toReflector) : array { |
115
|
|
|
return [ |
116
|
|
|
$fromReflector->reflect($className), |
117
|
|
|
$toReflector->reflect($className), |
118
|
|
|
$errors, |
119
|
|
|
]; |
120
|
|
|
}, |
121
|
|
|
array_keys($classes), |
122
|
|
|
$classes |
123
|
|
|
) |
124
|
|
|
)); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|