Passed
Pull Request — master (#108)
by Marco
02:35
created

Assertion::assertChangesEqual()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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