Test Failed
Push — master ( abf703...9671b2 )
by Jonathan
04:24 queued 01:11
created

ActionScheduler_TimezoneHelper_Test   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 95
rs 10
c 0
b 0
f 0
wmc 4
lcom 0
cbo 3
1
<?php
2
3
/**
4
 * @group timezone
5
 */
6
class ActionScheduler_TimezoneHelper_Test extends ActionScheduler_UnitTestCase {
7
8
	/**
9
	 * Ensure that the timezone string we expect works properly.
10
	 *
11
	 * @dataProvider local_timezone_provider
12
	 *
13
	 * @param $timezone_string
14
	 */
15
	public function test_local_timezone_strings( $timezone_string ) {
16
		$timezone_filter = function ( $tz ) use ( $timezone_string ) {
17
			return $timezone_string;
18
		};
19
20
		add_filter( 'option_timezone_string', $timezone_filter );
21
22
		$date     = new ActionScheduler_DateTime();
23
		$timezone = ActionScheduler_TimezoneHelper::set_local_timezone( $date )->getTimezone();
24
		$this->assertInstanceOf( 'DateTimeZone', $timezone );
25
		$this->assertEquals( $timezone_string, $timezone->getName() );
26
27
		remove_filter( 'option_timezone_string', $timezone_filter );
28
	}
29
30
	public function local_timezone_provider() {
31
		return array(
32
			array( 'America/New_York' ),
33
			array( 'Australia/Melbourne' ),
34
			array( 'UTC' ),
35
		);
36
	}
37
38
	/**
39
	 * Ensure that most GMT offsets don't return UTC as the timezone.
40
	 *
41
	 * @dataProvider local_timezone_offsets_provider
42
	 *
43
	 * @param $gmt_offset
44
	 */
45
	public function test_local_timezone_offsets( $gmt_offset ) {
46
		$gmt_filter = function ( $gmt ) use ( $gmt_offset ) {
47
			return $gmt_offset;
48
		};
49
50
		$date = new ActionScheduler_DateTime();
51
52
		add_filter( 'option_gmt_offset', $gmt_filter );
53
		ActionScheduler_TimezoneHelper::set_local_timezone( $date );
54
		remove_filter( 'option_gmt_offset', $gmt_filter );
55
56
		$offset_in_seconds = $gmt_offset * HOUR_IN_SECONDS;
57
58
		$this->assertEquals( $offset_in_seconds, $date->getOffset() );
59
		$this->assertEquals( $offset_in_seconds, $date->getOffsetTimestamp() - $date->getTimestamp() );
60
	}
61
62
	public function local_timezone_offsets_provider() {
63
		return array(
64
			array( '-11' ),
65
			array( '-10.5' ),
66
			array( '-10' ),
67
			array( '-9' ),
68
			array( '-8' ),
69
			array( '-7' ),
70
			array( '-6' ),
71
			array( '-5' ),
72
			array( '-4.5' ),
73
			array( '-4' ),
74
			array( '-3.5' ),
75
			array( '-3' ),
76
			array( '-2' ),
77
			array( '-1' ),
78
			array( '1' ),
79
			array( '1.5' ),
80
			array( '2' ),
81
			array( '3' ),
82
			array( '4' ),
83
			array( '5' ),
84
			array( '5.5' ),
85
			array( '5.75' ),
86
			array( '6' ),
87
			array( '7' ),
88
			array( '8' ),
89
			array( '8.5' ),
90
			array( '9' ),
91
			array( '9.5' ),
92
			array( '10' ),
93
			array( '10.5' ),
94
			array( '11' ),
95
			array( '11.5' ),
96
			array( '12' ),
97
			array( '13' ),
98
		);
99
	}
100
}
101