|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace AbterPhp\Admin\Helper; |
|
6
|
|
|
|
|
7
|
|
|
use PHPUnit\Framework\TestCase; |
|
8
|
|
|
|
|
9
|
|
|
class DateHelperTest extends TestCase |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @return array |
|
13
|
|
|
*/ |
|
14
|
|
|
public function formatProvider(): array |
|
15
|
|
|
{ |
|
16
|
|
|
return [ |
|
17
|
|
|
[new \DateTime('2010-11-27 09:08:59'), 'Y-m-d', '2010-11-27'], |
|
18
|
|
|
[new \DateTime('2010-11-27 09:08:59'), 'd.m.Y', '27.11.2010'], |
|
19
|
|
|
[null, 'Y-m-d', ''], |
|
20
|
|
|
]; |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @dataProvider formatProvider |
|
25
|
|
|
* |
|
26
|
|
|
* @param \DateTime|null $dateTime |
|
27
|
|
|
* @param string $dateFormat |
|
28
|
|
|
* @param string $expectedResult |
|
29
|
|
|
*/ |
|
30
|
|
|
public function testFormat(?\DateTime $dateTime, string $dateFormat, string $expectedResult) |
|
31
|
|
|
{ |
|
32
|
|
|
putenv(sprintf('ADMIN_DATE_FORMAT=%s', $dateFormat)); |
|
33
|
|
|
|
|
34
|
|
|
$actualResult = DateHelper::format($dateTime); |
|
35
|
|
|
|
|
36
|
|
|
$this->assertSame($expectedResult, $actualResult); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @return array |
|
41
|
|
|
*/ |
|
42
|
|
|
public function formatDateTimeProvider(): array |
|
43
|
|
|
{ |
|
44
|
|
|
return [ |
|
45
|
|
|
[new \DateTime('2010-11-27 09:08:59'), 'Y-m-d H:i:s', '2010-11-27 09:08:59'], |
|
46
|
|
|
[new \DateTime('2010-11-27 09:08:59'), 'd.m.Y H:i:s', '27.11.2010 09:08:59'], |
|
47
|
|
|
[null, 'Y-m-d H:i:s', ''], |
|
48
|
|
|
]; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @dataProvider formatDateTimeProvider |
|
53
|
|
|
* |
|
54
|
|
|
* @param \DateTime|null $dateTime |
|
55
|
|
|
* @param string $dateFormat |
|
56
|
|
|
* @param string $expectedResult |
|
57
|
|
|
*/ |
|
58
|
|
|
public function testFormatDateTime(?\DateTime $dateTime, string $dateFormat, string $expectedResult) |
|
59
|
|
|
{ |
|
60
|
|
|
putenv(sprintf('ADMIN_DATETIME_FORMAT=%s', $dateFormat)); |
|
61
|
|
|
|
|
62
|
|
|
$actualResult = DateHelper::formatDateTime($dateTime); |
|
63
|
|
|
|
|
64
|
|
|
$this->assertSame($expectedResult, $actualResult); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|