Completed
Push — master ( 901cc6...ad6830 )
by James
15s queued 11s
created

MethodAddedTest::testDiffs()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace RoaveTest\ApiCompare\Comparator\BackwardsCompatibility\InterfaceBased;
6
7
use PHPUnit\Framework\TestCase;
8
use Roave\ApiCompare\Change;
9
use Roave\ApiCompare\Comparator\BackwardsCompatibility\InterfaceBased\MethodAdded;
10
use Roave\BetterReflection\BetterReflection;
11
use Roave\BetterReflection\Reflection\ReflectionClass;
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
final class MethodAddedTest extends TestCase
20
{
21
    /**
22
     * @dataProvider interfacesToBeTested
23
     *
24
     * @param string[] $expectedMessages
25
     */
26
    public function testDiffs(
27
        ReflectionClass $fromInterface,
28
        ReflectionClass $toInterface,
29
        array $expectedMessages
30
    ) : void {
31
        $changes = (new MethodAdded())
32
            ->compare($fromInterface, $toInterface);
33
34
        self::assertSame(
35
            $expectedMessages,
36
            array_map(function (Change $change) : string {
37
                return $change->__toString();
38
            }, iterator_to_array($changes))
39
        );
40
    }
41
42
    /** @return (string[]|ReflectionClass)[][] */
43
    public function interfacesToBeTested() : array
44
    {
45
        $astLocator = (new BetterReflection())->astLocator();
46
47
        $fromLocator = new StringSourceLocator(
48
            <<<'PHP'
49
<?php
50
51
interface A {}
52
interface B {
53
    function removed() {}
54
}
55
interface C {
56
    function kept() {}
57
}
58
interface D {
59
    function casingChanged() {}
60
}
61
interface E {}
62
PHP
63
            ,
64
            $astLocator
65
        );
66
67
        $toLocator = new StringSourceLocator(
68
            <<<'PHP'
69
<?php
70
71
interface A {
72
    function added() {}
73
}
74
interface B {}
75
interface C {
76
    function kept() {}
77
}
78
interface D {
79
    function casingchanged() {}
80
}
81
interface E {
82
    function added1() {}
83
    function added2() {}
84
    function ADDED3() {}
85
}
86
PHP
87
            ,
88
            $astLocator
89
        );
90
91
        $fromClassReflector = new ClassReflector($fromLocator);
92
        $toClassReflector   = new ClassReflector($toLocator);
93
94
        $properties = [
95
            'A' => ['[BC] ADDED: Method added() was added to interface A'],
96
            'B' => [],
97
            'C' => [],
98
            'D' => [],
99
            'E' => [
100
                '[BC] ADDED: Method added1() was added to interface E',
101
                '[BC] ADDED: Method added2() was added to interface E',
102
                '[BC] ADDED: Method ADDED3() was added to interface E',
103
            ],
104
        ];
105
106
        return array_combine(
107
            array_keys($properties),
108
            array_map(
109
                function (string $className, array $errorMessages) use ($fromClassReflector, $toClassReflector
110
                ) : array {
111
                    return [
112
                        $fromClassReflector->reflect($className),
113
                        $toClassReflector->reflect($className),
114
                        $errorMessages,
115
                    ];
116
                },
117
                array_keys($properties),
118
                $properties
119
            )
120
        );
121
    }
122
}
123