I have registered a singleton in a Service Provider (which uses a Guzzle Client in it's constructor):
public function register()
{
$this->app->singleton(Channel::class, function ($app) {
return new ChannelClient(new Client([
'http_errors'=> false,
'timeout' => 10,
'connect_timeout' => 10
]));
});
}
I have a validation method:
public static function validateChannel($attribute, $value, $parameters, \Illuminate\Validation\Validator $validator)
{
$dataloader = app()->make(\App\Client\Channel::class);
if($dataloader->search($value)){
return true;
}
}
In the PHPUnit Test, how can I replace the app()->make(\App\Client\Channel::class);
with a mocked Client
class but still test the validation function within the test?
To use a mock in your tests you can do something like this:
public function test_my_controller () {
// Create a mock of the Random Interface
$mock = Mockery::mock(RandomInterface::class);
// Set our expectation for the methods that should be called
// and what is supposed to be returned
$mock->shouldReceive('someMethodName')->once()->andReturn('SomeNonRandomString');
// Tell laravel to use our mock when someone tries to resolve
// an instance of our interface
$this->app->instance(RandomInterface::class, $mock);
$this->post('/api/v1/do_things', ['email' => $this->email])
->seeInDatabase('things', [
'email' => $this->email,
'random' => 'SomeNonRandomString',
]);
}
Be sure to checkout the mockery documentation:
http://docs.mockery.io/en/latest/reference/expectations.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With