Completed
Pull Request — master (#38)
by Marco
02:14
created

PropertyScopeChangedTest::testDiffs()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace RoaveTest\ApiCompare\Comparator\BackwardsCompatibility\PropertyBased;
6
7
use PHPUnit\Framework\TestCase;
8
use Roave\ApiCompare\Change;
9
use Roave\ApiCompare\Comparator\BackwardsCompatibility\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 function array_combine;
15
use function array_keys;
16
use function array_map;
17
use function iterator_to_array;
18
19
/**
20
 * @covers \Roave\ApiCompare\Comparator\BackwardsCompatibility\PropertyBased\PropertyScopeChanged
21
 */
22
final class PropertyScopeChangedTest extends TestCase
23
{
24
    /**
25
     * @dataProvider propertiesToBeTested
26
     *
27
     * @param string[] $expectedMessages
28
     */
29
    public function testDiffs(
30
        ReflectionProperty $fromFunction,
31
        ReflectionProperty $toFunction,
32
        array $expectedMessages
33
    ) : void {
34
        $changes = (new PropertyScopeChanged())
35
            ->compare($fromFunction, $toFunction);
36
37
        self::assertSame(
38
            $expectedMessages,
39
            array_map(function (Change $change) : string {
40
                return $change->__toString();
41
            }, iterator_to_array($changes))
42
        );
43
    }
44
45
    /** @return (string[]|ReflectionProperty)[][] */
46
    public function propertiesToBeTested() : array
47
    {
48
        $astLocator = (new BetterReflection())->astLocator();
49
50
        $fromLocator = new StringSourceLocator(
51
            <<<'PHP'
52
<?php
53
54
class TheClass {
55
    public $publicInstanceToStatic;
56
    public static $publicStaticToInstance;
57
    public $publicInstanceToInstance;
58
    public static $publicStaticToStatic;
59
    
60
    protected $protectedInstanceToStatic;
61
    protected static $protectedStaticToInstance;
62
    protected $protectedInstanceToInstance;
63
    protected static $protectedStaticToStatic;
64
    
65
    private $privateInstanceToStatic;
66
    private static $privateStaticToInstance;
67
    private $privateInstanceToInstance;
68
    private static $privateStaticToStatic;
69
}
70
PHP
71
            ,
72
            $astLocator
73
        );
74
75
        $toLocator = new StringSourceLocator(
76
            <<<'PHP'
77
<?php
78
79
class TheClass {
80
    public static $publicInstanceToStatic;
81
    public $publicStaticToInstance;
82
    public $publicInstanceToInstance;
83
    public static $publicStaticToStatic;
84
    
85
    protected static $protectedInstanceToStatic;
86
    protected $protectedStaticToInstance;
87
    protected $protectedInstanceToInstance;
88
    protected static $protectedStaticToStatic;
89
    
90
    private static $privateInstanceToStatic;
91
    private $privateStaticToInstance;
92
    private $privateInstanceToInstance;
93
    private static $privateStaticToStatic;
94
}
95
PHP
96
            ,
97
            $astLocator
98
        );
99
100
        $fromClassReflector = new ClassReflector($fromLocator);
101
        $toClassReflector   = new ClassReflector($toLocator);
102
        $fromClass          = $fromClassReflector->reflect('TheClass');
103
        $toClass            = $toClassReflector->reflect('TheClass');
104
105
        $properties = [
106
            'publicInstanceToStatic'   => ['[BC] CHANGED: Property $publicInstanceToStatic of TheClass changed scope from instance to static'],
107
            'publicStaticToInstance'   => ['[BC] CHANGED: Property $publicStaticToInstance of TheClass changed scope from static to instance'],
108
            'publicInstanceToInstance' => [],
109
            'publicStaticToStatic'     => [],
110
111
            'protectedInstanceToStatic'    => ['[BC] CHANGED: Property $protectedInstanceToStatic of TheClass changed scope from instance to static'],
112
            'protectedStaticToInstance'    => ['[BC] CHANGED: Property $protectedStaticToInstance of TheClass changed scope from static to instance'],
113
            'protectedInstanceToInstance' => [],
114
            'protectedStaticToStatic'      => [],
115
116
            'privateInstanceToStatic'    => ['[BC] CHANGED: Property $privateInstanceToStatic of TheClass changed scope from instance to static'],
117
            'privateStaticToInstance'   => ['[BC] CHANGED: Property $privateStaticToInstance of TheClass changed scope from static to instance'],
118
            'privateInstanceToInstance' => [],
119
            'privateStaticToStatic'     => [],
120
        ];
121
122
        return array_combine(
123
            array_keys($properties),
124
            array_map(
125
                function (string $property, array $errorMessages) use ($fromClass, $toClass) : array {
126
                    return [
127
                        $fromClass->getProperty($property),
128
                        $toClass->getProperty($property),
129
                        $errorMessages,
130
                    ];
131
                },
132
                array_keys($properties),
133
                $properties
134
            )
135
        );
136
    }
137
}
138