Passing All Dates as Parameters

One possible option could be passing all the dates as parameters when needed:

class Post
{
// ...

public function __construct($aContent, $title, $createdAt = null)
{
// ...
$this->createdAt($createdAt ?: new DateTimeImmutable());
}

// ...

public function isNew($today = null)
{
return
($today ? :new DateTimeImmutable())
->diff($this->createdAt)
->days <= self::NEW_TIME_INTERVAL_DAYS;
}
}

This is the easiest approach for unit testing purposes. Just pass different pairs of dates to test all possible scenarios with 100 percent coverage. However, if you consider the client code that's creating and asking for the isNew() method result, things don't look so nice. The resulting code can be a bit weird because of always passing today's DateTime:

$aPost = new Post(
'Hello world!',
'Hi',
new DateTimeImmutable()
);

$aPost->isNew(
new DateTimeImmutable()
);
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset