Last active
September 9, 2015 08:11
-
-
Save jaggy/5b7e460a159b4432b1fe to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
Mock::instance(App\Http\Requests\AuthRequest::class)->bind(); | |
Mock::instance(Illuminate\Auth\Guard::class)->expects(function ($auth) { | |
$auth->shouldReceive('attempt')->andReturn(false); | |
})->bind(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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