Completed
Push — master ( 47534f...7ceab2 )
by Marco
10s
created

ComparatorTest::testCompare()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 10
nc 1
nop 0
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace RoaveTest\ApiCompare;
5
6
use PHPUnit\Framework\MockObject\MockObject;
7
use Roave\ApiCompare\Change;
8
use Roave\ApiCompare\Changes;
9
use Roave\ApiCompare\Comparator;
10
use PHPUnit\Framework\TestCase;
11
use Roave\ApiCompare\Comparator\BackwardsCompatibility\ClassBased\ClassBased;
12
use Roave\ApiCompare\Factory\DirectoryReflectorFactory;
13
14
/**
15
 * @covers \Roave\ApiCompare\Comparator
16
 */
17
final class ComparatorTest extends TestCase
18
{
19
    /** @var StringReflectorFactory|null */
20
    private static $stringReflectorFactory;
21
22
    /** @var ClassBased|MockObject */
23
    private $classBasedComparison;
24
25
    /** @var Comparator */
26
    private $comparator;
27
28
    public static function setUpBeforeClass() : void
29
    {
30
        self::$stringReflectorFactory = new StringReflectorFactory();
31
    }
32
33
    protected function setUp() : void
34
    {
35
        parent::setUp();
36
37
        $this->classBasedComparison = $this->createMock(ClassBased::class);
38
        $this->comparator           = new Comparator($this->classBasedComparison);
39
    }
40
41
    /**
42
     * @param mixed $expected
43
     * @param mixed $actual
44
     */
45
    private static function assertEqualsIgnoringOrder($expected, $actual): void
46
    {
47
        self::assertEquals($expected, $actual, '', 0.0, 10, true);
48
    }
49
50
    public function testRemovingAClassCausesABreak(): void
51
    {
52
        $this->classBasedComparatorWillNotBeCalled();
53
54
        self::assertEqualsIgnoringOrder(
55
            Changes::fromArray([
56
                Change::removed('Class A has been deleted', true),
57
            ]),
58
            $this->comparator->compare(
59
                self::$stringReflectorFactory->__invoke('<?php class A { private function foo() {} }'),
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('<?php class A { private function foo() {} }'),

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
                self::$stringReflectorFactory->__invoke('<?php ')
61
            )
62
        );
63
    }
64
65
    public function testRemovingAPrivateMethodDoesNotCauseBreak(): void
66
    {
67
        $this->classBasedComparatorWillBeCalled();
68
69
        self::assertEqualsIgnoringOrder(
70
            Changes::new(),
71
            $this->comparator->compare(
72
                self::$stringReflectorFactory->__invoke('<?php class A { private function foo() {} }'),
73
                self::$stringReflectorFactory->__invoke('<?php class A { }')
74
            )
75
        );
76
    }
77
78
    public function testRenamingParametersDoesNotCauseBcBreak(): void
79
    {
80
        $this->classBasedComparatorWillBeCalled();
81
82
        self::assertEqualsIgnoringOrder(
83
            Changes::new(),
84
            $this->comparator->compare(
85
                self::$stringReflectorFactory->__invoke('<?php class A { function foo(int $a, string $b) {} }'),
86
                self::$stringReflectorFactory->__invoke('<?php class A { function foo(int $b, string $a) {} }')
87
            )
88
        );
89
    }
90
91
    public function testMakingAClassFinal(): void
92
    {
93
        $this->classBasedComparatorWillBeCalled();
94
95
        self::assertEqualsIgnoringOrder(
96
            Changes::fromArray([
97
                Change::changed('Class A is now final', true),
98
            ]),
99
            $this->comparator->compare(
100
                self::$stringReflectorFactory->__invoke('<?php class A { }'),
101
                self::$stringReflectorFactory->__invoke('<?php final class A { }')
102
            )
103
        );
104
    }
105
106
    private function classBasedComparatorWillBeCalled() : void
107
    {
108
        $this
109
            ->classBasedComparison
110
            ->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

110
            ->/** @scrutinizer ignore-call */ 
111
              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...
111
            ->method('compare')
112
            ->willReturn(Changes::new());
113
    }
114
115
    private function classBasedComparatorWillNotBeCalled() : void
116
    {
117
        $this
118
            ->classBasedComparison
119
            ->expects(self::never())
120
            ->method('compare');
121
    }
122
}
123