1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SBL\Tests; |
4
|
|
|
|
5
|
|
|
use SBL\BySubpageLinksFinder; |
6
|
|
|
use SMW\DIWikiPage; |
7
|
|
|
use Title; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @covers \SBL\BySubpageLinksFinder |
11
|
|
|
* @group semantic-breadcrumb-links |
12
|
|
|
* |
13
|
|
|
* @license GNU GPL v2+ |
14
|
|
|
* @since 1.0 |
15
|
|
|
* |
16
|
|
|
* @author mwjames |
17
|
|
|
*/ |
18
|
|
|
class BySubpageLinksFinderTest extends \PHPUnit_Framework_TestCase { |
19
|
|
|
|
20
|
|
|
public function testCanConstruct() { |
21
|
|
|
|
22
|
|
|
$this->assertInstanceOf( |
23
|
|
|
'\SBL\BySubpageLinksFinder', |
24
|
|
|
new BySubpageLinksFinder() |
25
|
|
|
); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function testDisabledFinder() { |
29
|
|
|
|
30
|
|
|
$instance = new BySubpageLinksFinder(); |
31
|
|
|
$instance->setSubpageDiscoveryFallback( false ); |
32
|
|
|
|
33
|
|
|
$this->assertFalse( |
34
|
|
|
$instance->isDiscoveryFallback() |
35
|
|
|
); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @dataProvider titleProvider |
40
|
|
|
*/ |
41
|
|
|
public function testFindParentBreadcrumbs( $title, $count, $expected ) { |
42
|
|
|
|
43
|
|
|
$subject = DIWikiPage::newFromTitle( Title::newFromText( $title ) ); |
44
|
|
|
|
45
|
|
|
$instance = new BySubpageLinksFinder(); |
46
|
|
|
$instance->setSubpageDiscoveryFallback( true ); |
47
|
|
|
|
48
|
|
|
$this->assertEmpty( |
49
|
|
|
$instance->getParents() |
50
|
|
|
); |
51
|
|
|
|
52
|
|
|
$instance->findLinksBySubject( $subject ); |
53
|
|
|
|
54
|
|
|
$this->assertCount( |
55
|
|
|
$count, |
56
|
|
|
$instance->getParents() |
57
|
|
|
); |
58
|
|
|
|
59
|
|
|
$this->assertEquals( |
60
|
|
|
$expected, |
61
|
|
|
$instance->getParents() |
62
|
|
|
); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function titleProvider() { |
66
|
|
|
|
67
|
|
|
#0 |
68
|
|
|
$provider[] = [ |
|
|
|
|
69
|
|
|
'Foo', |
70
|
|
|
0, |
71
|
|
|
[] |
72
|
|
|
]; |
73
|
|
|
|
74
|
|
|
#1 |
75
|
|
|
$provider[] = [ |
76
|
|
|
'Foo/', |
77
|
|
|
1, |
78
|
|
|
[ |
79
|
|
|
new DIWikiPage( 'Foo', NS_MAIN ) |
80
|
|
|
] |
81
|
|
|
]; |
82
|
|
|
|
83
|
|
|
#2 |
84
|
|
|
$provider[] = [ |
85
|
|
|
'Foo/Bar/Baz', |
86
|
|
|
2, |
87
|
|
|
[ |
88
|
|
|
new DIWikiPage( 'Foo', NS_MAIN ), |
89
|
|
|
new DIWikiPage( 'Foo/Bar', NS_MAIN ) |
90
|
|
|
] |
91
|
|
|
]; |
92
|
|
|
|
93
|
|
|
#3 |
94
|
|
|
$provider[] = [ |
95
|
|
|
'Foo/Bar/Baz/Yin/Yan', |
96
|
|
|
4, |
97
|
|
|
[ |
98
|
|
|
new DIWikiPage( 'Foo', NS_MAIN ), |
99
|
|
|
new DIWikiPage( 'Foo/Bar', NS_MAIN ), |
100
|
|
|
new DIWikiPage( 'Foo/Bar/Baz', NS_MAIN ), |
101
|
|
|
new DIWikiPage( 'Foo/Bar/Baz/Yin', NS_MAIN ) |
102
|
|
|
] |
103
|
|
|
]; |
104
|
|
|
|
105
|
|
|
#4 /a/b |
106
|
|
|
$provider[] = [ |
107
|
|
|
'/a/b', |
108
|
|
|
1, |
109
|
|
|
[ |
110
|
|
|
new DIWikiPage( '/a', NS_MAIN ) |
111
|
|
|
] |
112
|
|
|
]; |
113
|
|
|
|
114
|
|
|
#5 /a//b/c |
115
|
|
|
$provider[] = [ |
116
|
|
|
'/a//b/c', |
117
|
|
|
2, |
118
|
|
|
[ |
119
|
|
|
new DIWikiPage( '/a', NS_MAIN ), |
120
|
|
|
new DIWikiPage( '/a//b', NS_MAIN ) |
121
|
|
|
] |
122
|
|
|
]; |
123
|
|
|
|
124
|
|
|
#6 (#23 issue) |
125
|
|
|
$provider[] = [ |
126
|
|
|
'Foo / Bar', |
127
|
|
|
0, |
128
|
|
|
[] |
129
|
|
|
]; |
130
|
|
|
|
131
|
|
|
#7 (#23 issue) |
132
|
|
|
$provider[] = [ |
133
|
|
|
'Foo /Bar', |
134
|
|
|
0, |
135
|
|
|
[] |
136
|
|
|
]; |
137
|
|
|
|
138
|
|
|
#8 (#23 issue) |
139
|
|
|
$provider[] = [ |
140
|
|
|
'Foo /Bar /Foobar', |
141
|
|
|
0, |
142
|
|
|
[] |
143
|
|
|
]; |
144
|
|
|
|
145
|
|
|
#9 |
146
|
|
|
$provider[] = [ |
147
|
|
|
'Help:Foo/Foobar', |
148
|
|
|
1, |
149
|
|
|
[ |
150
|
|
|
new DIWikiPage( 'Foo', NS_HELP ), |
151
|
|
|
] |
152
|
|
|
]; |
153
|
|
|
|
154
|
|
|
return $provider; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
} |
158
|
|
|
|
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.