Skip to content

Instantly share code, notes, and snippets.

@karptonite
Forked from JeffreyWay/BaseModel.php
Last active December 13, 2015 20:48
Show Gist options
  • Select an option

  • Save karptonite/4972230 to your computer and use it in GitHub Desktop.

Select an option

Save karptonite/4972230 to your computer and use it in GitHub Desktop.
<?php
class PostsTest extends TestCase {
public function testShow()
{
$testpost = ['text' => 'Test post', 'id' => 1];
$this->instanceMock('PostRepositoryInterface')->shouldReceive('find')->with(1)->once()->andReturn($testpost);
}
}
<?php
use Mockery as m;
class TestCase extends Illuminate\Foundation\Testing\TestCase {
//Other test case functions removed
//$mockargs can be a string or an array of arguments to pass into m::mock()
public function instanceMock($mockargs, $instanceName=null)
{
if(is_array( $mockargs))
{
$mockable = $mockargs[0];
$mock = call_user_func_array('m::mock', $mockargs);
}
else
{
$mockable = $mockargs;
$mock = m::mock($mockable);
}
$instanceName = $instanceName ?: $mockable;
App::instance($instanceName, $mock);
return $mock;
}
}
@karptonite
Copy link
Copy Markdown
Author

I'm not thrilled with the way the arguments are passed, but it does simplify things without injecting test logic into production code, some. There may be a more general way of passing in and processing the mockable.

@karptonite
Copy link
Copy Markdown
Author

Perhaps if the arguments were passed as an array in a second argument?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment