1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tests\Unit\SubPageList\Lister; |
4
|
|
|
|
5
|
|
|
use PHPUnit\Framework\TestCase; |
6
|
|
|
use SubPageList\Lister\Page; |
7
|
|
|
use SubPageList\Lister\PageHierarchyCreator; |
8
|
|
|
use Title; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @covers \SubPageList\Lister\PageHierarchyCreator |
12
|
|
|
* |
13
|
|
|
* @group SubPageList |
14
|
|
|
* |
15
|
|
|
* @licence GNU GPL v2+ |
16
|
|
|
* @author Jeroen De Dauw < [email protected] > |
17
|
|
|
*/ |
18
|
|
|
class PageHierarchyCreatorTest extends TestCase { |
19
|
|
|
|
20
|
|
|
public function testCanConstruct() { |
21
|
|
|
$this->newPageHierarchyCreator(); |
22
|
|
|
$this->assertTrue( true ); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
private function newPageHierarchyCreator() { |
26
|
|
|
$factory = $this->createMock( 'SubPageList\TitleFactory' ); |
27
|
|
|
|
28
|
|
|
$titleBuilder = [ $this, 'newMockTitle' ]; |
29
|
|
|
|
30
|
|
|
$factory->expects( $this->any() ) |
31
|
|
|
->method( 'newFromText' ) |
32
|
|
|
->will( $this->returnCallback( function( $titleText ) use ( $titleBuilder ) { |
33
|
|
|
return call_user_func( $titleBuilder, $titleText ); |
34
|
|
|
} ) ); |
35
|
|
|
|
36
|
|
|
return new PageHierarchyCreator( $factory ); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function testEmptyListResultsInEmptyList() { |
40
|
|
|
$hierarchyCreator = $this->newPageHierarchyCreator(); |
41
|
|
|
$hierarchy = $hierarchyCreator->createHierarchy( [] ); |
42
|
|
|
|
43
|
|
|
$this->assertInternalType( 'array', $hierarchy ); |
44
|
|
|
$this->assertEmpty( $hierarchy ); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function testCanOnlyPassInTitleObjects() { |
48
|
|
|
$hierarchyCreator = $this->newPageHierarchyCreator(); |
49
|
|
|
|
50
|
|
|
$this->expectException( 'InvalidArgumentException' ); |
51
|
|
|
|
52
|
|
|
$hierarchyCreator->createHierarchy( [ |
53
|
|
|
$this->newMockTitle( 'SomePage' ), |
54
|
|
|
42, |
55
|
|
|
$this->newMockTitle( 'OtherPage' ) |
56
|
|
|
] ); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function newMockTitle( $pageName ) { |
60
|
|
|
$title = $this->createMock( 'Title' ); |
61
|
|
|
|
62
|
|
|
$title->expects( $this->any() ) |
63
|
|
|
->method( 'getFullText' ) |
64
|
|
|
->will( $this->returnValue( $pageName ) ); |
65
|
|
|
|
66
|
|
|
return $title; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function testListWithOneTitleResultsInOnePage() { |
70
|
|
|
$title = $this->newMockTitle( 'SomePage' ); |
71
|
|
|
|
72
|
|
|
$hierarchyCreator = $this->newPageHierarchyCreator(); |
73
|
|
|
$hierarchy = $hierarchyCreator->createHierarchy( [ $title ] ); |
74
|
|
|
|
75
|
|
|
$this->assertPageCount( 1, $hierarchy ); |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @var Page $page |
79
|
|
|
*/ |
80
|
|
|
$page = reset( $hierarchy ); |
81
|
|
|
|
82
|
|
|
$this->assertEquals( $title, $page->getTitle() ); |
83
|
|
|
$this->assertEquals( [], $page->getSubPages() ); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
private function assertPageCount( $expectedCount, $hierarchy ) { |
87
|
|
|
$this->assertInternalType( 'array', $hierarchy ); |
88
|
|
|
$this->assertCount( $expectedCount, $hierarchy ); |
89
|
|
|
$this->assertContainsOnlyInstancesOf( 'SubPageList\Lister\Page', $hierarchy ); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function testMultipleTopLevelTitlesStayOnTopLevel() { |
93
|
|
|
$titles = [ |
94
|
|
|
$this->newMockTitle( 'SomePage' ), |
95
|
|
|
$this->newMockTitle( 'OtherPage' ), |
96
|
|
|
$this->newMockTitle( 'OhiThere' ), |
97
|
|
|
]; |
98
|
|
|
|
99
|
|
|
$hierarchyCreator = $this->newPageHierarchyCreator(); |
100
|
|
|
$hierarchy = $hierarchyCreator->createHierarchy( $titles ); |
101
|
|
|
|
102
|
|
|
$this->assertTopLevelTitlesEqual( $titles, $hierarchy ); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param Title[] $expectedTitles |
107
|
|
|
* @param Page[] $actualPages |
108
|
|
|
*/ |
109
|
|
|
private function assertTopLevelTitlesEqual( array $expectedTitles, array $actualPages ) { |
110
|
|
|
$actualTitles = []; |
111
|
|
|
|
112
|
|
|
foreach ( $actualPages as $actualPage ) { |
113
|
|
|
$actualTitles[] = $actualPage->getTitle(); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
$this->assertEquals( $expectedTitles, $actualTitles ); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function testPageAndSubPageResultInPageWithChild() { |
120
|
|
|
$topLevelPage = $this->newMockTitle( 'SomePage' ); |
121
|
|
|
$childPage = $this->newMockTitle( 'SomePage/ChildPage' ); |
122
|
|
|
|
123
|
|
|
$hierarchyCreator = $this->newPageHierarchyCreator(); |
124
|
|
|
|
125
|
|
|
$hierarchy = $hierarchyCreator->createHierarchy( [ $topLevelPage, $childPage ] ); |
126
|
|
|
|
127
|
|
|
$this->assertHasParentAndChild( $topLevelPage, $childPage, $hierarchy ); |
128
|
|
|
|
129
|
|
|
$hierarchy = $hierarchyCreator->createHierarchy( [ $childPage, $topLevelPage ] ); |
130
|
|
|
|
131
|
|
|
$this->assertHasParentAndChild( $topLevelPage, $childPage, $hierarchy ); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
private function assertHasParentAndChild( $topLevelPage, $childPage, array $hierarchy ) { |
135
|
|
|
$this->assertPageCount( 1, $hierarchy ); |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @var Page $page |
139
|
|
|
*/ |
140
|
|
|
$page = reset( $hierarchy ); |
141
|
|
|
|
142
|
|
|
$this->assertEquals( $topLevelPage, $page->getTitle() ); |
143
|
|
|
$this->assertEquals( [ new Page( $childPage ) ], $page->getSubPages() ); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
public function testInBetweenPagesAreCreated() { |
147
|
|
|
$topLevelPage = $this->newMockTitle( 'SomePage' ); |
148
|
|
|
$grandGrandChildPage = $this->newMockTitle( 'SomePage/ChildPage/GrandChildPage/HipsterPage' ); |
149
|
|
|
|
150
|
|
|
$hierarchyCreator = $this->newPageHierarchyCreator(); |
151
|
|
|
$hierarchy = $hierarchyCreator->createHierarchy( [ $topLevelPage, $grandGrandChildPage ] ); |
152
|
|
|
|
153
|
|
|
$this->assertEquals( |
154
|
|
|
[ |
155
|
|
|
new Page( |
156
|
|
|
$this->newMockTitle( 'SomePage' ), |
157
|
|
|
[ |
158
|
|
|
new Page( |
159
|
|
|
$this->newMockTitle( 'SomePage/ChildPage' ), |
160
|
|
|
[ |
161
|
|
|
new Page( |
162
|
|
|
$this->newMockTitle( 'SomePage/ChildPage/GrandChildPage' ), |
163
|
|
|
[ |
164
|
|
|
new Page( |
165
|
|
|
$this->newMockTitle( 'SomePage/ChildPage/GrandChildPage/HipsterPage' ) |
166
|
|
|
) |
167
|
|
|
] |
168
|
|
|
) |
169
|
|
|
] |
170
|
|
|
) |
171
|
|
|
] |
172
|
|
|
) |
173
|
|
|
], |
174
|
|
|
$hierarchy |
175
|
|
|
); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
public function testTopLevelPageGetsCreatedForSubPage() { |
179
|
|
|
$grandChildPage = $this->newMockTitle( 'SomePage/ChildPage/GrandChildPage' ); |
180
|
|
|
|
181
|
|
|
$hierarchyCreator = $this->newPageHierarchyCreator(); |
182
|
|
|
$hierarchy = $hierarchyCreator->createHierarchy( [ $grandChildPage ] ); |
183
|
|
|
|
184
|
|
|
$this->assertEquals( |
185
|
|
|
[ |
186
|
|
|
new Page( |
187
|
|
|
$this->newMockTitle( 'SomePage' ), |
188
|
|
|
[ |
189
|
|
|
new Page( |
190
|
|
|
$this->newMockTitle( 'SomePage/ChildPage' ), |
191
|
|
|
[ |
192
|
|
|
new Page( |
193
|
|
|
$this->newMockTitle( 'SomePage/ChildPage/GrandChildPage' ) |
194
|
|
|
) |
195
|
|
|
] |
196
|
|
|
) |
197
|
|
|
] |
198
|
|
|
) |
199
|
|
|
], |
200
|
|
|
$hierarchy |
201
|
|
|
); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
public function testMultipleChildrenForSameTopLevelPage() { |
205
|
|
|
$pages[] = $this->newMockTitle( 'SomePage/Child0/GrandChild00' ); |
|
|
|
|
206
|
|
|
|
207
|
|
|
$pages[] = $this->newMockTitle( 'SomePage/Child1/GrandChild10' ); |
208
|
|
|
$pages[] = $this->newMockTitle( 'SomePage/Child1/GrandChild11' ); |
209
|
|
|
|
210
|
|
|
$pages[] = $this->newMockTitle( 'SomePage/Child2/GrandChild20' ); |
211
|
|
|
$pages[] = $this->newMockTitle( 'SomePage/Child2' ); |
212
|
|
|
$pages[] = $this->newMockTitle( 'SomePage/Child2/GrandChild21' ); |
213
|
|
|
|
214
|
|
|
$hierarchyCreator = $this->newPageHierarchyCreator(); |
215
|
|
|
$hierarchy = $hierarchyCreator->createHierarchy( $pages ); |
216
|
|
|
|
217
|
|
|
$this->assertEquals( |
218
|
|
|
[ |
219
|
|
|
new Page( |
220
|
|
|
$this->newMockTitle( 'SomePage' ), |
221
|
|
|
[ |
222
|
|
|
new Page( |
223
|
|
|
$this->newMockTitle( 'SomePage/Child0' ), |
224
|
|
|
[ |
225
|
|
|
new Page( |
226
|
|
|
$this->newMockTitle( 'SomePage/Child0/GrandChild00' ) |
227
|
|
|
) |
228
|
|
|
] |
229
|
|
|
), |
230
|
|
|
new Page( |
231
|
|
|
$this->newMockTitle( 'SomePage/Child1' ), |
232
|
|
|
[ |
233
|
|
|
new Page( |
234
|
|
|
$this->newMockTitle( 'SomePage/Child0/GrandChild10' ) |
235
|
|
|
), |
236
|
|
|
new Page( |
237
|
|
|
$this->newMockTitle( 'SomePage/Child0/GrandChild11' ) |
238
|
|
|
) |
239
|
|
|
] |
240
|
|
|
), |
241
|
|
|
new Page( |
242
|
|
|
$this->newMockTitle( 'SomePage/Child2' ), |
243
|
|
|
[ |
244
|
|
|
new Page( |
245
|
|
|
$this->newMockTitle( 'SomePage/Child0/GrandChild20' ) |
246
|
|
|
), |
247
|
|
|
new Page( |
248
|
|
|
$this->newMockTitle( 'SomePage/Child0/GrandChild21' ) |
249
|
|
|
) |
250
|
|
|
] |
251
|
|
|
) |
252
|
|
|
] |
253
|
|
|
) |
254
|
|
|
], |
255
|
|
|
$hierarchy |
256
|
|
|
); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
} |
260
|
|
|
|
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.