PageHierarchyCreatorTest   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 242
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 242
rs 10
c 0
b 0
f 0
wmc 15
lcom 1
cbo 2

14 Methods

Rating   Name   Duplication   Size   Complexity  
A testCanConstruct() 0 4 1
A newPageHierarchyCreator() 0 13 1
A testEmptyListResultsInEmptyList() 0 7 1
A testCanOnlyPassInTitleObjects() 0 11 1
A newMockTitle() 0 9 1
A testListWithOneTitleResultsInOnePage() 0 16 1
A assertPageCount() 0 5 1
A testMultipleTopLevelTitlesStayOnTopLevel() 0 12 1
A assertTopLevelTitlesEqual() 0 9 2
A testPageAndSubPageResultInPageWithChild() 0 14 1
A assertHasParentAndChild() 0 11 1
A testInBetweenPagesAreCreated() 0 31 1
A testTopLevelPageGetsCreatedForSubPage() 0 25 1
A testMultipleChildrenForSameTopLevelPage() 0 54 1
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' );
0 ignored issues
show
Coding Style Comprehensibility introduced by
$pages was never initialized. Although not strictly required by PHP, it is generally a good practice to add $pages = 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...
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