|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Holiday Library. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Michał Mańko <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE.md |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Michalmanko\Holiday\Test\Provider; |
|
13
|
|
|
|
|
14
|
|
|
use ArrayObject; |
|
15
|
|
|
use DateTime; |
|
16
|
|
|
use DateTimeZone; |
|
17
|
|
|
use Michalmanko\Holiday\Holiday; |
|
18
|
|
|
use PHPUnit_Framework_MockObject_MockObject; |
|
19
|
|
|
use PHPUnit_Framework_TestCase; |
|
20
|
|
|
use ReflectionClass; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @author Michał Mańko <[email protected]> |
|
24
|
|
|
*/ |
|
25
|
|
|
class AbstractProviderTest extends PHPUnit_Framework_TestCase |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* @var PHPUnit_Framework_MockObject_MockObject |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $object; |
|
31
|
|
|
|
|
32
|
|
|
protected $holiday1; |
|
33
|
|
|
|
|
34
|
|
|
protected $holiday2; |
|
35
|
|
|
|
|
36
|
|
|
protected $schoolHoliday1; |
|
37
|
|
|
|
|
38
|
|
|
protected $schoolHoliday2; |
|
39
|
|
|
|
|
40
|
|
|
public function setUp() |
|
41
|
|
|
{ |
|
42
|
|
|
$this->holiday1 = $holiday1 = new Holiday( |
|
43
|
|
|
'Holiday 1', |
|
44
|
|
|
'2015-01-01', |
|
45
|
|
|
null, |
|
46
|
|
|
Holiday::TYPE_HOLIDAY |
|
47
|
|
|
); |
|
48
|
|
|
$this->holiday2 = $holiday2 = new Holiday( |
|
49
|
|
|
'Holiday 2', |
|
50
|
|
|
'2015-01-10', |
|
51
|
|
|
null, |
|
52
|
|
|
Holiday::TYPE_HOLIDAY |
|
53
|
|
|
); |
|
54
|
|
|
$this->schoolHoliday1 = $schoolHoliday1 = new Holiday( |
|
55
|
|
|
'School Holiday 1', |
|
56
|
|
|
'2015-01-02', |
|
57
|
|
|
null, |
|
58
|
|
|
Holiday::TYPE_SCHOOL_HOLIDAY |
|
59
|
|
|
); |
|
60
|
|
|
$this->schoolHoliday2 = $schoolHoliday2 = new Holiday( |
|
61
|
|
|
'School Holiday 2', |
|
62
|
|
|
'2015-01-01', |
|
63
|
|
|
null, |
|
64
|
|
|
Holiday::TYPE_SCHOOL_HOLIDAY |
|
65
|
|
|
); |
|
66
|
|
|
|
|
67
|
|
|
$this->object = $this->getMockForAbstractClass( |
|
68
|
|
|
'\\Michalmanko\\Holiday\\Provider\\AbstractProvider' |
|
69
|
|
|
); |
|
70
|
|
|
$this->object->expects($this->any()) |
|
71
|
|
|
->method('prepareHolidays') |
|
72
|
|
|
->withAnyParameters() |
|
73
|
|
|
->willReturnCallback(function () use ( |
|
74
|
|
|
$holiday1, |
|
75
|
|
|
$holiday2, |
|
76
|
|
|
$schoolHoliday1, |
|
77
|
|
|
$schoolHoliday2 |
|
78
|
|
|
) { |
|
79
|
|
|
$data = new ArrayObject(); |
|
80
|
|
|
|
|
81
|
|
|
$data->append($holiday1); |
|
82
|
|
|
$data->append($holiday2); |
|
83
|
|
|
$data->append($schoolHoliday1); |
|
84
|
|
|
$data->append($schoolHoliday2); |
|
85
|
|
|
|
|
86
|
|
|
return $data->getArrayCopy(); |
|
87
|
|
|
}); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function testGetTimeZone() |
|
91
|
|
|
{ |
|
92
|
|
|
$this->assertNull($this->object->getTimeZone()); |
|
93
|
|
|
|
|
94
|
|
|
$timezone = new DateTimeZone('Europe/London'); |
|
95
|
|
|
|
|
96
|
|
|
$mock = $this->getMockForAbstractClass( |
|
97
|
|
|
'\\Michalmanko\\Holiday\\Provider\\AbstractProvider', |
|
98
|
|
|
array($timezone) |
|
99
|
|
|
); |
|
100
|
|
|
|
|
101
|
|
|
$this->assertSame($timezone, $mock->getTimeZone()); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
public function testCreateHoliday() |
|
105
|
|
|
{ |
|
106
|
|
|
$holiday = $this->object->createHoliday( |
|
107
|
|
|
'holidayName', |
|
108
|
|
|
'2015-01-01' |
|
109
|
|
|
); |
|
110
|
|
|
|
|
111
|
|
|
$this->assertInstanceOf( |
|
112
|
|
|
'\\Michalmanko\\Holiday\\Holiday', |
|
113
|
|
|
$holiday |
|
114
|
|
|
); |
|
115
|
|
|
|
|
116
|
|
|
$this->assertEquals('holidayName', $holiday->getName()); |
|
117
|
|
|
|
|
118
|
|
|
$this->assertEquals('2015-01-01', $holiday->format('Y-m-d')); |
|
119
|
|
|
|
|
120
|
|
|
$this->assertEquals(date_default_timezone_get(), $holiday->getTimeZone()->getName()); |
|
121
|
|
|
|
|
122
|
|
|
$this->assertEquals(Holiday::TYPE_HOLIDAY, $holiday->getType()); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
public function testCreateHolidayWithTimeZone() |
|
126
|
|
|
{ |
|
127
|
|
|
$timezone = new DateTimeZone('Europe/London'); |
|
128
|
|
|
|
|
129
|
|
|
$mock = $this->getMockForAbstractClass( |
|
130
|
|
|
'\\Michalmanko\\Holiday\\Provider\\AbstractProvider', |
|
131
|
|
|
array($timezone) |
|
132
|
|
|
); |
|
133
|
|
|
|
|
134
|
|
|
$holiday = $mock->createHoliday( |
|
135
|
|
|
'holidayName', |
|
136
|
|
|
'2015-01-01' |
|
137
|
|
|
); |
|
138
|
|
|
|
|
139
|
|
|
$this->assertEquals($timezone->getName(), $holiday->getTimeZone()->getName()); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
public function testCreateHolidayWithType() |
|
143
|
|
|
{ |
|
144
|
|
|
$holiday = $this->object->createHoliday( |
|
145
|
|
|
'holidayName', |
|
146
|
|
|
'2015-01-01', |
|
147
|
|
|
Holiday::TYPE_SCHOOL_HOLIDAY |
|
148
|
|
|
); |
|
149
|
|
|
|
|
150
|
|
|
$this->assertEquals(Holiday::TYPE_SCHOOL_HOLIDAY, $holiday->getType()); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
public function testGetEaster() |
|
154
|
|
|
{ |
|
155
|
|
|
$getEaster = self::getMethod('getEaster'); |
|
156
|
|
|
$easter = $getEaster->invokeArgs($this->object, array(2015)); |
|
157
|
|
|
|
|
158
|
|
|
$this->assertEquals('2015-04-05', $easter->format('Y-m-d')); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
public function testGetHolidaysByYear() |
|
162
|
|
|
{ |
|
163
|
|
|
$this->object->expects($this->once()) |
|
164
|
|
|
->method('prepareHolidays') |
|
165
|
|
|
->with(2015); |
|
166
|
|
|
|
|
167
|
|
|
$this->object->getHolidaysByYear(2015); |
|
168
|
|
|
$holidays = $this->object->getHolidaysByYear(2015); |
|
169
|
|
|
|
|
170
|
|
|
$this->assertSame( |
|
171
|
|
|
array( |
|
172
|
|
|
$this->holiday1, |
|
173
|
|
|
$this->holiday2, |
|
174
|
|
|
$this->schoolHoliday1, |
|
175
|
|
|
$this->schoolHoliday2, |
|
176
|
|
|
), |
|
177
|
|
|
$holidays |
|
178
|
|
|
); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
public function testGetHolidaysByYearWithArrayObject() |
|
182
|
|
|
{ |
|
183
|
|
|
$mock = $this->getMockForAbstractClass( |
|
184
|
|
|
'\\Michalmanko\\Holiday\\Provider\\AbstractProvider' |
|
185
|
|
|
); |
|
186
|
|
|
$mock->expects($this->any()) |
|
187
|
|
|
->method('prepareHolidays') |
|
188
|
|
|
->withAnyParameters() |
|
189
|
|
|
->willReturn(new ArrayObject()); |
|
190
|
|
|
|
|
191
|
|
|
$mock->getHolidaysByYear(2015); |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
public function testGetHolidaysByYearWithInvalidData() |
|
195
|
|
|
{ |
|
196
|
|
|
$mock = $this->getMockForAbstractClass( |
|
197
|
|
|
'\\Michalmanko\\Holiday\\Provider\\AbstractProvider' |
|
198
|
|
|
); |
|
199
|
|
|
$this->setExpectedException( |
|
200
|
|
|
'\\Michalmanko\\Holiday\\Provider\\Exception\\UnexpectedValueException', |
|
201
|
|
|
sprintf( |
|
202
|
|
|
'Method %s::prepareHolidays() must returns an array', |
|
203
|
|
|
get_class($mock) |
|
204
|
|
|
) |
|
205
|
|
|
); |
|
206
|
|
|
$mock->expects($this->any()) |
|
207
|
|
|
->method('prepareHolidays') |
|
208
|
|
|
->withAnyParameters() |
|
209
|
|
|
->willReturn('string'); |
|
210
|
|
|
|
|
211
|
|
|
$mock->getHolidaysByYear(2015); |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
public function testGetHolidaysByYearWithTypeFilter() |
|
215
|
|
|
{ |
|
216
|
|
|
$holidays = $this->object->getHolidaysByYear(2015, Holiday::TYPE_SCHOOL_HOLIDAY); |
|
217
|
|
|
|
|
218
|
|
|
$this->assertSame( |
|
219
|
|
|
array( |
|
220
|
|
|
$this->schoolHoliday1, |
|
221
|
|
|
$this->schoolHoliday2, |
|
222
|
|
|
), |
|
223
|
|
|
$holidays |
|
224
|
|
|
); |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
public function testGetHolidays() |
|
228
|
|
|
{ |
|
229
|
|
|
$date = new DateTime('2015-01-01 12:00:00'); |
|
230
|
|
|
$dateTimestamp = $date->getTimestamp(); |
|
231
|
|
|
|
|
232
|
|
|
$holidays = $this->object->getHolidays($date); |
|
233
|
|
|
|
|
234
|
|
|
$this->assertEquals($dateTimestamp, $date->getTimestamp()); |
|
235
|
|
|
|
|
236
|
|
|
$this->assertEquals( |
|
237
|
|
|
array( |
|
238
|
|
|
$this->holiday1, |
|
239
|
|
|
$this->schoolHoliday2, |
|
240
|
|
|
), |
|
241
|
|
|
$holidays |
|
242
|
|
|
); |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
|
|
public function testGetHolidaysWithType() |
|
246
|
|
|
{ |
|
247
|
|
|
$date = new DateTime('2015-01-01 12:00:00'); |
|
248
|
|
|
|
|
249
|
|
|
$holidays = $this->object->getHolidays($date, Holiday::TYPE_SCHOOL_HOLIDAY); |
|
250
|
|
|
|
|
251
|
|
|
$this->assertEquals( |
|
252
|
|
|
array($this->schoolHoliday2), |
|
253
|
|
|
$holidays |
|
254
|
|
|
); |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
public function testGetHolidaysWithEndDate() |
|
258
|
|
|
{ |
|
259
|
|
|
$dateStart = new DateTime('2015-01-01 12:00:00'); |
|
260
|
|
|
$dateEnd = new DateTime('2015-01-01 14:00:00'); |
|
261
|
|
|
$dateEndTimestamp = $dateEnd->getTimestamp(); |
|
262
|
|
|
|
|
263
|
|
|
$holidays = $this->object->getHolidays($dateStart, $dateEnd); |
|
264
|
|
|
|
|
265
|
|
|
$this->assertEquals($dateEndTimestamp, $dateEnd->getTimestamp()); |
|
266
|
|
|
|
|
267
|
|
|
$this->assertEquals( |
|
268
|
|
|
array( |
|
269
|
|
|
$this->holiday1, |
|
270
|
|
|
$this->schoolHoliday2, |
|
271
|
|
|
), |
|
272
|
|
|
$holidays |
|
273
|
|
|
); |
|
274
|
|
|
} |
|
275
|
|
|
|
|
276
|
|
|
public function testGetHolidaysWithInvalidEndDate() |
|
277
|
|
|
{ |
|
278
|
|
|
$dateStart = new DateTime('2015-01-01 12:00:00'); |
|
279
|
|
|
|
|
280
|
|
|
$this->setExpectedException( |
|
281
|
|
|
'\\Michalmanko\\Holiday\\Provider\\Exception\\InvalidArgumentException', |
|
282
|
|
|
'$endDateOrType must be an instance of \\DateTime' |
|
283
|
|
|
); |
|
284
|
|
|
|
|
285
|
|
|
$this->object->getHolidays($dateStart, 'invalid-date', Holiday::TYPE_HOLIDAY); |
|
286
|
|
|
} |
|
287
|
|
|
|
|
288
|
|
|
public function testHasHolidays() |
|
289
|
|
|
{ |
|
290
|
|
|
$this->assertTrue($this->object->hasHolidays( |
|
291
|
|
|
new DateTime('2015-01-01'), |
|
292
|
|
|
new DateTime('2015-01-02') |
|
293
|
|
|
)); |
|
294
|
|
|
|
|
295
|
|
|
$this->assertFalse($this->object->hasHolidays( |
|
296
|
|
|
new DateTime('2015-02-01'), |
|
297
|
|
|
new DateTime('2015-02-02') |
|
298
|
|
|
)); |
|
299
|
|
|
} |
|
300
|
|
|
|
|
301
|
|
|
public function testIsHoliday() |
|
302
|
|
|
{ |
|
303
|
|
|
$this->assertTrue($this->object->isHoliday( |
|
304
|
|
|
new DateTime('2015-01-01') |
|
305
|
|
|
)); |
|
306
|
|
|
|
|
307
|
|
|
$this->assertFalse($this->object->isHoliday( |
|
308
|
|
|
new DateTime('2015-02-01') |
|
309
|
|
|
)); |
|
310
|
|
|
} |
|
311
|
|
|
|
|
312
|
|
|
protected static function getMethod($name) |
|
313
|
|
|
{ |
|
314
|
|
|
$class = new ReflectionClass('\\Michalmanko\\Holiday\\Provider\\AbstractProvider'); |
|
315
|
|
|
$method = $class->getMethod($name); |
|
316
|
|
|
$method->setAccessible(true); |
|
317
|
|
|
|
|
318
|
|
|
return $method; |
|
319
|
|
|
} |
|
320
|
|
|
} |
|
321
|
|
|
|