Passed
Pull Request — master (#12)
by John
15:17 queued 04:57
created

Test::testDispatch()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 11
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types = 1);
4
5
use PHPUnit\Framework\TestCase;
6
7
final class Test extends TestCase
8
{
9
	public function buttonProvider(): array
10
	{
11
		$btn = ['href' => 'link', 'show' => true];
12
		$btn1 = ['href' => 'link1', 'show' => true];
13
14
		return [
15
			[
16
				'test',
17
				[
18
					'before' => [
19
						'inserted_test' => $btn,
20
						'test' => $btn,
21
						'test1' => $btn1,
22
					],
23
					'after' => [
24
						'test' => $btn,
25
						'inserted_test' => $btn,
26
						'test1' => $btn1,
27
					],
28
					'child_of' => [
29
						'test' => [
30
							'href' => 'link',
31
							'show' => true,
32
							'sub_buttons' => [
33
								'inserted_test' => $btn,
34
							],
35
						],
36
						'test1' => $btn1,
37
					],
38
				],
39
			],
40
			[
41
				'test1',
42
				[
43
					'before' => [
44
						'test' => $btn,
45
						'inserted_test1' => $btn,
46
						'test1' => $btn1,
47
					],
48
					'after' => [
49
						'test' => $btn,
50
						'test1' => $btn1,
51
						'inserted_test1' => $btn,
52
					],
53
					'child_of' => [
54
						'test' => $btn,
55
						'test1' => [
56
							'href' => 'link1',
57
							'show' => true,
58
							'sub_buttons' => [
59
								'inserted_test1' => $btn,
60
							],
61
						],
62
					],
63
				],
64
			],
65
			[
66
				'dungeon',
67
				[
68
					'before' => [
69
						'test' => $btn,
70
						'test1' => $btn1,
71
					],
72
					'after' => [
73
						'test' => $btn,
74
						'test1' => $btn1,
75
					],
76
					'child_of' => [
77
						'test' => $btn,
78
						'test1' => $btn1,
79
					],
80
				],
81
			],
82
		];
83
	}
84
85
	/**
86
	 * @dataProvider buttonProvider
87
	 */
88
	public function testInsertButton(string $insertion_point, array $expected): void
89
	{
90
		$btn = ['href' => 'link', 'show' => true];
91
		$btn1 = ['href' => 'link1', 'show' => true];
92
93
		foreach (['before', 'after', 'child_of'] as $where)
94
		{
95
			$haystack = ['test' => $btn, 'test1' => $btn1];
96
			recursive_button(
97
				$btn,
98
				$haystack,
99
				$insertion_point,
100
				$where,
101
				'inserted_' . $insertion_point
102
			);
103
			$this->assertSame($expected[$where], $haystack);
104
		}
105
	}
106
107
	public function childButtonProvider(): array
108
	{
109
		$btn = ['href' => 'link', 'show' => true];
110
		return [
111
			[
112
				'sub',
113
				[
114
					'before' => [
115
						'test' => [
116
							'href' => 'link',
117
							'show' => true,
118
							'sub_buttons' => [
119
								'inserted_sub' => $btn,
120
								'sub' => $btn ,
121
								'sub1' => $btn,
122
							],
123
						],
124
					],
125
					'after' => [
126
						'test' => [
127
							'href' => 'link',
128
							'show' => true,
129
							'sub_buttons' => [
130
								'sub' => $btn,
131
								'inserted_sub' => $btn,
132
								'sub1' => $btn,
133
							],
134
						],
135
					],
136
					'child_of' => [
137
						'test' => [
138
							'href' => 'link',
139
							'show' => true,
140
							'sub_buttons' => [
141
								'sub' => [
142
									'href' => 'link',
143
									'show' => true,
144
									'sub_buttons' => [
145
										'inserted_sub' => $btn,
146
									],
147
								],
148
								'sub1' => $btn,
149
							],
150
						],
151
					],
152
				],
153
			],
154
			[
155
				'sub1',
156
				[
157
					'before' => [
158
						'test' => [
159
							'href' => 'link',
160
							'show' => true,
161
							'sub_buttons' => [
162
								'sub' => $btn,
163
								'inserted_sub1' => $btn,
164
								'sub1' => $btn,
165
							],
166
						],
167
					],
168
					'after' => [
169
						'test' => [
170
							'href' => 'link',
171
							'show' => true,
172
							'sub_buttons' => [
173
								'sub' => $btn,
174
								'sub1' => $btn,
175
								'inserted_sub1' => $btn,
176
							],
177
						],
178
					],
179
					'child_of' => [
180
						'test' => [
181
							'href' => 'link',
182
							'show' => true,
183
							'sub_buttons' => [
184
								'sub' => $btn,
185
								'sub1' => [
186
									'href' => 'link',
187
									'show' => true,
188
									'sub_buttons' => [
189
										'inserted_sub1' => $btn,
190
									],
191
								],
192
							],
193
						],
194
					],
195
				],
196
			],
197
			[
198
				'dungeon',
199
				[
200
					'before' => [
201
						'test' => [
202
							'href' => 'link',
203
							'show' => true,
204
							'sub_buttons' => [
205
								'sub' => $btn, 'sub1' => $btn,
206
							],
207
						],
208
					],
209
					'after' => [
210
						'test' => [
211
							'href' => 'link',
212
							'show' => true,
213
							'sub_buttons' => [
214
								'sub' => $btn, 'sub1' => $btn,
215
							],
216
						],
217
					],
218
					'child_of' => [
219
						'test' => [
220
							'href' => 'link',
221
							'show' => true,
222
							'sub_buttons' => [
223
								'sub' => $btn, 'sub1' => $btn,
224
							],
225
						],
226
					],
227
				],
228
			],
229
		];
230
	}
231
232
	/**
233
	 * @dataProvider childButtonProvider
234
	 */
235
	public function testInsertChildButton(string $insertion_point, array $expected): void
236
	{
237
		foreach (['before', 'after', 'child_of'] as $where)
238
		{
239
			$haystack = [
240
				'test' => [
241
					'href' => 'link',
242
					'show' => true,
243
					'sub_buttons' => [
244
						'sub' => [
245
							'href' => 'link',
246
							'show' => true,
247
						],
248
						'sub1' => [
249
							'href' => 'link',
250
							'show' => true,
251
						],
252
					],
253
				],
254
			];
255
			recursive_button(
256
				[
257
					'href' => 'link',
258
					'show' => true,
259
				],
260
				$haystack,
261
				$insertion_point,
262
				$where,
263
				'inserted_' . $insertion_point
264
			);
265
			$this->assertSame($expected[$where], $haystack);
266
		}
267
	}
268
269
	public function testHook(): void
270
	{
271
		global $modSettings;
272
273
		add_integration_function('integrate_menu_buttons', 'um_load_menu');
0 ignored issues
show
Bug introduced by
The function add_integration_function was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

273
		/** @scrutinizer ignore-call */ 
274
  add_integration_function('integrate_menu_buttons', 'um_load_menu');
Loading history...
274
		add_integration_function('integrate_menu_buttons', 'my_func');
275
		$this->assertEquals('um_load_menu,my_func', $modSettings['integrate_menu_buttons']);
276
		$dummy = [];
277
		um_load_menu($dummy);
278
		$this->assertEquals('my_func,um_load_menu', $modSettings['integrate_menu_buttons']);
279
		$dummy = [];
280
		um_load_menu($dummy);
281
		$this->assertEquals('my_func,um_load_menu', $modSettings['integrate_menu_buttons']);
282
	}
283
284
	public function testMenu(): void
285
	{
286
		global $modSettings;
287
288
		$modSettings['um_count'] = 2;
289
		$modSettings['um_button_2'] = '{"name":"Test","type":"forum","target":"_self","position":"before","link":"t","active":true,"groups":[-1,0,2],"parent":"register"}';
290
		$haystack = ['register' => 'l'];
291
		um_load_menu($haystack);
292
		$this->assertCount(2, $haystack);
293
		$this->assertArrayHasKey('um_button_2', $haystack);
294
		$this->assertCount(4, $haystack['um_button_2']);
295
		$this->assertArrayHasKey('title', $haystack['um_button_2']);
296
		$this->assertArrayHasKey('href', $haystack['um_button_2']);
297
		$this->assertEquals('Test', $haystack['um_button_2']['title']);
298
		$this->assertEquals('?t', $haystack['um_button_2']['href']);
299
		unset($modSettings['um_count'], $modSettings['um_button_2']);
300
	}
301
302
	public function testListButtons(): void
303
	{
304
		global $modSettings;
305
306
		$modSettings['um_count'] = 2;
307
		$modSettings['um_button_2'] = '{"name":"Test","type":"forum","target":"_self","position":"before","link":"t","active":true,"groups":[-1,0,2],"parent":"register"}';
308
		$haystack = (new UltimateMenu)->getButtonNames();
309
		$this->assertArrayHasKey('um_button_2', $haystack);
310
		$this->assertCount(2, $haystack['um_button_2']);
311
		$this->assertSame([0, 'Test'], $haystack['um_button_2']);
312
		$this->assertArrayHasKey('help', $haystack);
313
		$this->assertArrayHasKey('admin', $haystack);
314
		$this->assertArrayHasKey('profile', $haystack);
315
		$this->assertArrayHasKey('logout', $haystack);
316
		$this->assertArrayHasKey('register', $haystack);
317
		unset($modSettings['um_count'], $modSettings['um_button_2']);
318
	}
319
320
	public function testIntegration(): void
321
	{
322
		global $context, $modSettings;
323
324
		$modSettings['um_count'] = 2;
325
		$modSettings['um_button_2'] = '{"name":"Test","type":"forum","target":"_self","position":"before","link":"t","active":true,"groups":[0],"parent":"help"}';
326
		setupMenuContext();
0 ignored issues
show
Bug introduced by
The function setupMenuContext was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

326
		/** @scrutinizer ignore-call */ 
327
  setupMenuContext();
Loading history...
327
		$this->assertArrayHasKey('um_button_2', $context['menu_buttons']);
328
		$this->assertArrayHasKey('title', $context['menu_buttons']['um_button_2']);
329
		$this->assertArrayHasKey('href', $context['menu_buttons']['um_button_2']);
330
		$this->assertEquals('Test', $context['menu_buttons']['um_button_2']['title']);
331
		$this->assertEquals('?t', $context['menu_buttons']['um_button_2']['href']);
332
		unset($modSettings['um_count'], $modSettings['um_button_2']);
333
	}
334
335
	public function testDispatch(): void
336
	{
337
		$mock = $this->getMockBuilder('ManageUltimateMenu')
0 ignored issues
show
Deprecated Code introduced by
The function PHPUnit\Framework\MockOb...ckBuilder::setMethods() has been deprecated: https://github.com/sebastianbergmann/phpunit/pull/3687 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

337
		$mock = /** @scrutinizer ignore-deprecated */ $this->getMockBuilder('ManageUltimateMenu')

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
338
			->setMethods(array('ManageMenu'))
339
			->disableOriginalConstructor()
340
			->getMock();
341
342
		$mock->expects($this->once())
343
			 ->method('ManageMenu');
344
345
		$mock->__construct('');
346
	}
347
}
348