I'm trying to write tests for a package I'm developing (Thus I can't make actual requests). and somewhere in the package I've a class that has the request injected to the constructor that it uses in all its method.
public function __construct(\Symfony\Component\HttpFoundation\Request $request, ...$otherArgs){
$this->request = $request;
}
public function methodIwantToTest(){
$content = $this->request->getContent();
return $content;
}
What I want to do is simply mock that injected request and for example make the getContent function returns "test"... How can I go through this in my tests?
I'm using pestphp for testing and I tried this approach
$this->app->instance(
Request::class,
Mockery::mock(Request::class, function ($mock) {
$mock->shouldReceive('getContent')->andReturn('test');
})
);
You can use the makePartial method from Mockery to mock the Request class. This will allow you to mock the getContent method and return the value you want.