public function testBuildSchedule() { $schedule = [['rule' => 'foo', 'start' => 1, 'end' => 2, 'seconds' => 10, 'exists' => true], ['rule' => 'bar', 'start' => 2, 'end' => 5, 'seconds' => 100, 'exists' => false], ['rule' => 'baz', 'start' => 5, 'end' => 10, 'seconds' => 500, 'exists' => true]]; $rules = []; $i = 0; foreach ($schedule as $key => $options) { if ($options['exists']) { /* @var $rule \PHPUnit_Framework_MockObject_MockObject|RuleInterface */ $rule = $this->getMock('AnimeDb\\SmartSleep\\Rule\\RuleInterface'); $this->buildRule($rule, $options['start'], $options['end'], $options['seconds']); $this->collection->expects($this->at($i++))->method('has')->with($options['rule'])->will($this->returnValue(true)); $this->collection->expects($this->at($i++))->method('get')->with($options['rule'])->will($this->returnValue($rule)); $rules[] = $rule; } else { $this->collection->expects($this->at($i++))->method('has')->with($options['rule'])->will($this->returnValue(false)); } unset($schedule[$key]['exists']); } $schedule_obj = $this->builder->buildSchedule($schedule); $this->assertInstanceOf('AnimeDb\\SmartSleep\\Schedule', $schedule_obj); $this->assertEquals($rules, $schedule_obj->toArray()); }
<?php /** * AnimeDb package. * * @author Peter Gribanov <*****@*****.**> * @copyright Copyright (c) 2011, 'Peter Gribanov */ use AnimeDb\SmartSleep\Rule\HolidayRule; use AnimeDb\SmartSleep\Rule\WeekdayRule; use AnimeDb\SmartSleep\RuleCollection; use AnimeDb\SmartSleep\ScheduleBuilder; use AnimeDb\SmartSleep\SmartSleep; require __DIR__ . '/../bootstrap.php'; echo 'Functional test: Build schedule.' . PHP_EOL; $schedule = [['rule' => 'weekday', 'start' => 0, 'end' => 2, 'seconds' => 600], ['rule' => 'weekday', 'start' => 1, 'end' => 7, 'seconds' => 800], ['rule' => 'weekday', 'start' => 7, 'end' => 10, 'seconds' => 100], ['rule' => 'weekday', 'start' => 10, 'end' => 19, 'seconds' => 160], ['rule' => 'weekday', 'start' => 19, 'end' => 22, 'seconds' => 70], ['rule' => 'weekday', 'start' => 22, 'end' => 24, 'seconds' => 260], ['rule' => 'holiday', 'start' => 0, 'end' => 3, 'seconds' => 260], ['rule' => 'holiday', 'start' => 3, 'end' => 9, 'seconds' => 900], ['rule' => 'holiday', 'start' => 9, 'end' => 19, 'seconds' => 160], ['rule' => 'holiday', 'start' => 19, 'end' => 23, 'seconds' => 70], ['rule' => 'holiday', 'start' => 23, 'end' => 24, 'seconds' => 260]]; $collection = new RuleCollection(); $collection->set('weekday', new WeekdayRule()); $collection->set('holiday', new HolidayRule()); $builder = new ScheduleBuilder($collection); $smart = new SmartSleep($builder->buildSchedule($schedule)); $seconds = $smart->getSleepSeconds(new \DateTime()); echo sprintf('Sleep %s s.' . PHP_EOL, $seconds);