Completed
Push — master ( eebbc1...b931d6 )
by mw
225:23 queued 190:29
created

TimezoneTest::testIsValidAndIsMilitary()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 3
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace SMW\Tests\DataValues\Time;
4
5
use SMW\DataItemFactory;
6
use SMW\DataValues\Time\Timezone;
7
8
/**
9
 * @covers \SMW\DataValues\Time\Timezone
10
 * @group semantic-mediawiki
11
 *
12
 * @license GNU GPL v2+
13
 * @since 2.5
14
 *
15
 * @author mwjames
16
 */
17
class TimezoneTest extends \PHPUnit_Framework_TestCase {
18
19
	public function testCanConstruct() {
20
21
		$this->assertInstanceOf(
22
			Timezone::class,
23
			new Timezone()
24
		);
25
	}
26
27
	public function testListShortAbbreviations() {
28
29
		$this->assertInternalType(
30
			'array',
31
			Timezone::listShortAbbreviations()
32
		);
33
	}
34
35
	/**
36
	 * @dataProvider timezoneProvider
37
	 */
38
	public function testIsValidAndIsMilitary( $abbrevation, $isValid, $isMilitary ) {
39
40
		$this->assertEquals(
41
			$isValid,
42
			Timezone::isValid( $abbrevation )
43
		);
44
45
		$this->assertEquals(
46
			$isMilitary,
47
			Timezone::isMilitary( $abbrevation )
48
		);
49
	}
50
51
	/**
52
	 * @dataProvider timezoneProvider
53
	 */
54
	public function testGetIdByAbbreviation( $abbrevation, $isValid, $isMilitary, $expectedId ) {
55
56
		$this->assertEquals(
57
			$expectedId,
58
			Timezone::getIdByAbbreviation( $abbrevation )
59
		);
60
	}
61
62
	/**
63
	 * @dataProvider offsetProvider
64
	 */
65
	public function testGetOffsetByAbbreviation( $abbrevation, $expected ) {
66
67
		$this->assertEquals(
68
			$expected,
69
			Timezone::getOffsetByAbbreviation( $abbrevation )
70
		);
71
	}
72
73
	public function timezoneProvider() {
74
75
		$provider[] = array(
0 ignored issues
show
Coding Style Comprehensibility introduced by
$provider was never initialized. Although not strictly required by PHP, it is generally a good practice to add $provider = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
76
			'UTC',
77
			true,
78
			false,
79
			0
80
		);
81
82
		$provider[] = array(
83
			'Z',
84
			true,
85
			true,
86
			1
87
		);
88
89
		$provider[] = array(
90
			'Unknown',
91
			false,
92
			false,
93
			false
94
		);
95
96
		$provider[] = array(
97
			'Asia/Tokyo',
98
			true,
99
			false,
100
			'Asia/Tokyo'
101
		);
102
103
		$provider[] = array(
104
			'America/Los Angeles',
105
			true,
106
			false,
107
			'America/Los_Angeles'
108
		);
109
110
		$provider[] = array(
111
			'America/Los_Angeles',
112
			true,
113
			false,
114
			'America/Los_Angeles'
115
		);
116
117
		return $provider;
118
	}
119
120
	public function offsetProvider() {
121
122
		$provider[] = array(
0 ignored issues
show
Coding Style Comprehensibility introduced by
$provider was never initialized. Although not strictly required by PHP, it is generally a good practice to add $provider = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
123
			'UTC',
124
			0
125
		);
126
127
		$provider[] = array(
128
			'Z',
129
			0
130
		);
131
132
		$provider[] = array(
133
			'Unknown',
134
			false
135
		);
136
137
		$provider[] = array(
138
			'Asia/Tokyo',
139
			32400
140
		);
141
142
		// Maybe return PST or PDT during Daylight Savings Time
143
		/*
0 ignored issues
show
Unused Code Comprehensibility introduced by
55% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
144
		$provider[] = array(
145
			'America/Los Angeles',
146
			-25200
147
		);
148
149
		$provider[] = array(
150
			'America/Los_Angeles',
151
			-25200
152
		);
153
		*/
154
		return $provider;
155
	}
156
157
}
158