Skip to content
loading...

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

github logo ・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
twitter logo DISCUSS (1)
markdown guide
 

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.

Classic DEV Post from Mar 2

What effect is the Coronavirus having on your work life and personal life?

Do you go into an office on work days? Is your company taking precautions? Are you taking personal pr...

Dimitrios Desyllas profile image
Greedings I am Dimitrios Desyllas aka pc_magas. I am a php software engineer in e-table. I am interested in electron, C++, privacy enhancing terchnologies and cryptography

Like what you're reading?
Hit the +FOLLOW button.

❤️