Skip to content

Commit

Permalink
[8.x] Forget and clear a mocked/spied instance of an object in the co…
Browse files Browse the repository at this point in the history
…ntainer (#39713)

* Forget and clear a mocked/pied instance of an object in the container

* Style Fix

* Test for forgetMock

* Update InteractsWithContainer.php

Co-authored-by: Taylor Otwell <taylor@laravel.com>
  • Loading branch information
khalilst and taylorotwell committed Nov 22, 2021
1 parent 36dfae9 commit e1cae7e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
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';
}
}

0 comments on commit e1cae7e

Please sign in to comment.