Passed
Pull Request — master (#249)
by Wilmer
02:57
created

Assert   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 7
eloc 21
c 2
b 0
f 0
dl 0
loc 80
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A equalsWithoutLE() 0 6 1
A inaccessibleProperty() 0 14 2
A setInaccessibleProperty() 0 13 2
A invokeMethod() 0 16 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\ActiveRecord\Tests\Support;
6
7
use ReflectionClass;
8
use ReflectionObject;
9
10
use function str_replace;
11
12
/**
13
 * @psalm-suppress PropertyNotSetInConstructor
14
 */
15
final class Assert extends \PHPUnit\Framework\TestCase
16
{
17
    /**
18
     * Asserting two strings equality ignoring line endings.
19
     *
20
     * @param string $expected The expected string.
21
     * @param string $actual The actual string.
22
     * @param string $message The message to display if the assertion fails.
23
     */
24
    public static function equalsWithoutLE(string $expected, string $actual, string $message = ''): void
25
    {
26
        $expected = str_replace("\r\n", "\n", $expected);
27
        $actual = str_replace("\r\n", "\n", $actual);
28
29
        self::assertEquals($expected, $actual, $message);
30
    }
31
32
    /**
33
     * Gets an inaccessible object property.
34
     *
35
     * @param object $object The object to get the property from.
36
     * @param string $propertyName The name of the property to get.
37
     */
38
    public static function inaccessibleProperty(object $object, string $propertyName): mixed
39
    {
40
        $class = new ReflectionClass($object);
41
42
        $result = null;
43
44
        if ($propertyName !== '') {
45
            $property = $class->getProperty($propertyName);
46
47
            /** @psalm-var mixed $result */
48
            $result = $property->getValue($object);
49
        }
50
51
        return $result;
52
    }
53
54
    /**
55
     * Invokes an inaccessible method.
56
     *
57
     * @param object $object The object to invoke the method on.
58
     * @param string $method The name of the method to invoke.
59
     * @param array $args The arguments to pass to the method.
60
     */
61
    public static function invokeMethod(object $object, string $method, array $args = []): mixed
62
    {
63
        $reflection = new ReflectionObject($object);
64
65
        $result = null;
66
67
        if ($method !== '') {
68
            $method = $reflection->getMethod($method);
69
70
            $method->setAccessible(true);
71
72
            /** @psalm-var mixed $result */
73
            $result = $method->invokeArgs($object, $args);
74
        }
75
76
        return $result;
77
    }
78
79
    /**
80
     * Sets an inaccessible object property to a designated value.
81
     */
82
    public static function setInaccessibleProperty(
83
        object $object,
84
        string $propertyName,
85
        mixed $value
86
    ): void {
87
        $class = new ReflectionClass($object);
88
89
        if ($propertyName !== '') {
90
            $property = $class->getProperty($propertyName);
91
            $property->setValue($object, $value);
92
        }
93
94
        unset($class, $property);
95
    }
96
}
97