June 2017
Intermediate to advanced
394 pages
8h 52m
English
Another alternative is to use the Test Class pattern. The idea is to extend the Post class with a new one that we can manipulate to force specific scenarios. This new class is going to be used only for unit testing purposes. The bad news is that we have to modify the original Post class a bit, extracting some methods and changing some fields and methods from private to protected. Some developers may worry about increasing the visibility of class properties just because of testing reasons. However, we think that in most cases, it's worth it:
class Post{ protected $createdAt; public function isNew() { return ($this->today()) ->diff($this->createdAt) ->days <= self::NEW_TIME_INTERVAL_DAYS; } protected function today() { return new ...