|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Db\Tests\Support; |
|
6
|
|
|
|
|
7
|
|
|
use PHPUnit\Framework\Assert as PHPUnit; |
|
8
|
|
|
use ReflectionClass; |
|
9
|
|
|
use ReflectionException; |
|
10
|
|
|
use ReflectionObject; |
|
11
|
|
|
|
|
12
|
|
|
final class Assert extends PHPUnit |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* Asserts that value is one of expected values. |
|
16
|
|
|
*/ |
|
17
|
|
|
public static function assertIsOneOf(mixed $actual, array $expected, string $message = ''): void |
|
18
|
|
|
{ |
|
19
|
|
|
self::assertThat($actual, new IsOneOfAssert($expected), $message); |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Gets an inaccessible object property. |
|
24
|
|
|
* |
|
25
|
|
|
* @param bool $revoke whether to make property inaccessible after getting. |
|
26
|
|
|
*/ |
|
27
|
|
|
public static function getInaccessibleProperty(object $object, string $propertyName, bool $revoke = true): mixed |
|
28
|
|
|
{ |
|
29
|
|
|
$class = new ReflectionClass($object); |
|
30
|
|
|
|
|
31
|
|
|
while (!$class->hasProperty($propertyName)) { |
|
32
|
|
|
$class = $class->getParentClass(); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
$property = $class->getProperty($propertyName); |
|
36
|
|
|
$property->setAccessible(true); |
|
37
|
|
|
$result = $property->getValue($object); |
|
38
|
|
|
|
|
39
|
|
|
if ($revoke) { |
|
40
|
|
|
$property->setAccessible(false); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
return $result; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Invokes an inaccessible method. |
|
48
|
|
|
* |
|
49
|
|
|
* @param bool $revoke whether to make method inaccessible after execution. |
|
50
|
|
|
* |
|
51
|
|
|
* @throws ReflectionException |
|
52
|
|
|
*/ |
|
53
|
|
|
public static function invokeMethod(object $object, string $method, array $args = [], bool $revoke = true): mixed |
|
54
|
|
|
{ |
|
55
|
|
|
$reflection = new ReflectionObject($object); |
|
56
|
|
|
|
|
57
|
|
|
$method = $reflection->getMethod($method); |
|
58
|
|
|
|
|
59
|
|
|
$method->setAccessible(true); |
|
60
|
|
|
|
|
61
|
|
|
$result = $method->invokeArgs($object, $args); |
|
62
|
|
|
|
|
63
|
|
|
if ($revoke) { |
|
64
|
|
|
$method->setAccessible(false); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
return $result; |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|