Skip to content
This repository was archived by the owner on Oct 26, 2024. It is now read-only.

Commit 5ecd765

Browse files
committed
added more fixes for timer: need to finish two more tests
1 parent 7195be5 commit 5ecd765

File tree

4 files changed

+76
-28
lines changed

4 files changed

+76
-28
lines changed

src/Asm/Config/ConfigTimer.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ public function setConfig($file)
5050
foreach ($params as $paramKey => $paramVal) {
5151
switch ($paramKey) {
5252
case 'interval':
53-
5453
// check the contents of interval
5554
foreach ($paramVal as $intervalKey => $interval) {
5655

src/Asm/Test/TestData.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,27 +64,32 @@ public static function getYamlTimerConfigFile()
6464
6565
example_timer_config_4:
6666
day: [ monday ] # works only mondays
67-
time: [ "01:05:00", "17:00:00" ] # from 01:05:00 to 17:00:00
67+
time:
68+
- [ "01:05:00", "17:00:00" ] # from 01:05:00 to 17:00:00
6869
6970
example_timer_config_5:
70-
time: [ "01:05:00", "17:00:00" ] # from 01:05:00 to 17:00:00
71+
time:
72+
- [ "01:05:00", "17:00:00" ] # from 01:05:00 to 17:00:00
7173
7274
example_timer_config_6:
7375
holiday:
74-
use_gerneral: true # if false, uses holidays conf for shops
76+
use_gerneral: true # if false, uses separate holidays conf
7577
additional: [ sub, 1 ] # add or subract n days to holiday to create range
76-
interval: [ "16:00:00", "16:00:00" ] # start and end time
78+
interval:
79+
- [ "16:00:00", "16:00:00" ] # start and end time
7780
7881
general_shipping_promise:
7982
holiday:
8083
use_general: true
8184
additional: [ sub, 1 ] # add or subract n days to holiday to create range
82-
interval: [ "16:00:00", "16:00:00" ] # start and end time
85+
interval:
86+
- [ "16:00:00", "16:00:00" ] # start and end time
8387
day: [ monday, tuesday, wednesday, thursday, friday, sunday ]
8488
8589
shipping_promise_sunday:
8690
day: [ sunday ] # works only sundays
87-
time: [ "00:00:01", "16:00:00" ] # from 00:00:01 to 16:00:00
91+
time:
92+
- [ "00:00:01", "16:00:00" ] # from 00:00:01 to 16:00:00
8893
8994
9095

src/Asm/Tests/Timer/TimerTest.php

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,43 @@ public function testConstruct()
3636
);
3737

3838
$this->assertInstanceOf('Asm\Config\ConfigTimer', $config);
39-
4039
$timer = new Timer($config);
41-
4240
$this->assertInstanceOf('Asm\Timer\Timer', $timer);
4341

4442
return $timer;
4543
}
44+
45+
/**
46+
* @depends testConstruct
47+
* @covers \Asm\Timer\Timer::isTimerActive
48+
* @covers \Asm\Timer\Timer::checkDate
49+
* @covers \Asm\Timer\Timer::checkIntervals
50+
* @covers \Asm\Timer\Timer::checkDays
51+
* @covers \Asm\Timer\Timer::checkTime
52+
* @covers \Asm\Timer\Timer::checkHoliday
53+
* @param Timer $timer
54+
*/
55+
public function testIsTimerActive(Timer $timer)
56+
{
57+
$this->assertTrue(is_bool($timer->isTimerActive('example_timer_config_1')));
58+
$this->assertTrue(is_bool($timer->isTimerActive('example_timer_config_2')));
59+
$this->assertTrue(is_bool($timer->isTimerActive('example_timer_config_3')));
60+
$this->assertTrue(is_bool($timer->isTimerActive('example_timer_config_4')));
61+
$this->assertTrue(is_bool($timer->isTimerActive('example_timer_config_5')));
62+
//$this->assertTrue(is_bool($timer->isTimerActive('example_timer_config_6')));
63+
//$this->assertTrue(is_bool($timer->isTimerActive('general_shipping_promise')));
64+
}
65+
66+
/**
67+
* @depends testConstruct
68+
* @covers \Asm\Timer\Timer::isHoliday
69+
* @covers \Asm\Timer\Timer::checkHoliday
70+
* @param Timer $timer
71+
*/
72+
public function testIsHoliday(Timer $timer)
73+
{
74+
$this->assertFalse($timer->isHoliday('2014-03-03 00:00:00'));
75+
$this->assertTrue($timer->isHoliday('2014-12-24 12:30:01'));
76+
$this->assertTrue(is_bool($timer->isHoliday()));
77+
}
4678
}

