Passed
Pull Request — master (#50)
by Marco
02:46
created

PropertyVisibilityReducedTest::testDiffs()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 3
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
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 function array_combine;
15
use function array_keys;
16
use function array_map;
17
use function iterator_to_array;
18
19
/**
20
 * @covers \Roave\BackwardCompatibility\DetectChanges\BCBreak\PropertyBased\PropertyVisibilityReduced
21
 */
22
final class PropertyVisibilityReducedTest extends TestCase
23
{
24
    /**
25
     * @dataProvider propertiesToBeTested
26
     *
27
     * @param string[] $expectedMessages
28
     */
29
    public function testDiffs(
30
        ReflectionProperty $fromProperty,
31
        ReflectionProperty $toProperty,
32
        array $expectedMessages
33
    ) : void {
34
        $changes = (new PropertyVisibilityReduced())
35
            ->__invoke($fromProperty, $toProperty);
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 $publicMaintainedPublic;
56
    public $publicReducedToProtected;
57
    public $publicReducedToPrivate;
58
    protected $protectedMaintainedProtected;
59
    protected $protectedReducedToPrivate;
60
    protected $protectedIncreasedToPublic;
61
    private $privateMaintainedPrivate;
62
    private $privateIncreasedToProtected;
63
    private $privateIncreasedToPublic;
64
}
65
PHP
66
            ,
67
            $astLocator
68
        );
69
70
        $toLocator = new StringSourceLocator(
71
            <<<'PHP'
72
<?php
73
74
class TheClass {
75
    public $publicMaintainedPublic;
76
    protected $publicReducedToProtected;
77
    private $publicReducedToPrivate;
78
    protected $protectedMaintainedProtected;
79
    private $protectedReducedToPrivate;
80
    public $protectedIncreasedToPublic;
81
    private $privateMaintainedPrivate;
82
    protected $privateIncreasedToProtected;
83
    public $privateIncreasedToPublic;
84
}
85
PHP
86
            ,
87
            $astLocator
88
        );
89
90
        $fromClassReflector = new ClassReflector($fromLocator);
91
        $toClassReflector   = new ClassReflector($toLocator);
92
        $fromClass          = $fromClassReflector->reflect('TheClass');
93
        $toClass            = $toClassReflector->reflect('TheClass');
94
95
        $properties = [
96
97
            'publicMaintainedPublic' => [],
98
            'publicReducedToProtected' => ['[BC] CHANGED: Property TheClass#$publicReducedToProtected visibility reduced from public to protected'],
99
            'publicReducedToPrivate' => ['[BC] CHANGED: Property TheClass#$publicReducedToPrivate visibility reduced from public to private'],
100
            'protectedMaintainedProtected' => [],
101
            'protectedReducedToPrivate' => ['[BC] CHANGED: Property TheClass#$protectedReducedToPrivate visibility reduced from protected to private'],
102
            'protectedIncreasedToPublic' => [],
103
            'privateMaintainedPrivate' => [],
104
            'privateIncreasedToProtected' => [],
105
            'privateIncreasedToPublic' => [],
106
        ];
107
108
        return array_combine(
109
            array_keys($properties),
110
            array_map(
111
                function (string $property, array $errorMessages) use ($fromClass, $toClass) : array {
112
                    return [
113
                        $fromClass->getProperty($property),
114
                        $toClass->getProperty($property),
115
                        $errorMessages,
116
                    ];
117
                },
118
                array_keys($properties),
119
                $properties
120
            )
121
        );
122
    }
123
}
124