* Refactor holidayModel::getDaysBetween, and modify its unit test.
This commit is contained in:
@@ -236,21 +236,22 @@ class holidayModel extends model
|
||||
}
|
||||
|
||||
/**
|
||||
* Get diff days.
|
||||
* 获取开始和结束日期间的日期。
|
||||
* Get the dates between the begin and end.
|
||||
*
|
||||
* @param string $begin
|
||||
* @param string $end
|
||||
* @access public
|
||||
* @return bool
|
||||
* @return array
|
||||
*/
|
||||
public function getDaysBetween($begin, $end)
|
||||
public function getDaysBetween(string $begin, string $end): array
|
||||
{
|
||||
$beginTime = strtotime($begin);
|
||||
$endTime = strtotime($end);
|
||||
$days = ($endTime - $beginTime) / 86400;
|
||||
|
||||
$dateList = array();
|
||||
for($i = 0; $i <= $days; $i ++) $dateList[] = date('Y-m-d', strtotime("+$i days", $beginTime));
|
||||
for($i = 0; $i <= $days; $i ++) $dateList[] = date('Y-m-d', strtotime("+{$i} days", $beginTime));
|
||||
|
||||
return $dateList;
|
||||
}
|
||||
|
||||
@@ -197,14 +197,15 @@ class holidayTest
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getDaysBetweenTest method.
|
||||
* 测试获取开始和结束日期之间的日期。
|
||||
* Test get the dates between the begin and end.
|
||||
*
|
||||
* @param string $begin
|
||||
* @param string $end
|
||||
* @access public
|
||||
* @return int
|
||||
* @return int|array
|
||||
*/
|
||||
public function getDaysBetweenTest($begin, $end)
|
||||
public function getDaysBetweenTest(string $begin, string $end): int|array
|
||||
{
|
||||
$objects = $this->objectModel->getDaysBetween($begin, $end);
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#!/usr/bin/env php
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
include dirname(__FILE__, 5) . '/test/lib/init.php';
|
||||
include dirname(__FILE__, 2) . '/holiday.class.php';
|
||||
su('admin');
|
||||
@@ -24,4 +25,4 @@ r($holiday->getDaysBetweenTest('2022-04-01', '2022-04-21')) && p() && e('21');
|
||||
r($holiday->getDaysBetweenTest('2022-05-23', '2022-05-30')) && p() && e('8'); //返回2022-05-23到2022-05-30之间的天数
|
||||
r($holiday->getDaysBetweenTest('2021-01-01', '2022-01-01')) && p() && e('366'); //测试传入一间隔一整年的两天
|
||||
r($holiday->getDaysBetweenTest('2022-05-07', '2022-05-08')) && p() && e('2'); //返回2022-05-07到2022-05-08之间的天数
|
||||
r($holiday->getDaysBetweenTest('2022-05-07', '2022-05-07')) && p() && e('1'); //测试传入同一天的情况
|
||||
r($holiday->getDaysBetweenTest('2022-05-07', '2022-05-07')) && p() && e('1'); //测试传入同一天的情况
|
||||
|
||||
Reference in New Issue
Block a user