diff --git a/src/Support/Reflection.php b/src/Support/Reflection.php index 4c0cecf6..44805ba2 100644 --- a/src/Support/Reflection.php +++ b/src/Support/Reflection.php @@ -27,11 +27,19 @@ final class Reflection { $reflectionClass = new ReflectionClass($object); - $reflectionMethod = $reflectionClass->getMethod($method); + try { + $reflectionMethod = $reflectionClass->getMethod($method); - $reflectionMethod->setAccessible(true); + $reflectionMethod->setAccessible(true); - return $reflectionMethod->invoke($object, ...$args); + return $reflectionMethod->invoke($object, ...$args); + } catch (ReflectionException $exception) { + if (method_exists($object, '__call')) { + return $object->__call($method, $args); + } + + throw $exception; + } } /** diff --git a/tests/.snapshots/success.txt b/tests/.snapshots/success.txt index 57e655aa..4c61fce4 100644 --- a/tests/.snapshots/success.txt +++ b/tests/.snapshots/success.txt @@ -75,6 +75,10 @@ ✓ it is a test ✓ it is a higher order message test + PASS Tests\Features\Macro + ✓ it can call chained macro method + ✓ it will throw exception from call if no macro exists + PASS Tests\Features\Mocks ✓ it has bar @@ -159,5 +163,5 @@ WARN Tests\Visual\Success s visual snapshot of test suite on success - Tests: 6 skipped, 92 passed - Time: 3.40s + Tests: 6 skipped, 94 passed + Time: 3.73s diff --git a/tests/Features/Macro.php b/tests/Features/Macro.php new file mode 100644 index 00000000..fa9f63ee --- /dev/null +++ b/tests/Features/Macro.php @@ -0,0 +1,16 @@ +macro('bar', function () { + assertInstanceOf(TestCase::class, $this); +}); + +it('can call chained macro method')->bar(); + +it('will throw exception from call if no macro exists') + ->throws(BadMethodCallException::class) + ->foo();