Skip to content

Instantly share code, notes, and snippets.

@jaggy
Last active September 9, 2015 08:11
Show Gist options
  • Save jaggy/5b7e460a159b4432b1fe to your computer and use it in GitHub Desktop.
Save jaggy/5b7e460a159b4432b1fe to your computer and use it in GitHub Desktop.
<?php
Mock::instance(App\Http\Requests\AuthRequest::class)->bind();
Mock::instance(Illuminate\Auth\Guard::class)->expects(function ($auth) {
$auth->shouldReceive('attempt')->andReturn(false);
})->bind();
<?php
class Mock
{
/**
* Full path of the class name to mock.
*
* @var string
*/
protected $class;
/**
* Mockery instance.
*
* @var Mockery
*/
protected $instance;
/**
* Create a new mock instance.
*
* @param string $class
* @return Mock
*/
private function __construct($class)
{
$this->class = $class;
$this->instance = Mockery::mock($class);
}
/**
* Create a mock instance for the given class.
*
* @param string $class
* @return Mock
*/
public static function instance($class)
{
return new static($class);
}
/**
* Add the expectations from the mock.
*
* @param Closure $callback
* @return Mock
*/
public function expects(Closure $callback)
{
$callback->__invoke($this->instance);
return $this;
}
/**
* Bind the instance to to IoC container.
*
* @return void
*/
public function bind()
{
app()->instance($this->class, $this->instance);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment