|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Common tests stuff |
|
4
|
|
|
* |
|
5
|
|
|
* @see https://github.com/sergeymakinen/tests |
|
6
|
|
|
* @copyright Copyright (c) 2017 Sergey Makinen (https://makinen.ru) |
|
7
|
|
|
* @license https://github.com/sergeymakinen/tests/blob/master/LICENSE MIT License |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace SergeyMakinen\Tests\Util; |
|
11
|
|
|
|
|
12
|
|
|
trait ExtensionTrait |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* Returns the reflected property. |
|
16
|
|
|
* |
|
17
|
|
|
* @param object|string $object |
|
18
|
|
|
* @param string $name |
|
19
|
|
|
* @return \ReflectionProperty |
|
20
|
|
|
*/ |
|
21
|
6 |
|
protected function getProperty($object, $name) |
|
22
|
|
|
{ |
|
23
|
6 |
|
$class = new \ReflectionClass($object); |
|
24
|
6 |
|
while (!$class->hasProperty($name)) { |
|
25
|
6 |
|
$class = $class->getParentClass(); |
|
26
|
4 |
|
} |
|
27
|
6 |
|
return $class->getProperty($name); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Returns the private/protected property by its name. |
|
32
|
|
|
* |
|
33
|
|
|
* @param object|string $object |
|
34
|
|
|
* @param string $name |
|
35
|
|
|
* @return mixed |
|
36
|
|
|
*/ |
|
37
|
6 |
|
protected function getInaccessibleProperty($object, $name) |
|
38
|
|
|
{ |
|
39
|
6 |
|
$property = $this->getProperty($object, $name); |
|
40
|
6 |
|
$property->setAccessible(true); |
|
41
|
6 |
|
return $property->getValue(is_object($object) ? $object : null); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Sets the private/protected property value by its name. |
|
46
|
|
|
* |
|
47
|
|
|
* @param object|string $object |
|
48
|
|
|
* @param string $name |
|
49
|
|
|
* @param mixed $value |
|
50
|
|
|
*/ |
|
51
|
6 |
|
protected function setInaccessibleProperty($object, $name, $value) |
|
52
|
|
|
{ |
|
53
|
6 |
|
$property = $this->getProperty($object, $name); |
|
54
|
6 |
|
$property->setAccessible(true); |
|
55
|
6 |
|
$property->setValue(is_object($object) ? $object : null, $value); |
|
56
|
6 |
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Invokes the private/protected method by its name and returns its result. |
|
60
|
|
|
* |
|
61
|
|
|
* @param object|string $object |
|
62
|
|
|
* @param string $name |
|
63
|
|
|
* @param array $args |
|
64
|
|
|
* @return mixed |
|
65
|
|
|
*/ |
|
66
|
6 |
|
protected function invokeInaccessibleMethod($object, $name, array $args = []) |
|
67
|
|
|
{ |
|
68
|
6 |
|
$method = new \ReflectionMethod($object, $name); |
|
69
|
6 |
|
$method->setAccessible(true); |
|
70
|
6 |
|
return $method->invokeArgs(is_object($object) ? $object : null, $args); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|