Passed
Pull Request — master (#38)
by Marco
02:17
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 testWillRunInterfaceComparators() : void
110
    {
111
        $this->classBasedComparatorWillBeCalled();
112
        $this->methodBasedComparatorWillNotBeCalled();
113
        $this->propertyBasedComparatorWillNotBeCalled();
114
        $this->constantBasedComparatorWillNotBeCalled();
115
        $this->interfaceBasedComparatorWillBeCalled();
116
117
        self::assertEqualsIgnoringOrder(
118
            Changes::fromArray([
119
                Change::changed('class change', true),
120
                Change::changed('interface change', true),
121
            ]),
122
            $this->comparator->compare(
123
                self::$stringReflectorFactory->__invoke('<?php interface A {}'),
124
                self::$stringReflectorFactory->__invoke('<?php interface A {}')
125
            )
126
        );
127
    }
128
129
    /**
130
     * @param mixed $expected
131
     * @param mixed $actual
132
     */
133
    private static function assertEqualsIgnoringOrder($expected, $actual) : void
134
    {
135
        self::assertEquals($expected, $actual, '', 0.0, 10, true);
136
    }
137
138
    public function testRemovingAClassCausesABreak() : void
139
    {
140
        $this->classBasedComparatorWillNotBeCalled();
141
        $this->methodBasedComparatorWillNotBeCalled();
142
143
        self::assertEqualsIgnoringOrder(
144
            Changes::fromArray([
145
                Change::removed('Class A has been deleted', true),
146
            ]),
147
            $this->comparator->compare(
148
                self::$stringReflectorFactory->__invoke('<?php class A { private function foo() {} }'),
149
                self::$stringReflectorFactory->__invoke('<?php ')
150
            )
151
        );
152
    }
153
154
    private function classBasedComparatorWillBeCalled() : void
155
    {
156
        $this
157
            ->classBasedComparison
158
            ->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

158
            ->/** @scrutinizer ignore-call */ 
159
              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...
159
            ->method('compare')
160
            ->willReturn(Changes::fromArray([
161
                Change::changed('class change', true),
162
            ]));
163
    }
164
165
    private function classBasedComparatorWillNotBeCalled() : void
166
    {
167
        $this
168
            ->classBasedComparison
169
            ->expects(self::never())
170
            ->method('compare');
171
    }
172
173
    private function methodBasedComparatorWillBeCalled() : void
174
    {
175
        $this
176
            ->methodBasedComparison
177
            ->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

177
            ->/** @scrutinizer ignore-call */ 
178
              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...
178
            ->method('compare')
179
            ->willReturn(Changes::fromArray([
180
                Change::changed('method change', true),
181
            ]));
182
    }
183
184
    private function methodBasedComparatorWillNotBeCalled() : void
185
    {
186
        $this
187
            ->methodBasedComparison
188
            ->expects(self::never())
189
            ->method('compare');
190
    }
191
192
    private function propertyBasedComparatorWillBeCalled() : void
193
    {
194
        $this
195
            ->propertyBasedComparison
196
            ->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

196
            ->/** @scrutinizer ignore-call */ 
197
              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...
197
            ->method('compare')
198
            ->willReturn(Changes::fromArray([
199
                Change::changed('property change', true),
200
            ]));
201
    }
202
203
    private function propertyBasedComparatorWillNotBeCalled() : void
204
    {
205
        $this
206
            ->propertyBasedComparison
207
            ->expects(self::never())
208
            ->method('compare');
209
    }
210
211
    private function constantBasedComparatorWillBeCalled() : void
212
    {
213
        $this
214
            ->constantBasedComparison
215
            ->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

215
            ->/** @scrutinizer ignore-call */ 
216
              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...
216
            ->method('compare')
217
            ->willReturn(Changes::fromArray([
218
                Change::changed('constant change', true),
219
            ]));
220
    }
221
222
    private function constantBasedComparatorWillNotBeCalled() : void
223
    {
224
        $this
225
            ->constantBasedComparison
226
            ->expects(self::never())
227
            ->method('compare');
228
    }
229
230
    private function interfaceBasedComparatorWillBeCalled() : void
231
    {
232
        $this
233
            ->interfaceBasedComparison
234
            ->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

234
            ->/** @scrutinizer ignore-call */ 
235
              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...
235
            ->method('compare')
236
            ->willReturn(Changes::fromArray([
237
                Change::changed('interface change', true),
238
            ]));
239
    }
240
241
    private function interfaceBasedComparatorWillNotBeCalled() : void
242
    {
243
        $this
244
            ->interfaceBasedComparison
245
            ->expects(self::never())
246
            ->method('compare');
247
    }
248
}
249