Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.x] Forget and clear a mocked/spied instance of an object in the container #39713

Merged
merged 4 commits into from Nov 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -77,6 +77,19 @@ protected function spy($abstract, Closure $mock = null)
return $this->instance($abstract, Mockery::spy(...array_filter(func_get_args())));
}

/**
* Instruct the container to forget a previously mocked / spied instance of an object.
*
* @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.
*
Expand Down
21 changes: 21 additions & 0 deletions tests/Foundation/Testing/Concerns/InteractsWithContainerTest.php
Expand Up @@ -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';
}
}