0

So, I am trying to mock a class with AspectMock::Test.
I'm running into the issue that the mock is passed as a parameter and when I do this, I get the following error:

Argument 1 passed to Foo::spamEggs() must be an instance of Bar, 
instance of AspectMock\Proxy\ClassProxy given

The code in question

class Foo {
    public function spamEggs(Bar $bar): Egg
    {
        return $bar->getEgg();
    }
}

final class Bar {
    public function getEgg(): Egg
    {
        ...
    }
}

The test

class TestFoo {
    public function testSpamEggs()
    {
        $bar = Test::double('Bar', [
            'getEgg' => new Egg('green'),
        ]);
        $foo = new Foo();
        $egg = $foo->spamEggs($bar); // throws the error
        $this->assertEquals('green', $egg->colour);
    }
}

Is there any way to make this works without me having to remove my parameter typing?

The reason why I moved to AspectMock instead of Stub is that my Bar class is final and as far as I know, Stub does not support final mocks.

The question is not how I should mock this final class, but what to do about the typed parameter.

Any tips are welcome.

Nebulosar
  • 1,727
  • 3
  • 20
  • 46
  • You could use an implement and use an interface for the type hints instead? Otherwise, remove the `final`? – Jonnix Jul 26 '19 at 12:12
  • Yeah, the final is not really a class of my own, so thats a no. I don't fully understand the reason for the `final` in the first place.. I never used an interface to define the type hinting, I'll look it up. – Nebulosar Jul 26 '19 at 12:16
  • If that's not a class you can change then an interface isn't going to help either. Seems they intended for it to always be concrete. – Jonnix Jul 26 '19 at 12:22
  • Ok, I can't answer my own question anymore because it is marked as duplicate.. (Thanks Machavity!) . . . . . I finaly made it work with the use of `Stub::make` and this answer https://stackoverflow.com/a/50796273/6434747 – Nebulosar Jul 26 '19 at 12:52

0 Answers0