PropertyScopeChangedTest   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 45
c 0
b 0
f 0
dl 0
loc 117
rs 10
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A propertiesToBeTested() 0 89 1
A testDiffs() 0 13 1
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\PropertyScopeChanged;
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\PropertyScopeChanged
22
 */
23
final class PropertyScopeChangedTest extends TestCase
24
{
25
    /**
26
     * @param string[] $expectedMessages
27
     *
28
     * @dataProvider propertiesToBeTested
29
     */
30
    public function testDiffs(
31
        ReflectionProperty $fromFunction,
32
        ReflectionProperty $toFunction,
33
        array $expectedMessages
34
    ) : void {
35
        $changes = (new PropertyScopeChanged())
36
            ->__invoke($fromFunction, $toFunction);
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 $publicInstanceToStatic;
61
    public static $publicStaticToInstance;
62
    public $publicInstanceToInstance;
63
    public static $publicStaticToStatic;
64
    
65
    protected $protectedInstanceToStatic;
66
    protected static $protectedStaticToInstance;
67
    protected $protectedInstanceToInstance;
68
    protected static $protectedStaticToStatic;
69
    
70
    private $privateInstanceToStatic;
71
    private static $privateStaticToInstance;
72
    private $privateInstanceToInstance;
73
    private static $privateStaticToStatic;
74
}
75
PHP
76
            ,
77
            $astLocator
78
        );
79
80
        $toLocator = new StringSourceLocator(
81
            <<<'PHP'
82
<?php
83
84
class TheClass {
85
    public static $publicInstanceToStatic;
86
    public $publicStaticToInstance;
87
    public $publicInstanceToInstance;
88
    public static $publicStaticToStatic;
89
    
90
    protected static $protectedInstanceToStatic;
91
    protected $protectedStaticToInstance;
92
    protected $protectedInstanceToInstance;
93
    protected static $protectedStaticToStatic;
94
    
95
    private static $privateInstanceToStatic;
96
    private $privateStaticToInstance;
97
    private $privateInstanceToInstance;
98
    private static $privateStaticToStatic;
99
}
100
PHP
101
            ,
102
            $astLocator
103
        );
104
105
        $fromClassReflector = new ClassReflector($fromLocator);
106
        $toClassReflector   = new ClassReflector($toLocator);
107
        $fromClass          = $fromClassReflector->reflect('TheClass');
108
        $toClass            = $toClassReflector->reflect('TheClass');
109
110
        $properties = [
111
            'publicInstanceToStatic'   => ['[BC] CHANGED: Property $publicInstanceToStatic of TheClass changed scope from instance to static'],
112
            'publicStaticToInstance'   => ['[BC] CHANGED: Property $publicStaticToInstance of TheClass changed scope from static to instance'],
113
            'publicInstanceToInstance' => [],
114
            'publicStaticToStatic'     => [],
115
116
            'protectedInstanceToStatic'    => ['[BC] CHANGED: Property $protectedInstanceToStatic of TheClass changed scope from instance to static'],
117
            'protectedStaticToInstance'    => ['[BC] CHANGED: Property $protectedStaticToInstance of TheClass changed scope from static to instance'],
118
            'protectedInstanceToInstance' => [],
119
            'protectedStaticToStatic'      => [],
120
121
            'privateInstanceToStatic'    => ['[BC] CHANGED: Property $privateInstanceToStatic of TheClass changed scope from instance to static'],
122
            'privateStaticToInstance'   => ['[BC] CHANGED: Property $privateStaticToInstance of TheClass changed scope from static to instance'],
123
            'privateInstanceToInstance' => [],
124
            'privateStaticToStatic'     => [],
125
        ];
126
127
        return TypeRestriction::array(array_combine(
128
            array_keys($properties),
129
            array_map(
130
                /** @psalm-param list<string> $errorMessages https://github.com/vimeo/psalm/issues/2772 */
131
                static function (string $property, array $errorMessages) use ($fromClass, $toClass) : array {
132
                    return [
133
                        TypeRestriction::object($fromClass->getProperty($property)),
134
                        TypeRestriction::object($toClass->getProperty($property)),
135
                        $errorMessages,
136
                    ];
137
                },
138
                array_keys($properties),
139
                $properties
140
            )
141
        ));
142
    }
143
}
144