From 278af4b8354f8209cc258823bb155cadf8ea195d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A9lix=20Dorn?= Date: Tue, 25 Aug 2020 13:45:55 +0200 Subject: [PATCH] forward bad TestCase method calls to global functions --- src/Support/Reflection.php | 7 +++++++ tests/.snapshots/success.txt | 3 ++- tests/Autoload.php | 5 +++++ tests/Features/Helpers.php | 2 ++ 4 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/Support/Reflection.php b/src/Support/Reflection.php index 984cfef6..1266374b 100644 --- a/src/Support/Reflection.php +++ b/src/Support/Reflection.php @@ -6,6 +6,7 @@ namespace Pest\Support; use Closure; use Pest\Exceptions\ShouldNotHappen; +use Pest\TestSuite; use ReflectionClass; use ReflectionException; use ReflectionFunction; @@ -40,6 +41,12 @@ final class Reflection return $object->__call($method, $args); } + if (is_callable($method)) { + return Closure::fromCallable($method)->bindTo( + TestSuite::getInstance()->test + )(...$args); + } + throw $exception; } } diff --git a/tests/.snapshots/success.txt b/tests/.snapshots/success.txt index 240e75f9..8e16357d 100644 --- a/tests/.snapshots/success.txt +++ b/tests/.snapshots/success.txt @@ -244,6 +244,7 @@ ✓ it throws error if property do not exist ✓ it allows to call underlying protected/private methods ✓ it throws error if method do not exist + ✓ it can forward unexpected calls to any global function PASS Tests\Features\HigherOrderTests ✓ it proxies calls to object @@ -352,5 +353,5 @@ ✓ depends with defined arguments ✓ depends run test only once - Tests: 6 skipped, 207 passed + Tests: 6 skipped, 208 passed \ No newline at end of file diff --git a/tests/Autoload.php b/tests/Autoload.php index 1e181ad2..1d53a325 100644 --- a/tests/Autoload.php +++ b/tests/Autoload.php @@ -22,3 +22,8 @@ trait SecondPluginTrait Pest\Plugin::uses(PluginTrait::class); Pest\Plugin::uses(SecondPluginTrait::class); + +function _assertThat() +{ + expect(true)->toBeTrue(); +} diff --git a/tests/Features/Helpers.php b/tests/Features/Helpers.php index 163ede9d..3203da9b 100644 --- a/tests/Features/Helpers.php +++ b/tests/Features/Helpers.php @@ -40,3 +40,5 @@ it('allows to call underlying protected/private methods', function () { it('throws error if method do not exist', function () { test()->name(); })->throws(\ReflectionException::class, 'Call to undefined method PHPUnit\Framework\TestCase::name()'); + +it('can forward unexpected calls to any global function')->_assertThat();