Skip to content

Instantly share code, notes, and snippets.

@komron-m
Last active June 14, 2019 05:32
Show Gist options
  • Save komron-m/1cd42f236bc326e592f37fb58780ce79 to your computer and use it in GitHub Desktop.
Save komron-m/1cd42f236bc326e592f37fb58780ce79 to your computer and use it in GitHub Desktop.
Laravel helper for mocking instances.
<?php
namespace Tests\Unit\Core;
trait MockeryHelper {
/**
* @var array
* @key is Instance you want to mock ex: \App\Test\SomeDoer.php
* @value is mockery instance
*/
public $mockedInstances = [];
/**
* @param $instance
* @return \Mockery\MockInterface
*/
public function mockInstanceInContainer($instance) {
$mockedInstance = \Mockery::mock($instance);
$this->app->instance($instance, $mockedInstance);
$this->mockedInstances[$instance] = $mockedInstance;
return $mockedInstance;
}
/**
* @param $instance
* @return \Mockery\MockInterface
*/
public function assertMockedInstance($instance) {
return $this->getMock($instance);
}
/**
* @param $instance
* @return \Mockery\MockInterface
*/
public function getMock($instance) {
return $this->mockedInstances[$instance];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment