Passed
Pull Request — master (#395)
by Wilmer
04:09 queued 01:23
created

Assert::getInaccessibleProperty()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 9
c 1
b 0
f 0
nc 4
nop 3
dl 0
loc 19
rs 9.9666
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
final class Assert extends TestCase
13
{
14
    /**
15
     * Asserts that value is one of expected values.
16
     */
17
    public static function isOneOf(mixed $actual, array $expected, string $message = ''): void
18
    {
19
        self::assertThat($actual, new IsOneOfAssert($expected), $message);
0 ignored issues
show
Bug introduced by
The type Yiisoft\Db\Tests\Support\IsOneOfAssert was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
20
    }
21
22
    /**
23
     * Asserting two strings equality ignoring line endings.
24
     */
25
    public static function equalsWithoutLE(string $expected, string $actual, string $message = ''): void
26
    {
27
        $expected = str_replace("\r\n", "\n", $expected);
28
        $actual = str_replace("\r\n", "\n", $actual);
29
30
        self::assertEquals($expected, $actual, $message);
31
    }
32
33
    /**
34
     * Gets an inaccessible object property.
35
     *
36
     * @param bool $revoke whether to make property inaccessible after getting.
37
     */
38
    public static function getInaccessibleProperty(object $object, string $propertyName, bool $revoke = true): mixed
39
    {
40
        $class = new ReflectionClass($object);
41
42
        while (!$class->hasProperty($propertyName)) {
43
            $class = $class->getParentClass();
44
        }
45
46
        $property = $class->getProperty($propertyName);
47
48
        $property->setAccessible(true);
49
50
        $result = $property->getValue($object);
51
52
        if ($revoke) {
53
            $property->setAccessible(false);
54
        }
55
56
        return $result;
57
    }
58
59
    /**
60
     * Invokes an inaccessible method.
61
     *
62
     * @param object $object The object to invoke the method on.
63
     * @param string $method The name of the method to invoke.
64
     * @param array $args The arguments to pass to the method.
65
     *
66
     * @throws ReflectionException
67
     */
68
    public static function invokeMethod(object $object, string $method, array $args = []): mixed
69
    {
70
        $reflection = new ReflectionObject($object);
71
        $method = $reflection->getMethod($method);
72
        $method->setAccessible(true);
73
74
        return $method->invokeArgs($object, $args);
75
    }
76
}
77