Skip to content

Commit

Permalink
[8.x] bind mock instances as singletons so they are not overwritten
Browse files Browse the repository at this point in the history
  • Loading branch information
Nacoma committed Jun 19, 2021
1 parent 952f84a commit 8debb3b
Showing 1 changed file with 19 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,22 @@ protected function instance($abstract, $instance)
return $instance;
}

/**
* Register an instance of an object as a singleton in the container.
*
* @param $abstract
* @param $instance
* @return \Mockery\MockInterface
*/
protected function singletonInstance($abstract, $instance)
{
$this->app->singleton($abstract, function () use ($instance) {
return $instance;
});

return $instance;
}

/**
* Mock an instance of an object in the container.
*
Expand All @@ -50,7 +66,7 @@ protected function instance($abstract, $instance)
*/
protected function mock($abstract, Closure $mock = null)
{
return $this->instance($abstract, Mockery::mock(...array_filter(func_get_args())));
return $this->singletonInstance($abstract, Mockery::mock(...array_filter(func_get_args())));
}

/**
Expand All @@ -62,7 +78,7 @@ protected function mock($abstract, Closure $mock = null)
*/
protected function partialMock($abstract, Closure $mock = null)
{
return $this->instance($abstract, Mockery::mock(...array_filter(func_get_args()))->makePartial());
return $this->singletonInstance($abstract, Mockery::mock(...array_filter(func_get_args()))->makePartial());
}

/**
Expand All @@ -74,7 +90,7 @@ protected function partialMock($abstract, Closure $mock = null)
*/
protected function spy($abstract, Closure $mock = null)
{
return $this->instance($abstract, Mockery::spy(...array_filter(func_get_args())));
return $this->singletonInstance($abstract, Mockery::spy(...array_filter(func_get_args())));
}

/**
Expand Down

0 comments on commit 8debb3b

Please sign in to comment.