-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtest-utils.php
59 lines (52 loc) · 1.67 KB
/
test-utils.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?php declare(strict_types = 1);
/**
* Gets private property of a class.
*
* @param class-string $class_name Name of the class.
* @param string $property_name Name of the property.
*/
function get_private_property( string $class_name, string $property_name ): ReflectionProperty {
$reflector = new ReflectionClass( $class_name );
$property = $reflector->getProperty( $property_name );
/**
* @psalm-suppress UnusedMethodCall
*/
$property->setAccessible( true );
return $property;
}
/**
* Overrides the value of a private property on a given object. This is
* useful when mocking the internals of a class.
*
* Note that the property will no longer be private after setAccessible is
* called.
*
* @param class-string $class_name The fully qualified class name, including namespace.
* @param object $object_instance The object instance on which to set the value.
* @param string $property_name The name of the private property to override.
* @param mixed $value The value to set.
*/
function set_private_property(
string $class_name,
?object $object_instance,
string $property_name,
mixed $value
): void {
$property = get_private_property( $class_name, $property_name );
$property->setValue( $object_instance, $value );
}
/**
* Gets private method of a class.
*
* @param class-string $class_name Name of the class.
* @param string $method Name of the method.
*/
function get_private_method( string $class_name, string $method ): ReflectionMethod {
$reflector = new ReflectionClass( $class_name );
$method = $reflector->getMethod( $method );
/**
* @psalm-suppress UnusedMethodCall
*/
$method->setAccessible( true );
return $method;
}