/** * @param \DateTime $now * * @return int */ public function getSleepSeconds(\DateTime $now) { $rule = $this->schedule->getMatchedRule($now); if ($rule instanceof RuleInterface) { $seconds = $rule->getSeconds(); return $seconds > 0 ? $seconds : 0; } return 0; }
/** * Build schedule. * * <code> * [ * {rule: <rule_name>, start: <start_hour>, end: <end_hour>, seconds: <sleep_seconds>}, * ... * ] * </code> * * @param array $schedule * * @return Schedule */ public function buildSchedule(array $schedule) { $result = new Schedule(); foreach ($schedule as $options) { $rule = $this->buildRule($options['rule'], $options['start'], $options['end'], $options['seconds']); if ($rule instanceof RuleInterface) { $result->add($rule); } } return $result; }
/** * @dataProvider getMatchedRule * * @param int $count_rules * @param int $match_rule_number */ public function testGetMatchedRule($count_rules, $match_rule_number) { $time = new \DateTime(); $match_rule = null; for ($i = 1; $i <= $count_rules; ++$i) { /* @var $rule \PHPUnit_Framework_MockObject_MockObject|RuleInterface */ $rule = $this->getMock('AnimeDb\\SmartSleep\\Rule\\RuleInterface'); if ($match_rule_number && $i > $match_rule_number) { $rule->expects($this->never())->method('isMatched'); } else { $rule->expects($this->once())->method('isMatched')->with($time)->will($this->returnValue($i == $match_rule_number)); if ($i == $match_rule_number) { $match_rule = $rule; } } $this->schedule->add($rule); } $this->assertEquals($match_rule, $this->schedule->getMatchedRule($time)); }
public function testGetSleepSeconds() { $this->schedule->expects($this->once())->method('getMatchedRule')->with($this->time)->will($this->returnValue($this->rule)); $this->rule->expects($this->once())->method('getSeconds')->will($this->returnValue(10)); $this->assertEquals(10, $this->smart_sleep->getSleepSeconds($this->time)); }