From 608d5c09adf026989163a1de1176faed8a4a1548 Mon Sep 17 00:00:00 2001 From: Mior Muhammad Zaki Date: Wed, 4 Dec 2024 14:53:21 +0800 Subject: [PATCH] wip Signed-off-by: Mior Muhammad Zaki --- src/functions.php | 10 ++++++++++ tests/HelpersTest.php | 13 +++++++++++++ 2 files changed, 23 insertions(+) diff --git a/src/functions.php b/src/functions.php index f2a93ab51..1cfb2d020 100644 --- a/src/functions.php +++ b/src/functions.php @@ -462,5 +462,15 @@ function laravel_or_fail($app, ?string $caller = null): Application return $app; } + if (\is_null($caller)) { + $caller = transform(debug_backtrace()[1], function ($debug) { + if (isset($debug['class']) && isset($debug['function'])) { + return sprintf('%s::%s', $debug['class'], $debug['function']); + } + + return $debug['function']; + }); + } + throw Exceptions\ApplicationNotAvailableException::make($caller ?? debug_backtrace()[1]['function']); } diff --git a/tests/HelpersTest.php b/tests/HelpersTest.php index e32a689c9..b7960268e 100644 --- a/tests/HelpersTest.php +++ b/tests/HelpersTest.php @@ -3,9 +3,11 @@ namespace Orchestra\Testbench\Tests; use Illuminate\Foundation\Application; +use Orchestra\Testbench\Exceptions\ApplicationNotAvailableException; use Orchestra\Testbench\TestCase; use PHPUnit\Runner\Version; +use function Orchestra\Testbench\laravel_or_fail; use function Orchestra\Testbench\laravel_version_compare; use function Orchestra\Testbench\phpunit_version_compare; @@ -24,4 +26,15 @@ public function it_can_compare_phpunit_version() $this->assertSame(0, phpunit_version_compare(Version::id())); $this->assertTrue(phpunit_version_compare(Version::id(), '==')); } + + /** + * @test + */ + public function it_can_throw_application_not_available_application_when_app_is_not_laravel() + { + $this->expectException(ApplicationNotAvailableException::class); + $this->expectExceptionMessage(sprintf('Application is not available to run [%s]', __METHOD__)); + + laravel_or_fail(null); + } }