PropertyBecameInternalTest::testDiffs()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 7
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 13
rs 10
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\PropertyBecameInternal;
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\PropertyBecameInternal
22
 */
23
final class PropertyBecameInternalTest 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 PropertyBecameInternal())
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 $nonInternal;
61
    public $becameInternal;
62
    /** @internal */
63
    public $becameNonInternal;
64
    /** @internal */
65
    public $stayedInternal;
66
}
67
PHP
68
            ,
69
            $astLocator
70
        );
71
72
        $toLocator = new StringSourceLocator(
73
            <<<'PHP'
74
<?php
75
76
class TheClass {
77
    public $nonInternal;
78
    /** @internal */
79
    public $becameInternal;
80
    public $becameNonInternal;
81
    /** @internal */
82
    public $stayedInternal;
83
}
84
PHP
85
            ,
86
            $astLocator
87
        );
88
89
        $fromClassReflector = new ClassReflector($fromLocator);
90
        $toClassReflector   = new ClassReflector($toLocator);
91
        $fromClass          = $fromClassReflector->reflect('TheClass');
92
        $toClass            = $toClassReflector->reflect('TheClass');
93
94
        $properties = [
95
            'nonInternal'       => [],
96
            'becameInternal'    => ['[BC] CHANGED: Property TheClass#$becameInternal was marked "@internal"'],
97
            'becameNonInternal' => [],
98
            'stayedInternal'    => [],
99
        ];
100
101
        return TypeRestriction::array(array_combine(
102
            array_keys($properties),
103
            array_map(
104
                /** @psalm-param list<string> $errorMessages https://github.com/vimeo/psalm/issues/2772 */
105
                static function (string $property, array $errorMessages) use ($fromClass, $toClass) : array {
106
                    return [
107
                        TypeRestriction::object($fromClass->getProperty($property)),
108
                        TypeRestriction::object($toClass->getProperty($property)),
109
                        $errorMessages,
110
                    ];
111
                },
112
                array_keys($properties),
113
                $properties
114
            )
115
        ));
116
    }
117
}
118