I have a legacy class that I quickly want to write a couple of tests for. Unfortunately we have a singleton call in the constructer, and not enough time at present to refactor it as it should.
function __construct(){
$this->_dbConnect = DbConnect::getInstance();
// very long constructer (sigh) omitted below ...
}
Is it acceptable practice to do this, so as to have mockable legacy code :
function __construct(DbConnect $dbConnect = null){
$this->_dbConnect = isset($dbConnect) ? $dbConnect : DbConnect::getInstance();
// <snip>
}
If you want to test only this class – yes, it’s normal code.
Sorry for 1-line answer, but your question contains answer already 🙂