From 0b84dd5580792d97214d70db584471a9812571d7 Mon Sep 17 00:00:00 2001 From: Peter Gribanov Date: Tue, 25 Jul 2017 11:24:50 +0300 Subject: [PATCH 01/17] rename RuleInterface -> Rule --- src/Rule/{RuleInterface.php => Rule.php} | 8 ++++---- src/Rule/RuleBase.php | 2 +- src/RuleCollection.php | 10 +++++----- src/Schedule.php | 14 +++++++------- src/ScheduleBuilder.php | 6 +++--- src/SmartSleep.php | 4 ++-- tests/unit/Rule/RandMaxSecondsTestCase.php | 4 ++-- tests/unit/Rule/TestCase.php | 4 ++-- tests/unit/RuleCollectionTest.php | 4 ++-- tests/unit/ScheduleBuilderTest.php | 6 +++--- tests/unit/ScheduleTest.php | 10 +++++----- tests/unit/SmartSleepTest.php | 4 ++-- tests/unit/TestRule.php | 4 ++-- 13 files changed, 40 insertions(+), 40 deletions(-) rename src/Rule/{RuleInterface.php => Rule.php} (87%) diff --git a/src/Rule/RuleInterface.php b/src/Rule/Rule.php similarity index 87% rename from src/Rule/RuleInterface.php rename to src/Rule/Rule.php index ec0efd1..534f285 100644 --- a/src/Rule/RuleInterface.php +++ b/src/Rule/Rule.php @@ -8,7 +8,7 @@ namespace AnimeDb\SmartSleep\Rule; -interface RuleInterface +interface Rule { /** * @return int @@ -18,7 +18,7 @@ public function getStart(); /** * @param int $start * - * @return RuleInterface + * @return Rule */ public function setStart($start); @@ -30,7 +30,7 @@ public function getEnd(); /** * @param int $end * - * @return RuleInterface + * @return Rule */ public function setEnd($end); @@ -42,7 +42,7 @@ public function getSeconds(); /** * @param int $seconds * - * @return RuleInterface + * @return Rule */ public function setSeconds($seconds); diff --git a/src/Rule/RuleBase.php b/src/Rule/RuleBase.php index 73d807b..635b128 100644 --- a/src/Rule/RuleBase.php +++ b/src/Rule/RuleBase.php @@ -8,7 +8,7 @@ namespace AnimeDb\SmartSleep\Rule; -abstract class RuleBase implements RuleInterface +abstract class RuleBase implements Rule { /** * @var int diff --git a/src/RuleCollection.php b/src/RuleCollection.php index c517318..759388a 100644 --- a/src/RuleCollection.php +++ b/src/RuleCollection.php @@ -8,22 +8,22 @@ namespace AnimeDb\SmartSleep; -use AnimeDb\SmartSleep\Rule\RuleInterface; +use AnimeDb\SmartSleep\Rule\Rule; class RuleCollection { /** - * @var RuleInterface[] + * @var Rule[] */ protected $rules = []; /** * @param string $name - * @param RuleInterface $rule + * @param Rule $rule * * @return RuleCollection */ - public function set($name, RuleInterface $rule) + public function set($name, Rule $rule) { $this->rules[$name] = clone $rule; @@ -33,7 +33,7 @@ public function set($name, RuleInterface $rule) /** * @param string $name * - * @return RuleInterface|null + * @return Rule|null */ public function get($name) { diff --git a/src/Schedule.php b/src/Schedule.php index 7cdfca0..a38b17f 100644 --- a/src/Schedule.php +++ b/src/Schedule.php @@ -8,17 +8,17 @@ namespace AnimeDb\SmartSleep; -use AnimeDb\SmartSleep\Rule\RuleInterface; +use AnimeDb\SmartSleep\Rule\Rule; class Schedule implements \IteratorAggregate, \Countable { /** - * @var RuleInterface[] + * @var Rule[] */ protected $rules = []; /** - * @param RuleInterface[] $rules + * @param Rule[] $rules */ public function __construct(array $rules = []) { @@ -28,11 +28,11 @@ public function __construct(array $rules = []) } /** - * @param RuleInterface $rule + * @param Rule $rule * * @return true */ - public function add(RuleInterface $rule) + public function add(Rule $rule) { $this->rules[] = $rule; @@ -56,7 +56,7 @@ public function isEmpty() } /** - * @return RuleInterface[] + * @return Rule[] */ public function toArray() { @@ -74,7 +74,7 @@ public function getIterator() /** * @param \DateTime $time * - * @return RuleInterface|null + * @return Rule|null */ public function getMatchedRule(\DateTime $time) { diff --git a/src/ScheduleBuilder.php b/src/ScheduleBuilder.php index 5425e9c..46d4cb6 100644 --- a/src/ScheduleBuilder.php +++ b/src/ScheduleBuilder.php @@ -8,7 +8,7 @@ namespace AnimeDb\SmartSleep; -use AnimeDb\SmartSleep\Rule\RuleInterface; +use AnimeDb\SmartSleep\Rule\Rule; class ScheduleBuilder { @@ -31,7 +31,7 @@ public function __construct(RuleCollection $collection) * @param int $end * @param int $seconds * - * @return RuleInterface|null + * @return Rule|null */ public function buildRule($name, $start, $end, $seconds) { @@ -67,7 +67,7 @@ public function buildSchedule(array $schedule) foreach ($schedule as $options) { $rule = $this->buildRule($options['rule'], $options['start'], $options['end'], $options['seconds']); - if ($rule instanceof RuleInterface) { + if ($rule instanceof Rule) { $result->add($rule); } } diff --git a/src/SmartSleep.php b/src/SmartSleep.php index af41110..2a6df3c 100644 --- a/src/SmartSleep.php +++ b/src/SmartSleep.php @@ -8,7 +8,7 @@ namespace AnimeDb\SmartSleep; -use AnimeDb\SmartSleep\Rule\RuleInterface; +use AnimeDb\SmartSleep\Rule\Rule; class SmartSleep { @@ -33,7 +33,7 @@ public function __construct(Schedule $schedule) public function getSleepSeconds(\DateTime $now) { $rule = $this->schedule->getMatchedRule($now); - if ($rule instanceof RuleInterface) { + if ($rule instanceof Rule) { $seconds = $rule->getSeconds(); return $seconds > 0 ? $seconds : 0; diff --git a/tests/unit/Rule/RandMaxSecondsTestCase.php b/tests/unit/Rule/RandMaxSecondsTestCase.php index 7b68800..30c4f68 100644 --- a/tests/unit/Rule/RandMaxSecondsTestCase.php +++ b/tests/unit/Rule/RandMaxSecondsTestCase.php @@ -9,7 +9,7 @@ namespace AnimeDb\SmartSleep\Tests\Unit\Rule; use AnimeDb\SmartSleep\Rule\RandMaxSecondsRuleBase; -use AnimeDb\SmartSleep\Rule\RuleInterface; +use AnimeDb\SmartSleep\Rule\Rule; abstract class RandMaxSecondsTestCase extends TestCase { @@ -18,7 +18,7 @@ abstract class RandMaxSecondsTestCase extends TestCase * * @param int $min_sleep_seconds * - * @return RuleInterface + * @return Rule */ abstract protected function getRuleForMinSleepSeconds($min_sleep_seconds); diff --git a/tests/unit/Rule/TestCase.php b/tests/unit/Rule/TestCase.php index cdbc4a4..ad3c98a 100644 --- a/tests/unit/Rule/TestCase.php +++ b/tests/unit/Rule/TestCase.php @@ -8,12 +8,12 @@ namespace AnimeDb\SmartSleep\Tests\Unit\Rule; -use AnimeDb\SmartSleep\Rule\RuleInterface; +use AnimeDb\SmartSleep\Rule\Rule; abstract class TestCase extends \PHPUnit_Framework_TestCase { /** - * @return RuleInterface + * @return Rule */ abstract protected function getRule(); diff --git a/tests/unit/RuleCollectionTest.php b/tests/unit/RuleCollectionTest.php index eb01573..11d98ca 100644 --- a/tests/unit/RuleCollectionTest.php +++ b/tests/unit/RuleCollectionTest.php @@ -8,7 +8,7 @@ namespace AnimeDb\SmartSleep\Tests\Unit; -use AnimeDb\SmartSleep\Rule\RuleInterface; +use AnimeDb\SmartSleep\Rule\Rule; use AnimeDb\SmartSleep\RuleCollection; class RuleCollectionTest extends \PHPUnit_Framework_TestCase @@ -28,7 +28,7 @@ public function testGetSet() $this->assertFalse($this->collection->has('foo')); $this->assertNull($this->collection->get('foo')); - /* @var $rule \PHPUnit_Framework_MockObject_MockObject|RuleInterface */ + /* @var $rule \PHPUnit_Framework_MockObject_MockObject|Rule */ $rule = $this->getMock('AnimeDb\SmartSleep\Rule\RuleInterface'); $this->assertEquals($this->collection, $this->collection->set('foo', $rule)); diff --git a/tests/unit/ScheduleBuilderTest.php b/tests/unit/ScheduleBuilderTest.php index fed8216..7787152 100644 --- a/tests/unit/ScheduleBuilderTest.php +++ b/tests/unit/ScheduleBuilderTest.php @@ -8,7 +8,7 @@ namespace AnimeDb\SmartSleep\Tests\Unit; -use AnimeDb\SmartSleep\Rule\RuleInterface; +use AnimeDb\SmartSleep\Rule\Rule; use AnimeDb\SmartSleep\RuleCollection; use AnimeDb\SmartSleep\ScheduleBuilder; @@ -50,7 +50,7 @@ public function testBuildRule() $end = 2; $seconds = 10; - /* @var $rule \PHPUnit_Framework_MockObject_MockObject|RuleInterface */ + /* @var $rule \PHPUnit_Framework_MockObject_MockObject|Rule */ $rule = $this->getMock('AnimeDb\SmartSleep\Rule\RuleInterface'); $this->buildRule($rule, $start, $end, $seconds); @@ -81,7 +81,7 @@ public function testBuildSchedule() $i = 0; foreach ($schedule as $key => $options) { if ($options['exists']) { - /* @var $rule \PHPUnit_Framework_MockObject_MockObject|RuleInterface */ + /* @var $rule \PHPUnit_Framework_MockObject_MockObject|Rule */ $rule = $this->getMock('AnimeDb\SmartSleep\Rule\RuleInterface'); $this->buildRule($rule, $options['start'], $options['end'], $options['seconds']); diff --git a/tests/unit/ScheduleTest.php b/tests/unit/ScheduleTest.php index be44e15..e94f03d 100644 --- a/tests/unit/ScheduleTest.php +++ b/tests/unit/ScheduleTest.php @@ -8,7 +8,7 @@ namespace AnimeDb\SmartSleep\Tests\Unit; -use AnimeDb\SmartSleep\Rule\RuleInterface; +use AnimeDb\SmartSleep\Rule\Rule; use AnimeDb\SmartSleep\Schedule; class ScheduleTest extends \PHPUnit_Framework_TestCase @@ -31,7 +31,7 @@ public function testInstanceOf() public function testConstruct() { - /* @var $rules \PHPUnit_Framework_MockObject_MockObject[]|RuleInterface[] */ + /* @var $rules \PHPUnit_Framework_MockObject_MockObject[]|Rule[] */ $rules = [ $this->getMock('AnimeDb\SmartSleep\Rule\RuleInterface'), $this->getMock('AnimeDb\SmartSleep\Rule\RuleInterface'), @@ -52,7 +52,7 @@ public function testAdd() $this->assertEquals(0, $this->schedule->count()); $this->assertTrue($this->schedule->isEmpty()); - /* @var $rule1 \PHPUnit_Framework_MockObject_MockObject|RuleInterface */ + /* @var $rule1 \PHPUnit_Framework_MockObject_MockObject|Rule */ $rule1 = $this->getMock('AnimeDb\SmartSleep\Rule\RuleInterface'); $this->schedule->add($rule1); @@ -60,7 +60,7 @@ public function testAdd() $this->assertEquals(1, $this->schedule->count()); $this->assertFalse($this->schedule->isEmpty()); - /* @var $rule2 \PHPUnit_Framework_MockObject_MockObject|RuleInterface */ + /* @var $rule2 \PHPUnit_Framework_MockObject_MockObject|Rule */ $rule2 = $this->getMock('AnimeDb\SmartSleep\Rule\RuleInterface'); $this->schedule->add($rule2); @@ -93,7 +93,7 @@ public function testGetMatchedRule($count_rules, $match_rule_number) $match_rule = null; for ($i = 1; $i <= $count_rules; ++$i) { - /* @var $rule \PHPUnit_Framework_MockObject_MockObject|RuleInterface */ + /* @var $rule \PHPUnit_Framework_MockObject_MockObject|Rule */ $rule = $this->getMock('AnimeDb\SmartSleep\Rule\RuleInterface'); if ($match_rule_number && $i > $match_rule_number) { $rule diff --git a/tests/unit/SmartSleepTest.php b/tests/unit/SmartSleepTest.php index b666d56..9972a2e 100644 --- a/tests/unit/SmartSleepTest.php +++ b/tests/unit/SmartSleepTest.php @@ -10,7 +10,7 @@ use AnimeDb\SmartSleep\Schedule; use AnimeDb\SmartSleep\SmartSleep; -use AnimeDb\SmartSleep\Rule\RuleInterface; +use AnimeDb\SmartSleep\Rule\Rule; class SmartSleepTest extends \PHPUnit_Framework_TestCase { @@ -20,7 +20,7 @@ class SmartSleepTest extends \PHPUnit_Framework_TestCase protected $schedule; /** - * @var \PHPUnit_Framework_MockObject_MockObject|RuleInterface + * @var \PHPUnit_Framework_MockObject_MockObject|Rule */ protected $rule; diff --git a/tests/unit/TestRule.php b/tests/unit/TestRule.php index d2b1c34..f82980a 100644 --- a/tests/unit/TestRule.php +++ b/tests/unit/TestRule.php @@ -8,12 +8,12 @@ namespace AnimeDb\SmartSleep\Tests\Unit; -use AnimeDb\SmartSleep\Rule\RuleInterface; +use AnimeDb\SmartSleep\Rule\Rule; /** * Rule only for tests. */ -class TestRule implements RuleInterface +class TestRule implements Rule { /** * @var int From f20685da9951342b46fe31cf7b0a88595f735d97 Mon Sep 17 00:00:00 2001 From: Peter Gribanov Date: Tue, 25 Jul 2017 11:27:04 +0300 Subject: [PATCH 02/17] rename SmartSleep::getSleepSeconds() -> SmartSleep::sleepForSeconds() --- src/RuleCollection.php | 2 +- src/Schedule.php | 2 +- src/ScheduleBuilder.php | 2 +- src/SmartSleep.php | 4 ++-- tests/functional/build_schedule.php | 2 +- tests/functional/simple_schedule.php | 2 +- tests/unit/SmartSleepTest.php | 6 +++--- 7 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/RuleCollection.php b/src/RuleCollection.php index 759388a..4742056 100644 --- a/src/RuleCollection.php +++ b/src/RuleCollection.php @@ -15,7 +15,7 @@ class RuleCollection /** * @var Rule[] */ - protected $rules = []; + private $rules = []; /** * @param string $name diff --git a/src/Schedule.php b/src/Schedule.php index a38b17f..9255957 100644 --- a/src/Schedule.php +++ b/src/Schedule.php @@ -15,7 +15,7 @@ class Schedule implements \IteratorAggregate, \Countable /** * @var Rule[] */ - protected $rules = []; + private $rules = []; /** * @param Rule[] $rules diff --git a/src/ScheduleBuilder.php b/src/ScheduleBuilder.php index 46d4cb6..e58dd28 100644 --- a/src/ScheduleBuilder.php +++ b/src/ScheduleBuilder.php @@ -15,7 +15,7 @@ class ScheduleBuilder /** * @var RuleCollection */ - protected $collection; + private $collection; /** * @param RuleCollection $collection diff --git a/src/SmartSleep.php b/src/SmartSleep.php index 2a6df3c..7897d55 100644 --- a/src/SmartSleep.php +++ b/src/SmartSleep.php @@ -15,7 +15,7 @@ class SmartSleep /** * @var Schedule */ - protected $schedule = []; + private $schedule = []; /** * @param Schedule $schedule @@ -30,7 +30,7 @@ public function __construct(Schedule $schedule) * * @return int */ - public function getSleepSeconds(\DateTime $now) + public function sleepForSeconds(\DateTime $now) { $rule = $this->schedule->getMatchedRule($now); if ($rule instanceof Rule) { diff --git a/tests/functional/build_schedule.php b/tests/functional/build_schedule.php index d70a143..f440bc6 100644 --- a/tests/functional/build_schedule.php +++ b/tests/functional/build_schedule.php @@ -37,6 +37,6 @@ $smart = new SmartSleep($builder->buildSchedule($schedule)); -$seconds = $smart->getSleepSeconds(new \DateTime()); +$seconds = $smart->sleepForSeconds(new \DateTime()); echo sprintf('Sleep %s s.'.PHP_EOL, $seconds); diff --git a/tests/functional/simple_schedule.php b/tests/functional/simple_schedule.php index a30e80a..5dcd0aa 100644 --- a/tests/functional/simple_schedule.php +++ b/tests/functional/simple_schedule.php @@ -21,6 +21,6 @@ (new EverydayRule())->setStart(23)->setEnd(24)->setSeconds(60), // [23:00, 24:00) ])); -$seconds = $smart->getSleepSeconds(new \DateTime()); +$seconds = $smart->sleepForSeconds(new \DateTime()); echo sprintf('Sleep %s s.'.PHP_EOL, $seconds); diff --git a/tests/unit/SmartSleepTest.php b/tests/unit/SmartSleepTest.php index 9972a2e..e525446 100644 --- a/tests/unit/SmartSleepTest.php +++ b/tests/unit/SmartSleepTest.php @@ -51,7 +51,7 @@ public function testGetSleepSecondsNoMatches() ->with($this->time) ->will($this->returnValue(null)); - $this->assertEquals(0, $this->smart_sleep->getSleepSeconds($this->time)); + $this->assertEquals(0, $this->smart_sleep->sleepForSeconds($this->time)); } public function testGetSleepSecondsBadSeconds() @@ -67,7 +67,7 @@ public function testGetSleepSecondsBadSeconds() ->method('getSeconds') ->will($this->returnValue(-1)); - $this->assertEquals(0, $this->smart_sleep->getSleepSeconds($this->time)); + $this->assertEquals(0, $this->smart_sleep->sleepForSeconds($this->time)); } public function testGetSleepSeconds() @@ -83,6 +83,6 @@ public function testGetSleepSeconds() ->method('getSeconds') ->will($this->returnValue(10)); - $this->assertEquals(10, $this->smart_sleep->getSleepSeconds($this->time)); + $this->assertEquals(10, $this->smart_sleep->sleepForSeconds($this->time)); } } From f26461da80165d50465bac5ed8e8cb487c52e6a4 Mon Sep 17 00:00:00 2001 From: Peter Gribanov Date: Tue, 25 Jul 2017 11:40:23 +0300 Subject: [PATCH 03/17] rename methods Rule::getStart() -> Rule::start(), Rule::getEnd() -> Rule::end() and Rule::getSeconds() -> Rule::seconds() --- src/Rule/EverydayRule.php | 2 +- src/Rule/HolidayRule.php | 4 ++-- src/Rule/OnceDayRule.php | 2 +- src/Rule/OnceMonthRule.php | 2 +- src/Rule/OnceWeekRule.php | 2 +- src/Rule/RandMaxSecondsRuleBase.php | 4 ++-- src/Rule/Rule.php | 6 +++--- src/Rule/RuleBase.php | 6 +++--- src/Rule/WeekdayRule.php | 4 ++-- src/SmartSleep.php | 2 +- tests/unit/Rule/OnceDayRuleTest.php | 4 ++-- tests/unit/Rule/OnceMonthRuleTest.php | 6 +++--- tests/unit/Rule/OnceWeekRuleTest.php | 4 ++-- tests/unit/Rule/RandMaxSecondsTestCase.php | 2 +- tests/unit/TestRule.php | 6 +++--- 15 files changed, 28 insertions(+), 28 deletions(-) diff --git a/src/Rule/EverydayRule.php b/src/Rule/EverydayRule.php index 8c7b7e2..596a2e0 100644 --- a/src/Rule/EverydayRule.php +++ b/src/Rule/EverydayRule.php @@ -17,6 +17,6 @@ class EverydayRule extends RandMaxSecondsRuleBase */ public function isMatched(\DateTime $time) { - return $this->getStart() <= $time->format('G') && $this->getEnd() > $time->format('G'); + return $this->start() <= $time->format('G') && $this->end() > $time->format('G'); } } diff --git a/src/Rule/HolidayRule.php b/src/Rule/HolidayRule.php index 9c14151..4e9fdbf 100644 --- a/src/Rule/HolidayRule.php +++ b/src/Rule/HolidayRule.php @@ -18,7 +18,7 @@ class HolidayRule extends RandMaxSecondsRuleBase public function isMatched(\DateTime $time) { return $time->format('N') > 5 && - $this->getStart() <= $time->format('G') && - $this->getEnd() > $time->format('G'); + $this->start() <= $time->format('G') && + $this->end() > $time->format('G'); } } diff --git a/src/Rule/OnceDayRule.php b/src/Rule/OnceDayRule.php index 8ee201a..da4f8af 100644 --- a/src/Rule/OnceDayRule.php +++ b/src/Rule/OnceDayRule.php @@ -35,7 +35,7 @@ public function isMatched(\DateTime $time) /** * @return int */ - public function getSeconds() + public function seconds() { $offset_time = clone $this->time; $offset_time->modify('+1 day 00:00:00'); diff --git a/src/Rule/OnceMonthRule.php b/src/Rule/OnceMonthRule.php index e2e36e4..3773caf 100644 --- a/src/Rule/OnceMonthRule.php +++ b/src/Rule/OnceMonthRule.php @@ -35,7 +35,7 @@ public function isMatched(\DateTime $time) /** * @return int */ - public function getSeconds() + public function seconds() { // interval duration [next month, next month +1) $offset_time = clone $this->time; diff --git a/src/Rule/OnceWeekRule.php b/src/Rule/OnceWeekRule.php index 000f824..3d2becf 100644 --- a/src/Rule/OnceWeekRule.php +++ b/src/Rule/OnceWeekRule.php @@ -35,7 +35,7 @@ public function isMatched(\DateTime $time) /** * @return int */ - public function getSeconds() + public function seconds() { $offset_time = clone $this->time; $offset_time->modify('+1 week 00:00:00'); diff --git a/src/Rule/RandMaxSecondsRuleBase.php b/src/Rule/RandMaxSecondsRuleBase.php index 2112c4f..5ac03b6 100644 --- a/src/Rule/RandMaxSecondsRuleBase.php +++ b/src/Rule/RandMaxSecondsRuleBase.php @@ -33,8 +33,8 @@ public function __construct($min_sleep_seconds = self::DEFAULT_MIN_SLEEP_SECONDS /** * @return int */ - public function getSeconds() + public function seconds() { - return rand($this->min_sleep_seconds, parent::getSeconds()); + return rand($this->min_sleep_seconds, parent::seconds()); } } diff --git a/src/Rule/Rule.php b/src/Rule/Rule.php index 534f285..f17e74d 100644 --- a/src/Rule/Rule.php +++ b/src/Rule/Rule.php @@ -13,7 +13,7 @@ interface Rule /** * @return int */ - public function getStart(); + public function start(); /** * @param int $start @@ -25,7 +25,7 @@ public function setStart($start); /** * @return int */ - public function getEnd(); + public function end(); /** * @param int $end @@ -37,7 +37,7 @@ public function setEnd($end); /** * @return int */ - public function getSeconds(); + public function seconds(); /** * @param int $seconds diff --git a/src/Rule/RuleBase.php b/src/Rule/RuleBase.php index 635b128..c6d424e 100644 --- a/src/Rule/RuleBase.php +++ b/src/Rule/RuleBase.php @@ -28,7 +28,7 @@ abstract class RuleBase implements Rule /** * @return int */ - public function getStart() + public function start() { return $this->start; } @@ -48,7 +48,7 @@ public function setStart($start) /** * @return int */ - public function getEnd() + public function end() { return $this->end; } @@ -68,7 +68,7 @@ public function setEnd($end) /** * @return int */ - public function getSeconds() + public function seconds() { return $this->seconds; } diff --git a/src/Rule/WeekdayRule.php b/src/Rule/WeekdayRule.php index 8dd6372..58544ad 100644 --- a/src/Rule/WeekdayRule.php +++ b/src/Rule/WeekdayRule.php @@ -18,7 +18,7 @@ class WeekdayRule extends RandMaxSecondsRuleBase public function isMatched(\DateTime $time) { return $time->format('N') <= 5 && - $this->getStart() <= $time->format('G') && - $this->getEnd() > $time->format('G'); + $this->start() <= $time->format('G') && + $this->end() > $time->format('G'); } } diff --git a/src/SmartSleep.php b/src/SmartSleep.php index 7897d55..2b6dd9b 100644 --- a/src/SmartSleep.php +++ b/src/SmartSleep.php @@ -34,7 +34,7 @@ public function sleepForSeconds(\DateTime $now) { $rule = $this->schedule->getMatchedRule($now); if ($rule instanceof Rule) { - $seconds = $rule->getSeconds(); + $seconds = $rule->seconds(); return $seconds > 0 ? $seconds : 0; } diff --git a/tests/unit/Rule/OnceDayRuleTest.php b/tests/unit/Rule/OnceDayRuleTest.php index 7c1d784..2bca665 100644 --- a/tests/unit/Rule/OnceDayRuleTest.php +++ b/tests/unit/Rule/OnceDayRuleTest.php @@ -52,7 +52,7 @@ public function testGetSecondsFromConstruct() { $limit = strtotime('+2 day 00:00:00') - time(); - $seconds = $this->rule->getSeconds(); + $seconds = $this->rule->seconds(); // -1 seconds because is long wait execute test $this->assertTrue($seconds >= -1); @@ -68,7 +68,7 @@ public function testGetSecondsFromMatched() $this->rule->isMatched($time); - $seconds = $this->rule->getSeconds(); + $seconds = $this->rule->seconds(); $this->assertTrue($seconds >= 0); $this->assertTrue($seconds < $limit); } diff --git a/tests/unit/Rule/OnceMonthRuleTest.php b/tests/unit/Rule/OnceMonthRuleTest.php index 90bb19c..b08c62b 100644 --- a/tests/unit/Rule/OnceMonthRuleTest.php +++ b/tests/unit/Rule/OnceMonthRuleTest.php @@ -54,7 +54,7 @@ public function testGetSecondsFromConstruct() $limit_time->modify('first day of this month'); $limit = $limit_time->getTimestamp() - time(); - $seconds = $this->rule->getSeconds(); + $seconds = $this->rule->seconds(); // -1 seconds because is long wait execute test $this->assertTrue($seconds >= -1); @@ -69,7 +69,7 @@ public function testGetSecondsFromMatched() $this->rule->isMatched($time); - $seconds = $this->rule->getSeconds(); + $seconds = $this->rule->seconds(); $this->assertTrue($seconds >= 0); $this->assertTrue($seconds < $limit); } @@ -82,7 +82,7 @@ public function testGetSecondsFromMatchedForFebruary() $this->rule->isMatched($time); - $seconds = $this->rule->getSeconds(); + $seconds = $this->rule->seconds(); $this->assertTrue($seconds >= 0); $this->assertTrue($seconds < $limit); } diff --git a/tests/unit/Rule/OnceWeekRuleTest.php b/tests/unit/Rule/OnceWeekRuleTest.php index b530431..c919b3e 100644 --- a/tests/unit/Rule/OnceWeekRuleTest.php +++ b/tests/unit/Rule/OnceWeekRuleTest.php @@ -52,7 +52,7 @@ public function testGetSecondsFromConstruct() { $limit = strtotime('+2 week 00:00:00') - time(); - $seconds = $this->rule->getSeconds(); + $seconds = $this->rule->seconds(); // -1 seconds because is long wait execute test $this->assertTrue($seconds >= -1); @@ -67,7 +67,7 @@ public function testGetSecondsFromMatched() $this->rule->isMatched($time); - $seconds = $this->rule->getSeconds(); + $seconds = $this->rule->seconds(); $this->assertTrue($seconds >= 0); $this->assertTrue($seconds < $limit); } diff --git a/tests/unit/Rule/RandMaxSecondsTestCase.php b/tests/unit/Rule/RandMaxSecondsTestCase.php index 30c4f68..2592e68 100644 --- a/tests/unit/Rule/RandMaxSecondsTestCase.php +++ b/tests/unit/Rule/RandMaxSecondsTestCase.php @@ -62,7 +62,7 @@ public function testRandSeconds($default) $max = $min + 1; $this->assertEquals($rule, $rule->setSeconds($max)); - $seconds = $rule->getSeconds(); + $seconds = $rule->seconds(); $this->assertTrue($seconds == $min || $seconds == $max); } } diff --git a/tests/unit/TestRule.php b/tests/unit/TestRule.php index f82980a..d8fdf5f 100644 --- a/tests/unit/TestRule.php +++ b/tests/unit/TestRule.php @@ -33,7 +33,7 @@ class TestRule implements Rule /** * @return int */ - public function getStart() + public function start() { return $this->start; } @@ -53,7 +53,7 @@ public function setStart($start) /** * @return int */ - public function getEnd() + public function end() { return $this->end; } @@ -73,7 +73,7 @@ public function setEnd($end) /** * @return int */ - public function getSeconds() + public function seconds() { return $this->seconds; } From a423d37875de5e4bd6051f6f216d8da2648fa2c1 Mon Sep 17 00:00:00 2001 From: Peter Gribanov Date: Tue, 25 Jul 2017 11:43:59 +0300 Subject: [PATCH 04/17] RandMaxSecondsRuleBase use 0 as a min sleep seconds --- src/Rule/RandMaxSecondsRuleBase.php | 22 +--------------------- 1 file changed, 1 insertion(+), 21 deletions(-) diff --git a/src/Rule/RandMaxSecondsRuleBase.php b/src/Rule/RandMaxSecondsRuleBase.php index 5ac03b6..8d1107c 100644 --- a/src/Rule/RandMaxSecondsRuleBase.php +++ b/src/Rule/RandMaxSecondsRuleBase.php @@ -10,31 +10,11 @@ abstract class RandMaxSecondsRuleBase extends RuleBase { - /** - * Default sleep at least seconds. - * - * @var int - */ - const DEFAULT_MIN_SLEEP_SECONDS = 10; - - /** - * @var int - */ - protected $min_sleep_seconds; - - /** - * @param int $min_sleep_seconds - */ - public function __construct($min_sleep_seconds = self::DEFAULT_MIN_SLEEP_SECONDS) - { - $this->min_sleep_seconds = $min_sleep_seconds; - } - /** * @return int */ public function seconds() { - return rand($this->min_sleep_seconds, parent::seconds()); + return rand(0, parent::seconds()); } } From 92d9295841016d3884d36a99681522dd7358dc37 Mon Sep 17 00:00:00 2001 From: Peter Gribanov Date: Tue, 25 Jul 2017 12:54:51 +0300 Subject: [PATCH 05/17] optimize rules --- src/Rule/EverydayRule.php | 17 ++++- src/Rule/HolidayRule.php | 23 +++++- ...condsRuleBase.php => HourIntervalRule.php} | 12 +-- ...RuleBase.php => HourIntervalRuleTrait.php} | 49 ++---------- src/Rule/OnceDayRule.php | 4 +- src/Rule/OnceMonthRule.php | 4 +- src/Rule/OnceWeekRule.php | 4 +- src/Rule/RandMaxSecondsRuleTrait.php | 33 ++++++++ src/Rule/Rule.php | 31 -------- src/Rule/RuleTrait.php | 33 ++++++++ src/Rule/WeekdayRule.php | 23 +++++- tests/functional/build_schedule.php | 32 ++++---- tests/functional/simple_schedule.php | 10 +-- tests/unit/Rule/EverydayRuleTest.php | 64 ++++------------ tests/unit/Rule/HolidayRuleTest.php | 76 +++++-------------- tests/unit/Rule/OnceDayRuleTest.php | 30 +------- tests/unit/Rule/OnceMonthRuleTest.php | 30 +------- tests/unit/Rule/OnceWeekRuleTest.php | 30 +------- tests/unit/Rule/RandMaxSecondsTestCase.php | 68 ----------------- tests/unit/Rule/TestCase.php | 46 ----------- tests/unit/Rule/WeekdayRuleTest.php | 76 +++++-------------- tests/unit/ScheduleTest.php | 14 ++-- tests/unit/SmartSleepTest.php | 31 ++++---- 23 files changed, 250 insertions(+), 490 deletions(-) rename src/Rule/{RandMaxSecondsRuleBase.php => HourIntervalRule.php} (60%) rename src/Rule/{RuleBase.php => HourIntervalRuleTrait.php} (53%) create mode 100644 src/Rule/RandMaxSecondsRuleTrait.php create mode 100644 src/Rule/RuleTrait.php delete mode 100644 tests/unit/Rule/RandMaxSecondsTestCase.php delete mode 100644 tests/unit/Rule/TestCase.php diff --git a/src/Rule/EverydayRule.php b/src/Rule/EverydayRule.php index 596a2e0..85b13a7 100644 --- a/src/Rule/EverydayRule.php +++ b/src/Rule/EverydayRule.php @@ -8,8 +8,23 @@ namespace AnimeDb\SmartSleep\Rule; -class EverydayRule extends RandMaxSecondsRuleBase +class EverydayRule implements HourIntervalRule { + use HourIntervalRuleTrait; + use RandMaxSecondsRuleTrait; + + /** + * @param int $start + * @param int $end + * @param int $seconds + */ + public function __construct($start, $end, $seconds) + { + $this->setStart($start); + $this->setEnd($end); + $this->setSeconds($seconds); + } + /** * @param \DateTime $time * diff --git a/src/Rule/HolidayRule.php b/src/Rule/HolidayRule.php index 4e9fdbf..818eb95 100644 --- a/src/Rule/HolidayRule.php +++ b/src/Rule/HolidayRule.php @@ -8,8 +8,23 @@ namespace AnimeDb\SmartSleep\Rule; -class HolidayRule extends RandMaxSecondsRuleBase +class HolidayRule implements HourIntervalRule { + use HourIntervalRuleTrait; + use RandMaxSecondsRuleTrait; + + /** + * @param int $start + * @param int $end + * @param int $seconds + */ + public function __construct($start, $end, $seconds) + { + $this->setStart($start); + $this->setEnd($end); + $this->setSeconds($seconds); + } + /** * @param \DateTime $time * @@ -17,8 +32,10 @@ class HolidayRule extends RandMaxSecondsRuleBase */ public function isMatched(\DateTime $time) { - return $time->format('N') > 5 && + return + $time->format('N') > 5 && $this->start() <= $time->format('G') && - $this->end() > $time->format('G'); + $this->end() > $time->format('G') + ; } } diff --git a/src/Rule/RandMaxSecondsRuleBase.php b/src/Rule/HourIntervalRule.php similarity index 60% rename from src/Rule/RandMaxSecondsRuleBase.php rename to src/Rule/HourIntervalRule.php index 8d1107c..4521ded 100644 --- a/src/Rule/RandMaxSecondsRuleBase.php +++ b/src/Rule/HourIntervalRule.php @@ -8,13 +8,15 @@ namespace AnimeDb\SmartSleep\Rule; -abstract class RandMaxSecondsRuleBase extends RuleBase +interface HourIntervalRule extends Rule { /** * @return int */ - public function seconds() - { - return rand(0, parent::seconds()); - } + public function start(); + + /** + * @return int + */ + public function end(); } diff --git a/src/Rule/RuleBase.php b/src/Rule/HourIntervalRuleTrait.php similarity index 53% rename from src/Rule/RuleBase.php rename to src/Rule/HourIntervalRuleTrait.php index c6d424e..2f169c1 100644 --- a/src/Rule/RuleBase.php +++ b/src/Rule/HourIntervalRuleTrait.php @@ -8,7 +8,7 @@ namespace AnimeDb\SmartSleep\Rule; -abstract class RuleBase implements Rule +trait HourIntervalRuleTrait { /** * @var int @@ -20,68 +20,35 @@ abstract class RuleBase implements Rule */ private $end = -1; - /** - * @var int - */ - private $seconds = 0; - - /** - * @return int - */ - public function start() - { - return $this->start; - } - /** * @param int $start - * - * @return RuleBase */ - public function setStart($start) + protected function setStart($start) { $this->start = $start; - - return $this; - } - - /** - * @return int - */ - public function end() - { - return $this->end; } /** * @param int $end - * - * @return RuleBase */ - public function setEnd($end) + protected function setEnd($end) { $this->end = $end; - - return $this; } /** * @return int */ - public function seconds() + public function start() { - return $this->seconds; + return $this->start; } /** - * @param int $seconds - * - * @return RuleBase + * @return int */ - public function setSeconds($seconds) + public function end() { - $this->seconds = $seconds; - - return $this; + return $this->end; } } diff --git a/src/Rule/OnceDayRule.php b/src/Rule/OnceDayRule.php index da4f8af..83c8c08 100644 --- a/src/Rule/OnceDayRule.php +++ b/src/Rule/OnceDayRule.php @@ -8,12 +8,12 @@ namespace AnimeDb\SmartSleep\Rule; -class OnceDayRule extends RuleBase +class OnceDayRule implements Rule { /** * @var \DateTime */ - protected $time; + private $time; public function __construct() { diff --git a/src/Rule/OnceMonthRule.php b/src/Rule/OnceMonthRule.php index 3773caf..c9db9fe 100644 --- a/src/Rule/OnceMonthRule.php +++ b/src/Rule/OnceMonthRule.php @@ -8,12 +8,12 @@ namespace AnimeDb\SmartSleep\Rule; -class OnceMonthRule extends RuleBase +class OnceMonthRule implements Rule { /** * @var \DateTime */ - protected $time; + private $time; public function __construct() { diff --git a/src/Rule/OnceWeekRule.php b/src/Rule/OnceWeekRule.php index 3d2becf..ca44e00 100644 --- a/src/Rule/OnceWeekRule.php +++ b/src/Rule/OnceWeekRule.php @@ -8,12 +8,12 @@ namespace AnimeDb\SmartSleep\Rule; -class OnceWeekRule extends RuleBase +class OnceWeekRule implements Rule { /** * @var \DateTime */ - protected $time; + private $time; public function __construct() { diff --git a/src/Rule/RandMaxSecondsRuleTrait.php b/src/Rule/RandMaxSecondsRuleTrait.php new file mode 100644 index 0000000..d96090b --- /dev/null +++ b/src/Rule/RandMaxSecondsRuleTrait.php @@ -0,0 +1,33 @@ + + * @copyright Copyright (c) 2011, Peter Gribanov + */ + +namespace AnimeDb\SmartSleep\Rule; + +trait RandMaxSecondsRuleTrait +{ + /** + * @var int + */ + private $seconds = 0; + + /** + * @param int $seconds + */ + protected function setSeconds($seconds) + { + $this->seconds = $seconds; + } + + /** + * @return int + */ + public function seconds() + { + return rand(0, $this->seconds); + } +} diff --git a/src/Rule/Rule.php b/src/Rule/Rule.php index f17e74d..4f746e1 100644 --- a/src/Rule/Rule.php +++ b/src/Rule/Rule.php @@ -10,42 +10,11 @@ interface Rule { - /** - * @return int - */ - public function start(); - - /** - * @param int $start - * - * @return Rule - */ - public function setStart($start); - - /** - * @return int - */ - public function end(); - - /** - * @param int $end - * - * @return Rule - */ - public function setEnd($end); - /** * @return int */ public function seconds(); - /** - * @param int $seconds - * - * @return Rule - */ - public function setSeconds($seconds); - /** * @param \DateTime $time * diff --git a/src/Rule/RuleTrait.php b/src/Rule/RuleTrait.php new file mode 100644 index 0000000..904c29b --- /dev/null +++ b/src/Rule/RuleTrait.php @@ -0,0 +1,33 @@ + + * @copyright Copyright (c) 2011, Peter Gribanov + */ + +namespace AnimeDb\SmartSleep\Rule; + +trait RuleTrait +{ + /** + * @var int + */ + private $seconds = 0; + + /** + * @param int $seconds + */ + protected function setSeconds($seconds) + { + $this->seconds = $seconds; + } + + /** + * @return int + */ + public function seconds() + { + return $this->seconds; + } +} diff --git a/src/Rule/WeekdayRule.php b/src/Rule/WeekdayRule.php index 58544ad..fd4acde 100644 --- a/src/Rule/WeekdayRule.php +++ b/src/Rule/WeekdayRule.php @@ -8,8 +8,23 @@ namespace AnimeDb\SmartSleep\Rule; -class WeekdayRule extends RandMaxSecondsRuleBase +class WeekdayRule implements HourIntervalRule { + use HourIntervalRuleTrait; + use RandMaxSecondsRuleTrait; + + /** + * @param int $start + * @param int $end + * @param int $seconds + */ + public function __construct($start, $end, $seconds) + { + $this->setStart($start); + $this->setEnd($end); + $this->setSeconds($seconds); + } + /** * @param \DateTime $time * @@ -17,8 +32,10 @@ class WeekdayRule extends RandMaxSecondsRuleBase */ public function isMatched(\DateTime $time) { - return $time->format('N') <= 5 && + return + $time->format('N') <= 5 && $this->start() <= $time->format('G') && - $this->end() > $time->format('G'); + $this->end() > $time->format('G') + ; } } diff --git a/tests/functional/build_schedule.php b/tests/functional/build_schedule.php index f440bc6..5f062fe 100644 --- a/tests/functional/build_schedule.php +++ b/tests/functional/build_schedule.php @@ -7,7 +7,6 @@ */ use AnimeDb\SmartSleep\Rule\HolidayRule; use AnimeDb\SmartSleep\Rule\WeekdayRule; -use AnimeDb\SmartSleep\RuleCollection; use AnimeDb\SmartSleep\ScheduleBuilder; use AnimeDb\SmartSleep\SmartSleep; @@ -16,26 +15,23 @@ 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], + ['rule' => WeekdayRule::class, 'start' => 0, 'end' => 2, 'seconds' => 600], + ['rule' => WeekdayRule::class, 'start' => 1, 'end' => 7, 'seconds' => 800], + ['rule' => WeekdayRule::class, 'start' => 7, 'end' => 10, 'seconds' => 100], + ['rule' => WeekdayRule::class, 'start' => 10, 'end' => 19, 'seconds' => 160], + ['rule' => WeekdayRule::class, 'start' => 19, 'end' => 22, 'seconds' => 70], + ['rule' => WeekdayRule::class, 'start' => 22, 'end' => 24, 'seconds' => 260], + ['rule' => HolidayRule::class, 'start' => 0, 'end' => 3, 'seconds' => 260], + ['rule' => HolidayRule::class, 'start' => 3, 'end' => 9, 'seconds' => 900], + ['rule' => HolidayRule::class, 'start' => 9, 'end' => 19, 'seconds' => 160], + ['rule' => HolidayRule::class, 'start' => 19, 'end' => 23, 'seconds' => 70], + ['rule' => HolidayRule::class, 'start' => 23, 'end' => 24, 'seconds' => 260], ]; -$collection = new RuleCollection(); -$collection->set('weekday', new WeekdayRule()); -$collection->set('holiday', new HolidayRule()); +$builder = new ScheduleBuilder(); +$schedule = $builder->buildSchedule($schedule); -$builder = new ScheduleBuilder($collection); - -$smart = new SmartSleep($builder->buildSchedule($schedule)); +$smart = new SmartSleep($schedule); $seconds = $smart->sleepForSeconds(new \DateTime()); diff --git a/tests/functional/simple_schedule.php b/tests/functional/simple_schedule.php index 5dcd0aa..73084ae 100644 --- a/tests/functional/simple_schedule.php +++ b/tests/functional/simple_schedule.php @@ -14,11 +14,11 @@ echo 'Functional test: Simple schedule.'.PHP_EOL; $smart = new SmartSleep(new Schedule([ - (new EverydayRule())->setStart(0)->setEnd(3)->setSeconds(260), // [00:00, 03:00) - (new EverydayRule())->setStart(3)->setEnd(9)->setSeconds(900), // [03:00, 09:00) - (new EverydayRule())->setStart(9)->setEnd(19)->setSeconds(160), // [09:00, 19:00) - (new EverydayRule())->setStart(19)->setEnd(23)->setSeconds(70), // [19:00, 23:00) - (new EverydayRule())->setStart(23)->setEnd(24)->setSeconds(60), // [23:00, 24:00) + new EverydayRule(0, 3, 260), // [00:00, 03:00) + new EverydayRule(3, 9, 900), // [03:00, 09:00) + new EverydayRule(9, 19, 160), // [09:00, 19:00) + new EverydayRule(19, 23, 70), // [19:00, 23:00) + new EverydayRule(23, 24, 60), // [23:00, 24:00) ])); $seconds = $smart->sleepForSeconds(new \DateTime()); diff --git a/tests/unit/Rule/EverydayRuleTest.php b/tests/unit/Rule/EverydayRuleTest.php index e3da300..74ba58d 100644 --- a/tests/unit/Rule/EverydayRuleTest.php +++ b/tests/unit/Rule/EverydayRuleTest.php @@ -10,56 +10,19 @@ use AnimeDb\SmartSleep\Rule\EverydayRule; -class EverydayRuleTest extends RandMaxSecondsTestCase +class EverydayRuleTest extends \PHPUnit_Framework_TestCase { - /** - * @var EverydayRule - */ - protected $rule; - - /** - * @var EverydayRule - */ - protected $rule_limited; - - protected function setUp() - { - $this->rule = new EverydayRule(); - } - - /** - * @return EverydayRule - */ - protected function getRule() - { - return $this->rule; - } - - /** - * @param int $min_sleep_seconds - * - * @return EverydayRule - */ - public function getRuleForMinSleepSeconds($min_sleep_seconds) - { - if (!$this->rule_limited) { - $this->rule_limited = new EverydayRule($min_sleep_seconds); - } - - return $this->rule_limited; - } - /** * @return array */ public function getMatches() { return [ - [new \DateTime('2:59'), 3, 5, false], - [new \DateTime('3:00'), 3, 5, true], - [new \DateTime('4:00'), 3, 5, true], - [new \DateTime('4:59'), 3, 5, true], - [new \DateTime('5:00'), 3, 5, false], + [new \DateTime('2:59'), 3, 5, 10, false], + [new \DateTime('3:00'), 3, 5, 50, true], + [new \DateTime('4:00'), 3, 5, 100, true], + [new \DateTime('4:59'), 3, 5, 200, true], + [new \DateTime('5:00'), 3, 5, 550, false], ]; } @@ -69,18 +32,21 @@ public function getMatches() * @param \DateTime $time * @param int $start * @param int $end + * @param int $seconds * @param bool $match */ - public function testIsMatched(\DateTime $time, $start, $end, $match) + public function testIsMatched(\DateTime $time, $start, $end, $seconds, $match) { - $this->rule - ->setStart($start) - ->setEnd($end); + $rule = new EverydayRule($start, $end, $seconds); + + $this->assertEquals($start, $rule->start()); + $this->assertEquals($end, $rule->end()); + $this->assertTrue($rule->seconds() <= $seconds); if ($match) { - $this->assertTrue($this->rule->isMatched($time)); + $this->assertTrue($rule->isMatched($time)); } else { - $this->assertFalse($this->rule->isMatched($time)); + $this->assertFalse($rule->isMatched($time)); } } } diff --git a/tests/unit/Rule/HolidayRuleTest.php b/tests/unit/Rule/HolidayRuleTest.php index 2fe3020..9fba437 100644 --- a/tests/unit/Rule/HolidayRuleTest.php +++ b/tests/unit/Rule/HolidayRuleTest.php @@ -10,45 +10,8 @@ use AnimeDb\SmartSleep\Rule\HolidayRule; -class HolidayRuleTest extends RandMaxSecondsTestCase +class HolidayRuleTest extends \PHPUnit_Framework_TestCase { - /** - * @var HolidayRule - */ - protected $rule; - - /** - * @var HolidayRule - */ - protected $rule_limited; - - protected function setUp() - { - $this->rule = new HolidayRule(); - } - - /** - * @return HolidayRule - */ - protected function getRule() - { - return $this->rule; - } - - /** - * @param int $min_sleep_seconds - * - * @return HolidayRule - */ - protected function getRuleForMinSleepSeconds($min_sleep_seconds) - { - if (!$this->rule_limited) { - $this->rule_limited = new HolidayRule($min_sleep_seconds); - } - - return $this->rule_limited; - } - /** * @return array */ @@ -56,18 +19,18 @@ public function getMatches() { return [ // test by day time - [new \DateTime('25-06-2016 2:59'), 3, 5, false], - [new \DateTime('25-06-2016 3:00'), 3, 5, true], - [new \DateTime('25-06-2016 4:00'), 3, 5, true], - [new \DateTime('25-06-2016 4:59'), 3, 5, true], - [new \DateTime('25-06-2016 5:00'), 3, 5, false], + [new \DateTime('25-06-2016 2:59'), 3, 5, 10, false], + [new \DateTime('25-06-2016 3:00'), 3, 5, 20, true], + [new \DateTime('25-06-2016 4:00'), 3, 5, 50, true], + [new \DateTime('25-06-2016 4:59'), 3, 5, 100, true], + [new \DateTime('25-06-2016 5:00'), 3, 5, 150, false], // tests by week day - [new \DateTime('20-06-2016 4:00'), 3, 5, false], - [new \DateTime('21-06-2016 4:00'), 3, 5, false], - [new \DateTime('22-06-2016 4:00'), 3, 5, false], - [new \DateTime('24-06-2016 4:00'), 3, 5, false], - [new \DateTime('25-06-2016 4:00'), 3, 5, true], - [new \DateTime('26-06-2016 4:00'), 3, 5, true], + [new \DateTime('20-06-2016 4:00'), 3, 5, 300, false], + [new \DateTime('21-06-2016 4:00'), 3, 5, 550, false], + [new \DateTime('22-06-2016 4:00'), 3, 5, 0, false], + [new \DateTime('24-06-2016 4:00'), 3, 5, 333, false], + [new \DateTime('25-06-2016 4:00'), 3, 5, 123, true], + [new \DateTime('26-06-2016 4:00'), 3, 5, 999, true], ]; } @@ -77,18 +40,21 @@ public function getMatches() * @param \DateTime $time * @param int $start * @param int $end + * @param int $seconds * @param bool $match */ - public function testIsMatched(\DateTime $time, $start, $end, $match) + public function testIsMatched(\DateTime $time, $start, $end, $seconds, $match) { - $this->rule - ->setStart($start) - ->setEnd($end); + $rule = new HolidayRule($start, $end, $seconds); + + $this->assertEquals($start, $rule->start()); + $this->assertEquals($end, $rule->end()); + $this->assertTrue($rule->seconds() <= $seconds); if ($match) { - $this->assertTrue($this->rule->isMatched($time)); + $this->assertTrue($rule->isMatched($time)); } else { - $this->assertFalse($this->rule->isMatched($time)); + $this->assertFalse($rule->isMatched($time)); } } } diff --git a/tests/unit/Rule/OnceDayRuleTest.php b/tests/unit/Rule/OnceDayRuleTest.php index 2bca665..57a833c 100644 --- a/tests/unit/Rule/OnceDayRuleTest.php +++ b/tests/unit/Rule/OnceDayRuleTest.php @@ -10,44 +10,18 @@ use AnimeDb\SmartSleep\Rule\OnceDayRule; -class OnceDayRuleTest extends TestCase +class OnceDayRuleTest extends \PHPUnit_Framework_TestCase { /** * @var OnceDayRule */ - protected $rule; + private $rule; protected function setUp() { $this->rule = new OnceDayRule(); } - /** - * @return OnceDayRule - */ - protected function getRule() - { - return $this->rule; - } - - /** - * @return array - */ - public function getGettersAndSetters() - { - return [ - ['getStart', 'setStart', -1], - ['getEnd', 'setEnd', -1], - // not check get/set seconds - ]; - } - - public function testSetSeconds() - { - // setted seconds not used - $this->assertEquals($this->rule, $this->rule->setSeconds(123)); - } - public function testGetSecondsFromConstruct() { $limit = strtotime('+2 day 00:00:00') - time(); diff --git a/tests/unit/Rule/OnceMonthRuleTest.php b/tests/unit/Rule/OnceMonthRuleTest.php index b08c62b..bcfb476 100644 --- a/tests/unit/Rule/OnceMonthRuleTest.php +++ b/tests/unit/Rule/OnceMonthRuleTest.php @@ -10,44 +10,18 @@ use AnimeDb\SmartSleep\Rule\OnceMonthRule; -class OnceMonthRuleTest extends TestCase +class OnceMonthRuleTest extends \PHPUnit_Framework_TestCase { /** * @var OnceMonthRule */ - protected $rule; + private $rule; protected function setUp() { $this->rule = new OnceMonthRule(); } - /** - * @return OnceMonthRule - */ - protected function getRule() - { - return $this->rule; - } - - /** - * @return array - */ - public function getGettersAndSetters() - { - return [ - ['getStart', 'setStart', -1], - ['getEnd', 'setEnd', -1], - // not check get/set seconds - ]; - } - - public function testSetSeconds() - { - // setted seconds not used - $this->assertEquals($this->rule, $this->rule->setSeconds(123)); - } - public function testGetSecondsFromConstruct() { $limit_time = new \DateTime('+2 month 00:00:00'); diff --git a/tests/unit/Rule/OnceWeekRuleTest.php b/tests/unit/Rule/OnceWeekRuleTest.php index c919b3e..3a7cac5 100644 --- a/tests/unit/Rule/OnceWeekRuleTest.php +++ b/tests/unit/Rule/OnceWeekRuleTest.php @@ -10,44 +10,18 @@ use AnimeDb\SmartSleep\Rule\OnceWeekRule; -class OnceWeekRuleTest extends TestCase +class OnceWeekRuleTest extends \PHPUnit_Framework_TestCase { /** * @var OnceWeekRule */ - protected $rule; + private $rule; protected function setUp() { $this->rule = new OnceWeekRule(); } - /** - * @return OnceWeekRule - */ - protected function getRule() - { - return $this->rule; - } - - /** - * @return array - */ - public function getGettersAndSetters() - { - return [ - ['getStart', 'setStart', -1], - ['getEnd', 'setEnd', -1], - // not check get/set seconds - ]; - } - - public function testSetSeconds() - { - // setted seconds not used - $this->assertEquals($this->rule, $this->rule->setSeconds(123)); - } - public function testGetSecondsFromConstruct() { $limit = strtotime('+2 week 00:00:00') - time(); diff --git a/tests/unit/Rule/RandMaxSecondsTestCase.php b/tests/unit/Rule/RandMaxSecondsTestCase.php deleted file mode 100644 index 2592e68..0000000 --- a/tests/unit/Rule/RandMaxSecondsTestCase.php +++ /dev/null @@ -1,68 +0,0 @@ - - * @copyright Copyright (c) 2011, Peter Gribanov - */ - -namespace AnimeDb\SmartSleep\Tests\Unit\Rule; - -use AnimeDb\SmartSleep\Rule\RandMaxSecondsRuleBase; -use AnimeDb\SmartSleep\Rule\Rule; - -abstract class RandMaxSecondsTestCase extends TestCase -{ - /** - * Create rule and set min sleep seconds to construct. - * - * @param int $min_sleep_seconds - * - * @return Rule - */ - abstract protected function getRuleForMinSleepSeconds($min_sleep_seconds); - - /** - * @return array - */ - public function getGettersAndSetters() - { - return [ - ['getStart', 'setStart', -1], - ['getEnd', 'setEnd', -1], - // not check get/set seconds - ]; - } - - /** - * @return array - */ - public function getRandSeconds() - { - return [ - [true], - [false], - ]; - } - - /** - * @dataProvider getRandSeconds - * - * @param bool $default - */ - public function testRandSeconds($default) - { - if ($default) { - $min = RandMaxSecondsRuleBase::DEFAULT_MIN_SLEEP_SECONDS; - $rule = $this->getRule(); - } else { - $min = 4; - $rule = $this->getRuleForMinSleepSeconds($min); - } - $max = $min + 1; - - $this->assertEquals($rule, $rule->setSeconds($max)); - $seconds = $rule->seconds(); - $this->assertTrue($seconds == $min || $seconds == $max); - } -} diff --git a/tests/unit/Rule/TestCase.php b/tests/unit/Rule/TestCase.php deleted file mode 100644 index ad3c98a..0000000 --- a/tests/unit/Rule/TestCase.php +++ /dev/null @@ -1,46 +0,0 @@ - - * @copyright Copyright (c) 2011, Peter Gribanov - */ - -namespace AnimeDb\SmartSleep\Tests\Unit\Rule; - -use AnimeDb\SmartSleep\Rule\Rule; - -abstract class TestCase extends \PHPUnit_Framework_TestCase -{ - /** - * @return Rule - */ - abstract protected function getRule(); - - /** - * @return array - */ - public function getGettersAndSetters() - { - return [ - ['getStart', 'setStart', -1], - ['getEnd', 'setEnd', -1], - ['getSeconds', 'setSeconds', 0], - ]; - } - - /** - * @dataProvider getGettersAndSetters - * - * @param string $getter - * @param string $setter - * @param int $new_value - * @param int $default_value - */ - public function testGetSet($getter, $setter, $default_value, $new_value = 123) - { - $this->assertEquals($default_value, call_user_func([$this->getRule(), $getter])); - $this->assertEquals($this->getRule(), call_user_func([$this->getRule(), $setter], $new_value)); - $this->assertEquals($new_value, call_user_func([$this->getRule(), $getter])); - } -} diff --git a/tests/unit/Rule/WeekdayRuleTest.php b/tests/unit/Rule/WeekdayRuleTest.php index 5573ef3..086c2a1 100644 --- a/tests/unit/Rule/WeekdayRuleTest.php +++ b/tests/unit/Rule/WeekdayRuleTest.php @@ -10,45 +10,8 @@ use AnimeDb\SmartSleep\Rule\WeekdayRule; -class WeekdayRuleTest extends RandMaxSecondsTestCase +class WeekdayRuleTest extends \PHPUnit_Framework_TestCase { - /** - * @var WeekdayRule - */ - protected $rule; - - /** - * @var WeekdayRule - */ - protected $rule_limited; - - protected function setUp() - { - $this->rule = new WeekdayRule(); - } - - /** - * @return WeekdayRule - */ - protected function getRule() - { - return $this->rule; - } - - /** - * @param int $min_sleep_seconds - * - * @return WeekdayRule - */ - protected function getRuleForMinSleepSeconds($min_sleep_seconds) - { - if (!$this->rule_limited) { - $this->rule_limited = new WeekdayRule($min_sleep_seconds); - } - - return $this->rule_limited; - } - /** * @return array */ @@ -56,18 +19,18 @@ public function getMatches() { return [ // test by day time - [new \DateTime('23-06-2016 2:59'), 3, 5, false], - [new \DateTime('23-06-2016 3:00'), 3, 5, true], - [new \DateTime('23-06-2016 4:00'), 3, 5, true], - [new \DateTime('23-06-2016 4:59'), 3, 5, true], - [new \DateTime('23-06-2016 5:00'), 3, 5, false], + [new \DateTime('23-06-2016 2:59'), 3, 5, 10, false], + [new \DateTime('23-06-2016 3:00'), 3, 5, 25, true], + [new \DateTime('23-06-2016 4:00'), 3, 5, 33, true], + [new \DateTime('23-06-2016 4:59'), 3, 5, 0, true], + [new \DateTime('23-06-2016 5:00'), 3, 5, 67, false], // tests by week day - [new \DateTime('20-06-2016 4:00'), 3, 5, true], - [new \DateTime('21-06-2016 4:00'), 3, 5, true], - [new \DateTime('22-06-2016 4:00'), 3, 5, true], - [new \DateTime('24-06-2016 4:00'), 3, 5, true], - [new \DateTime('25-06-2016 4:00'), 3, 5, false], - [new \DateTime('26-06-2016 4:00'), 3, 5, false], + [new \DateTime('20-06-2016 4:00'), 3, 5, 100, true], + [new \DateTime('21-06-2016 4:00'), 3, 5, 150, true], + [new \DateTime('22-06-2016 4:00'), 3, 5, 350, true], + [new \DateTime('24-06-2016 4:00'), 3, 5, 550, true], + [new \DateTime('25-06-2016 4:00'), 3, 5, 123, false], + [new \DateTime('26-06-2016 4:00'), 3, 5, 999, false], ]; } @@ -77,18 +40,21 @@ public function getMatches() * @param \DateTime $time * @param int $start * @param int $end + * @param int $seconds * @param bool $match */ - public function testIsMatched(\DateTime $time, $start, $end, $match) + public function testIsMatched(\DateTime $time, $start, $end, $seconds, $match) { - $this->rule - ->setStart($start) - ->setEnd($end); + $rule = new WeekdayRule($start, $end, $seconds); + + $this->assertEquals($start, $rule->start()); + $this->assertEquals($end, $rule->end()); + $this->assertTrue($rule->seconds() <= $seconds); if ($match) { - $this->assertTrue($this->rule->isMatched($time)); + $this->assertTrue($rule->isMatched($time)); } else { - $this->assertFalse($this->rule->isMatched($time)); + $this->assertFalse($rule->isMatched($time)); } } } diff --git a/tests/unit/ScheduleTest.php b/tests/unit/ScheduleTest.php index e94f03d..dd4837f 100644 --- a/tests/unit/ScheduleTest.php +++ b/tests/unit/ScheduleTest.php @@ -16,7 +16,7 @@ class ScheduleTest extends \PHPUnit_Framework_TestCase /** * @var Schedule */ - protected $schedule; + private $schedule; protected function setUp() { @@ -33,9 +33,9 @@ public function testConstruct() { /* @var $rules \PHPUnit_Framework_MockObject_MockObject[]|Rule[] */ $rules = [ - $this->getMock('AnimeDb\SmartSleep\Rule\RuleInterface'), - $this->getMock('AnimeDb\SmartSleep\Rule\RuleInterface'), - $this->getMock('AnimeDb\SmartSleep\Rule\RuleInterface'), + $this->getMock(Rule::class), + $this->getMock(Rule::class), + $this->getMock(Rule::class), ]; $schedule = new Schedule($rules); @@ -53,7 +53,7 @@ public function testAdd() $this->assertTrue($this->schedule->isEmpty()); /* @var $rule1 \PHPUnit_Framework_MockObject_MockObject|Rule */ - $rule1 = $this->getMock('AnimeDb\SmartSleep\Rule\RuleInterface'); + $rule1 = $this->getMock(Rule::class); $this->schedule->add($rule1); $this->assertEquals([$rule1], $this->schedule->toArray()); @@ -61,7 +61,7 @@ public function testAdd() $this->assertFalse($this->schedule->isEmpty()); /* @var $rule2 \PHPUnit_Framework_MockObject_MockObject|Rule */ - $rule2 = $this->getMock('AnimeDb\SmartSleep\Rule\RuleInterface'); + $rule2 = $this->getMock(Rule::class); $this->schedule->add($rule2); $this->assertEquals([$rule1, $rule2], $this->schedule->toArray()); @@ -94,7 +94,7 @@ public function testGetMatchedRule($count_rules, $match_rule_number) for ($i = 1; $i <= $count_rules; ++$i) { /* @var $rule \PHPUnit_Framework_MockObject_MockObject|Rule */ - $rule = $this->getMock('AnimeDb\SmartSleep\Rule\RuleInterface'); + $rule = $this->getMock(Rule::class); if ($match_rule_number && $i > $match_rule_number) { $rule ->expects($this->never()) diff --git a/tests/unit/SmartSleepTest.php b/tests/unit/SmartSleepTest.php index e525446..c61c5e1 100644 --- a/tests/unit/SmartSleepTest.php +++ b/tests/unit/SmartSleepTest.php @@ -17,28 +17,28 @@ class SmartSleepTest extends \PHPUnit_Framework_TestCase /** * @var \PHPUnit_Framework_MockObject_MockObject|Schedule */ - protected $schedule; + private $schedule; /** * @var \PHPUnit_Framework_MockObject_MockObject|Rule */ - protected $rule; + private $rule; /** * @var SmartSleep */ - protected $smart_sleep; + private $smart_sleep; /** * @var \DateTime */ - protected $time; + private $time; protected function setUp() { $this->time = new \DateTime(); - $this->rule = $this->getMock('AnimeDb\SmartSleep\Rule\RuleInterface'); - $this->schedule = $this->getMock('AnimeDb\SmartSleep\Schedule'); + $this->rule = $this->getMock(Rule::class); + $this->schedule = $this->getMock(Schedule::class); $this->smart_sleep = new SmartSleep($this->schedule); } @@ -49,7 +49,8 @@ public function testGetSleepSecondsNoMatches() ->expects($this->once()) ->method('getMatchedRule') ->with($this->time) - ->will($this->returnValue(null)); + ->will($this->returnValue(null)) + ; $this->assertEquals(0, $this->smart_sleep->sleepForSeconds($this->time)); } @@ -60,12 +61,14 @@ public function testGetSleepSecondsBadSeconds() ->expects($this->once()) ->method('getMatchedRule') ->with($this->time) - ->will($this->returnValue($this->rule)); + ->will($this->returnValue($this->rule)) + ; $this->rule ->expects($this->once()) - ->method('getSeconds') - ->will($this->returnValue(-1)); + ->method('seconds') + ->will($this->returnValue(-1)) + ; $this->assertEquals(0, $this->smart_sleep->sleepForSeconds($this->time)); } @@ -76,12 +79,14 @@ public function testGetSleepSeconds() ->expects($this->once()) ->method('getMatchedRule') ->with($this->time) - ->will($this->returnValue($this->rule)); + ->will($this->returnValue($this->rule)) + ; $this->rule ->expects($this->once()) - ->method('getSeconds') - ->will($this->returnValue(10)); + ->method('seconds') + ->will($this->returnValue(10)) + ; $this->assertEquals(10, $this->smart_sleep->sleepForSeconds($this->time)); } From fb92ebdc9360a8d1433e5ddd9e0f51bd62ad96fe Mon Sep 17 00:00:00 2001 From: Peter Gribanov Date: Tue, 25 Jul 2017 12:55:34 +0300 Subject: [PATCH 06/17] rename RandMaxSecondsRuleTrait -> RandSecondsRuleTrait --- src/Rule/EverydayRule.php | 2 +- src/Rule/HolidayRule.php | 2 +- .../{RandMaxSecondsRuleTrait.php => RandSecondsRuleTrait.php} | 2 +- src/Rule/WeekdayRule.php | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) rename src/Rule/{RandMaxSecondsRuleTrait.php => RandSecondsRuleTrait.php} (94%) diff --git a/src/Rule/EverydayRule.php b/src/Rule/EverydayRule.php index 85b13a7..6af2781 100644 --- a/src/Rule/EverydayRule.php +++ b/src/Rule/EverydayRule.php @@ -11,7 +11,7 @@ class EverydayRule implements HourIntervalRule { use HourIntervalRuleTrait; - use RandMaxSecondsRuleTrait; + use RandSecondsRuleTrait; /** * @param int $start diff --git a/src/Rule/HolidayRule.php b/src/Rule/HolidayRule.php index 818eb95..32e1628 100644 --- a/src/Rule/HolidayRule.php +++ b/src/Rule/HolidayRule.php @@ -11,7 +11,7 @@ class HolidayRule implements HourIntervalRule { use HourIntervalRuleTrait; - use RandMaxSecondsRuleTrait; + use RandSecondsRuleTrait; /** * @param int $start diff --git a/src/Rule/RandMaxSecondsRuleTrait.php b/src/Rule/RandSecondsRuleTrait.php similarity index 94% rename from src/Rule/RandMaxSecondsRuleTrait.php rename to src/Rule/RandSecondsRuleTrait.php index d96090b..f84fb93 100644 --- a/src/Rule/RandMaxSecondsRuleTrait.php +++ b/src/Rule/RandSecondsRuleTrait.php @@ -8,7 +8,7 @@ namespace AnimeDb\SmartSleep\Rule; -trait RandMaxSecondsRuleTrait +trait RandSecondsRuleTrait { /** * @var int diff --git a/src/Rule/WeekdayRule.php b/src/Rule/WeekdayRule.php index fd4acde..d039535 100644 --- a/src/Rule/WeekdayRule.php +++ b/src/Rule/WeekdayRule.php @@ -11,7 +11,7 @@ class WeekdayRule implements HourIntervalRule { use HourIntervalRuleTrait; - use RandMaxSecondsRuleTrait; + use RandSecondsRuleTrait; /** * @param int $start From 59c5567f989a7ea6636c002446cb3d020c556573 Mon Sep 17 00:00:00 2001 From: Peter Gribanov Date: Tue, 25 Jul 2017 12:58:14 +0300 Subject: [PATCH 07/17] rename Schedule::getMatchedRule() -> Schedule::matchedRule() --- src/Schedule.php | 6 +----- src/SmartSleep.php | 2 +- tests/unit/ScheduleTest.php | 2 +- tests/unit/SmartSleepTest.php | 6 +++--- 4 files changed, 6 insertions(+), 10 deletions(-) diff --git a/src/Schedule.php b/src/Schedule.php index 9255957..2edcba2 100644 --- a/src/Schedule.php +++ b/src/Schedule.php @@ -29,14 +29,10 @@ public function __construct(array $rules = []) /** * @param Rule $rule - * - * @return true */ public function add(Rule $rule) { $this->rules[] = $rule; - - return true; } /** @@ -76,7 +72,7 @@ public function getIterator() * * @return Rule|null */ - public function getMatchedRule(\DateTime $time) + public function matchedRule(\DateTime $time) { foreach ($this->rules as $rule) { if ($rule->isMatched($time)) { diff --git a/src/SmartSleep.php b/src/SmartSleep.php index 2b6dd9b..e4e29d5 100644 --- a/src/SmartSleep.php +++ b/src/SmartSleep.php @@ -32,7 +32,7 @@ public function __construct(Schedule $schedule) */ public function sleepForSeconds(\DateTime $now) { - $rule = $this->schedule->getMatchedRule($now); + $rule = $this->schedule->matchedRule($now); if ($rule instanceof Rule) { $seconds = $rule->seconds(); diff --git a/tests/unit/ScheduleTest.php b/tests/unit/ScheduleTest.php index dd4837f..4053191 100644 --- a/tests/unit/ScheduleTest.php +++ b/tests/unit/ScheduleTest.php @@ -113,6 +113,6 @@ public function testGetMatchedRule($count_rules, $match_rule_number) $this->schedule->add($rule); } - $this->assertEquals($match_rule, $this->schedule->getMatchedRule($time)); + $this->assertEquals($match_rule, $this->schedule->matchedRule($time)); } } diff --git a/tests/unit/SmartSleepTest.php b/tests/unit/SmartSleepTest.php index c61c5e1..6ee31c2 100644 --- a/tests/unit/SmartSleepTest.php +++ b/tests/unit/SmartSleepTest.php @@ -47,7 +47,7 @@ public function testGetSleepSecondsNoMatches() { $this->schedule ->expects($this->once()) - ->method('getMatchedRule') + ->method('matchedRule') ->with($this->time) ->will($this->returnValue(null)) ; @@ -59,7 +59,7 @@ public function testGetSleepSecondsBadSeconds() { $this->schedule ->expects($this->once()) - ->method('getMatchedRule') + ->method('matchedRule') ->with($this->time) ->will($this->returnValue($this->rule)) ; @@ -77,7 +77,7 @@ public function testGetSleepSeconds() { $this->schedule ->expects($this->once()) - ->method('getMatchedRule') + ->method('matchedRule') ->with($this->time) ->will($this->returnValue($this->rule)) ; From 16b5816adcfa8e72b7c84e3c56dd0f53524673e8 Mon Sep 17 00:00:00 2001 From: Peter Gribanov Date: Tue, 25 Jul 2017 13:14:50 +0300 Subject: [PATCH 08/17] remove RuleCollection and ScheduleBuilder --- src/RuleCollection.php | 52 ---------- src/ScheduleBuilder.php | 77 --------------- tests/functional/build_schedule.php | 38 ------- tests/unit/Rule/EverydayRuleTest.php | 2 +- tests/unit/Rule/HolidayRuleTest.php | 2 +- tests/unit/Rule/WeekdayRuleTest.php | 2 +- tests/unit/RuleCollectionTest.php | 57 ----------- tests/unit/ScheduleBuilderTest.php | 142 --------------------------- tests/unit/TestRule.php | 102 ------------------- 9 files changed, 3 insertions(+), 471 deletions(-) delete mode 100644 src/RuleCollection.php delete mode 100644 src/ScheduleBuilder.php delete mode 100644 tests/functional/build_schedule.php delete mode 100644 tests/unit/RuleCollectionTest.php delete mode 100644 tests/unit/ScheduleBuilderTest.php delete mode 100644 tests/unit/TestRule.php diff --git a/src/RuleCollection.php b/src/RuleCollection.php deleted file mode 100644 index 4742056..0000000 --- a/src/RuleCollection.php +++ /dev/null @@ -1,52 +0,0 @@ - - * @copyright Copyright (c) 2011, Peter Gribanov - */ - -namespace AnimeDb\SmartSleep; - -use AnimeDb\SmartSleep\Rule\Rule; - -class RuleCollection -{ - /** - * @var Rule[] - */ - private $rules = []; - - /** - * @param string $name - * @param Rule $rule - * - * @return RuleCollection - */ - public function set($name, Rule $rule) - { - $this->rules[$name] = clone $rule; - - return $this; - } - - /** - * @param string $name - * - * @return Rule|null - */ - public function get($name) - { - return isset($this->rules[$name]) ? clone $this->rules[$name] : null; - } - - /** - * @param string $name - * - * @return bool - */ - public function has($name) - { - return isset($this->rules[$name]); - } -} diff --git a/src/ScheduleBuilder.php b/src/ScheduleBuilder.php deleted file mode 100644 index e58dd28..0000000 --- a/src/ScheduleBuilder.php +++ /dev/null @@ -1,77 +0,0 @@ - - * @copyright Copyright (c) 2011, Peter Gribanov - */ - -namespace AnimeDb\SmartSleep; - -use AnimeDb\SmartSleep\Rule\Rule; - -class ScheduleBuilder -{ - /** - * @var RuleCollection - */ - private $collection; - - /** - * @param RuleCollection $collection - */ - public function __construct(RuleCollection $collection) - { - $this->collection = $collection; - } - - /** - * @param string $name - * @param int $start - * @param int $end - * @param int $seconds - * - * @return Rule|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); - } - - /** - * Build schedule. - * - * - * [ - * {rule: , start: , end: , seconds: }, - * ... - * ] - * - * - * @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 Rule) { - $result->add($rule); - } - } - - return $result; - } -} diff --git a/tests/functional/build_schedule.php b/tests/functional/build_schedule.php deleted file mode 100644 index 5f062fe..0000000 --- a/tests/functional/build_schedule.php +++ /dev/null @@ -1,38 +0,0 @@ - - * @copyright Copyright (c) 2011, 'Peter Gribanov - */ -use AnimeDb\SmartSleep\Rule\HolidayRule; -use AnimeDb\SmartSleep\Rule\WeekdayRule; -use AnimeDb\SmartSleep\ScheduleBuilder; -use AnimeDb\SmartSleep\SmartSleep; - -require __DIR__.'/../bootstrap.php'; - -echo 'Functional test: Build schedule.'.PHP_EOL; - -$schedule = [ - ['rule' => WeekdayRule::class, 'start' => 0, 'end' => 2, 'seconds' => 600], - ['rule' => WeekdayRule::class, 'start' => 1, 'end' => 7, 'seconds' => 800], - ['rule' => WeekdayRule::class, 'start' => 7, 'end' => 10, 'seconds' => 100], - ['rule' => WeekdayRule::class, 'start' => 10, 'end' => 19, 'seconds' => 160], - ['rule' => WeekdayRule::class, 'start' => 19, 'end' => 22, 'seconds' => 70], - ['rule' => WeekdayRule::class, 'start' => 22, 'end' => 24, 'seconds' => 260], - ['rule' => HolidayRule::class, 'start' => 0, 'end' => 3, 'seconds' => 260], - ['rule' => HolidayRule::class, 'start' => 3, 'end' => 9, 'seconds' => 900], - ['rule' => HolidayRule::class, 'start' => 9, 'end' => 19, 'seconds' => 160], - ['rule' => HolidayRule::class, 'start' => 19, 'end' => 23, 'seconds' => 70], - ['rule' => HolidayRule::class, 'start' => 23, 'end' => 24, 'seconds' => 260], -]; - -$builder = new ScheduleBuilder(); -$schedule = $builder->buildSchedule($schedule); - -$smart = new SmartSleep($schedule); - -$seconds = $smart->sleepForSeconds(new \DateTime()); - -echo sprintf('Sleep %s s.'.PHP_EOL, $seconds); diff --git a/tests/unit/Rule/EverydayRuleTest.php b/tests/unit/Rule/EverydayRuleTest.php index 74ba58d..273adce 100644 --- a/tests/unit/Rule/EverydayRuleTest.php +++ b/tests/unit/Rule/EverydayRuleTest.php @@ -41,7 +41,7 @@ public function testIsMatched(\DateTime $time, $start, $end, $seconds, $match) $this->assertEquals($start, $rule->start()); $this->assertEquals($end, $rule->end()); - $this->assertTrue($rule->seconds() <= $seconds); + $this->assertLessThanOrEqual($seconds, $rule->seconds()); if ($match) { $this->assertTrue($rule->isMatched($time)); diff --git a/tests/unit/Rule/HolidayRuleTest.php b/tests/unit/Rule/HolidayRuleTest.php index 9fba437..c7e9298 100644 --- a/tests/unit/Rule/HolidayRuleTest.php +++ b/tests/unit/Rule/HolidayRuleTest.php @@ -49,7 +49,7 @@ public function testIsMatched(\DateTime $time, $start, $end, $seconds, $match) $this->assertEquals($start, $rule->start()); $this->assertEquals($end, $rule->end()); - $this->assertTrue($rule->seconds() <= $seconds); + $this->assertLessThanOrEqual($seconds, $rule->seconds()); if ($match) { $this->assertTrue($rule->isMatched($time)); diff --git a/tests/unit/Rule/WeekdayRuleTest.php b/tests/unit/Rule/WeekdayRuleTest.php index 086c2a1..a0d2a30 100644 --- a/tests/unit/Rule/WeekdayRuleTest.php +++ b/tests/unit/Rule/WeekdayRuleTest.php @@ -49,7 +49,7 @@ public function testIsMatched(\DateTime $time, $start, $end, $seconds, $match) $this->assertEquals($start, $rule->start()); $this->assertEquals($end, $rule->end()); - $this->assertTrue($rule->seconds() <= $seconds); + $this->assertLessThanOrEqual($seconds, $rule->seconds()); if ($match) { $this->assertTrue($rule->isMatched($time)); diff --git a/tests/unit/RuleCollectionTest.php b/tests/unit/RuleCollectionTest.php deleted file mode 100644 index 11d98ca..0000000 --- a/tests/unit/RuleCollectionTest.php +++ /dev/null @@ -1,57 +0,0 @@ - - * @copyright Copyright (c) 2011, Peter Gribanov - */ - -namespace AnimeDb\SmartSleep\Tests\Unit; - -use AnimeDb\SmartSleep\Rule\Rule; -use AnimeDb\SmartSleep\RuleCollection; - -class RuleCollectionTest extends \PHPUnit_Framework_TestCase -{ - /** - * @var RuleCollection - */ - protected $collection; - - protected function setUp() - { - $this->collection = new RuleCollection(); - } - - public function testGetSet() - { - $this->assertFalse($this->collection->has('foo')); - $this->assertNull($this->collection->get('foo')); - - /* @var $rule \PHPUnit_Framework_MockObject_MockObject|Rule */ - $rule = $this->getMock('AnimeDb\SmartSleep\Rule\RuleInterface'); - - $this->assertEquals($this->collection, $this->collection->set('foo', $rule)); - $this->assertTrue($this->collection->has('foo')); - $this->assertEquals($rule, $this->collection->get('foo')); - } - - public function testSetClone() - { - $rule = new TestRule(); - $this->collection->set('foo', $rule); - - $rule->setSeconds(123); - $this->assertNotEquals($rule, $this->collection->get('foo')); - } - - 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')); - } -} diff --git a/tests/unit/ScheduleBuilderTest.php b/tests/unit/ScheduleBuilderTest.php deleted file mode 100644 index 7787152..0000000 --- a/tests/unit/ScheduleBuilderTest.php +++ /dev/null @@ -1,142 +0,0 @@ - - * @copyright Copyright (c) 2011, Peter Gribanov - */ - -namespace AnimeDb\SmartSleep\Tests\Unit; - -use AnimeDb\SmartSleep\Rule\Rule; -use AnimeDb\SmartSleep\RuleCollection; -use AnimeDb\SmartSleep\ScheduleBuilder; - -class ScheduleBuilderTest extends \PHPUnit_Framework_TestCase -{ - /** - * @var \PHPUnit_Framework_MockObject_MockObject|RuleCollection - */ - protected $collection; - - /** - * @var ScheduleBuilder - */ - protected $builder; - - protected function setUp() - { - $this->collection = $this->getMock('AnimeDb\SmartSleep\RuleCollection'); - - $this->builder = new ScheduleBuilder($this->collection); - } - - public function testBuildRuleFailed() - { - $name = 'foo'; - $this->collection - ->expects($this->once()) - ->method('has') - ->with($name) - ->will($this->returnValue(false)); - - $this->assertNull($this->builder->buildRule($name, 1, 2, 10)); - } - - public function testBuildRule() - { - $name = 'foo'; - $start = 1; - $end = 2; - $seconds = 10; - - /* @var $rule \PHPUnit_Framework_MockObject_MockObject|Rule */ - $rule = $this->getMock('AnimeDb\SmartSleep\Rule\RuleInterface'); - $this->buildRule($rule, $start, $end, $seconds); - - $this->collection - ->expects($this->once()) - ->method('has') - ->with($name) - ->will($this->returnValue(true)); - - $this->collection - ->expects($this->once()) - ->method('get') - ->with($name) - ->will($this->returnValue($rule)); - - $this->assertEquals($rule, $this->builder->buildRule($name, $start, $end, $seconds)); - } - - 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|Rule */ - $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()); - } - - /** - * @param \PHPUnit_Framework_MockObject_MockObject $rule - * @param int $start - * @param int $end - * @param int $seconds - */ - protected function buildRule(\PHPUnit_Framework_MockObject_MockObject $rule, $start, $end, $seconds) - { - $rule - ->expects($this->once()) - ->method('setEnd') - ->with($end) - ->will($this->returnSelf()); - $rule - ->expects($this->once()) - ->method('setStart') - ->with($start) - ->will($this->returnSelf()); - $rule - ->expects($this->once()) - ->method('setSeconds') - ->with($seconds) - ->will($this->returnSelf()); - } -} diff --git a/tests/unit/TestRule.php b/tests/unit/TestRule.php deleted file mode 100644 index d8fdf5f..0000000 --- a/tests/unit/TestRule.php +++ /dev/null @@ -1,102 +0,0 @@ - - * @copyright Copyright (c) 2011, Peter Gribanov - */ - -namespace AnimeDb\SmartSleep\Tests\Unit; - -use AnimeDb\SmartSleep\Rule\Rule; - -/** - * Rule only for tests. - */ -class TestRule implements Rule -{ - /** - * @var int - */ - private $start = -1; - - /** - * @var int - */ - private $end = -1; - - /** - * @var int - */ - private $seconds = 0; - - /** - * @return int - */ - public function start() - { - return $this->start; - } - - /** - * @param int $start - * - * @return TestRule - */ - public function setStart($start) - { - $this->start = $start; - - return $this; - } - - /** - * @return int - */ - public function end() - { - return $this->end; - } - - /** - * @param int $end - * - * @return TestRule - */ - public function setEnd($end) - { - $this->end = $end; - - return $this; - } - - /** - * @return int - */ - public function seconds() - { - return $this->seconds; - } - - /** - * @param int $seconds - * - * @return TestRule - */ - public function setSeconds($seconds) - { - $this->seconds = $seconds; - - return $this; - } - - /** - * @param \DateTime $time - * - * @return bool - */ - public function isMatched(\DateTime $time) - { - return true; - } -} From f4dfb6a4a5c2753f4efb264411f34767987cd208 Mon Sep 17 00:00:00 2001 From: Peter Gribanov Date: Tue, 25 Jul 2017 13:19:31 +0300 Subject: [PATCH 09/17] not test on PHP 5.4 --- .travis.yml | 1 - README.md | 31 ++++++++++++++++++++++++++++++- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index a819f2e..0646da4 100644 --- a/.travis.yml +++ b/.travis.yml @@ -16,7 +16,6 @@ matrix: - php: 7.0 - php: 5.6 - php: 5.5 - - php: 5.4 - php: hhvm sudo: required dist: trusty diff --git a/README.md b/README.md index 76327fc..d2ca681 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,36 @@ composer require anime-db/smart-sleep ## How-to -See [functional tests](https://github.com/anime-db/smart-sleep/tree/master/tests/functional) for more details. +First build schedule + +```php +use AnimeDb\SmartSleep\Rule\EverydayRule; +use AnimeDb\SmartSleep\Schedule; + +$schedule = new Schedule([ + new EverydayRule(0, 3, 260), // [00:00, 03:00) + new EverydayRule(3, 9, 900), // [03:00, 09:00) + new EverydayRule(9, 19, 160), // [09:00, 19:00) + new EverydayRule(19, 23, 70), // [19:00, 23:00) + new EverydayRule(23, 24, 60), // [23:00, 24:00) +]); +``` + +Configure SmartSleep + +```php +use AnimeDb\SmartSleep\SmartSleep; + +$smart = new SmartSleep($schedule); +``` + +And now we can sleep + +```php +$seconds = $smart->sleepForSeconds(new \DateTime()); + +sleep($seconds); +``` ## License From c952b0eeb86dc790d4e37e07dd777b41f7eb2651 Mon Sep 17 00:00:00 2001 From: Peter Gribanov Date: Tue, 25 Jul 2017 13:29:57 +0300 Subject: [PATCH 10/17] correct OnceMonthRule --- src/Rule/OnceMonthRule.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/Rule/OnceMonthRule.php b/src/Rule/OnceMonthRule.php index c9db9fe..9960b7e 100644 --- a/src/Rule/OnceMonthRule.php +++ b/src/Rule/OnceMonthRule.php @@ -41,13 +41,11 @@ public function seconds() $offset_time = clone $this->time; $offset_time->modify('+1 month 00:00:00')->modify('first day of this month'); - $limit_time = clone $this->time; - $limit_time->modify('+2 month 00:00:00')->modify('first day of this month'); + $limit_time = clone $offset_time; + $limit_time->modify('+1 month'); $limit = $limit_time->getTimestamp() - $offset_time->getTimestamp(); // offset to next month - $offset_time = clone $this->time; - $offset_time->modify('+1 month 00:00:00')->modify('first day of this month'); $offset = $offset_time->getTimestamp() - $this->time->getTimestamp(); return $offset + rand(0, $limit); From 6d5723f822696e7d6163955737c0c294f26f87cd Mon Sep 17 00:00:00 2001 From: Peter Gribanov Date: Tue, 25 Jul 2017 13:32:51 +0300 Subject: [PATCH 11/17] add rules docs --- README.md | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 62 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index d2ca681..89b571e 100644 --- a/README.md +++ b/README.md @@ -7,9 +7,11 @@ [![StyleCI](https://styleci.io/repos/61719557/shield?branch=master)](https://styleci.io/repos/61719557) [![License](https://img.shields.io/packagist/l/anime-db/smart-sleep.svg?maxAge=3600)](https://github.com/anime-db/smart-sleep) -# SmartSleep util +SmartSleep +========== -## Installation +Installation +------------ Pretty simple with [Composer](http://packagist.org), run: @@ -17,7 +19,8 @@ Pretty simple with [Composer](http://packagist.org), run: composer require anime-db/smart-sleep ``` -## How-to +How-to +------ First build schedule @@ -50,6 +53,61 @@ $seconds = $smart->sleepForSeconds(new \DateTime()); sleep($seconds); ``` -## License +Rules +----- + +### EverydayRule + +The rule corresponds to any day in the specified time interval. + +```php +$rule = new EverydayRule($start_hour, $end_hour, $max_sleep_seconds) +``` + +### HolidayRule + +The rule corresponds to the holiday at the specified time interval. + +```php +$rule = new HolidayRule($start_hour, $end_hour, $max_sleep_seconds) +``` + +### WeekdayRule + +The rule corresponds to the weekday at the specified time interval. + +```php +$rule = new WeekdayRule($start_hour, $end_hour, $max_sleep_seconds) +``` + +### OnceDayRule + +The rule always corresponds to the specified time. +Returns the seconds in the next day. + +```php +$rule = new OnceDayRule() +``` + +### OnceWeekRule + +The rule always corresponds to the specified time. +Returns the seconds in the next week. + +```php +$rule = new OnceWeekRule() +``` + +### OnceMonthRule + +The rule always corresponds to the specified time. +Returns the seconds in the next month. + +```php +$rule = new OnceMonthRule() +``` + +License +------- This bundle is under the [MIT license](http://opensource.org/licenses/MIT). See the complete license in the file: LICENSE From 3f1a7441725eef69bf8dd7cdb89b7d9b1fc0d551 Mon Sep 17 00:00:00 2001 From: Peter Gribanov Date: Tue, 25 Jul 2017 13:50:19 +0300 Subject: [PATCH 12/17] correct test second intervals --- tests/unit/Rule/EverydayRuleTest.php | 1 + tests/unit/Rule/HolidayRuleTest.php | 1 + tests/unit/Rule/OnceDayRuleTest.php | 8 ++++---- tests/unit/Rule/OnceMonthRuleTest.php | 12 ++++++------ tests/unit/Rule/OnceWeekRuleTest.php | 8 ++++---- tests/unit/Rule/WeekdayRuleTest.php | 1 + 6 files changed, 17 insertions(+), 14 deletions(-) diff --git a/tests/unit/Rule/EverydayRuleTest.php b/tests/unit/Rule/EverydayRuleTest.php index 273adce..7c0a7c1 100644 --- a/tests/unit/Rule/EverydayRuleTest.php +++ b/tests/unit/Rule/EverydayRuleTest.php @@ -41,6 +41,7 @@ public function testIsMatched(\DateTime $time, $start, $end, $seconds, $match) $this->assertEquals($start, $rule->start()); $this->assertEquals($end, $rule->end()); + $this->assertGreaterThanOrEqual(0, $seconds); $this->assertLessThanOrEqual($seconds, $rule->seconds()); if ($match) { diff --git a/tests/unit/Rule/HolidayRuleTest.php b/tests/unit/Rule/HolidayRuleTest.php index c7e9298..6ba403d 100644 --- a/tests/unit/Rule/HolidayRuleTest.php +++ b/tests/unit/Rule/HolidayRuleTest.php @@ -49,6 +49,7 @@ public function testIsMatched(\DateTime $time, $start, $end, $seconds, $match) $this->assertEquals($start, $rule->start()); $this->assertEquals($end, $rule->end()); + $this->assertGreaterThanOrEqual(0, $seconds); $this->assertLessThanOrEqual($seconds, $rule->seconds()); if ($match) { diff --git a/tests/unit/Rule/OnceDayRuleTest.php b/tests/unit/Rule/OnceDayRuleTest.php index 57a833c..6babb2a 100644 --- a/tests/unit/Rule/OnceDayRuleTest.php +++ b/tests/unit/Rule/OnceDayRuleTest.php @@ -29,8 +29,8 @@ public function testGetSecondsFromConstruct() $seconds = $this->rule->seconds(); // -1 seconds because is long wait execute test - $this->assertTrue($seconds >= -1); - $this->assertTrue($seconds < $limit); + $this->assertGreaterThanOrEqual(-1, $seconds); + $this->assertLessThan($limit, $seconds); } public function testGetSecondsFromMatched() @@ -43,7 +43,7 @@ public function testGetSecondsFromMatched() $this->rule->isMatched($time); $seconds = $this->rule->seconds(); - $this->assertTrue($seconds >= 0); - $this->assertTrue($seconds < $limit); + $this->assertGreaterThanOrEqual(0, $seconds); + $this->assertLessThan($limit, $seconds); } } diff --git a/tests/unit/Rule/OnceMonthRuleTest.php b/tests/unit/Rule/OnceMonthRuleTest.php index bcfb476..0149a2f 100644 --- a/tests/unit/Rule/OnceMonthRuleTest.php +++ b/tests/unit/Rule/OnceMonthRuleTest.php @@ -31,8 +31,8 @@ public function testGetSecondsFromConstruct() $seconds = $this->rule->seconds(); // -1 seconds because is long wait execute test - $this->assertTrue($seconds >= -1); - $this->assertTrue($seconds < $limit); + $this->assertGreaterThanOrEqual(-1, $seconds); + $this->assertLessThan($limit, $seconds); } public function testGetSecondsFromMatched() @@ -44,8 +44,8 @@ public function testGetSecondsFromMatched() $this->rule->isMatched($time); $seconds = $this->rule->seconds(); - $this->assertTrue($seconds >= 0); - $this->assertTrue($seconds < $limit); + $this->assertGreaterThanOrEqual(0, $seconds); + $this->assertLessThan($limit, $seconds); } public function testGetSecondsFromMatchedForFebruary() @@ -57,7 +57,7 @@ public function testGetSecondsFromMatchedForFebruary() $this->rule->isMatched($time); $seconds = $this->rule->seconds(); - $this->assertTrue($seconds >= 0); - $this->assertTrue($seconds < $limit); + $this->assertGreaterThanOrEqual(0, $seconds); + $this->assertLessThan($limit, $seconds); } } diff --git a/tests/unit/Rule/OnceWeekRuleTest.php b/tests/unit/Rule/OnceWeekRuleTest.php index 3a7cac5..7cf4428 100644 --- a/tests/unit/Rule/OnceWeekRuleTest.php +++ b/tests/unit/Rule/OnceWeekRuleTest.php @@ -29,8 +29,8 @@ public function testGetSecondsFromConstruct() $seconds = $this->rule->seconds(); // -1 seconds because is long wait execute test - $this->assertTrue($seconds >= -1); - $this->assertTrue($seconds < $limit); + $this->assertGreaterThanOrEqual(-1, $seconds); + $this->assertLessThan($limit, $seconds); } public function testGetSecondsFromMatched() @@ -42,7 +42,7 @@ public function testGetSecondsFromMatched() $this->rule->isMatched($time); $seconds = $this->rule->seconds(); - $this->assertTrue($seconds >= 0); - $this->assertTrue($seconds < $limit); + $this->assertGreaterThanOrEqual(0, $seconds); + $this->assertLessThan($limit, $seconds); } } diff --git a/tests/unit/Rule/WeekdayRuleTest.php b/tests/unit/Rule/WeekdayRuleTest.php index a0d2a30..f5806bc 100644 --- a/tests/unit/Rule/WeekdayRuleTest.php +++ b/tests/unit/Rule/WeekdayRuleTest.php @@ -49,6 +49,7 @@ public function testIsMatched(\DateTime $time, $start, $end, $seconds, $match) $this->assertEquals($start, $rule->start()); $this->assertEquals($end, $rule->end()); + $this->assertGreaterThanOrEqual(0, $seconds); $this->assertLessThanOrEqual($seconds, $rule->seconds()); if ($match) { From 9537a2763aa48c94daa70ca3fc76a0a526572f52 Mon Sep 17 00:00:00 2001 From: Peter Gribanov Date: Tue, 25 Jul 2017 13:50:56 +0300 Subject: [PATCH 13/17] correct test method names --- tests/unit/Rule/OnceDayRuleTest.php | 4 ++-- tests/unit/Rule/OnceMonthRuleTest.php | 6 +++--- tests/unit/Rule/OnceWeekRuleTest.php | 4 ++-- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/tests/unit/Rule/OnceDayRuleTest.php b/tests/unit/Rule/OnceDayRuleTest.php index 6babb2a..d0308cc 100644 --- a/tests/unit/Rule/OnceDayRuleTest.php +++ b/tests/unit/Rule/OnceDayRuleTest.php @@ -22,7 +22,7 @@ protected function setUp() $this->rule = new OnceDayRule(); } - public function testGetSecondsFromConstruct() + public function testSecondsFromConstruct() { $limit = strtotime('+2 day 00:00:00') - time(); @@ -33,7 +33,7 @@ public function testGetSecondsFromConstruct() $this->assertLessThan($limit, $seconds); } - public function testGetSecondsFromMatched() + public function testSecondsFromMatched() { $time = new \DateTime('23-06-2016 13:42:15'); diff --git a/tests/unit/Rule/OnceMonthRuleTest.php b/tests/unit/Rule/OnceMonthRuleTest.php index 0149a2f..0cff1fc 100644 --- a/tests/unit/Rule/OnceMonthRuleTest.php +++ b/tests/unit/Rule/OnceMonthRuleTest.php @@ -22,7 +22,7 @@ protected function setUp() $this->rule = new OnceMonthRule(); } - public function testGetSecondsFromConstruct() + public function testSecondsFromConstruct() { $limit_time = new \DateTime('+2 month 00:00:00'); $limit_time->modify('first day of this month'); @@ -35,7 +35,7 @@ public function testGetSecondsFromConstruct() $this->assertLessThan($limit, $seconds); } - public function testGetSecondsFromMatched() + public function testSecondsFromMatched() { $time = new \DateTime('23-06-2016 13:42:15'); $limit_time = new \DateTime('01-08-2016 13:42:15'); @@ -48,7 +48,7 @@ public function testGetSecondsFromMatched() $this->assertLessThan($limit, $seconds); } - public function testGetSecondsFromMatchedForFebruary() + public function testSecondsFromMatchedForFebruary() { $time = new \DateTime('31-01-2016 13:42:15'); $limit_time = new \DateTime('01-03-2016 13:42:15'); diff --git a/tests/unit/Rule/OnceWeekRuleTest.php b/tests/unit/Rule/OnceWeekRuleTest.php index 7cf4428..9477bd1 100644 --- a/tests/unit/Rule/OnceWeekRuleTest.php +++ b/tests/unit/Rule/OnceWeekRuleTest.php @@ -22,7 +22,7 @@ protected function setUp() $this->rule = new OnceWeekRule(); } - public function testGetSecondsFromConstruct() + public function testSecondsFromConstruct() { $limit = strtotime('+2 week 00:00:00') - time(); @@ -33,7 +33,7 @@ public function testGetSecondsFromConstruct() $this->assertLessThan($limit, $seconds); } - public function testGetSecondsFromMatched() + public function testSecondsFromMatched() { $time = new \DateTime('23-06-2016 13:42:15'); $limit_time = new \DateTime('07-07-2016 00:00:00'); From 62434ab2f2322c2d376f5fe1f6eae3fefc6b595e Mon Sep 17 00:00:00 2001 From: Peter Gribanov Date: Tue, 25 Jul 2017 13:54:16 +0300 Subject: [PATCH 14/17] correct get offset time in OnceMonthRule --- src/Rule/OnceDayRule.php | 2 +- src/Rule/OnceMonthRule.php | 2 +- src/Rule/OnceWeekRule.php | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Rule/OnceDayRule.php b/src/Rule/OnceDayRule.php index 83c8c08..de022af 100644 --- a/src/Rule/OnceDayRule.php +++ b/src/Rule/OnceDayRule.php @@ -38,7 +38,7 @@ public function isMatched(\DateTime $time) public function seconds() { $offset_time = clone $this->time; - $offset_time->modify('+1 day 00:00:00'); + $offset_time->modify('+1 day')->setTime(0, 0, 0); $offset = $offset_time->getTimestamp() - $this->time->getTimestamp(); // offset to next day return $offset + rand(0, 86400); // 86400 is a 1 day diff --git a/src/Rule/OnceMonthRule.php b/src/Rule/OnceMonthRule.php index 9960b7e..9ae4a45 100644 --- a/src/Rule/OnceMonthRule.php +++ b/src/Rule/OnceMonthRule.php @@ -39,7 +39,7 @@ public function seconds() { // interval duration [next month, next month +1) $offset_time = clone $this->time; - $offset_time->modify('+1 month 00:00:00')->modify('first day of this month'); + $offset_time->modify('first day of this month')->modify('+1 month')->setTime(0, 0, 0); $limit_time = clone $offset_time; $limit_time->modify('+1 month'); diff --git a/src/Rule/OnceWeekRule.php b/src/Rule/OnceWeekRule.php index ca44e00..6a72658 100644 --- a/src/Rule/OnceWeekRule.php +++ b/src/Rule/OnceWeekRule.php @@ -38,7 +38,7 @@ public function isMatched(\DateTime $time) public function seconds() { $offset_time = clone $this->time; - $offset_time->modify('+1 week 00:00:00'); + $offset_time->modify('+1 week')->setTime(0, 0, 0); $offset = $offset_time->getTimestamp() - $this->time->getTimestamp(); // offset to next week return $offset + rand(0, 604800); // 604800 is a 1 week From 7a92dadf2d2ce1cc98ff1d3fb1443d42c5071452 Mon Sep 17 00:00:00 2001 From: Peter Gribanov Date: Tue, 25 Jul 2017 13:55:36 +0300 Subject: [PATCH 15/17] rename trait HourIntervalRuleTrait -> HourlyIntervalRuleTrait --- src/Rule/EverydayRule.php | 2 +- src/Rule/HolidayRule.php | 2 +- .../{HourIntervalRuleTrait.php => HourlyIntervalRuleTrait.php} | 2 +- src/Rule/WeekdayRule.php | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) rename src/Rule/{HourIntervalRuleTrait.php => HourlyIntervalRuleTrait.php} (96%) diff --git a/src/Rule/EverydayRule.php b/src/Rule/EverydayRule.php index 6af2781..4bef225 100644 --- a/src/Rule/EverydayRule.php +++ b/src/Rule/EverydayRule.php @@ -10,7 +10,7 @@ class EverydayRule implements HourIntervalRule { - use HourIntervalRuleTrait; + use HourlyIntervalRuleTrait; use RandSecondsRuleTrait; /** diff --git a/src/Rule/HolidayRule.php b/src/Rule/HolidayRule.php index 32e1628..61f411d 100644 --- a/src/Rule/HolidayRule.php +++ b/src/Rule/HolidayRule.php @@ -10,7 +10,7 @@ class HolidayRule implements HourIntervalRule { - use HourIntervalRuleTrait; + use HourlyIntervalRuleTrait; use RandSecondsRuleTrait; /** diff --git a/src/Rule/HourIntervalRuleTrait.php b/src/Rule/HourlyIntervalRuleTrait.php similarity index 96% rename from src/Rule/HourIntervalRuleTrait.php rename to src/Rule/HourlyIntervalRuleTrait.php index 2f169c1..a27edd3 100644 --- a/src/Rule/HourIntervalRuleTrait.php +++ b/src/Rule/HourlyIntervalRuleTrait.php @@ -8,7 +8,7 @@ namespace AnimeDb\SmartSleep\Rule; -trait HourIntervalRuleTrait +trait HourlyIntervalRuleTrait { /** * @var int diff --git a/src/Rule/WeekdayRule.php b/src/Rule/WeekdayRule.php index d039535..b2e3906 100644 --- a/src/Rule/WeekdayRule.php +++ b/src/Rule/WeekdayRule.php @@ -10,7 +10,7 @@ class WeekdayRule implements HourIntervalRule { - use HourIntervalRuleTrait; + use HourlyIntervalRuleTrait; use RandSecondsRuleTrait; /** From cbb5ade80f1fea3e641e4bce849ae48f1711cb74 Mon Sep 17 00:00:00 2001 From: Peter Gribanov Date: Tue, 25 Jul 2017 14:09:10 +0300 Subject: [PATCH 16/17] add SpecificDayRule --- README.md | 9 ++++ src/Rule/SpecificDayRule.php | 48 ++++++++++++++++++++ tests/unit/Rule/SpecificDayRuleTest.php | 58 +++++++++++++++++++++++++ tests/unit/ScheduleTest.php | 4 +- 4 files changed, 117 insertions(+), 2 deletions(-) create mode 100644 src/Rule/SpecificDayRule.php create mode 100644 tests/unit/Rule/SpecificDayRuleTest.php diff --git a/README.md b/README.md index 89b571e..a51f947 100644 --- a/README.md +++ b/README.md @@ -56,6 +56,15 @@ sleep($seconds); Rules ----- +### SpecificDayRule + +The rule corresponds to specific day in the specified time interval. +Can be used for public holidays. + +```php +$rule = new SpecificDayRule(new \DateTime('2017-01-01'), $start_hour, $end_hour, $max_sleep_seconds) +``` + ### EverydayRule The rule corresponds to any day in the specified time interval. diff --git a/src/Rule/SpecificDayRule.php b/src/Rule/SpecificDayRule.php new file mode 100644 index 0000000..09c2364 --- /dev/null +++ b/src/Rule/SpecificDayRule.php @@ -0,0 +1,48 @@ + + * @copyright Copyright (c) 2011, Peter Gribanov + */ + +namespace AnimeDb\SmartSleep\Rule; + +class SpecificDayRule implements HourIntervalRule +{ + use HourlyIntervalRuleTrait; + use RandSecondsRuleTrait; + + /** + * @var \DateTime + */ + private $day; + + /** + * @param \DateTime $day + * @param int $start + * @param int $end + * @param int $seconds + */ + public function __construct(\DateTime $day, $start, $end, $seconds) + { + $this->day = clone $day; + $this->setStart($start); + $this->setEnd($end); + $this->setSeconds($seconds); + } + + /** + * @param \DateTime $time + * + * @return bool + */ + public function isMatched(\DateTime $time) + { + return + $this->day->format('Ymd') == $time->format('Ymd') && + $this->start() <= $time->format('G') && + $this->end() > $time->format('G') + ; + } +} diff --git a/tests/unit/Rule/SpecificDayRuleTest.php b/tests/unit/Rule/SpecificDayRuleTest.php new file mode 100644 index 0000000..24fd1fa --- /dev/null +++ b/tests/unit/Rule/SpecificDayRuleTest.php @@ -0,0 +1,58 @@ + + * @copyright Copyright (c) 2011, Peter Gribanov + */ + +namespace AnimeDb\SmartSleep\Tests\Unit\Rule; + +use AnimeDb\SmartSleep\Rule\SpecificDayRule; + +class SpecificDayRuleTest extends \PHPUnit_Framework_TestCase +{ + /** + * @return array + */ + public function getMatches() + { + return [ + // in current day + [new \DateTime('2:59'), new \DateTime(), 3, 5, 10, false], + [new \DateTime('3:00'), new \DateTime(), 3, 5, 50, true], + [new \DateTime('4:00'), new \DateTime(), 3, 5, 100, true], + [new \DateTime('4:59'), new \DateTime(), 3, 5, 200, true], + [new \DateTime('5:00'), new \DateTime(), 3, 5, 550, false], + // in New Year + [new \DateTime('2016-12-31 4:00'), new \DateTime('2017-01-01'), 0, 10, 500, false], + [new \DateTime('2017-01-02 4:00'), new \DateTime('2017-01-01'), 0, 10, 500, false], + ]; + } + + /** + * @dataProvider getMatches + * + * @param \DateTime $time + * @param \DateTime $day + * @param int $start + * @param int $end + * @param int $seconds + * @param bool $match + */ + public function testIsMatched(\DateTime $time, \DateTime $day, $start, $end, $seconds, $match) + { + $rule = new SpecificDayRule($day, $start, $end, $seconds); + + $this->assertEquals($start, $rule->start()); + $this->assertEquals($end, $rule->end()); + $this->assertGreaterThanOrEqual(0, $seconds); + $this->assertLessThanOrEqual($seconds, $rule->seconds()); + + if ($match) { + $this->assertTrue($rule->isMatched($time)); + } else { + $this->assertFalse($rule->isMatched($time)); + } + } +} diff --git a/tests/unit/ScheduleTest.php b/tests/unit/ScheduleTest.php index 4053191..debf4aa 100644 --- a/tests/unit/ScheduleTest.php +++ b/tests/unit/ScheduleTest.php @@ -25,8 +25,8 @@ protected function setUp() public function testInstanceOf() { - $this->assertInstanceOf('IteratorAggregate', $this->schedule); - $this->assertInstanceOf('Countable', $this->schedule); + $this->assertInstanceOf(\IteratorAggregate::class, $this->schedule); + $this->assertInstanceOf(\Countable::class, $this->schedule); } public function testConstruct() From ca3ea71b4476fd36022d7f08474852833c353f29 Mon Sep 17 00:00:00 2001 From: Peter Gribanov Date: Tue, 25 Jul 2017 14:12:04 +0300 Subject: [PATCH 17/17] remove RuleTrait --- src/Rule/RuleTrait.php | 33 --------------------------------- 1 file changed, 33 deletions(-) delete mode 100644 src/Rule/RuleTrait.php diff --git a/src/Rule/RuleTrait.php b/src/Rule/RuleTrait.php deleted file mode 100644 index 904c29b..0000000 --- a/src/Rule/RuleTrait.php +++ /dev/null @@ -1,33 +0,0 @@ - - * @copyright Copyright (c) 2011, Peter Gribanov - */ - -namespace AnimeDb\SmartSleep\Rule; - -trait RuleTrait -{ - /** - * @var int - */ - private $seconds = 0; - - /** - * @param int $seconds - */ - protected function setSeconds($seconds) - { - $this->seconds = $seconds; - } - - /** - * @return int - */ - public function seconds() - { - return $this->seconds; - } -}