vendredi 28 octobre 2016

Mocking a wrapper time function

I have a created a minor wrapper function for the php time() method in my class

class MyClass
{
    public function myFuncion($unixTimeStamp)
    {
        $pending = $this->isPending($unixTimeStamp)

        // data manipulation

        return $result
    }

    protected function isPending($unixTimeStamp)
    {
        if ($unixTimeStamp > $this->currentTime()) {
            return true;
        }

        return false;
    }

    public function currentTime()
    {
        return time();
    }
}

I want to test the public function myFunction() in this class but I am at a bit of a loss how I can mock the currentTime method without mocking the SUT itself (MyClass)

What is the correct way of doing this? I felt creating a time class with a single method (getCurrentTime) and injecting it into MyClass while correct, was excessive. Is this the best method regardless?

What other options do I have?

Aucun commentaire:

Enregistrer un commentaire