From 8d7a6b12c4dcbdb1a520847ab09623dd8b93336e Mon Sep 17 00:00:00 2001 From: Khalil Laleh Date: Sat, 20 Nov 2021 23:03:40 +0330 Subject: [PATCH 1/4] Forget and clear a mocked/pied instance of an object in the container --- .../Testing/Concerns/InteractsWithContainer.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/Illuminate/Foundation/Testing/Concerns/InteractsWithContainer.php b/src/Illuminate/Foundation/Testing/Concerns/InteractsWithContainer.php index c84852e0040c..d8f00136075c 100644 --- a/src/Illuminate/Foundation/Testing/Concerns/InteractsWithContainer.php +++ b/src/Illuminate/Foundation/Testing/Concerns/InteractsWithContainer.php @@ -77,6 +77,19 @@ protected function spy($abstract, Closure $mock = null) return $this->instance($abstract, Mockery::spy(...array_filter(func_get_args()))); } + /** + * Forget and clear a mocked/spied instance of an object in the container. + * + * @param string $abstract + * @return $this + */ + protected function forgetMock($abstract) + { + $this->app->forgetInstance($abstract); + + return $this; + } + /** * Register an empty handler for Laravel Mix in the container. * From 42de3185340517d503b7a6465a3680065bee32c2 Mon Sep 17 00:00:00 2001 From: Khalil Laleh Date: Sat, 20 Nov 2021 23:12:52 +0330 Subject: [PATCH 2/4] Style Fix --- .../Foundation/Testing/Concerns/InteractsWithContainer.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Foundation/Testing/Concerns/InteractsWithContainer.php b/src/Illuminate/Foundation/Testing/Concerns/InteractsWithContainer.php index d8f00136075c..c3eaea6cd05f 100644 --- a/src/Illuminate/Foundation/Testing/Concerns/InteractsWithContainer.php +++ b/src/Illuminate/Foundation/Testing/Concerns/InteractsWithContainer.php @@ -80,7 +80,7 @@ protected function spy($abstract, Closure $mock = null) /** * Forget and clear a mocked/spied instance of an object in the container. * - * @param string $abstract + * @param string $abstract * @return $this */ protected function forgetMock($abstract) From 1841a90e6987d7953052bc6b97f78c4bf4970282 Mon Sep 17 00:00:00 2001 From: Khalil Laleh Date: Sun, 21 Nov 2021 01:33:04 +0330 Subject: [PATCH 3/4] Test for forgetMock --- .../Concerns/InteractsWithContainerTest.php | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/tests/Foundation/Testing/Concerns/InteractsWithContainerTest.php b/tests/Foundation/Testing/Concerns/InteractsWithContainerTest.php index 4bf62fcfb738..dddc721de22e 100644 --- a/tests/Foundation/Testing/Concerns/InteractsWithContainerTest.php +++ b/tests/Foundation/Testing/Concerns/InteractsWithContainerTest.php @@ -27,4 +27,25 @@ public function testWithMixRestoresOriginalHandlerAndReturnsInstance() $this->assertSame($handler, resolve(Mix::class)); $this->assertSame($this, $instance); } + + public function testForgetMock() + { + $this->mock(IntanceStub::class) + ->shouldReceive('execute') + ->once() + ->andReturn('bar'); + + $this->assertSame('bar', $this->app->make(IntanceStub::class)->execute()); + + $this->forgetMock(IntanceStub::class); + $this->assertSame('foo', $this->app->make(IntanceStub::class)->execute()); + } +} + +class IntanceStub +{ + public function execute() + { + return 'foo'; + } } From 9bbafdd41bbfdbeeaba71012f8456aba9a644061 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Mon, 22 Nov 2021 09:03:39 -0600 Subject: [PATCH 4/4] Update InteractsWithContainer.php --- .../Foundation/Testing/Concerns/InteractsWithContainer.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Foundation/Testing/Concerns/InteractsWithContainer.php b/src/Illuminate/Foundation/Testing/Concerns/InteractsWithContainer.php index c3eaea6cd05f..6949f6f8c5da 100644 --- a/src/Illuminate/Foundation/Testing/Concerns/InteractsWithContainer.php +++ b/src/Illuminate/Foundation/Testing/Concerns/InteractsWithContainer.php @@ -78,7 +78,7 @@ protected function spy($abstract, Closure $mock = null) } /** - * Forget and clear a mocked/spied instance of an object in the container. + * Instruct the container to forget a previously mocked / spied instance of an object. * * @param string $abstract * @return $this