Completed
Pull Request — master (#202)
by Jaap
03:16
created

PropertyChangedTest::finalClassProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 52
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 24
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 52
rs 9.536

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\ClassBased;
6
7
use PHPUnit\Framework\TestCase;
8
use Roave\BackwardCompatibility\Change;
9
use Roave\BackwardCompatibility\Changes;
10
use Roave\BackwardCompatibility\DetectChanges\BCBreak\ClassBased\PropertyChanged;
11
use Roave\BackwardCompatibility\DetectChanges\BCBreak\PropertyBased\PropertyBased;
12
use Roave\BetterReflection\BetterReflection;
13
use Roave\BetterReflection\Reflection\ReflectionProperty;
14
use Roave\BetterReflection\Reflector\ClassReflector;
15
use Roave\BetterReflection\SourceLocator\Type\StringSourceLocator;
16
use RoaveTest\BackwardCompatibility\Assertion;
17
18
/**
19
 * @covers \Roave\BackwardCompatibility\DetectChanges\BCBreak\ClassBased\PropertyChanged
20
 */
21
final class PropertyChangedTest extends TestCase
22
{
23
    public function testWillDetectChangesInProperties() : void
24
    {
25
        $astLocator = (new BetterReflection())->astLocator();
26
27
        $fromLocator = new StringSourceLocator(
28
            <<<'PHP'
29
<?php
30
31
class TheClass {
32
    public $a;
33
    protected $b;
34
    private $c;
35
    public static $d;
36
    public $G;
37
}
38
PHP
39
            ,
40
            $astLocator
41
        );
42
43
        $toLocator = new StringSourceLocator(
44
            <<<'PHP'
45
<?php
46
47
class TheClass {
48
    protected $b;
49
    public static $d;
50
    public $e;
51
    public $f;
52
    public $g;
53
}
54
PHP
55
            ,
56
            $astLocator
57
        );
58
59
        $comparator = $this->createMock(PropertyBased::class);
60
61
        $comparator
62
            ->expects(self::exactly(2))
63
            ->method('__invoke')
64
            ->willReturnCallback(static function (ReflectionProperty $from, ReflectionProperty $to) : Changes {
65
                $propertyName = $from->getName();
66
67
                self::assertSame($propertyName, $to->getName());
68
69
                return Changes::fromList(Change::added($propertyName, true));
70
            });
71
72
        Assertion::assertChangesEqual(
73
            Changes::fromList(
74
                Change::added('b', true),
75
                Change::added('d', true)
76
            ),
77
            (new PropertyChanged($comparator))->__invoke(
78
                (new ClassReflector($fromLocator))->reflect('TheClass'),
79
                (new ClassReflector($toLocator))->reflect('TheClass')
80
            )
81
        );
82
    }
83
84
    /**
85
     * @dataProvider finalClassProvider
86
     */
87
    public function testWillIgnoreChangeInFinalClass($fromLocator, $toLocator) : void
88
    {
89
90
91
        $comparator = $this->createMock(PropertyBased::class);
92
93
        $comparator
94
            ->expects(self::exactly(2))
95
            ->method('__invoke')
96
            ->willReturnCallback(static function (ReflectionProperty $from, ReflectionProperty $to) : Changes {
97
                $propertyName = $from->getName();
98
99
                self::assertSame($propertyName, $to->getName());
100
101
                return Changes::fromList(Change::added($propertyName, true));
102
            });
103
104
        Assertion::assertChangesEqual(
105
            Changes::empty(),
106
            (new PropertyChanged($comparator))->__invoke(
107
                (new ClassReflector($fromLocator))->reflect('TheClass'),
108
                (new ClassReflector($toLocator))->reflect('TheClass')
109
            )
110
        );
111
    }
112
113
    public function finalClassProvider() : array
114
    {
115
        $astLocator = (new BetterReflection())->astLocator();
116
117
        return [
118
            'final class' => [
119
                new StringSourceLocator(
120
                    <<<'PHP'
121
<?php
122
123
final class TheClass {
124
    protected $b;
125
}
126
PHP
127
                    ,
128
                    $astLocator
129
                ),
130
                new StringSourceLocator(
131
                    <<<'PHP'
132
<?php
133
final class TheClass {
134
    private $b;
135
}
136
PHP
137
                    ,
138
                    $astLocator
139
                ),
140
            ],
141
            'final class extending baseclass' => [
142
                new StringSourceLocator(
143
                    <<<'PHP'
144
<?php
145
abstract class baseClass {}
146
147
final class TheClass extends baseClass {
148
    protected $b;
149
}
150
PHP
151
                    ,
152
                    $astLocator
153
                ),
154
                new StringSourceLocator(
155
                    <<<'PHP'
156
<?php
157
abstract class baseClass {}
158
159
final class TheClass extends baseClass{
160
    private $b;
161
}
162
PHP
163
                    ,
164
                    $astLocator
165
                ),
166
            ],
167
        ];
168
169
    }
170
}
171