2

I'm stubbing a class in PHPUnit. I need to stub a method on that class called expects. However, that method already exists on the PHPUnit mocking base class. How would I circumvent that? It obviously throws because I'm trying to redeclare expects (Fatal error: Cannot redeclare Mock_Inlet_399f0eba::expects())

$inlet = $this->getMockForAbstractClass(Inlet::class);

If I tried to stub a method called method, I could do...

$inlet->expects($this->any())->method('method')->willReturn([Foo::class]);

...as the mock is placed on InvocationMocker instead of the plain MockObject.

Is there a similar workaround for methods called expects, or will I have to either rename the method or mock it with some other mocking framework?

rdiz
  • 6,136
  • 1
  • 29
  • 41
  • have you tried this http://php.net/manual/de/function.override-function.php ? or using namespaces ? namespace MyNamespace { function expects($object) { ... – Doktor OSwaldo Sep 23 '16 at 13:31
  • Mocks doesn't care about namespaces. Also the `override_function` is specific to APD. Thanks for your willingness to help anyway! – rdiz Sep 23 '16 at 13:33
  • 1
    FYI I renamed `expects` to `expected`. Leaving the question open though, it'd be interesting to have an answer to this, even though it's an extreme edge case. – rdiz Sep 23 '16 at 14:10
  • It should be possible to use [Decorator](http://stackoverflow.com/questions/3377664/php-how-is-it-possible-to-rename-methods) for 'renaming' mock methods. – BVengerov Sep 26 '16 at 12:47
  • Nice idea @BVengerov. I'm not sure how that'd behave with typehinting, but I'll give it a shot when I get back to the project. – rdiz Sep 26 '16 at 12:59
  • @Dencker aren't all edge cases extreme by definition? – Josue Alexander Ibarra Nov 23 '16 at 17:45

0 Answers0