src/Asm/Timer/Timer.php

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@ public function isTimerActive($type)
6464
// pre-check holidays
6565
if (isset($this->currentConf['holiday'])) {
6666
// check if general holidays are to be used
67-
if (true === $this->currentConf['holiday']['use_general']) {
67+
if (isset($this->currentConf['holiday']['use_general'])
68+
&& true === (bool)$this->currentConf['holiday']['use_general']
69+
) {
6870
if (false == $this->isHoliday()) {
6971
$return = $this->checkDate();
7072
}
@@ -84,7 +86,7 @@ public function isTimerActive($type)
8486
/**
8587
* calculate difference between holiday DateTime objects and current or given time
8688
*
87-
* @param mixed $date
89+
* @param string|null $date
8890
* @return bool|\DateTime
8991
*/
9092
public function isHoliday($date = null)
@@ -95,6 +97,7 @@ public function isHoliday($date = null)
9597
/**
9698
* returns holiday object, if set
9799
*
100+
* @codeCoverageIgnore
98101
* @return \DateTime|null
99102
*/
100103
public function getHoliday()
@@ -137,7 +140,7 @@ private function checkDate()
137140
/**
138141
* check for holidays
139142
*
140-
* @param \DateTime|null $date
143+
* @param string|null $date
141144
* @return bool
142145
*/
143146
private function checkHoliday($date = null)
@@ -220,6 +223,10 @@ private function checkIntervals(array $intervals = array())
220223
}
221224

222225
foreach ($intervals as $interval) {
226+
// this means: just some time
227+
if (!is_a($interval[0], 'DateTime') || !is_a($interval[1], 'DateTime')) {
228+
$interval = $this->checkTime($intervals);
229+
}
223230

224231
$intervalStart = $today->diff($interval[0]);
225232
$intervalEnd = $today->diff($interval[1]);
@@ -255,31 +262,36 @@ private function checkDays()
255262
/**
256263
* do time comparison
257264
*
265+
* @param array $intervals
258266
* @return bool
259267
*/
260-
private function checkTime()
268+
private function checkTime($intervals = array())
261269
{
262-
$intervals = array();
270+
$return = array();
271+
if (empty($intervals)) {
272+
$intervals = $this->currentConf['time'];
273+
}
263274

264-
foreach ($this->currentConf['time'] as $intKey => $arrTime) {
275+
foreach ($intervals as $intKey => $intervalParts) {
265276
// build objects for comparison
266-
$objStartTime = new \DateTime();
267-
$startTime = explode(':', $arrTime[0]);
268-
$intervals[$intKey][0] = $objStartTime->setTime(
269-
$startTime[0],
270-
$startTime[1],
271-
$startTime[2]
277+
$startTime = new \DateTime();
278+
$startTimeParts = explode(':', $intervalParts[0]);
279+
280+
$return[$intKey][0] = $startTime->setTime(
281+
$startTimeParts[0],
282+
$startTimeParts[1],
283+
$startTimeParts[2]
272284
);
273285

274-
$objEndTime = new \DateTime();
275-
$endTime = explode(':', $arrTime[1]);
276-
$intervals[$intKey][1] = $objEndTime->setTime(
277-
$endTime[0],
278-
$endTime[1],
279-
$endTime[2]
286+
$endTime = new \DateTime();
287+
$endTimeParts = explode(':', $intervalParts[1]);
288+
$return[$intKey][1] = $endTime->setTime(
289+
$endTimeParts[0],
290+
$endTimeParts[1],
291+
$endTimeParts[2]
280292
);
281293
}
282294

283-
return $this->checkIntervals($intervals);
295+
return $this->checkIntervals($return);
284296
}
285297
}

0 commit comments

Comments
 (0)