Completed
Pull Request — master (#1)
by James
01:44
created

ComparatorTest   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 45
rs 10
c 0
b 0
f 0
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A testRenamingParametersDoesNotCauseBcBreak() 0 18 1
A testCompare() 0 12 1
A assertEqualsIgnoringOrder() 0 3 1
1
<?php
2
declare(strict_types=1);
3
4
namespace RoaveTest\ApiCompare;
5
6
use Roave\ApiCompare\Comparator;
7
use PHPUnit\Framework\TestCase;
8
use Roave\ApiCompare\Factory\DirectoryReflectorFactory;
9
use Roave\BetterReflection\BetterReflection;
10
use Roave\BetterReflection\Reflector\ClassReflector;
11
use Roave\BetterReflection\SourceLocator\Type\AggregateSourceLocator;
12
use Roave\BetterReflection\SourceLocator\Type\EvaledCodeSourceLocator;
13
use Roave\BetterReflection\SourceLocator\Type\PhpInternalSourceLocator;
14
use Roave\BetterReflection\SourceLocator\Type\StringSourceLocator;
15
16
/**
17
 * @covers \Roave\ApiCompare\Comparator
18
 */
19
final class ComparatorTest extends TestCase
20
{
21
    /**
22
     * @param mixed $expected
23
     * @param mixed $actual
24
     */
25
    private static function assertEqualsIgnoringOrder($expected, $actual): void
26
    {
27
        self::assertEquals($expected, $actual, '', 0.0, 10, true);
28
    }
29
30
    public function testCompare(): void
31
    {
32
        $reflectorFactory = new DirectoryReflectorFactory();
33
        self::assertEqualsIgnoringOrder(
34
            [
35
                '[BC] Parameter something (position 0) in Thing::__construct has been deleted',
36
                '[BC] Method methodGone in class Thing has been deleted',
37
                '[BC] Class ClassGone has been deleted',
38
            ],
39
            (new Comparator())->compare(
40
                $reflectorFactory->__invoke(__DIR__ . '/../asset/api/old'),
41
                $reflectorFactory->__invoke(__DIR__ . '/../asset/api/new')
42
            )
43
        );
44
    }
45
46
    public function testRenamingParametersDoesNotCauseBcBreak(): void
47
    {
48
        $reflectorFactory = function (string $sourceCode): ClassReflector {
49
            $astLocator = (new BetterReflection())->astLocator();
50
            return new ClassReflector(
51
                new AggregateSourceLocator([
52
                    new PhpInternalSourceLocator($astLocator),
53
                    new EvaledCodeSourceLocator($astLocator),
54
                    new StringSourceLocator($sourceCode, $astLocator),
55
                ])
56
            );
57
        };
58
59
        self::assertEqualsIgnoringOrder(
60
            [],
61
            (new Comparator())->compare(
62
                $reflectorFactory('<?php class A { function foo(int $a, string $b) {} }'),
63
                $reflectorFactory('<?php class A { function foo(int $b, string $a) {} }')
64
            )
65
        );
66
    }
67
}
68