Mockery Overload - Abstract Class

Viewed 1012

I'm fairly new to TDD and I'm currently writing tests in a Laravel project and I'm using the Mockery library. I've encountered an issue when trying to mock overload a new class instance.

The problem seems to be that the mocked class loses its inheritance and are therefore not passing the parameter validation when being passed to a method. However when I create a mock of the Child without overloading its inheritance seems to be picked up.

I have to classes called Parent and Child. Child extends Parent.

// Test
public function test() {
    Mockery::mock('overload:Child');
    $results = service();
}

// Functions
public function service() {
    $child = new Child();
    serviceMethod($child);
}

public function serviceMethod(Parent $o) {
    // Do something
}

This returns that serviceMethod expects Parent, not Child.

I'm obviously missing some crucial detail here, would anyone be able to point it out for me?

1 Answers

I ran into this same issue. We can pass the base class as a second argument. This will fix the type error.

Try this:

Mockery::mock('overload:Child', 'Parent');

or

Mockery::mock('overload:' . Child::class, Parent::class);
Related