Completed
Pull Request — master (#38)
by Marco
03:32
created

testWillNotRunSubComparatorsIfSymbolsWereDeleted()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

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

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

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

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