Completed
Push — master ( 85853a...5df06c )
by James
20s queued 12s
created

PropertyBecameInternalTest::testDiffs()   A

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 function array_keys;
15
use function array_map;
16
use function iterator_to_array;
17
use function Safe\array_combine;
18
19
/**
20
 * @covers \Roave\BackwardCompatibility\DetectChanges\BCBreak\PropertyBased\PropertyBecameInternal
21
 */
22
final class PropertyBecameInternalTest extends TestCase
23
{
24
    /**
25
     * @param string[] $expectedMessages
26
     *
27
     * @dataProvider propertiesToBeTested
28
     */
29
    public function testDiffs(
30
        ReflectionProperty $fromFunction,
31
        ReflectionProperty $toFunction,
32
        array $expectedMessages
33
    ) : void {
34
        $changes = (new PropertyBecameInternal())
35
            ->__invoke($fromFunction, $toFunction);
36
37
        self::assertSame(
0 ignored issues
show
Bug Best Practice introduced by
The method PHPUnit\Framework\Assert::assertSame() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

37
        self::/** @scrutinizer ignore-call */ 
38
              assertSame(
Loading history...
38
            $expectedMessages,
0 ignored issues
show
Bug introduced by
$expectedMessages of type string[] is incompatible with the type PHPUnit\Framework\T expected by parameter $expected of PHPUnit\Framework\Assert::assertSame(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

38
            /** @scrutinizer ignore-type */ $expectedMessages,
Loading history...
39
            array_map(static function (Change $change) : string {
40
                return $change->__toString();
41
            }, iterator_to_array($changes))
42
        );
43
    }
44
45
    /**
46
     * @return array<string, array<int, ReflectionProperty|array<int, string>>>
47
     *
48
     * @psalm-return array<string, array{0: ReflectionProperty, 1: ReflectionProperty, 2: array<int, string>}>
49
     */
50
    public function propertiesToBeTested() : array
51
    {
52
        $astLocator = (new BetterReflection())->astLocator();
53
54
        $fromLocator = new StringSourceLocator(
55
            <<<'PHP'
56
<?php
57
58
class TheClass {
59
    public $nonInternal;
60
    public $becameInternal;
61
    /** @internal */
62
    public $becameNonInternal;
63
    /** @internal */
64
    public $stayedInternal;
65
}
66
PHP
67
            ,
68
            $astLocator
69
        );
70
71
        $toLocator = new StringSourceLocator(
72
            <<<'PHP'
73
<?php
74
75
class TheClass {
76
    public $nonInternal;
77
    /** @internal */
78
    public $becameInternal;
79
    public $becameNonInternal;
80
    /** @internal */
81
    public $stayedInternal;
82
}
83
PHP
84
            ,
85
            $astLocator
86
        );
87
88
        $fromClassReflector = new ClassReflector($fromLocator);
89
        $toClassReflector   = new ClassReflector($toLocator);
90
        $fromClass          = $fromClassReflector->reflect('TheClass');
91
        $toClass            = $toClassReflector->reflect('TheClass');
92
93
        $properties = [
94
            'nonInternal'       => [],
95
            'becameInternal'    => ['[BC] CHANGED: Property TheClass#$becameInternal was marked "@internal"'],
96
            'becameNonInternal' => [],
97
            'stayedInternal'    => [],
98
        ];
99
100
        return array_combine(
101
            array_keys($properties),
102
            array_map(
103
                static function (string $property, array $errorMessages) use ($fromClass, $toClass) : array {
104
                    return [
105
                        $fromClass->getProperty($property),
106
                        $toClass->getProperty($property),
107
                        $errorMessages,
108
                    ];
109
                },
110
                array_keys($properties),
111
                $properties
112
            )
113
        );
114
    }
115
}
116