Skip to content

Commit

Permalink
[11.x] Container shares fixed values/initialized instances instead of…
Browse files Browse the repository at this point in the history
… singleton closure resolutions (#51804)

* Container shares fixed values/initialized instances instead of singleton closure resolutions

* Style code
  • Loading branch information
seriquynh authored Jun 14, 2024
1 parent c9d9117 commit 1a473f8
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 106 deletions.
10 changes: 3 additions & 7 deletions tests/Auth/AuthorizeMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()))
Expand Down
2 changes: 1 addition & 1 deletion tests/Database/PruneCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
Expand Down
6 changes: 1 addition & 5 deletions tests/Foundation/Bootstrap/HandleExceptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 1 addition & 3 deletions tests/Foundation/Console/RouteListCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
4 changes: 1 addition & 3 deletions tests/Mail/MailableQueuedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
96 changes: 24 additions & 72 deletions tests/Routing/RoutingRouteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
});
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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 '';
});
Expand Down Expand Up @@ -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 '';
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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.');
});
Expand All @@ -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.');
});
Expand All @@ -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.');
});
Expand All @@ -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.');
});
Expand All @@ -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.');
});
Expand All @@ -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.');
});
Expand Down Expand Up @@ -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));
Expand Down
8 changes: 2 additions & 6 deletions tests/Testing/TestResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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';
Expand Down
12 changes: 3 additions & 9 deletions tests/View/Blade/BladeEchoHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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(['<?php', '?>']));
Expand All @@ -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(['<?php', '?>']));
Expand Down

0 comments on commit 1a473f8

Please sign in to comment.