1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SMW\Tests\Utils; |
4
|
|
|
|
5
|
|
|
use SMW\Utils\StatsFormatter; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* @covers \SMW\Utils\StatsFormatter |
9
|
|
|
* @group semantic-mediawiki |
10
|
|
|
* |
11
|
|
|
* @license GNU GPL v2+ |
12
|
|
|
* @since 2.5 |
13
|
|
|
* |
14
|
|
|
* @author mwjames |
15
|
|
|
*/ |
16
|
|
|
class StatsFormatterTest extends \PHPUnit_Framework_TestCase { |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @dataProvider statsProvider |
20
|
|
|
*/ |
21
|
|
|
public function testGetStatsFromFlatKey( $stats, $expected ) { |
22
|
|
|
|
23
|
|
|
$this->assertEquals( |
24
|
|
|
$expected, |
25
|
|
|
StatsFormatter::getStatsFromFlatKey( $stats ) |
26
|
|
|
); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @dataProvider formatProvider |
31
|
|
|
*/ |
32
|
|
|
public function testFormat( $stats, $format, $expected ) { |
33
|
|
|
|
34
|
|
|
$this->assertInternalType( |
35
|
|
|
$expected, |
36
|
|
|
StatsFormatter::format( $stats, $format ) |
37
|
|
|
); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function formatProvider() { |
41
|
|
|
|
42
|
|
|
$provider[] = array( |
|
|
|
|
43
|
|
|
array( 'Foo' => 1, 'Bar' => 1 ), |
44
|
|
|
StatsFormatter::FORMAT_PLAIN, |
45
|
|
|
'string' |
46
|
|
|
); |
47
|
|
|
|
48
|
|
|
$provider[] = array( |
49
|
|
|
array( 'Foo' => 1, 'Bar' => 1 ), |
50
|
|
|
StatsFormatter::FORMAT_HTML, |
51
|
|
|
'string' |
52
|
|
|
); |
53
|
|
|
|
54
|
|
|
$provider[] = array( |
55
|
|
|
array( 'Foo' => 1, 'Bar' => 1 ), |
56
|
|
|
StatsFormatter::FORMAT_JSON, |
57
|
|
|
'string' |
58
|
|
|
); |
59
|
|
|
|
60
|
|
|
$provider[] = array( |
61
|
|
|
array( 'Foo' => 1, 'Bar' => 1 ), |
62
|
|
|
null, |
63
|
|
|
'array' |
64
|
|
|
); |
65
|
|
|
|
66
|
|
|
return $provider; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function statsProvider() { |
70
|
|
|
|
71
|
|
|
$provider[] = array( |
|
|
|
|
72
|
|
|
array( 'Foo' => 1, 'Bar' => 1 ), |
73
|
|
|
array( |
74
|
|
|
'Foo' => 1, |
75
|
|
|
'Bar' => 1 |
76
|
|
|
) |
77
|
|
|
); |
78
|
|
|
|
79
|
|
|
$provider[] = array( |
80
|
|
|
array( 'Foo.foobar' => 1, 'Bar' => 1 ), |
81
|
|
|
array( |
82
|
|
|
'Foo' => array( 'foobar' => 1 ), |
83
|
|
|
'Bar' => 1 |
84
|
|
|
) |
85
|
|
|
); |
86
|
|
|
|
87
|
|
|
$provider[] = array( |
88
|
|
|
array( 'Foo.foobar' => 5, 'Bar' => 1, 'Foo.foobar.baz' => 1 ), |
89
|
|
|
array( |
90
|
|
|
'Foo' => array( 'foobar' => array( 5, 'baz' => 1 ) ), |
91
|
|
|
'Bar' => 1 |
92
|
|
|
) |
93
|
|
|
); |
94
|
|
|
|
95
|
|
|
return $provider; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
} |
99
|
|
|
|
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:
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 thebar
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.