Files
phalapi-pro/tests/base/Common/TimeUtil_Test.php
2022-03-21 11:16:38 +08:00

119 lines
3.0 KiB
PHP

<?php
/**
* PhalApi_App\Common\TimeUtil_Test
*
* 针对 ../src/app/Common/TimeUtil.php App\Common\TimeUtil 类的PHPUnit单元测试
*
* @author: dogstar 20191231
*/
namespace tests\Base\Common;
use Base\Common\TimeUtil;
class PhpUnderControl_BaseCommonTimeUtil_Test extends \PHPUnit\Framework\TestCase
{
public $appCommonTimeUtil;
protected function setUp()
{
parent::setUp();
$this->appCommonTimeUtil = new \Base\Common\TimeUtil();
}
protected function tearDown()
{
// 输出本次单元测试所执行的SQL语句
// var_dump(\PhalApi\DI()->tracer->getSqls());
// 输出本次单元测试所涉及的追踪埋点
// var_dump(\PhalApi\DI()->tracer->getStack());
}
/**
* @group testCreateDefaultTimeLine
*/
public function testCreateDefaultTimeLine()
{
$start = $end = date("Y-m-d");
// day
$rs = $this->appCommonTimeUtil::createDefaultTimeLine($start, $end, 'day');
$expect = array(
$start => array(
'time' => $start,
'total' => 0,
)
);
$this->assertEquals($rs, $expect);
// month
$month = date("Y-m-00");
$rs2 = $this->appCommonTimeUtil::createDefaultTimeLine($start, $end, 'month');
$expect2 = array(
$month => array(
'time' => $month,
'total' => 0,
)
);
$this->assertEquals($rs2, $expect2);
// hour
$rs3 = $this->appCommonTimeUtil::createDefaultTimeLine($start, $end, 'hour');
$expect3 = array();
for ($i = 0; $i <= 23; $i++){
if($i < 10){
$i = '0'.$i;
}
$time = $start.' '.$i.':00:00';
$expect3[$time] = array(
'time' => $time,
'total' => 0,
);
}
$this->assertEquals($rs3, $expect3);
// minutes
$rs4 = $this->appCommonTimeUtil::createDefaultTimeLine($start, $end, 'minutes');
$expect4 = array();
for ($i = 0; $i <= 23; $i++){
if($i < 10){
$i = '0'.$i;
}
for ($j = 0; $j <= 59; $j++){
if($j < 10){
$j = '0'.$j;
}
$time = $start.' '.$i.':'.$j.':00';
$expect4[$time] = array(
'time' => $time,
'total' => 0,
);
}
}
$this->assertEquals($rs4, $expect4);
}
/**
* @group testFormatStartTime
*/
public function testFormatStartTime()
{
$date1 = date("Y-m-d");
$format1 = '00:00:00';
$expect1 = date("Y-m-d 00:00:00");
$rs1 = $this->appCommonTimeUtil::formatStartTime($date1, $format1);
$this->assertEquals($rs1, $expect1);
$format2 = 'H-i';
$expect2 = date("Y-m-d 00-00");
$rs2 = $this->appCommonTimeUtil::formatStartTime($date1, $format2);
$this->assertEquals($rs2, $expect2);
}
}