Passed
Pull Request — master (#109)
by Marco
05:58 queued 03:28
created

AncestorRemovedTest::interfacesToBeTested()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 61
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 30
nc 1
nop 0
dl 0
loc 61
rs 9.44
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace RoaveTest\BackwardCompatibility\DetectChanges\BCBreak\InterfaceBased;
6
7
use PHPUnit\Framework\TestCase;
8
use Roave\BackwardCompatibility\Change;
9
use Roave\BackwardCompatibility\DetectChanges\BCBreak\InterfaceBased\AncestorRemoved;
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
/**
20
 * @covers \Roave\BackwardCompatibility\DetectChanges\BCBreak\InterfaceBased\AncestorRemoved
21
 */
22
final class AncestorRemovedTest extends TestCase
23
{
24
    /**
25
     * @dataProvider interfacesToBeTested
26
     *
27
     * @param string[] $expectedMessages
28
     */
29
    public function testDiffs(
30
        ReflectionClass $fromInterface,
31
        ReflectionClass $toInterace,
32
        array $expectedMessages
33
    ) : void {
34
        $changes = (new AncestorRemoved())
35
            ->__invoke($fromInterface, $toInterace);
36
37
        self::assertSame(
38
            $expectedMessages,
39
            array_map(function (Change $change) : string {
40
                return $change->__toString();
41
            }, iterator_to_array($changes))
42
        );
43
    }
44
45
    /** @return (string[]|ReflectionClass)[][] */
46
    public function interfacesToBeTested() : array
47
    {
48
        $locator       = (new BetterReflection())->astLocator();
49
        $fromReflector = new ClassReflector(new StringSourceLocator(
50
            <<<'PHP'
51
<?php
52
53
interface IA {}
54
interface IB extends IA {}
55
interface IC extends IB {}
56
interface ID {}
57
interface ParentInterfaceAdded {}
58
interface ParentInterfaceRemoved extends IA {}
59
interface ParentInterfaceIndirectlyRemoved extends IB {}
60
interface ParentInterfaceVeryIndirectlyRemoved extends IC {}
61
interface ParentInterfaceOrderSwapped extends IA, ID {}
62
PHP
63
            ,
64
            $locator
65
        ));
66
        $toReflector   = new ClassReflector(new StringSourceLocator(
67
            <<<'PHP'
68
<?php
69
70
interface IA {}
71
interface IB {}
72
interface IC extends IB {}
73
interface ID {}
74
interface ParentInterfaceAdded extends IA {}
75
interface ParentInterfaceRemoved {}
76
interface ParentInterfaceIndirectlyRemoved extends IB {}
77
interface ParentInterfaceVeryIndirectlyRemoved extends IC {}
78
interface ParentInterfaceOrderSwapped extends ID, IA {}
79
PHP
80
            ,
81
            $locator
82
        ));
83
84
        $interfaces = [
85
            'IA' => [],
86
            'IB' => ['[BC] REMOVED: These ancestors of IB have been removed: ["IA"]'],
87
            'IC' => ['[BC] REMOVED: These ancestors of IC have been removed: ["IA"]'],
88
            'ParentInterfaceAdded' => [],
89
            'ParentInterfaceRemoved' => ['[BC] REMOVED: These ancestors of ParentInterfaceRemoved have been removed: ["IA"]'],
90
            'ParentInterfaceIndirectlyRemoved' => ['[BC] REMOVED: These ancestors of ParentInterfaceIndirectlyRemoved have been removed: ["IA"]'],
91
            'ParentInterfaceVeryIndirectlyRemoved' => ['[BC] REMOVED: These ancestors of ParentInterfaceVeryIndirectlyRemoved have been removed: ["IA"]'],
92
            'ParentInterfaceOrderSwapped' => [],
93
        ];
94
95
        return array_combine(
96
            array_keys($interfaces),
97
            array_map(
98
                function (string $interfaceName, array $errors) use ($fromReflector, $toReflector) : array {
99
                    return [
100
                        $fromReflector->reflect($interfaceName),
101
                        $toReflector->reflect($interfaceName),
102
                        $errors,
103
                    ];
104
                },
105
                array_keys($interfaces),
106
                $interfaces
107
            )
108
        );
109
    }
110
}
111