Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
20 / 20 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| TestUtils | |
100.00% |
20 / 20 |
|
100.00% |
2 / 2 |
9 | |
100.00% |
1 / 1 |
| __construct | n/a |
0 / 0 |
n/a |
0 / 0 |
1 | |||||
| setMember | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
4 | |||
| getMember | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
4 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Jingga |
| 4 | * |
| 5 | * PHP Version 8.1 |
| 6 | * |
| 7 | * @package phpOMS\Utils |
| 8 | * @copyright Dennis Eichhorn |
| 9 | * @license OMS License 2.0 |
| 10 | * @version 1.0.0 |
| 11 | * @link https://jingga.app |
| 12 | */ |
| 13 | declare(strict_types=1); |
| 14 | |
| 15 | namespace phpOMS\Utils; |
| 16 | |
| 17 | /** |
| 18 | * Test utils. |
| 19 | * |
| 20 | * Only for testing purposes. MUST NOT be used for other purposes. |
| 21 | * |
| 22 | * @package phpOMS\Utils |
| 23 | * @license OMS License 2.0 |
| 24 | * @link https://jingga.app |
| 25 | * @since 1.0.0 |
| 26 | */ |
| 27 | final class TestUtils |
| 28 | { |
| 29 | /** |
| 30 | * Constructor. |
| 31 | * |
| 32 | * @since 1.0.0 |
| 33 | * @codeCoverageIgnore |
| 34 | */ |
| 35 | private function __construct() |
| 36 | { |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Set private object member |
| 41 | * |
| 42 | * @param object $obj Object to modify |
| 43 | * @param string $name Member name to modify |
| 44 | * @param mixed $value Value to set |
| 45 | * |
| 46 | * @return bool The function returns true after setting the member |
| 47 | * |
| 48 | * @since 1.0.0 |
| 49 | */ |
| 50 | public static function setMember(object $obj, string $name, mixed $value) : bool |
| 51 | { |
| 52 | $reflectionClass = new \ReflectionClass(\get_class($obj)); |
| 53 | |
| 54 | if (!$reflectionClass->hasProperty($name)) { |
| 55 | return false; |
| 56 | } |
| 57 | |
| 58 | $reflectionProperty = $reflectionClass->getProperty($name); |
| 59 | |
| 60 | if (!($accessible = $reflectionProperty->isPublic())) { |
| 61 | $reflectionProperty->setAccessible(true); |
| 62 | } |
| 63 | |
| 64 | $reflectionProperty->setValue($obj, $value); |
| 65 | |
| 66 | if (!$accessible) { |
| 67 | $reflectionProperty->setAccessible(false); |
| 68 | } |
| 69 | |
| 70 | return true; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Get private object member |
| 75 | * |
| 76 | * @param object $obj Object to read |
| 77 | * @param string $name Member name to read |
| 78 | * |
| 79 | * @return mixed Returns the member variable value |
| 80 | * |
| 81 | * @since 1.0.0 |
| 82 | */ |
| 83 | public static function getMember(object $obj, string $name) : mixed |
| 84 | { |
| 85 | $reflectionClass = new \ReflectionClass($obj); |
| 86 | |
| 87 | if (!$reflectionClass->hasProperty($name)) { |
| 88 | return null; |
| 89 | } |
| 90 | |
| 91 | $reflectionProperty = $reflectionClass->getProperty($name); |
| 92 | |
| 93 | if (!($accessible = $reflectionProperty->isPublic())) { |
| 94 | $reflectionProperty->setAccessible(true); |
| 95 | } |
| 96 | |
| 97 | $value = $reflectionProperty->getValue($obj); |
| 98 | |
| 99 | if (!$accessible) { |
| 100 | $reflectionProperty->setAccessible(false); |
| 101 | } |
| 102 | |
| 103 | return $value; |
| 104 | } |
| 105 | } |