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

MethodAddedTest   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
B interfacesToBeTested() 0 76 1
A testDiffs() 0 13 1
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_map;
15
use function iterator_to_array;
16
17
final class MethodAddedTest extends TestCase
18
{
19
    /**
20
     * @dataProvider interfacesToBeTested
21
     *
22
     * @param string[] $expectedMessages
23
     */
24
    public function testDiffs(
25
        ReflectionClass $fromInterface,
26
        ReflectionClass $toInterface,
27
        array $expectedMessages
28
    ) : void {
29
        $changes = (new MethodAdded())
30
            ->compare($fromInterface, $toInterface);
31
32
        self::assertSame(
33
            $expectedMessages,
34
            array_map(function (Change $change) : string {
35
                return $change->__toString();
36
            }, iterator_to_array($changes))
37
        );
38
    }
39
40
    /** @return (string[]|ReflectionClass)[][] */
41
    public function interfacesToBeTested() : array
42
    {
43
        $astLocator = (new BetterReflection())->astLocator();
44
45
        $fromLocator = new StringSourceLocator(
46
            <<<'PHP'
47
<?php
48
49
interface A {}
50
interface B {
51
    function removed() {}
52
}
53
interface C {
54
    function kept() {}
55
}
56
interface D {
57
    function casingChanged() {}
58
}
59
interface E {}
60
PHP
61
            ,
62
            $astLocator
63
        );
64
65
        $toLocator = new StringSourceLocator(
66
            <<<'PHP'
67
<?php
68
69
interface A {
70
    function added() {}
71
}
72
interface B {}
73
interface C {
74
    function kept() {}
75
}
76
interface D {
77
    function casingchanged() {}
78
}
79
interface E {
80
    function added1() {}
81
    function added2() {}
82
    function ADDED3() {}
83
}
84
PHP
85
            ,
86
            $astLocator
87
        );
88
89
        $fromClassReflector = new ClassReflector($fromLocator);
90
        $toClassReflector   = new ClassReflector($toLocator);
91
92
        $properties = [
93
            'A' => ['[BC] ADDED: Method added() was added to interface A'],
94
            'B' => [],
95
            'C' => [],
96
            'D' => [],
97
            'E' => [
98
                '[BC] ADDED: Method added1() was added to interface E',
99
                '[BC] ADDED: Method added2() was added to interface E',
100
                '[BC] ADDED: Method ADDED3() was added to interface E',
101
            ],
102
        ];
103
104
        return array_combine(
105
            array_keys($properties),
106
            array_map(
107
                function (string $className, array $errorMessages) use ($fromClassReflector, $toClassReflector
108
                ) : array {
109
                    return [
110
                        $fromClassReflector->reflect($className),
111
                        $toClassReflector->reflect($className),
112
                        $errorMessages,
113
                    ];
114
                },
115
                array_keys($properties),
116
                $properties
117
            )
118
        );
119
    }
120
}
121