|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Jasny\Twig; |
|
4
|
|
|
|
|
5
|
|
|
use Jasny\Twig\DateExtension; |
|
6
|
|
|
use Jasny\Twig\TestHelper; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* @covers Jasny\Twig\DateExtension |
|
10
|
|
|
*/ |
|
11
|
|
|
class DateExtensionTest extends \PHPUnit_Framework_TestCase |
|
12
|
|
|
{ |
|
13
|
|
|
use TestHelper; |
|
14
|
|
|
|
|
15
|
|
|
public function setUp() |
|
16
|
|
|
{ |
|
17
|
|
|
date_default_timezone_set('UTC'); |
|
18
|
|
|
\Locale::setDefault("en_EN"); |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
protected function getExtension() |
|
22
|
|
|
{ |
|
23
|
|
|
return new DateExtension(); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
|
|
27
|
|
|
public function localDateTimeProvider() |
|
28
|
|
|
{ |
|
29
|
|
|
return [ |
|
30
|
|
|
['9/20/2015', '20-09-2015', "{{ '20-09-2015'|localdate }}"], |
|
31
|
|
|
['September 20, 2015', '20 september 2015', "{{ '20-09-2015'|localdate('long') }}"], |
|
32
|
|
|
['9/20/15', "20-09-15", "{{ '20-09-2015'|localdate('short') }}"], |
|
33
|
|
|
['Sunday, September 20, 2015', "zondag 20 september 2015", "{{ '20-09-2015'|localdate('full') }}"], |
|
34
|
|
|
['20|09|2015', "20|09|2015", "{{ '20-09-2015'|localdate('dd|MM|yyyy') }}"], |
|
35
|
|
|
|
|
36
|
|
|
['11:14 PM', "23:14", "{{ '23:14:12'|localtime }}"], |
|
37
|
|
|
['11:14:12 PM GMT', "23:14:12 GMT", "{{ '23:14:12'|localtime('long') }}"], |
|
38
|
|
|
['11:14 PM', "23:14", "{{ '23:14:12'|localtime('short') }}"], |
|
39
|
|
|
['11:14:12 PM GMT', "23:14:12 GMT", "{{ '23:14:12'|localtime('full') }}"], |
|
40
|
|
|
['23|14|12', "23|14|12", "{{ '23:14:12'|localtime('HH|mm|ss') }}"], |
|
41
|
|
|
|
|
42
|
|
|
['9/20/2015, 11:14 PM', '20-09-2015 23:14', "{{ '20-09-2015 23:14:12'|localdatetime }}"], |
|
43
|
|
|
['20|23', '20|23', "{{ '20-09-2015 23:14:12'|localdatetime('dd|HH') }}"], |
|
44
|
|
|
[ |
|
45
|
|
|
'9/20/15, 11:14:12 PM GMT', |
|
46
|
|
|
'20-09-15 23:14:12 GMT', |
|
47
|
|
|
"{{ '20-09-2015 23:14:12'|localdatetime({date: 'short', time: 'full'}) }}" |
|
48
|
|
|
], |
|
49
|
|
|
[ |
|
50
|
|
|
'20150920 11:14:12 PM GMT', |
|
51
|
|
|
'20150920 23:14:12 GMT', |
|
52
|
|
|
"{{ '20-09-2015 23:14:12'|localdatetime({date: 'yyyyMMdd', time: 'full'}) }}" |
|
53
|
|
|
] |
|
54
|
|
|
]; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @dataProvider localDateTimeProvider |
|
59
|
|
|
* |
|
60
|
|
|
* @param string $en |
|
61
|
|
|
* @param string $nl |
|
62
|
|
|
* @param string $template |
|
63
|
|
|
*/ |
|
64
|
|
|
public function testLocalDateTimeEn($en, $nl, $template) |
|
65
|
|
|
{ |
|
66
|
|
|
if (!\Locale::setDefault("en_EN")) { |
|
67
|
|
|
return $this->markAsSkipped("Unable to set locale to 'en_EN'"); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
$this->assertRender($en, $template); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @dataProvider localDateTimeProvider |
|
75
|
|
|
* |
|
76
|
|
|
* @param string $en |
|
77
|
|
|
* @param string $nl |
|
78
|
|
|
* @param string $template |
|
79
|
|
|
*/ |
|
80
|
|
|
public function testLocalDateTimeNL($en, $nl, $template) |
|
81
|
|
|
{ |
|
82
|
|
|
if (!\Locale::setDefault("nl_NL")) { |
|
83
|
|
|
return $this->markAsSkipped("Unable to set locale to 'nl_NL'"); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
$this->assertRender($nl, $template); |
|
87
|
|
|
|
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
|
|
91
|
|
|
public function durationProvider() |
|
92
|
|
|
{ |
|
93
|
|
|
return [ |
|
94
|
|
|
['31s', "{{ 31|duration }}"], |
|
95
|
|
|
['17m 31s', "{{ 1051|duration }}"], |
|
96
|
|
|
['3h 17m 31s', "{{ 11851|duration }}"], |
|
97
|
|
|
['2d 3h 17m 31s', "{{ 184651|duration }}"], |
|
98
|
|
|
['3w 2d 3h 17m 31s', "{{ 1999051|duration }}"], |
|
99
|
|
|
['1y 3w 2d 3h 17m 31s', "{{ 33448651|duration }}"], |
|
100
|
|
|
|
|
101
|
|
|
['17 minute(s)', "{{ 1051|duration([null, ' minute(s)', ' hour(s)', ' day(s)']) }}"], |
|
102
|
|
|
['3 hour(s)', "{{ 11851|duration([null, null, ' hour(s)']) }}"], |
|
103
|
|
|
['2 day(s)', "{{ 184651|duration([null, null, null, ' day(s)']) }}"], |
|
104
|
|
|
['3 week(s)', "{{ 1999051|duration([null, null, null, null, ' week(s)']) }}"], |
|
105
|
|
|
['1 year(s)', "{{ 33448651|duration([null, null, null, null, null, ' year(s)']) }}"], |
|
106
|
|
|
|
|
107
|
|
|
['3u:17m', "{{ 11851|duration([null, 'm', 'u'], ':') }}"], |
|
108
|
|
|
['3:17h', "{{ 11851|duration([null, '', ''], ':') }}h"], |
|
109
|
|
|
]; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @dataProvider durationProvider |
|
114
|
|
|
* |
|
115
|
|
|
* @param string $expect |
|
116
|
|
|
* @param string $template |
|
117
|
|
|
*/ |
|
118
|
|
|
public function testDuration($expect, $template) |
|
119
|
|
|
{ |
|
120
|
|
|
$this->assertRender($expect, $template); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
|
|
124
|
|
|
public function ageProvider() |
|
125
|
|
|
{ |
|
126
|
|
|
$time = time() - (((32 * 365) + 100) * 24 * 3600); |
|
127
|
|
|
$date = date('Y-m-d', $time); |
|
128
|
|
|
|
|
129
|
|
|
return [ |
|
130
|
|
|
['32', "{{ $time|age }}"], |
|
131
|
|
|
['32', "{{ '$date'|age }}"] |
|
132
|
|
|
]; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* @dataProvider ageProvider |
|
137
|
|
|
* |
|
138
|
|
|
* @param string $expect |
|
139
|
|
|
* @param string $template |
|
140
|
|
|
*/ |
|
141
|
|
|
public function testAge($expect, $template) |
|
142
|
|
|
{ |
|
143
|
|
|
$this->assertRender($expect, $template); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
|
|
147
|
|
|
public function filterProvider() |
|
148
|
|
|
{ |
|
149
|
|
|
return [ |
|
150
|
|
|
['localdate'], |
|
151
|
|
|
['localtime'], |
|
152
|
|
|
['localdatetime'], |
|
153
|
|
|
['duration'], |
|
154
|
|
|
['age'] |
|
155
|
|
|
]; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* @dataProvider filterProvider |
|
160
|
|
|
* |
|
161
|
|
|
* @param string $filter |
|
162
|
|
|
*/ |
|
163
|
|
|
public function testWithNull($filter) |
|
164
|
|
|
{ |
|
165
|
|
|
$this->assertRender('-', '{{ null|' . $filter . '("//")|default("-") }}'); |
|
166
|
|
|
} |
|
167
|
|
|
} |
|
168
|
|
|
|