Completed
Push — master ( 901cc6...ad6830 )
by James
15s queued 11s
created

ComparatorTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
eloc 8
nc 1
nop 0
cc 1
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\InterfaceBased\InterfaceBased;
14
use Roave\ApiCompare\Comparator\BackwardsCompatibility\TraitBased\TraitBased;
15
16
/**
17
 * @covers \Roave\ApiCompare\Comparator
18
 */
19
final class ComparatorTest extends TestCase
20
{
21
    /** @var StringReflectorFactory|null */
22
    private static $stringReflectorFactory;
23
24
    /** @var ClassBased|MockObject */
25
    private $classBasedComparison;
26
27
    /** @var InterfaceBased|MockObject */
28
    private $interfaceBasedComparison;
29
30
    /** @var TraitBased|MockObject */
31
    private $traitBasedComparison;
32
33
    /** @var Comparator */
34
    private $comparator;
35
36
    public static function setUpBeforeClass() : void
37
    {
38
        self::$stringReflectorFactory = new StringReflectorFactory();
39
    }
40
41
    protected function setUp() : void
42
    {
43
        parent::setUp();
44
45
        $this->classBasedComparison     = $this->createMock(ClassBased::class);
46
        $this->interfaceBasedComparison = $this->createMock(InterfaceBased::class);
47
        $this->traitBasedComparison     = $this->createMock(TraitBased::class);
48
        $this->comparator               = new Comparator(
49
            $this->classBasedComparison,
50
            $this->interfaceBasedComparison,
51
            $this->traitBasedComparison
52
        );
53
    }
54
55
    public function testWillRunSubComparators() : void
56
    {
57
        $this->classBasedComparatorWillBeCalled();
58
        $this->interfaceBasedComparatorWillNotBeCalled();
59
        $this->traitBasedComparatorWillNotBeCalled();
60
61
        self::assertEqualsIgnoringOrder(
62
            Changes::fromArray([
63
                Change::changed('class change', true),
64
            ]),
65
            $this->comparator->compare(
66
                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

66
                self::$stringReflectorFactory->/** @scrutinizer ignore-call */ 
67
                                               __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...
67
                    <<<'PHP'
68
<?php
69
70
class A {
71
    const A_CONSTANT = 'foo';
72
    public $aProperty;
73
    public function aMethod() {}
74
}
75
PHP
76
                ),
77
                self::$stringReflectorFactory->__invoke(
78
                    <<<'PHP'
79
<?php
80
81
class A {
82
    const A_CONSTANT = 'foo';
83
    public $aProperty;
84
    public function aMethod() {}
85
}
86
PHP
87
                )
88
            )
89
        );
90
    }
91
92
    public function testWillNotRunSubComparatorsIfSymbolsWereDeleted() : void
93
    {
94
        $this->classBasedComparatorWillBeCalled();
95
        $this->interfaceBasedComparatorWillNotBeCalled();
96
        $this->traitBasedComparatorWillNotBeCalled();
97
98
        self::assertEqualsIgnoringOrder(
99
            Changes::fromArray([
100
                Change::changed('class change', true),
101
            ]),
102
            $this->comparator->compare(
103
                self::$stringReflectorFactory->__invoke(
104
                    <<<'PHP'
105
<?php
106
107
class A {
108
    const A_CONSTANT = 'foo';
109
    public $aProperty;
110
    public function aMethod() {}
111
}
112
PHP
113
                ),
114
                self::$stringReflectorFactory->__invoke(
115
                    <<<'PHP'
116
<?php
117
118
class A {}
119
PHP
120
                )
121
            )
122
        );
123
    }
124
125
    public function testWillRunInterfaceComparators() : void
