FunctionBecameInternalTest   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 37
c 1
b 0
f 0
dl 0
loc 87
rs 10
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A functionsToBeTested() 0 59 1
A testDiffs() 0 13 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace RoaveTest\BackwardCompatibility\DetectChanges\BCBreak\FunctionBased;
6
7
use PHPUnit\Framework\TestCase;
8
use Roave\BackwardCompatibility\Change;
9
use Roave\BackwardCompatibility\DetectChanges\BCBreak\FunctionBased\FunctionBecameInternal;
10
use Roave\BetterReflection\BetterReflection;
11
use Roave\BetterReflection\Reflection\ReflectionFunctionAbstract;
12
use Roave\BetterReflection\Reflector\ClassReflector;
13
use Roave\BetterReflection\Reflector\FunctionReflector;
14
use Roave\BetterReflection\SourceLocator\Type\StringSourceLocator;
15
use RoaveTest\BackwardCompatibility\TypeRestriction;
16
use function array_combine;
17
use function array_keys;
18
use function array_map;
19
use function iterator_to_array;
20
21
/** @covers \Roave\BackwardCompatibility\DetectChanges\BCBreak\FunctionBased\FunctionBecameInternal */
22
final class FunctionBecameInternalTest extends TestCase
23
{
24
    /**
25
     * @param string[] $expectedMessages
26
     *
27
     * @dataProvider functionsToBeTested
28
     */
29
    public function testDiffs(
30
        ReflectionFunctionAbstract $fromFunction,
31
        ReflectionFunctionAbstract $toFunction,
32
        array $expectedMessages
33
    ) : void {
34
        $changes = (new FunctionBecameInternal())
35
            ->__invoke($fromFunction, $toFunction);
36
37
        self::assertSame(
38
            $expectedMessages,
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, ReflectionFunctionAbstract|array<int, string>>>
47
     *
48
     * @psalm-return array<string, array{0: ReflectionFunctionAbstract, 1: ReflectionFunctionAbstract, 2: list<string>}>
49
     */
50
    public function functionsToBeTested() : array
51
    {
52
        $astLocator = (new BetterReflection())->astLocator();
53
54
        $fromLocator = new StringSourceLocator(
55
            <<<'PHP'
56
<?php
57
58
function a() {}
59
function b() {}
60
/** @internal */
61
function c() {}
62
/** @internal */
63
function d() {}
64
PHP
65
            ,
66
            $astLocator
67
        );
68
69
        $toLocator = new StringSourceLocator(
70
            <<<'PHP'
71
<?php
72
73
function a() {}
74
/** @internal */
75
function b() {}
76
function c() {}
77
/** @internal */
78
function d() {}
79
PHP
80
            ,
81
            $astLocator
82
        );
83
84
        $fromClassReflector = new ClassReflector($fromLocator);
85
        $toClassReflector   = new ClassReflector($toLocator);
86
        $fromReflector      = new FunctionReflector($fromLocator, $fromClassReflector);
87
        $toReflector        = new FunctionReflector($toLocator, $toClassReflector);
88
89
        $functions = [
90
            'a' => [],
91
            'b' => ['[BC] CHANGED: b() was marked "@internal"'],
92
            'c' => [],
93
            'd' => [],
94
        ];
95
96
        return TypeRestriction::array(array_combine(
97
            array_keys($functions),
98
            array_map(
99
                /** @psalm-param list<string> $errorMessages https://github.com/vimeo/psalm/issues/2772 */
100
                static function (string $function, array $errorMessages) use ($fromReflector, $toReflector) : array {
101
                    return [
102
                        $fromReflector->reflect($function),
103
                        $toReflector->reflect($function),
104
                        $errorMessages,
105
                    ];
106
                },
107
                array_keys($functions),
108
                $functions
109
            )
110
        ));
111
    }
112
}
113