Completed
Pull Request — master (#38)
by Marco
05:15
created

ComparatorTest::testMakingAClassFinal()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 0
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace RoaveTest\ApiCompare;
6
7
use PHPUnit\Framework\MockObject\MockObject;
8
use PHPUnit\Framework\TestCase;
9
use Roave\ApiCompare\Change;
10
use Roave\ApiCompare\Changes;
11
use Roave\ApiCompare\Comparator;
12
use Roave\ApiCompare\Comparator\BackwardsCompatibility\ClassBased\ClassBased;
13
use Roave\ApiCompare\Comparator\BackwardsCompatibility\ClassConstantBased\ConstantBased;
14
use Roave\ApiCompare\Comparator\BackwardsCompatibility\InterfaceBased\InterfaceBased;
15
use Roave\ApiCompare\Comparator\BackwardsCompatibility\MethodBased\MethodBased;
16
use Roave\ApiCompare\Comparator\BackwardsCompatibility\PropertyBased\PropertyBased;
17
18
/**
19
 * @covers \Roave\ApiCompare\Comparator
20
 */
21
final class ComparatorTest extends TestCase
22
{
23
    /** @var StringReflectorFactory|null */
24
    private static $stringReflectorFactory;
25
26
    /** @var ClassBased|MockObject */
27
    private $classBasedComparison;
28
29
    /** @var InterfaceBased|MockObject */
30
    private $interfaceBasedComparison;
31
32
    /** @var MethodBased|MockObject */
33
    private $methodBasedComparison;
34
35
    /** @var PropertyBased|MockObject */
36
    private $propertyBasedComparison;
37
38
    /** @var ConstantBased|MockObject */
39
    private $constantBasedComparison;
40
41
    /** @var Comparator */
42
    private $comparator;
43
44
    public static function setUpBeforeClass() : void
45
    {
46
        self::$stringReflectorFactory = new StringReflectorFactory();
47
    }
48
49
    protected function setUp() : void
50
    {
51
        parent::setUp();
52
53
        $this->classBasedComparison     = $this->createMock(ClassBased::class);
54
        $this->interfaceBasedComparison = $this->createMock(InterfaceBased::class);
55
        $this->methodBasedComparison    = $this->createMock(MethodBased::class);
56
        $this->propertyBasedComparison  = $this->createMock(PropertyBased::class);
57
        $this->constantBasedComparison  = $this->createMock(ConstantBased::class);
58
        $this->comparator               = new Comparator(
59
            $this->classBasedComparison,
60
            $this->interfaceBasedComparison,
61
            $this->methodBasedComparison,
62
            $this->propertyBasedComparison,
63
            $this->constantBasedComparison
64
        );
65
    }
66
67
    public function testWillRunSubComparators() : void
68
    {
69
        $this->classBasedComparatorWillBeCalled();
70
        $this->methodBasedComparatorWillBeCalled();
71
        $this->propertyBasedComparatorWillBeCalled();
72
        $this->constantBasedComparatorWillBeCalled();
73
        $this->interfaceBasedComparatorWillNotBeCalled();
74
75
        self::assertEqualsIgnoringOrder(
76
            Changes::fromArray([
77
                Change::changed('class change', true),
78
                Change::changed('constant change', true),
79
                Change::changed('property change', true),
80
                Change::changed('method change', true),
81
            ]),
82
            $this->comparator->compare(
83
                self::$stringReflectorFactory->__invoke(
0 ignored issues
show
Bug introduced by
The method __invoke() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

83
                self::$stringReflectorFactory->/** @scrutinizer ignore-call */ 
84
                                               __invoke(

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
84
                    <<<'PHP'
85
<?php
86
87
class A {
88
    const A_CONSTANT = 'foo';
89
    public $aProperty;
90
    public function aMethod() {}
91
}
92
PHP
93
                ),
94
                self::$stringReflectorFactory->__invoke(
95
                    <<<'PHP'
96
<?php
97
98
class A {
99
    const A_CONSTANT = 'foo';
100
    public $aProperty;
101
    public function aMethod() {}
102
}
103
PHP
104
                )
105
            )
106
        );
107
    }
108
109
    public function testWillNotRunSubComparatorsIfSymbolsWereDeleted() : void
110
    {
111
        $this->classBasedComparatorWillBeCalled();
112
        $this->methodBasedComparatorWillNotBeCalled();
113
        $this->propertyBasedComparatorWillNotBeCalled();
114
        $this->constantBasedComparatorWillNotBeCalled();
115
        $this->interfaceBasedComparatorWillNotBeCalled();
116
117
        self::assertEqualsIgnoringOrder(
118
            Changes::fromArray([
119
                Change::changed('class change', true),
120
            ]),
121
            $this->comparator->compare(
122
                self::$stringReflectorFactory->__invoke(
123
                    <<<'PHP'
124
<?php
125
126
class A {
127
    const A_CONSTANT = 'foo';
128
    public $aProperty;
129
    public function aMethod() {}
130
}
131
PHP
132
                ),
133
                self::$stringReflectorFactory->__invoke(
134
                    <<<'PHP'
135
<?php
136
137
class A {}
138
PHP
139
                )
140
            )
141
        );
142
    }
143
144
    public function testWillRunInterfaceComparators() : void
145
    {
146
        $this->classBasedComparatorWillBeCalled();
147
        $this->methodBasedComparatorWillNotBeCalled();
148
        $this->propertyBasedComparatorWillNotBeCalled();
149
        $this->constantBasedComparatorWillNotBeCalled();
150
        $this->interfaceBasedComparatorWillBeCalled();
151
152
        self::assertEqualsIgnoringOrder(
153
            Changes::fromArray([
154
                Change::changed('class change', true),
155
                Change::changed('interface change', true),
156
            ]),
157
            $this->comparator->compare(
158
                self::$stringReflectorFactory->__invoke('<?php interface A {}'),
159
                self::$stringReflectorFactory->__invoke('<?php interface A {}')
160
            )
161
        );
162
    }
163
164
    /**
165
     * @param mixed $expected
166
     * @param mixed $actual
167
     */
168
    private static function assertEqualsIgnoringOrder($expected, $actual) : void
169
    {
170
        self::assertEquals($expected, $actual, '', 0.0, 10, true);
171
    }
172
173
    public function testRemovingAClassCausesABreak() : void
174
    {
175
        $this->classBasedComparatorWillNotBeCalled();
176
        $this->methodBasedComparatorWillNotBeCalled();
177
178
        self::assertEqualsIgnoringOrder(
179
            Changes::fromArray([
180
                Change::removed('Class A has been deleted', true),
181
            ]),
182
            $this->comparator->compare(
183
                self::$stringReflectorFactory->__invoke('<?php class A { private function foo() {} }'),
184
                self::$stringReflectorFactory->__invoke('<?php ')
185
            )
186
        );
187
    }
188
189
    private function classBasedComparatorWillBeCalled() : void
190
    {
191
        $this
192
            ->classBasedComparison
193
            ->expects(self::atLeastOnce())
0 ignored issues
show
Bug introduced by
The method expects() does not exist on Roave\ApiCompare\Compara...y\ClassBased\ClassBased. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

193
            ->/** @scrutinizer ignore-call */ 
194
              expects(self::atLeastOnce())

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
194
            ->method('compare')
195
            ->willReturn(Changes::fromArray([
196
                Change::changed('class change', true),
197
            ]));
198
    }
199
200
    private function classBasedComparatorWillNotBeCalled() : void
201
    {
202
        $this
203
            ->classBasedComparison
204
            ->expects(self::never())
205
            ->method('compare');
206
    }
207
208
    private function methodBasedComparatorWillBeCalled() : void
209
    {
210
        $this
211
            ->methodBasedComparison
212
            ->expects(self::atLeastOnce())
0 ignored issues
show
Bug introduced by
The method expects() does not exist on Roave\ApiCompare\Compara...MethodBased\MethodBased. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

212
            ->/** @scrutinizer ignore-call */ 
213
              expects(self::atLeastOnce())

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
213
            ->method('compare')
214
            ->willReturn(Changes::fromArray([
215
                Change::changed('method change', true),
216
            ]));
217
    }
218
219
    private function methodBasedComparatorWillNotBeCalled() : void
220
    {
221
        $this
222
            ->methodBasedComparison
223
            ->expects(self::never())
224
            ->method('compare');
225
    }
226
227
    private function propertyBasedComparatorWillBeCalled() : void
228
    {
229
        $this
230
            ->propertyBasedComparison
231
            ->expects(self::atLeastOnce())
0 ignored issues
show
Bug introduced by
The method expects() does not exist on Roave\ApiCompare\Compara...ertyBased\PropertyBased. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

231
            ->/** @scrutinizer ignore-call */ 
232
              expects(self::atLeastOnce())

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
232
            ->method('compare')
233
            ->willReturn(Changes::fromArray([
234
                Change::changed('property change', true),
235
            ]));
236
    }
237
238
    private function propertyBasedComparatorWillNotBeCalled() : void
239
    {
240
        $this
241
            ->propertyBasedComparison
242
            ->expects(self::never())
243
            ->method('compare');
244
    }
245
246
    private function constantBasedComparatorWillBeCalled() : void
247
    {
248
        $this
249
            ->constantBasedComparison
250
            ->expects(self::atLeastOnce())
0 ignored issues
show
Bug introduced by
The method expects() does not exist on Roave\ApiCompare\Compara...tantBased\ConstantBased. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

250
            ->/** @scrutinizer ignore-call */ 
251
              expects(self::atLeastOnce())

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
251
            ->method('compare')
252
            ->willReturn(Changes::fromArray([
253
                Change::changed('constant change', true),
254
            ]));
255
    }
256
257
    private function constantBasedComparatorWillNotBeCalled() : void
258
    {
259
        $this
260
            ->constantBasedComparison
261
            ->expects(self::never())
262
            ->method('compare');
263
    }
264
265
    private function interfaceBasedComparatorWillBeCalled() : void
266
    {
267
        $this
268
            ->interfaceBasedComparison
269
            ->expects(self::atLeastOnce())
0 ignored issues
show
Bug introduced by
The method expects() does not exist on Roave\ApiCompare\Compara...aceBased\InterfaceBased. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

269
            ->/** @scrutinizer ignore-call */ 
270
              expects(self::atLeastOnce())

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
270
            ->method('compare')
271
            ->willReturn(Changes::fromArray([
272
                Change::changed('interface change', true),
273
            ]));
274
    }
275
276
    private function interfaceBasedComparatorWillNotBeCalled() : void
277
    {
278
        $this
279
            ->interfaceBasedComparison
280
            ->expects(self::never())
281
            ->method('compare');
282
    }
283
}
284