|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace MerchOne\PhpApiSdk\Tests; |
| 4 | + |
| 5 | +use PHPUnit\Framework\TestCase as BaseTestCase; |
| 6 | +use ReflectionClass; |
| 7 | +use ReflectionException; |
| 8 | + |
| 9 | +abstract class TestCase extends BaseTestCase |
| 10 | +{ |
| 11 | + /** |
| 12 | + * @param object $object |
| 13 | + * @param string $property |
| 14 | + * @return string|int|float|bool|array|object|null |
| 15 | + * |
| 16 | + * @throws ReflectionException |
| 17 | + */ |
| 18 | + protected function getObjectProperty(object $object, string $property) |
| 19 | + { |
| 20 | + $reflection = new ReflectionClass($object); |
| 21 | + $property = $reflection->getProperty($property); |
| 22 | + $property->setAccessible(true); |
| 23 | + |
| 24 | + return $property->getValue($object); |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * @param object $object |
| 29 | + * @param string $method |
| 30 | + * @param array $arguments |
| 31 | + * @return string|int|float|bool|array|object|null |
| 32 | + * |
| 33 | + * @throws ReflectionException |
| 34 | + */ |
| 35 | + protected function callObjectMethod(object $object, string $method, array $arguments = []) |
| 36 | + { |
| 37 | + $reflection = new ReflectionClass($object); |
| 38 | + $method = $reflection->getMethod($method); |
| 39 | + $method->setAccessible(true); |
| 40 | + |
| 41 | + return $method->invoke($object, ...$arguments); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * @param object $object |
| 46 | + * @param string $property |
| 47 | + * @param $value |
| 48 | + * @return void |
| 49 | + * |
| 50 | + * @throws ReflectionException |
| 51 | + */ |
| 52 | + protected function setObjectProperty(object $object, string $property, $value): void |
| 53 | + { |
| 54 | + $reflection = new ReflectionClass($object); |
| 55 | + $property = $reflection->getProperty($property); |
| 56 | + $property->setAccessible(true); |
| 57 | + $property->setValue($object, $value); |
| 58 | + } |
| 59 | +} |
0 commit comments