Passed
Pull Request — master (#38)
by Marco
02:40
created

ParameterDefaultValueChangedTest   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 2
dl 0
loc 122
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B functionsToBeTested() 0 98 1
A testDiffs() 0 13 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace RoaveTest\ApiCompare\Comparator\BackwardsCompatibility\FunctionBased;
6
7
use PHPUnit\Framework\TestCase;
8
use Roave\ApiCompare\Change;
9
use Roave\ApiCompare\Comparator\BackwardsCompatibility\FunctionBased\ParameterDefaultValueChanged;
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 function array_map;
16
use function iterator_to_array;
17
18
/**
19
 * @covers \Roave\ApiCompare\Comparator\BackwardsCompatibility\FunctionBased\ParameterDefaultValueChanged
20
 */
21
final class ParameterDefaultValueChangedTest extends TestCase
22
{
23
    /**
24
     * @dataProvider functionsToBeTested
25
     *
26
     * @param string[] $expectedMessages
27
     */
28
    public function testDiffs(
29
        ReflectionFunctionAbstract $fromFunction,
30
        ReflectionFunctionAbstract $toFunction,
31
        array $expectedMessages
32
    ) : void {
33
        $changes = (new ParameterDefaultValueChanged())
34
            ->compare($fromFunction, $toFunction);
35
36
        self::assertSame(
37
            $expectedMessages,
38
            array_map(function (Change $change) : string {
39
                return $change->__toString();
40
            }, iterator_to_array($changes))
41
        );
42
    }
43
44
    /** @return (string[]|ReflectionFunctionAbstract)[][] */
45
    public function functionsToBeTested() : array
46
    {
47
        $astLocator = (new BetterReflection())->astLocator();
48
49
        $fromLocator = new StringSourceLocator(
50
            <<<'PHP'
51
<?php
52
53
namespace {
54
   function changed($a = 1) {}
55
   function defaultAdded($a) {}
56
   function defaultRemoved($a = null) {}
57
   function defaultTypeChanged($a = '1') {}
58
   function notChanged($a = 1, $b = 2, $c = 3) {}
59
   function namesChanged($a = 1, $b = 2, $c = 3) {}
60
   function orderChanged($a = 1, $b = 2, $c = 3) {}
61
   class C {
62
       static function changed1($a = 1) {}
63
       function changed2($a = 1) {}
64
   }
65
}
66
PHP
67
            ,
68
            $astLocator
69
        );
70
71
        $toLocator = new StringSourceLocator(
72
            <<<'PHP'
73
<?php
74
75
namespace {
76
   function changed($a = 2) {}
77
   function defaultAdded($a = 1) {}
78
   function defaultRemoved($a) {}
79
   function defaultTypeChanged($a = 1) {}
80
   function notChanged($a = 1, $b = 2, $c = 3) {}
81
   function namesChanged($d = 1, $e = 2, $f = 3) {}
82
   function orderChanged($c = 3, $b = 2, $a = 1) {}
83
   class C {
84
       static function changed1($a = 2) {}
85
       function changed2($a = 2) {}
86
   }
87
}
88
PHP
89
            ,
90
            $astLocator
91
        );
92
93
        $fromClassReflector = new ClassReflector($fromLocator);
94
        $toClassReflector   = new ClassReflector($toLocator);
95
        $fromReflector      = new FunctionReflector($fromLocator, $fromClassReflector);
96
        $toReflector        = new FunctionReflector($toLocator, $toClassReflector);
97
98
        $functions = [
99
            'changed'            => [
100
                '[BC] CHANGED: Default parameter value for for parameter $a of changed() changed from 1 to 2',
101
            ],
102
            'defaultAdded'       => [],
103
            'defaultRemoved'     => [],
104
            'defaultTypeChanged' => [
105
                '[BC] CHANGED: Default parameter value for for parameter $a of defaultTypeChanged() changed from \'1\' to 1',
106
            ],
107
            'notChanged'         => [],
108
            'namesChanged'       => [],
109
            'orderChanged'       => [
110
                '[BC] CHANGED: Default parameter value for for parameter $a of orderChanged() changed from 1 to 3',
111
                '[BC] CHANGED: Default parameter value for for parameter $c of orderChanged() changed from 3 to 1',
112
            ],
113
        ];
114
115
        return array_merge(
116
            array_combine(
117
                array_keys($functions),
118
                array_map(
119
                    function (string $function, array $errorMessages) use ($fromReflector, $toReflector) : array {
120
                        return [
121
                            $fromReflector->reflect($function),
122
                            $toReflector->reflect($function),
123
                            $errorMessages,
124
                        ];
125
                    },
126
                    array_keys($functions),
127
                    $functions
128
                )
129
            ),
130
            [
131
                'C::changed1' => [
132
                    $fromClassReflector->reflect('C')->getMethod('changed1'),
133
                    $toClassReflector->reflect('C')->getMethod('changed1'),
134
                    [
135
                        '[BC] CHANGED: Default parameter value for for parameter $a of C::changed1() changed from 1 to 2',
136
                    ],
137
                ],
138
                'C#changed2'  => [
139
                    $fromClassReflector->reflect('C')->getMethod('changed2'),
140
                    $toClassReflector->reflect('C')->getMethod('changed2'),
141
                    [
142
                        '[BC] CHANGED: Default parameter value for for parameter $a of C#changed2() changed from 1 to 2',
143
                    ],
144
                ],
145
            ]
146
        );
147
    }
148
}
149