vendredi 31 mai 2019

Avoid use of new Class inside a method to test

I have a simple class in PHP without dependency injection. I would like to know a best practice to avoid use of new History() to mock it inside tests. So my goal is to have History mockable. This is my code:

class TrelloCycleTime
{
    private $historyCards;

    public function __construct()
    {
        $this->historyCards = [];
    }

    public function getAll()
    {
        $this->historyCards = new HistoryCards('some params');
    }
}

A way is to create a method setHistoryCards and pass my historyCards mocked, but I don't like it so much.

Another way it could be to have a factory.

Any suggestions?

Aucun commentaire:

Enregistrer un commentaire