Пример #1
0
 /**
  * @param string $name
  * @param int $start
  * @param int $end
  * @param int $seconds
  *
  * @return RuleInterface|null
  */
 public function buildRule($name, $start, $end, $seconds)
 {
     if (!$this->collection->has($name)) {
         return null;
     }
     return $this->collection->get($name)->setEnd($end)->setStart($start)->setSeconds($seconds);
 }
Пример #2
0
 public function testGetClone()
 {
     $rule = new TestRule();
     $this->collection->set('foo', $rule);
     $collection_rule = $this->collection->get('foo');
     $collection_rule->setSeconds(123);
     $this->assertNotEquals($collection_rule, $this->collection->get('foo'));
 }
Пример #3
0
 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());
 }
Пример #4
0
<?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);