1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace ArpTest\LaminasDateTime\View\Helper; |
6
|
|
|
|
7
|
|
|
use Arp\LaminasDateTime\View\Helper\DateTimeHelper; |
8
|
|
|
use Laminas\View\Helper\HelperInterface; |
9
|
|
|
use PHPUnit\Framework\TestCase; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @covers \Arp\LaminasDateTime\View\Helper\DateTimeHelper |
13
|
|
|
*/ |
14
|
|
|
final class DateTimeHelperTest extends TestCase |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Assert that the view helper implements HelperInterface. |
18
|
|
|
*/ |
19
|
|
|
public function testImplementsHelperInterface(): void |
20
|
|
|
{ |
21
|
|
|
$helper = new DateTimeHelper(\DateTimeInterface::ATOM); |
22
|
|
|
|
23
|
|
|
$this->assertInstanceOf(HelperInterface::class, $helper); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Assert that when we pass NULL to DateTimeHelper::__invoke() that an empty string will be returned |
28
|
|
|
*/ |
29
|
|
|
public function testInvokeWillReturnEmptyStringForNullDateTime(): void |
30
|
|
|
{ |
31
|
|
|
$helper = new DateTimeHelper(\DateTimeInterface::ATOM); |
32
|
|
|
|
33
|
|
|
$this->assertSame('', $helper(null)); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Assert that when calling __invoke() without a format option, the returned date time string will be formatted |
38
|
|
|
* to the value provided when creating the class |
39
|
|
|
* |
40
|
|
|
* @dataProvider getInvokeWillFormatDateTimeAccordingToConstructorArgumentFormatData |
41
|
|
|
*/ |
42
|
|
|
public function testInvokeWillFormatDateTimeAccordingToConstructorArgumentFormat(string $format): void |
43
|
|
|
{ |
44
|
|
|
$helper = new DateTimeHelper($format); |
45
|
|
|
|
46
|
|
|
$dateTime = new \DateTime(); |
47
|
|
|
|
48
|
|
|
$this->assertSame($dateTime->format($format), $helper($dateTime)); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @return array<mixed> |
53
|
|
|
*/ |
54
|
|
|
public function getInvokeWillFormatDateTimeAccordingToConstructorArgumentFormatData(): array |
55
|
|
|
{ |
56
|
|
|
return [ |
57
|
|
|
[\DateTimeInterface::ATOM], |
58
|
|
|
[\DateTimeInterface::RFC822], |
59
|
|
|
['Y-m-d H:i:s'], |
60
|
|
|
]; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Assert that when calling __invoke() without a format option, the returned date time string will be formatted |
65
|
|
|
* to the value provided when creating the class. |
66
|
|
|
* |
67
|
|
|
* @dataProvider getInvokeWillFormatDateTimeAccordingToOptionArgumentData |
68
|
|
|
*/ |
69
|
|
|
public function testInvokeWillFormatDateTimeAccordingToOptionArgument(string $format): void |
70
|
|
|
{ |
71
|
|
|
$helper = new DateTimeHelper(\DateTimeInterface::ATOM); |
72
|
|
|
|
73
|
|
|
$dateTime = new \DateTime(); |
74
|
|
|
$options = [ |
75
|
|
|
'format' => $format, |
76
|
|
|
]; |
77
|
|
|
|
78
|
|
|
$this->assertSame($dateTime->format($format), $helper($dateTime, $options)); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return array<mixed> |
83
|
|
|
*/ |
84
|
|
|
public function getInvokeWillFormatDateTimeAccordingToOptionArgumentData(): array |
85
|
|
|
{ |
86
|
|
|
return [ |
87
|
|
|
[\DateTimeInterface::ATOM], |
88
|
|
|
[\DateTimeInterface::RFC822], |
89
|
|
|
['Y-m-d H:i:s'], |
90
|
|
|
]; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|