Completed
Pull Request — master (#108)
by Marco
04:12
created

Assertion::assertChangesEqual()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 3
dl 0
loc 13
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace RoaveTest\BackwardCompatibility;
6
7
use Generator;
8
use PHPUnit\Framework\Assert;
9
use ReflectionProperty;
10
use Roave\BackwardCompatibility\Changes;
11
12
abstract class Assertion
13
{
14
    /** @var ReflectionProperty|null */
15
    private static $reflectionGenerator;
16
17
    final private function __construct()
18
    {
19
    }
20
21
    public static function assertChangesEqual(
22
        Changes $expected,
23
        Changes $actual,
24
        string $message = ''
25
    ) : void {
26
        Assert::assertInstanceOf(
27
            Generator::class,
28
            self::reflectionGenerator()->getValue($actual),
29
            'Generator must NOT be exhausted'
30
        );
31
        // Forces eager initialisation of the `Changes` instances, allowing us to compare them by value
32
        Assert::assertSame(count($expected), count($actual));
33
        Assert::assertEquals($expected, $actual, $message);
34
    }
35
36
    private static function reflectionGenerator() : ReflectionProperty {
37
        if (self::$reflectionGenerator) {
38
            return self::$reflectionGenerator;
39
        }
40
41
        self::$reflectionGenerator = new ReflectionProperty(Changes::class, 'generator');
42
43
        self::$reflectionGenerator->setAccessible(true);
44
45
        return self::$reflectionGenerator;
46
    }
47
}
48