John Davidson

php - Replace all class instances with stub


I am testing a class, let's call it ClassUnderTest using another class, let's call it OtherClass. In my Test I do:

$OtherClassStub = $this->createStub(OtherClass::class);
$ClassUnderTest->otherClass = $OtherClassStub;

That works. But when the $ClassUnderTest calls new OtherClass(), the original OtherClass class is created instead of the stub.

How can I achieve that every possible instance of OtherClass in the context of the test is replaced by the stub?

My Answer:


Rate this post

3 of 5 based on 3425 votes


© 2024 - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us