DEV Community


Why I am unable to mock a php static function that is used in factory patern?

pcmagas profile image Dimitrios Desyllas ・1 min read

Hello May I have some help on that?

I have the following class:

public function MyClass
  public static foo():string
    // Some logic there
    // This is a dummy value just for explaining the problem
    return 'n123';

  public static factory():MyClass
    return new MyClass();

Also I have the following class as well:

class MyClassConsumer


Editor guide
recursivefaults profile image
Ryan Latta

Your code looks like it has a few issues.

I think the reason you are having a specific problem right now though is you've done a spy() which is a mock(). Then you are trying to alias it, which won't work. It looks like you alias to classes that haven't been loaded. In this case you just did a spy to it, so it is loaded, so you shouldn't alias.

Hope this helps.