I suspect that the “best” answer to my question is to use dependency injection and completely eliminate this problem. Unfortunately, I do not have such an option ...
I need to write a test for a class that instantiates a third-party library. I want to trick / stub a library class so that it doesn't call API calls in real time.
I use phpunit in the CakePHP v3.x framework. I can mock the library and create responses to the stub, but this does not prevent the "real" class from creating an instance of code outside of my test. I thought I was trying to make fun of the class upstream of the instance, but there are a lot of them, which would make the test incredibly cumbersome to write / support.
Is there a way to somehow "drown" the class instance? Similarly, how can we say that the php module expects an API call and sets the returned data?
source
share