Passed
Push — master ( 88983b...1e7624 )
by Wilmer
02:43
created

Assert::invokeMethod()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 7
c 1
b 1
f 0
dl 0
loc 16
rs 10
cc 2
nc 2
nop 3
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
            $property->setAccessible(true);
48
49
            /** @psalm-var mixed $result */
50
            $result = $property->getValue($object);
51
        }
52
53
        return $result;
54
    }
55
56
    /**
57
     * Invokes an inaccessible method.
58
     *
59
     * @param object $object The object to invoke the method on.
60
     * @param string $method The name of the method to invoke.
61
     * @param array $args The arguments to pass to the method.
62
     */
63
    public static function invokeMethod(object $object, string $method, array $args = []): mixed
64
    {
65
        $reflection = new ReflectionObject($object);
66
67
        $result = null;
68
69
        if ($method !== '') {
70
            $method = $reflection->getMethod($method);
71
72
            $method->setAccessible(true);
73
74
            /** @psalm-var mixed $result */
75
            $result = $method->invokeArgs($object, $args);
76
        }
77
78
        return $result;
79
    }
80
81
    /**
82
     * Sets an inaccessible object property to a designated value.
83
     */
84
    public static function setInaccessibleProperty(
85
        object $object,
86
        string $propertyName,
87
        mixed $value
88
    ): void {
89
        $class = new ReflectionClass($object);
90
91
        if ($propertyName !== '') {
92
            $property = $class->getProperty($propertyName);
93
            $property->setValue($object, $value);
94
        }
95
96
        unset($class, $property);
97
    }
98
}
99