Skip to content

Commit 6d7611f

Browse files
committed
feat: add Testing::getProperty
1 parent ad00aa4 commit 6d7611f

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

src/Testing/TestingHelper.php

+22-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace Php\Support\Testing;
66

7-
use function instance;
7+
use ReflectionClass;
88

99
trait TestingHelper
1010
{
@@ -21,12 +21,30 @@ trait TestingHelper
2121
* @example
2222
* $result = $this->runProtectedMethod($sp, "registerService", T::class);
2323
*/
24-
public function runProtectedMethod(string|object $class, string $method, ...$params): mixed
24+
protected function runProtectedMethod(string|object $class, string $method, ...$params): mixed
2525
{
26-
$class = instance($class);
27-
2826
$methodReflex = new \ReflectionMethod($class, $method);
2927
$methodReflex->setAccessible(true);
3028
return $methodReflex->invoke($class, ...$params);
3129
}
30+
31+
/**
32+
* Get a instance property (public/private/protected) value.
33+
*
34+
* @param object|string $object
35+
* @param string $property_name
36+
*
37+
* @return mixed
38+
* @throws \ReflectionException
39+
*
40+
*/
41+
protected function getProperty(object|string $object, string $propertyName): mixed
42+
{
43+
$reflection = new ReflectionClass($object);
44+
45+
$property = $reflection->getProperty($propertyName);
46+
$property->setAccessible(true);
47+
48+
return $property->getValue($object);
49+
}
3250
}

0 commit comments

Comments
 (0)