126
    {
127
        $this->classBasedComparatorWillNotBeCalled();
128
        $this->interfaceBasedComparatorWillBeCalled();
129
        $this->traitBasedComparatorWillNotBeCalled();
130
131
        self::assertEqualsIgnoringOrder(
132
            Changes::fromArray([
133
                Change::changed('interface change', true),
134
            ]),
135
            $this->comparator->compare(
136
                self::$stringReflectorFactory->__invoke('<?php interface A {}'),
137
                self::$stringReflectorFactory->__invoke('<?php interface A {}')
138
            )
139
        );
140
    }
141
142
    public function testWillRunTraitComparators() : void
143
    {
144
        $this->classBasedComparatorWillNotBeCalled();
145
        $this->interfaceBasedComparatorWillNotBeCalled();
146
        $this->traitBasedComparatorWillBeCalled();
147
148
        self::assertEqualsIgnoringOrder(
149
            Changes::fromArray([
150
                Change::changed('trait change', true),
151
            ]),
152
            $this->comparator->compare(
153
                self::$stringReflectorFactory->__invoke('<?php trait A {}'),
154
                self::$stringReflectorFactory->__invoke('<?php trait A {}')
155
            )
156
        );
157
    }
158
159
    /**
160
     * @param mixed $expected
161
     * @param mixed $actual
162
     */
163
    private static function assertEqualsIgnoringOrder($expected, $actual) : void
164
    {
165
        self::assertEquals($expected, $actual, '', 0.0, 10, true);
166
    }
167
168
    public function testRemovingAClassCausesABreak() : void
169
    {
170
        $this->classBasedComparatorWillNotBeCalled();
171
        $this->interfaceBasedComparatorWillNotBeCalled();
172
        $this->traitBasedComparatorWillNotBeCalled();
173
174
        self::assertEqualsIgnoringOrder(
175
            Changes::fromArray([
176
                Change::removed('Class A has been deleted', true),
177
            ]),
178
            $this->comparator->compare(
179
                self::$stringReflectorFactory->__invoke('<?php class A { private function foo() {} }'),
180
                self::$stringReflectorFactory->__invoke('<?php ')
181
            )
182
        );
183
    }
184
185
    private function classBasedComparatorWillBeCalled() : void
186
    {
187
        $this
188
            ->classBasedComparison
189
            ->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

189
            ->/** @scrutinizer ignore-call */ 
190
              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...
190
            ->method('compare')
191
            ->willReturn(Changes::fromArray([
192
                Change::changed('class change', true),
193
            ]));
194
    }
195
196
    private function classBasedComparatorWillNotBeCalled() : void
197
    {
198
        $this
199
            ->classBasedComparison
200
            ->expects(self::never())
201
            ->method('compare');
202
    }
203
204
    private function interfaceBasedComparatorWillBeCalled() : void
205
    {
206
        $this
207
            ->interfaceBasedComparison
208
            ->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

208
            ->/** @scrutinizer ignore-call */ 
209
              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...
209
            ->method('compare')
210
            ->willReturn(Changes::fromArray([
211
                Change::changed('interface change', true),
212
            ]));
213
    }
214
215
    private function interfaceBasedComparatorWillNotBeCalled() : void
216
    {
217
        $this
218
            ->interfaceBasedComparison
219
            ->expects(self::never())
220
            ->method('compare');
221
    }
222
223
    private function traitBasedComparatorWillBeCalled() : void
224
    {
225
        $this
226
            ->traitBasedComparison
227
            ->expects(self::atLeastOnce())
0 ignored issues
show
Bug introduced by
The method expects() does not exist on Roave\ApiCompare\Compara...y\TraitBased\TraitBased. ( Ignorable by Annotation )

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

227
            ->/** @scrutinizer ignore-call */ 
228
              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...
228
            ->method('compare')
229
            ->willReturn(Changes::fromArray([
230
                Change::changed('trait change', true),
231
            ]));
232
    }
233
234
    private function traitBasedComparatorWillNotBeCalled() : void
235
    {
236
        $this
237
            ->traitBasedComparison
238
            ->expects(self::never())
239
            ->method('compare');
240
    }
241
}
242