Saturday 15 August 2015

php - Is it possible to mock an object that is instanciated inside the tested class -



php - Is it possible to mock an object that is instanciated inside the tested class -

the method i'm testing looks :

function x($arg) { // code ... $object = new object; // code ... }

is there way mock object ?

not phpunit mock object in unit test can't.

depending on how load classes, able create "mock" class replace object. this:

your test file:

class object { <mock functionality here> } class testcase extends phpunit_framework_testcase { <your tests> }

however cause problems if want test object in test. in case fatal error stating cannot redefine class. this not best practice.

your best bet refactor class utilize dependency injection. alter class takes object either constructor argument or part of method signature.

this article may helpful: http://misko.hevery.com/2008/07/08/how-to-think-about-the-new-operator/

php mocking phpunit

No comments:

Post a Comment