Reputation: 2378
I am trying to test a factory that it returns the correct instance. But ApiAuthRequest
class has to implement a specific interface in order for the test to succeed. How I can do it?
I have tried to create a class in the test, which works but I am not sure if it's the right way.
public function factory_returns_correct_instance(AllowedHttpMethod $httpMethod): void
{
$request = $this->partialMock(ApiAuthRequest::class, function(MockInterface $request) {
$request->shouldReceive('getModel')->andReturn($this->model);
});
$restrictor = ModelRestrictionFactory::create($request, $httpMethod);
$this->assertInstanceOf(ModelSystem::class, $restrictor);
}
// If I do this in the test class, test works.
class ApiAuthRequest implements HasModelSystemRestriction
{
}
Upvotes: 0
Views: 58
Reputation: 2378
This is possible by using \Mockery::mock();
where the first argument is the class followed by the interface.
public function factory_returns_model_system_instance(AllowedHttpMethod $httpMethod): void
{
$request = \Mockery::mock(ApiAuthRequest::class, HasModelSystemRestriction::class);
$request->shouldReceive('getModel')->andReturn($this->model);
$request->shouldReceive('instanceof')->with(HasModelSystemRestriction::class)->andReturn(true);
$restrictor = ModelRestrictionFactory::create($request, $httpMethod);
$this->assertInstanceOf(ModelSystem::class, restrictor);
}
Upvotes: 0