From 345ae33b8ba256db3415588bdddcec1115e25480 Mon Sep 17 00:00:00 2001 From: Cody Date: Thu, 24 Jun 2021 17:00:10 -0500 Subject: [PATCH 1/2] ensure alias is rebound when mocking items in the container (issue #37789) --- .../Testing/Concerns/InteractsWithContainer.php | 2 ++ .../Concerns/InteractsWithContainerTest.php | 14 ++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/src/Illuminate/Foundation/Testing/Concerns/InteractsWithContainer.php b/src/Illuminate/Foundation/Testing/Concerns/InteractsWithContainer.php index b86ad5ac36a8..f17208ae6d13 100644 --- a/src/Illuminate/Foundation/Testing/Concerns/InteractsWithContainer.php +++ b/src/Illuminate/Foundation/Testing/Concerns/InteractsWithContainer.php @@ -86,6 +86,8 @@ protected function spy($abstract, Closure $mock = null) */ protected function singletonInstance($abstract, $instance) { + $abstract = $this->app->getAlias($abstract); + $this->app->singleton($abstract, function () use ($instance) { return $instance; }); diff --git a/tests/Foundation/Testing/Concerns/InteractsWithContainerTest.php b/tests/Foundation/Testing/Concerns/InteractsWithContainerTest.php index d8f9e6d254ae..76e9b4bc9786 100644 --- a/tests/Foundation/Testing/Concerns/InteractsWithContainerTest.php +++ b/tests/Foundation/Testing/Concerns/InteractsWithContainerTest.php @@ -2,7 +2,9 @@ namespace Illuminate\Tests\Foundation\Testing\Concerns; +use Illuminate\Http\Request; use Illuminate\Foundation\Mix; +use Mockery\MockInterface; use Orchestra\Testbench\TestCase; use stdClass; @@ -35,4 +37,16 @@ public function testSingletonBoundInstancesCanBeResolved() $this->assertEquals('bar', $this->app->make('foo')); $this->assertEquals('bar', $this->app->make('foo', ['with' => 'params'])); } + + public function testSingletonRebindsAlias() + { + $this->app->instance('request', Request::create('/example')); + + $this->mock(Request::class, function (MockInterface $mock) { + + }); + + $this->assertInstanceOf(MockInterface::class, resolve(Request::class)); + $this->assertInstanceOf(MockInterface::class, resolve('request')); + } } From 7615b9c57126bccad9ce56da6837c07a375d4f21 Mon Sep 17 00:00:00 2001 From: Cody Date: Thu, 24 Jun 2021 17:04:20 -0500 Subject: [PATCH 2/2] fix style --- .../Testing/Concerns/InteractsWithContainerTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Foundation/Testing/Concerns/InteractsWithContainerTest.php b/tests/Foundation/Testing/Concerns/InteractsWithContainerTest.php index 76e9b4bc9786..5bb975ea970d 100644 --- a/tests/Foundation/Testing/Concerns/InteractsWithContainerTest.php +++ b/tests/Foundation/Testing/Concerns/InteractsWithContainerTest.php @@ -2,8 +2,8 @@ namespace Illuminate\Tests\Foundation\Testing\Concerns; -use Illuminate\Http\Request; use Illuminate\Foundation\Mix; +use Illuminate\Http\Request; use Mockery\MockInterface; use Orchestra\Testbench\TestCase; use stdClass; @@ -43,7 +43,7 @@ public function testSingletonRebindsAlias() $this->app->instance('request', Request::create('/example')); $this->mock(Request::class, function (MockInterface $mock) { - + // }); $this->assertInstanceOf(MockInterface::class, resolve(Request::class));