From 1a473f820f434fd71359f67ad62fea3e70e6e31d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Qu=E1=BB=B3nh=20Nguy=E1=BB=85n?= Date: Fri, 14 Jun 2024 23:21:59 +0700 Subject: [PATCH] [11.x] Container shares fixed values/initialized instances instead of singleton closure resolutions (#51804) * Container shares fixed values/initialized instances instead of singleton closure resolutions * Style code --- tests/Auth/AuthorizeMiddlewareTest.php | 10 +- tests/Database/PruneCommandTest.php | 2 +- .../Bootstrap/HandleExceptionsTest.php | 6 +- .../Console/RouteListCommandTest.php | 4 +- tests/Mail/MailableQueuedTest.php | 4 +- tests/Routing/RoutingRouteTest.php | 96 +++++-------------- tests/Testing/TestResponseTest.php | 8 +- tests/View/Blade/BladeEchoHandlerTest.php | 12 +-- 8 files changed, 36 insertions(+), 106 deletions(-) diff --git a/tests/Auth/AuthorizeMiddlewareTest.php b/tests/Auth/AuthorizeMiddlewareTest.php index 3b221dd97239..0186d54536b7 100644 --- a/tests/Auth/AuthorizeMiddlewareTest.php +++ b/tests/Auth/AuthorizeMiddlewareTest.php @@ -43,9 +43,7 @@ protected function setUp(): void $this->container->bind(CallableDispatcherContract::class, fn ($app) => new CallableDispatcher($app)); - $this->container->singleton(Registrar::class, function () { - return $this->router; - }); + $this->container->instance(Registrar::class, $this->router); } protected function tearDown(): void @@ -318,10 +316,8 @@ public function testModelInstanceAsParameter() $request = m::mock(Request::class); - $nextParam = null; - - $next = function ($param) use (&$nextParam) { - $nextParam = $param; + $next = function () { + // }; (new Authorize($this->gate())) diff --git a/tests/Database/PruneCommandTest.php b/tests/Database/PruneCommandTest.php index 96c036c9094b..96ecd4348e56 100644 --- a/tests/Database/PruneCommandTest.php +++ b/tests/Database/PruneCommandTest.php @@ -212,7 +212,7 @@ public function testTheCommandDispatchesEvents() }); $dispatcher->shouldReceive('forget')->once()->with(ModelsPruned::class); - Application::getInstance()->singleton(DispatcherContract::class, fn () => $dispatcher); + Application::getInstance()->instance(DispatcherContract::class, $dispatcher); $this->artisan(['--model' => PrunableTestModelWithPrunableRecords::class]); } diff --git a/tests/Foundation/Bootstrap/HandleExceptionsTest.php b/tests/Foundation/Bootstrap/HandleExceptionsTest.php index 43762e03c696..bec5ca03b34d 100644 --- a/tests/Foundation/Bootstrap/HandleExceptionsTest.php +++ b/tests/Foundation/Bootstrap/HandleExceptionsTest.php @@ -23,11 +23,7 @@ protected function setUp(): void { $this->app = m::mock(Application::setInstance(new Application)); - $this->config = new Config(); - - $this->app->singleton('config', function () { - return $this->config; - }); + $this->app->instance('config', $this->config = new Config()); } protected function handleExceptions() diff --git a/tests/Foundation/Console/RouteListCommandTest.php b/tests/Foundation/Console/RouteListCommandTest.php index b032b36acf64..bc251434e57a 100644 --- a/tests/Foundation/Console/RouteListCommandTest.php +++ b/tests/Foundation/Console/RouteListCommandTest.php @@ -48,9 +48,7 @@ protected function setUp(): void $kernel->prependToMiddlewarePriority('Middleware 5'); - $laravel->singleton(Kernel::class, function () use ($kernel) { - return $kernel; - }); + $laravel->instance(Kernel::class, $kernel); $router->get('/example', function () { return 'Hello World'; diff --git a/tests/Mail/MailableQueuedTest.php b/tests/Mail/MailableQueuedTest.php index 274dea386754..7570d0a297a1 100644 --- a/tests/Mail/MailableQueuedTest.php +++ b/tests/Mail/MailableQueuedTest.php @@ -66,9 +66,7 @@ public function testQueuedMailableWithAttachmentFromDiskSent() $filesystemFactory = $this->getMockBuilder(FilesystemManager::class) ->setConstructorArgs([$app]) ->getMock(); - $container->singleton('filesystem', function () use ($filesystemFactory) { - return $filesystemFactory; - }); + $container->instance('filesystem', $filesystemFactory); $queueFake = new QueueFake($app); $mailer = $this->getMockBuilder(Mailer::class) ->setConstructorArgs($this->getMocks()) diff --git a/tests/Routing/RoutingRouteTest.php b/tests/Routing/RoutingRouteTest.php index 58871055a6b1..519b36b112a4 100644 --- a/tests/Routing/RoutingRouteTest.php +++ b/tests/Routing/RoutingRouteTest.php @@ -492,9 +492,7 @@ public function testNullValuesCanBeInjectedIntoRoutes() { $container = new Container; $router = new Router(new Dispatcher, $container); - $container->singleton(Registrar::class, function () use ($router) { - return $router; - }); + $container->instance(Registrar::class, $router); $container->bind(RoutingTestUserModel::class, function () { }); @@ -1099,9 +1097,7 @@ public function testModelBindingThroughIOC() { $container = new Container; $router = new Router(new Dispatcher, $container); - $container->singleton(Registrar::class, function () use ($router) { - return $router; - }); + $container->instance(Registrar::class, $router); $container->bind(CallableDispatcherContract::class, fn ($app) => new CallableDispatcher($app)); $container->bind(RouteModelInterface::class, RouteModelBindingStub::class); $router->get('foo/{bar}', ['middleware' => SubstituteBindings::class, 'uses' => function ($name) { @@ -1598,9 +1594,7 @@ public function testRouterFiresRoutedEvent() { $container = new Container; $router = new Router(new Dispatcher, $container); - $container->singleton(Registrar::class, function () use ($router) { - return $router; - }); + $container->instance(Registrar::class, $router); $router->get('foo/bar', function () { return ''; }); @@ -1634,9 +1628,7 @@ public function testRouterFiresRouteMatchingEvent() { $container = new Container; $router = new Router($events = new Dispatcher, $container); - $container->singleton(Registrar::class, function () use ($router) { - return $router; - }); + $container->instance(Registrar::class, $router); $container->bind(CallableDispatcherContract::class, fn ($app) => new CallableDispatcher($app)); $router->get('foo/bar', function () { return ''; @@ -1988,9 +1980,7 @@ public function testImplicitBindingThroughIOC() { $container = new Container; $router = new Router(new Dispatcher, $container); - $container->singleton(Registrar::class, function () use ($router) { - return $router; - }); + $container->instance(Registrar::class, $router); $container->bind(CallableDispatcherContract::class, fn ($app) => new CallableDispatcher($app)); $container->bind(RoutingTestUserModel::class, RoutingTestExtendedUserModel::class); @@ -2067,17 +2057,11 @@ public function testRouteRedirect() { $container = new Container; $router = new Router(new Dispatcher, $container); - $container->singleton(Registrar::class, function () use ($router) { - return $router; - }); + $container->instance(Registrar::class, $router); $request = Request::create('contact_us', 'GET'); - $container->singleton(Request::class, function () use ($request) { - return $request; - }); + $container->instance(Request::class, $request); $urlGenerator = new UrlGenerator(new RouteCollection, $request); - $container->singleton(UrlGenerator::class, function () use ($urlGenerator) { - return $urlGenerator; - }); + $container->instance(UrlGenerator::class, $urlGenerator); $router->get('contact_us', function () { throw new Exception('Route should not be reachable.'); }); @@ -2092,17 +2076,11 @@ public function testRouteRedirectRetainsExistingStartingForwardSlash() { $container = new Container; $router = new Router(new Dispatcher, $container); - $container->singleton(Registrar::class, function () use ($router) { - return $router; - }); + $container->instance(Registrar::class, $router); $request = Request::create('contact_us', 'GET'); - $container->singleton(Request::class, function () use ($request) { - return $request; - }); + $container->instance(Request::class, $request); $urlGenerator = new UrlGenerator(new RouteCollection, $request); - $container->singleton(UrlGenerator::class, function () use ($urlGenerator) { - return $urlGenerator; - }); + $container->instance(UrlGenerator::class, $urlGenerator); $router->get('contact_us', function () { throw new Exception('Route should not be reachable.'); }); @@ -2117,17 +2095,11 @@ public function testRouteRedirectStripsMissingStartingForwardSlash() { $container = new Container; $router = new Router(new Dispatcher, $container); - $container->singleton(Registrar::class, function () use ($router) { - return $router; - }); + $container->instance(Registrar::class, $router); $request = Request::create('contact_us', 'GET'); - $container->singleton(Request::class, function () use ($request) { - return $request; - }); + $container->instance(Request::class, $request); $urlGenerator = new UrlGenerator(new RouteCollection, $request); - $container->singleton(UrlGenerator::class, function () use ($urlGenerator) { - return $urlGenerator; - }); + $container->instance(UrlGenerator::class, $urlGenerator); $router->get('contact_us', function () { throw new Exception('Route should not be reachable.'); }); @@ -2145,17 +2117,11 @@ public function testRouteRedirectExceptionWhenMissingExpectedParameters() $container = new Container; $router = new Router(new Dispatcher, $container); - $container->singleton(Registrar::class, function () use ($router) { - return $router; - }); + $container->instance(Registrar::class, $router); $request = Request::create('users', 'GET'); - $container->singleton(Request::class, function () use ($request) { - return $request; - }); + $container->instance(Request::class, $request); $urlGenerator = new UrlGenerator(new RouteCollection, $request); - $container->singleton(UrlGenerator::class, function () use ($urlGenerator) { - return $urlGenerator; - }); + $container->instance(UrlGenerator::class, $urlGenerator); $router->get('users', function () { throw new Exception('Route should not be reachable.'); }); @@ -2168,17 +2134,11 @@ public function testRouteRedirectWithCustomStatus() { $container = new Container; $router = new Router(new Dispatcher, $container); - $container->singleton(Registrar::class, function () use ($router) { - return $router; - }); + $container->instance(Registrar::class, $router); $request = Request::create('contact_us', 'GET'); - $container->singleton(Request::class, function () use ($request) { - return $request; - }); + $container->instance(Request::class, $request); $urlGenerator = new UrlGenerator(new RouteCollection, $request); - $container->singleton(UrlGenerator::class, function () use ($urlGenerator) { - return $urlGenerator; - }); + $container->instance(UrlGenerator::class, $urlGenerator); $router->get('contact_us', function () { throw new Exception('Route should not be reachable.'); }); @@ -2193,17 +2153,11 @@ public function testRoutePermanentRedirect() { $container = new Container; $router = new Router(new Dispatcher, $container); - $container->singleton(Registrar::class, function () use ($router) { - return $router; - }); + $container->instance(Registrar::class, $router); $request = Request::create('contact_us', 'GET'); - $container->singleton(Request::class, function () use ($request) { - return $request; - }); + $container->instance(Request::class, $request); $urlGenerator = new UrlGenerator(new RouteCollection, $request); - $container->singleton(UrlGenerator::class, function () use ($urlGenerator) { - return $urlGenerator; - }); + $container->instance(UrlGenerator::class, $urlGenerator); $router->get('contact_us', function () { throw new Exception('Route should not be reachable.'); }); @@ -2272,9 +2226,7 @@ protected function getRouter($container = null) $router = new Router($container->make(Dispatcher::class), $container); - $container->singleton(Registrar::class, function () use ($router) { - return $router; - }); + $container->instance(Registrar::class, $router); $container->bind(ControllerDispatcherContract::class, fn ($app) => new ControllerDispatcher($app)); $container->bind(CallableDispatcherContract::class, fn ($app) => new CallableDispatcher($app)); diff --git a/tests/Testing/TestResponseTest.php b/tests/Testing/TestResponseTest.php index d448f62f1d0a..9a4632954099 100644 --- a/tests/Testing/TestResponseTest.php +++ b/tests/Testing/TestResponseTest.php @@ -2199,9 +2199,7 @@ public function testAssertCookie() { $container = Container::getInstance(); $encrypter = new Encrypter(str_repeat('a', 16)); - $container->singleton('encrypter', function () use ($encrypter) { - return $encrypter; - }); + $container->instance('encrypter', $encrypter); $cookieName = 'cookie-name'; $cookieValue = 'cookie-value'; @@ -2442,9 +2440,7 @@ public function testGetEncryptedCookie() { $container = Container::getInstance(); $encrypter = new Encrypter(str_repeat('a', 16)); - $container->singleton('encrypter', function () use ($encrypter) { - return $encrypter; - }); + $container->instance('encrypter', $encrypter); $cookieName = 'cookie-name'; $cookieValue = 'cookie-value'; diff --git a/tests/View/Blade/BladeEchoHandlerTest.php b/tests/View/Blade/BladeEchoHandlerTest.php index ec40ac57d73a..e36ca00cd71e 100644 --- a/tests/View/Blade/BladeEchoHandlerTest.php +++ b/tests/View/Blade/BladeEchoHandlerTest.php @@ -60,9 +60,7 @@ public function testHandlerLogicWorksCorrectly($blade) throw new Exception('The fluent object has been successfully handled!'); }); - app()->singleton('blade.compiler', function () { - return $this->compiler; - }); + app()->instance('blade.compiler', $this->compiler); $exampleObject = new Fluent(); @@ -84,9 +82,7 @@ public function testHandlerWorksWithIterables($blade, $closure, $expectedOutput) { $this->compiler->stringable('iterable', $closure); - app()->singleton('blade.compiler', function () { - return $this->compiler; - }); + app()->instance('blade.compiler', $this->compiler); ob_start(); eval(Str::of($this->compiler->compileString($blade))->remove([''])); @@ -108,9 +104,7 @@ public static function handlerWorksWithIterableDataProvider() #[DataProvider('nonStringableDataProvider')] public function testHandlerWorksWithNonStringables($blade, $expectedOutput) { - app()->singleton('blade.compiler', function () { - return $this->compiler; - }); + app()->instance('blade.compiler', $this->compiler); ob_start(); eval(Str::of($this->compiler->compileString($blade))->remove(['']));