DEV Community

Dimitrios Desyllas
Dimitrios Desyllas

Posted on

1

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

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

Top comments (1)

Collapse
 
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.

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay