From 1c1766ff199be95b4d4c546b56293bc3f723acb8 Mon Sep 17 00:00:00 2001 From: "Ralph J. Smit" <59207045+ralphjsmit@users.noreply.github.com> Date: Thu, 1 Sep 2022 20:37:29 +0200 Subject: [PATCH] [9.x] Make Application macroable (#43966) * Make Applicaiton macroable * Update FoundationApplicationTest.php --- src/Illuminate/Foundation/Application.php | 3 +++ tests/Foundation/FoundationApplicationTest.php | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/src/Illuminate/Foundation/Application.php b/src/Illuminate/Foundation/Application.php index 196c7da68f09..b3f43006f0df 100755 --- a/src/Illuminate/Foundation/Application.php +++ b/src/Illuminate/Foundation/Application.php @@ -21,6 +21,7 @@ use Illuminate\Support\Env; use Illuminate\Support\ServiceProvider; use Illuminate\Support\Str; +use Illuminate\Support\Traits\Macroable; use RuntimeException; use Symfony\Component\HttpFoundation\Request as SymfonyRequest; use Symfony\Component\HttpFoundation\Response as SymfonyResponse; @@ -30,6 +31,8 @@ class Application extends Container implements ApplicationContract, CachesConfiguration, CachesRoutes, HttpKernelInterface { + use Macroable; + /** * The Laravel framework version. * diff --git a/tests/Foundation/FoundationApplicationTest.php b/tests/Foundation/FoundationApplicationTest.php index d96cf62311eb..f22c81c59f37 100755 --- a/tests/Foundation/FoundationApplicationTest.php +++ b/tests/Foundation/FoundationApplicationTest.php @@ -7,6 +7,7 @@ use Illuminate\Foundation\Application; use Illuminate\Foundation\Bootstrap\RegisterFacades; use Illuminate\Foundation\Events\LocaleUpdated; +use Illuminate\Support\Facades\App; use Illuminate\Support\ServiceProvider; use Mockery as m; use PHPUnit\Framework\TestCase; @@ -499,6 +500,23 @@ public function testEnvPathsAreAbsoluteInWindows() $_SERVER['APP_EVENTS_CACHE'] ); } + + /** @test */ + public function testMacroable(): void + { + $app = new Application; + $app['env'] = 'foo'; + + $app->macro('foo', function() { + return $this->environment('foo'); + }); + + $this->assertTrue($app->foo()); + + $app['env'] = 'bar'; + + $this->assertFalse($app->foo()); + } } class ApplicationBasicServiceProviderStub extends ServiceProvider