Passed
Pull Request — master (#431)
by Wilmer
02:38
created

Assert::isOneOf()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Tests\Support;
6
7
use PHPUnit\Framework\TestCase;
8
use ReflectionClass;
9
use ReflectionException;
10
use ReflectionObject;
11
12
/**
13
 * @psalm-suppress PropertyNotSetInConstructor
14
 */
15
final class Assert extends TestCase
16
{
17
    /**
18
     * Asserts that value is one of expected values.
19
     */
20
    public static function isOneOf(mixed $actual, array $expected, string $message = ''): void
21
    {
22
        self::assertThat($actual, new IsOneOfAssert($expected), $message);
23
    }
24
25
    /**
26
     * Asserting two strings equality ignoring line endings.
27
     */
28
    public static function equalsWithoutLE(string $expected, string $actual, string $message = ''): void
29
    {
30
        $expected = str_replace("\r\n", "\n", $expected);
31
        $actual = str_replace("\r\n", "\n", $actual);
32
33
        self::assertEquals($expected, $actual, $message);
34
    }
35
36
    /**
37
     * Gets an inaccessible object property.
38
     *
39
     * @param bool $revoke whether to make property inaccessible after getting.
40
     */
41
    public static function getInaccessibleProperty(object $object, string $propertyName, bool $revoke = true): mixed
42
    {
43
        $class = new ReflectionClass($object);
44
45
        while (!$class->hasProperty($propertyName)) {
46
            $class = $class->getParentClass();
47
        }
48
49
        $property = $class->getProperty($propertyName);
50
51
        $property->setAccessible(true);
52
53
        /** @psalm-var mixed $result */
54
        $result = $property->getValue($object);
55
56
        if ($revoke) {
57
            $property->setAccessible(false);
58
        }
59
60
        return $result;
61
    }
62
63
    /**
64
     * Sets an inaccessible object property.
65
     *
66
     * @param object $object The object to set the property on.
67
     * @param string $propertyName The name of the property to set.
68
     * @param mixed $value The value to set.
69
     */
70
    public static function setInaccessibleProperty(object $object, string $propertyName, mixed $value): void
71
    {
72
        $class = new ReflectionClass($object);
73
74
        while (!$class->hasProperty($propertyName)) {
75
            $class = $class->getParentClass();
76
        }
77
78
        $property = $class->getProperty($propertyName);
79
        $property->setAccessible(true);
80
        $property->setValue($object, $value);
81
    }
82
83
    /**
84
     * Invokes an inaccessible method.
85
     *
86
     * @param object $object The object to invoke the method on.
87
     * @param string $method The name of the method to invoke.
88
     * @param array $args The arguments to pass to the method.
89
     *
90
     * @throws ReflectionException
91
     */
92
    public static function invokeMethod(object $object, string $method, array $args = []): mixed
93
    {
94
        $reflection = new ReflectionObject($object);
95
        $method = $reflection->getMethod($method);
96
        $method->setAccessible(true);
97
98
        return $method->invokeArgs($object, $args);
99
    }
100
}
101