1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Popy\Calendar\Tests; |
4
|
|
|
|
5
|
|
|
use DateTimeZone; |
6
|
|
|
use DateTimeImmutable; |
7
|
|
|
use PHPUnit_Framework_TestCase; |
8
|
|
|
use Popy\Calendar\Factory\ConfigurableFactory; |
9
|
|
|
|
10
|
|
|
class GregorianComposedImplementationTest extends PHPUnit_Framework_TestCase |
11
|
|
|
{ |
12
|
|
|
protected $calendar; |
13
|
|
|
protected $timezone; |
14
|
|
|
|
15
|
|
|
public function setUp() |
16
|
|
|
{ |
17
|
|
|
$factory = new ConfigurableFactory(); |
18
|
|
|
$this->calendar = $factory->build(); |
19
|
|
|
|
20
|
|
|
$this->timezone = new DateTimeZone('UTC'); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function provideFormattingTests() |
24
|
|
|
{ |
25
|
|
|
// Testing timestamp 0 |
26
|
|
|
yield ['1970-01-01 00:00:00 UTC', 'U', '0']; |
27
|
|
|
|
28
|
|
|
// Basic time offset |
29
|
|
|
yield ['1970-01-01 00:00:00 +01:00', 'U', '-3600']; |
30
|
|
|
yield ['1970-01-01 00:00:00 -01:00', 'U', '3600']; |
31
|
|
|
|
32
|
|
|
// Basic date fields |
33
|
|
|
yield ['2015-06-02 00:00:00 UTC', 'Y-m-d', '2015-06-02']; |
34
|
|
|
yield ['2015-06-02 00:00:00 UTC', 'y-n-j', '15-6-2']; |
35
|
|
|
// Default configuration should convert back this properly |
36
|
|
|
yield ['1975-06-02 00:00:00 UTC', 'y-n-j', '75-6-2']; |
37
|
|
|
|
38
|
|
|
yield ['2005-01-01 00:00:00 UTC', 'jS F Y', '1st January 2005']; |
39
|
|
|
yield ['2005-02-02 00:00:00 UTC', 'jS F Y', '2nd February 2005']; |
40
|
|
|
yield ['2005-03-03 00:00:00 UTC', 'jS F Y', '3rd March 2005']; |
41
|
|
|
yield ['2005-04-04 00:00:00 UTC', 'jS F Y', '4th April 2005']; |
42
|
|
|
yield ['2005-05-05 00:00:00 UTC', 'jS M Y', '5th May 2005']; |
43
|
|
|
yield ['2005-06-06 00:00:00 UTC', 'jS M Y', '6th Jun 2005']; |
44
|
|
|
yield ['2005-07-07 00:00:00 UTC', 'jS M Y', '7th Jul 2005']; |
45
|
|
|
yield ['2005-08-08 00:00:00 UTC', 'jS M Y', '8th Aug 2005']; |
46
|
|
|
|
47
|
|
|
// Leap years and day count |
48
|
|
|
yield ['1900-02-01 00:00:00 UTC', 'L t', '0 28', true]; |
49
|
|
|
yield ['2000-02-01 00:00:00 UTC', 'L t', '1 29', true]; |
50
|
|
|
|
51
|
|
|
// ISO 8601 tests |
52
|
|
|
yield ['2003-01-01 00:00:00 UTC', 'o W N/l H:i:s', '2003 01 3/Wednesday 00:00:00']; |
53
|
|
|
yield ['2005-01-01 00:00:00 UTC', 'o W N/l H:i:s', '2004 53 6/Saturday 00:00:00']; |
54
|
|
|
// Same with non iso day indexes |
55
|
|
|
yield ['2003-01-01 00:00:00 UTC', 'o W w H:i:s', '2003 01 3 00:00:00']; |
56
|
|
|
yield ['2005-01-01 00:00:00 UTC', 'o W w H:i:s', '2004 53 6 00:00:00']; |
57
|
|
|
yield ['2005-01-02 00:00:00 UTC', 'o W w H:i:s', '2004 53 0 00:00:00']; |
58
|
|
|
|
59
|
|
|
// Time |
60
|
|
|
// Basic test |
61
|
|
|
yield ['2005-01-01 17:30:10 UTC', 'Y-m-d H:i:s', '2005-01-01 17:30:10']; |
62
|
|
|
// Testing 0 padding |
63
|
|
|
yield ['2005-01-01 05:30:10 UTC', 'Y-m-d H:i:s', '2005-01-01 05:30:10']; |
64
|
|
|
// Testing without 0 padding |
65
|
|
|
yield ['2005-01-01 05:30:10 UTC', 'Y-m-d G:i:s', '2005-01-01 5:30:10']; |
66
|
|
|
// Testing 12hour format with 0 padding ... |
67
|
|
|
yield ['2005-01-01 17:30:10 UTC', 'Y-m-d ha:i:s', '2005-01-01 05pm:30:10']; |
68
|
|
|
/// ... and without |
69
|
|
|
yield ['2005-01-01 17:30:10 UTC', 'Y-m-d ga:i:s', '2005-01-01 5pm:30:10']; |
70
|
|
|
// Testing midnight special case |
71
|
|
|
yield ['2005-01-01 00:00:00 UTC', 'Y-m-d ha:i:s', '2005-01-01 12am:00:00']; |
72
|
|
|
yield ['2005-01-01 00:00:00 UTC', 'Y-m-d ga:i:s', '2005-01-01 12am:00:00']; |
73
|
|
|
// Testing noon special case |
74
|
|
|
yield ['2005-01-01 12:00:00 UTC', 'Y-m-d ha:i:s', '2005-01-01 12pm:00:00']; |
75
|
|
|
yield ['2005-01-01 12:00:00 UTC', 'Y-m-d ga:i:s', '2005-01-01 12pm:00:00']; |
76
|
|
|
// testing uppercase AM/PM |
77
|
|
|
yield ['2005-01-01 17:30:10 UTC', 'Y-m-d gA:i:s', '2005-01-01 5PM:30:10']; |
78
|
|
|
yield ['2005-01-01 05:30:10 UTC', 'Y-m-d hA:i:s', '2005-01-01 05AM:30:10']; |
79
|
|
|
// Testing swatch time |
80
|
|
|
yield ['2005-01-01 0:00:00 +01:00', 'Y-m-d B', '2005-01-01 000']; |
81
|
|
|
yield ['2005-01-01 0:01:27 +01:00', 'Y-m-d B', '2005-01-01 001']; |
82
|
|
|
yield ['2005-01-01 12:00:00 +01:00', 'Y-m-d B', '2005-01-01 500']; |
83
|
|
|
// testing with different time zone |
84
|
|
|
yield ['2005-01-01 11:57:36 UTC', 'Y-m-d B', '2005-01-01 540']; |
85
|
|
|
yield ['2005-01-01 00:00:00 -11:00', 'Y-m-d B', '2005-01-01 500']; |
86
|
|
|
|
87
|
|
|
// testing mili/micro seconds |
88
|
|
|
yield ['2005-01-01 00:00:00 UTC', 'Y-m-d H:i:s.u', '2005-01-01 00:00:00.000000']; |
89
|
|
|
yield ['2005-01-01 00:00:00.123456 UTC', 'Y-m-d H:i:s.u', '2005-01-01 00:00:00.123456']; |
90
|
|
|
yield ['2005-01-01 00:00:00.123000 UTC', 'Y-m-d H:i:s.v', '2005-01-01 00:00:00.123']; |
91
|
|
|
yield ['2005-01-01 00:00:00.123456 UTC', 'Y-m-d H:i:s.vµ', '2005-01-01 00:00:00.123456']; |
92
|
|
|
|
93
|
|
|
// testing recursive formats |
94
|
|
|
yield ['2005-01-01 11:57:36 +01:00', 'c', '2005-01-01T11:57:36+01:00']; |
95
|
|
|
yield ['2005-01-01 11:57:36 +01:00', 'r', 'Sat, 01 Jan 2005 11:57:36 +0100']; |
96
|
|
|
// not alone |
97
|
|
|
yield ['2005-01-01 11:57:36 +01:00', '\\YY c \\YY', 'Y2005 2005-01-01T11:57:36+01:00 Y2005']; |
98
|
|
|
yield ['2005-01-01 11:57:36 +01:00', '\\YY r \\YY', 'Y2005 Sat, 01 Jan 2005 11:57:36 +0100 Y2005']; |
99
|
|
|
|
100
|
|
|
// Day of the year test |
101
|
|
|
yield ['2005-01-01 00:00:00 UTC', 'Y z', '2005 0']; |
102
|
|
|
yield ['2005-03-02 00:00:00 UTC', 'Y z', '2005 60']; |
103
|
|
|
|
104
|
|
|
// Timezones / offsets |
105
|
|
|
yield ['2005-01-01 00:00:00 Europe/Berlin', 'Y-m-d H:i:s e', '2005-01-01 00:00:00 Europe/Berlin']; |
106
|
|
|
yield ['2005-01-01 00:00:00 Europe/Berlin', 'Y-m-d H:i:s T I', '2005-01-01 00:00:00 CET 0']; |
107
|
|
|
yield ['2005-01-01 00:00:00 Europe/Berlin', 'Y-m-d H:i:s Z', '2005-01-01 00:00:00 3600']; |
108
|
|
|
yield ['2005-01-01 00:00:00 Europe/Berlin', 'Y-m-d H:i:s O', '2005-01-01 00:00:00 +0100']; |
109
|
|
|
yield ['2005-01-01 00:00:00 Europe/Berlin', 'Y-m-d H:i:s P', '2005-01-01 00:00:00 +01:00']; |
110
|
|
|
|
111
|
|
|
// now with DST |
112
|
|
|
yield ['2005-06-01 00:00:00 Europe/Berlin', 'Y-m-d H:i:s T I', '2005-06-01 00:00:00 CEST 1']; |
113
|
|
|
yield ['2005-06-01 00:00:00 Europe/Berlin', 'Y-m-d H:i:s Z', '2005-06-01 00:00:00 7200']; |
114
|
|
|
yield ['2005-06-01 00:00:00 Europe/Berlin', 'Y-m-d H:i:s O', '2005-06-01 00:00:00 +0200']; |
115
|
|
|
yield ['2005-06-01 00:00:00 Europe/Berlin', 'Y-m-d H:i:s P', '2005-06-01 00:00:00 +02:00']; |
116
|
|
|
|
117
|
|
|
// Testing some symbol escaping : |
118
|
|
|
yield ['2005-01-01 00:00:00.123456 UTC', 'Y-m-d H:i:s.vµ \\Y-\\m-\\d \\H:\\i:\\s.\\v\\µ', '2005-01-01 00:00:00.123456 Y-m-d H:i:s.vµ']; |
119
|
|
|
|
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @dataProvider provideFormattingTests |
124
|
|
|
*/ |
125
|
|
|
public function testFormat($date, $format, $formatted) |
126
|
|
|
{ |
127
|
|
|
$date = new DateTimeImmutable($date); |
128
|
|
|
|
129
|
|
|
$res = $this->calendar->format($date, $format); |
130
|
|
|
|
131
|
|
|
$this->assertSame($formatted, $res); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @dataProvider provideFormattingTests |
136
|
|
|
*/ |
137
|
|
|
public function testParse($date, $format, $formatted, $skip = false) |
138
|
|
|
{ |
139
|
|
|
if ($skip) { |
140
|
|
|
return; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
$date = new DateTimeImmutable($date); |
144
|
|
|
$res = $this->calendar->parse($formatted, $format, $this->timezone); |
145
|
|
|
|
146
|
|
|
if ($res) { |
147
|
|
|
$res = $res->getTimestamp(); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
// COmpare timestamps, as timezone can be lost. |
151
|
|
|
$this->assertSame($date->getTimestamp(), $res); |
152
|
|
|
} |
153
|
|
|
} |