Completed
Push — master ( 7aacb2...2b39b6 )
by
unknown
06:55
created

ComponentLibraryTest::testUnknownComponentClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace BootstrapComponents\Tests\Unit;
4
5
use BootstrapComponents\ComponentLibrary;
6
use \PHPUnit_Framework_TestCase;
7
8
/**
9
 * @covers  \BootstrapComponents\ComponentLibrary
10
 *
11
 * @ingroup Test
12
 *
13
 * @group   extension-bootstrap-components
14
 * @group   mediawiki-databaseless
15
 *
16
 * @license GNU GPL v3+
17
 *
18
 * @since   1.0
19
 * @author  Tobias Oetterer
20
 */
21
class ComponentLibraryTest extends PHPUnit_Framework_TestCase {
22
23
	public function testCanConstruct() {
24
25
		$this->assertInstanceOf(
26
			ComponentLibrary::class,
27
			new ComponentLibrary()
28
		);
29
	}
30
31
	/**
32
	 * @param string $componentName
33
	 * @param string $expectedParserHookString
34
	 *
35
	 * @dataProvider compileParserHookStringProvider
36
	 */
37
	public function testCanCompileParserHookStringFor( $componentName, $expectedParserHookString ) {
38
		$this->assertEquals(
39
			$expectedParserHookString,
40
			ComponentLibrary::compileParserHookStringFor( $componentName )
41
		);
42
	}
43
44
	public function testCanCompileMagicWordsArray() {
45
		$instance = new ComponentLibrary();
46
		$this->assertEquals(
47
			[
48
				'bootstrap_badge'    => [ 0, 'bootstrap_badge' ],
49
				'bootstrap_button'   => [ 0, 'bootstrap_button' ],
50
				'bootstrap_carousel' => [ 0, 'bootstrap_carousel' ],
51
				'bootstrap_label'    => [ 0, 'bootstrap_label' ],
52
				'bootstrap_tooltip'  => [ 0, 'bootstrap_tooltip' ],
53
			],
54
			$instance->compileMagicWordsArray()
55
		);
56
	}
57
58
	/**
59
	 * @param string $componentName
60
	 *
61
	 * @throws \ConfigException
62
	 *
63
	 * @dataProvider componentNameAndClassProvider
64
	 */
65
	public function testIsRegistered( $componentName ) {
66
		$instance = new ComponentLibrary();
67
		$this->assertEquals(
68
			true,
69
			$instance->isRegistered( $componentName )
70
		);
71
	}
72
73
	/**
74
	 * @param string   $component
75
	 * @param string[] $expectedAliases
76
	 *
77
	 * @throws \ConfigException
78
	 * @throws \MWException
79
	 *
80
	 * @dataProvider componentAliasesProvider
81
	 */
82
	public function testGetAliasesFor( $component, $expectedAliases ) {
83
		$instance = new ComponentLibrary();
84
		$this->assertEquals(
85
			$expectedAliases,
86
			$instance->getAliasesFor( $component )
87
		);
88
	}
89
90
	/**
91
	 * @param string   $component
92
	 * @param string[] $expectedAttributes
93
	 *
94
	 * @throws \ConfigException
95
	 * @throws \MWException
96
	 *
97
	 * @dataProvider componentAttributesProvider
98
	 */
99
	public function testGetAttributesFor( $component, $expectedAttributes ) {
100
		$instance = new ComponentLibrary();
101
		$this->assertEquals(
102
			$expectedAttributes,
103
			$instance->getAttributesFor( $component )
104
		);
105
	}
106
107
	/**
108
	 * @param string $componentName
109
	 * @param string $componentClass
110
	 *
111
	 * @throws \ConfigException
112
	 *
113
	 * @dataProvider componentNameAndClassProvider
114
	 */
115
	public function testGetClassFor( $componentName, $componentClass ) {
116
		$instance = new ComponentLibrary();
117
		$this->assertEquals(
118
			$componentClass,
119
			$instance->getClassFor( $componentName )
120
		);
121
	}
122
123
	/**
124
	 * @throws \ConfigException
125
	 */
126
	public function testGetAllRegisteredComponents() {
127
		$instance = new ComponentLibrary();
128
		$this->assertEquals(
129
			array_keys( $this->componentNameAndClassProvider() ),
130
			$instance->getRegisteredComponents()
131
		);
132
	}
133
134
	/**
135
	 * @param string $componentName
136
	 *
137
	 * @throws \ConfigException
138
	 *
139
	 * @dataProvider componentNameAndClassProvider
140
	 */
141
	public function testGetHandlerTypeFor( $componentName ) {
142
		$instance = new ComponentLibrary();
143
144
		$this->assertContains(
145
			$instance->getHandlerTypeFor( $componentName ),
146
			[ ComponentLibrary::HANDLER_TYPE_PARSER_FUNCTION, ComponentLibrary::HANDLER_TYPE_TAG_EXTENSION ]
147
		);
148
	}
149
150
	/**
151
	 * @throws \ConfigException
152
	 */
153
	public function testGetHandlerTypeForUnknownComponent() {
154
		$instance = new ComponentLibrary();
155
156
		$this->assertEquals(
157
			'UNKNOWN',
158
			$instance->getHandlerTypeFor( 'unknown' )
159
		);
160
	}
161
162
	/**
163
	 * @param string $componentName
164
	 * @param bool   $isParserFunction
165
	 *
166
	 * @throws \ConfigException
167
	 *
168
	 * @dataProvider handlerTypeProvider
169
	 */
170
	public function testIsHandlerType( $componentName, $isParserFunction ) {
171
		$instance = new ComponentLibrary();
172
173
		$this->assertTrue(
174
			!$isParserFunction xor $instance->isParserFunction( $componentName )
175
		);
176
		$this->assertTrue(
177
			$isParserFunction xor $instance->isTagExtension( $componentName )
178
		);
179
	}
180
181
	/**
182
	 * @param string $componentName
183
	 * @param string $skinName
184
	 * @param array  $expectedModules
185
	 *
186
	 * @throws \ConfigException
187
	 *
188
	 * @dataProvider modulesForComponentsProvider
189
	 */
190
	public function testGetModulesFor( $componentName, $skinName, $expectedModules ) {
191
		$instance = new ComponentLibrary();
192
		$this->assertEquals(
193
			$expectedModules,
194
			$instance->getModulesFor( $componentName, $skinName )
195
		);
196
	}
197
198
	/**
199
	 * @param string $componentName
200
	 * @param string $componentClass
201
	 *
202
	 * @throws \ConfigException
203
	 * @throws \MWException
204
	 *
205
	 * @dataProvider componentNameAndClassProvider
206
	 */
207
	public function testGetNameFor( $componentName, $componentClass ) {
208
		$instance = new ComponentLibrary();
209
		$this->assertEquals(
210
			$componentName,
211
			$instance->getNameFor( $componentClass )
212
		);
213
	}
214
215
	/**
216
	 * @param bool|string[] $whiteList
217
	 * @param string[]      $expectedComponents
218
	 *
219
	 * @throws \ConfigException
220
	 *
221
	 * @dataProvider whiteListProvider
222
	 */
223
	public function testSetWhiteList( $whiteList, $expectedComponents ) {
224
		$instance = new ComponentLibrary( $whiteList );
225
		$this->assertEquals(
226
			$expectedComponents,
227
			$instance->getRegisteredComponents()
228
		);
229
	}
230
231
	/**
232
	 * @param string $method
233
	 *
234
	 * @throws \ConfigException
235
	 *
236
	 * @expectedException \MWException
237
	 *
238
	 * @dataProvider exceptionThrowingMethodsProvider
239
	 */
240
	public function testFails( $method ) {
241
		$instance = new ComponentLibrary();
242
243
		$this->expectException( 'MWException' );
244
245
		call_user_func_array( [ $instance, $method ], [ null ] );
246
	}
247
248
	/**
249
	 * @throws \ConfigException
250
	 */
251
	public function testRegisterVsKnown() {
252
		$instance = new ComponentLibrary( [ 'alert', 'modal', 'panel' ] );
253
		$this->assertEquals(
254
			[ 'alert', 'modal', 'panel', ],
255
			$instance->getRegisteredComponents()
256
		);
257
		$this->assertEquals(
258
			ComponentLibrary::HANDLER_TYPE_TAG_EXTENSION,
259
			$instance->getHandlerTypeFor( 'well' )
260
		);
261
		foreach ( $this->modulesForComponentsProvider() as $args ) {
262
			[ $component, $skin, $expectedModules ] = $args;
0 ignored issues
show
Bug introduced by
The variable $component does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $skin does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $expectedModules does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
263
			$this->assertEquals(
264
				$expectedModules,
265
				$instance->getModulesFor( $component, $skin )
266
			);
267
		}
268
		$this->assertFalse(
269
			$instance->isRegistered( 'well' )
270
		);
271
	}
272
273
	/**
274
	 * @throws \ConfigException
275
	 *
276
	 * @expectedException \MWException
277
	 */
278
	public function testUnknownComponentName() {
279
		$instance = new ComponentLibrary( true );
280
281
		$this->expectException( 'MWException' );
282
		$instance->getClassFor( 'foobar' );
283
	}
284
285
	/**
286
	 * @throws \ConfigException
287
	 *
288
	 * @expectedException \MWException
289
	 */
290
	public function testUnknownComponentClass() {
291
		$instance = new ComponentLibrary( true );
292
293
		$this->expectException( 'MWException' );
294
		$instance->getNameFor( '\BootstrapComponents\Components\Foobar' );
295
	}
296
297
	/**
298
	 * @return array
299
	 */
300
	public function compileParserHookStringProvider() {
301
		return [
302
			'accordion' => [ 'accordion', 'bootstrap_accordion' ],
303
			'alert'     => [ 'alert', 'bootstrap_alert' ],
304
			'badge'     => [ 'badge', 'bootstrap_badge' ],
305
			'button'    => [ 'button', 'bootstrap_button' ],
306
			'carousel'  => [ 'carousel', 'bootstrap_carousel' ],
307
			'card'      => [ 'card', 'bootstrap_card' ],
308
			'collapse'  => [ 'collapse', 'bootstrap_collapse' ],
309
			'jumbotron' => [ 'jumbotron', 'bootstrap_jumbotron' ],
310
			'label'     => [ 'label', 'bootstrap_label' ],
311
			'modal'     => [ 'modal', 'bootstrap_modal' ],
312
			'panel'     => [ 'panel', 'bootstrap_panel' ],
313
			'popover'   => [ 'popover', 'bootstrap_popover' ],
314
			'tooltip'   => [ 'tooltip', 'bootstrap_tooltip' ],
315
			'well'      => [ 'well', 'bootstrap_well' ],
316
		];
317
	}
318
319
	/**
320
	 * @return array[]
321
	 */
322
	public function componentNameAndClassProvider() {
323
		return [
324
			'accordion' => [ 'accordion', 'BootstrapComponents\\Components\\Accordion' ],
325
			'alert'     => [ 'alert', 'BootstrapComponents\\Components\\Alert' ],
326
			'badge'     => [ 'badge', 'BootstrapComponents\\Components\\Badge' ],
327
			'button'    => [ 'button', 'BootstrapComponents\\Components\\Button' ],
328
			'card'      => [ 'card', 'BootstrapComponents\\Components\\Card' ],
329
			'carousel'  => [ 'carousel', 'BootstrapComponents\\Components\\Carousel' ],
330
			'collapse'  => [ 'collapse', 'BootstrapComponents\\Components\\Collapse' ],
331
			'jumbotron' => [ 'jumbotron', 'BootstrapComponents\\Components\\Jumbotron' ],
332
			'modal'     => [ 'modal', 'BootstrapComponents\\Components\\Modal' ],
333
			'popover'   => [ 'popover', 'BootstrapComponents\\Components\\Popover' ],
334
			'tooltip'   => [ 'tooltip', 'BootstrapComponents\\Components\\Tooltip' ],
335
			'label'     => [ 'badge', 'BootstrapComponents\\Components\\Badge' ],
336
			'panel'     => [ 'card', 'BootstrapComponents\\Components\\Card' ],
337
			'well'      => [ 'card', 'BootstrapComponents\\Components\\Card' ],
338
		];
339
	}
340
341
	/**
342
	 * @return array
343
	 */
344
	public function componentAliasesProvider() {
345
		return [
346
			'alert' => [ 'alert', [] ],
347
			'button' => [ 'button', [] ],
348
			'card' => [ 'card', [ 'footing' => 'footer', 'heading' => 'header', 'title' => 'header', ], ],
349
			'panel' => [ 'panel', [ 'footing' => 'footer', 'heading' => 'header', 'title' => 'header', ], ],
350
		];
351
	}
352
353
	/**
354
	 * @return array
355
	 */
356
	public function componentAttributesProvider() {
357
		return [
358
			'accordion' => [ 'accordion', [ 'class', 'id', 'style' ] ],
359
			'alert'     => [ 'alert', [ 'color', 'dismissible', 'class', 'id', 'style' ] ],
360
			'modal'     => [ 'modal', [ 'color', 'footer', 'header', 'size', 'text', 'class', 'id', 'style' ] ],
361
		];
362
	}
363
364
	/**
365
	 * @return array[]
366
	 */
367
	public function exceptionThrowingMethodsProvider() {
368
		return [
369
			'getAttributesFor' => [ 'getAttributesFor' ],
370
			'getClassFor'      => [ 'getClassFor' ],
371
			'getNameFor'       => [ 'getNameFor' ],
372
		];
373
	}
374
375
	/**
376
	 * @return array
377
	 */
378
	public function handlerTypeProvider() {
379
		return [
380
			'accordion' => [ 'accordion', false ],
381
			'panel'     => [ 'panel', false ],
382
			'popover'   => [ 'popover', false ],
383
			'button'    => [ 'button', true ],
384
			'tooltip'   => [ 'tooltip', true ],
385
		];
386
	}
387
388
	/**
389
	 * @return array[]
390
	 */
391
	public function modulesForComponentsProvider() {
392
		return [
393
			'button'          => [
394
				'button',
395
				null,
396
				[ 'ext.bootstrapComponents.button.fix' ],
397
			],
398
			'button_vector'   => [
399
				'button',
400
				'vector',
401
				[ 'ext.bootstrapComponents.button.fix' ],
402
			],
403
			'carousel'        => [
404
				'carousel',
405
				null,
406
				[ 'ext.bootstrapComponents.carousel' ],
407
			],
408
			'carousel_vector' => [
409
				'carousel',
410
				'vector',
411
				[ 'ext.bootstrapComponents.carousel' ],
412
			],
413
			'modal'           => [
414
				'modal',
415
				null,
416
				[ 'ext.bootstrapComponents.button.fix', 'ext.bootstrapComponents.modal.fix' ],
417
			],
418
			'modal_vector'    => [
419
				'modal',
420
				'vector',
421
				[ 'ext.bootstrapComponents.button.fix', 'ext.bootstrapComponents.modal.fix', 'ext.bootstrapComponents.modal.vector-fix' ],
422
			],
423
			'popover'         => [
424
				'popover',
425
				null,
426
				[ 'ext.bootstrapComponents.button.fix', 'ext.bootstrapComponents.popover' ],
427
			],
428
			'popover_vector'  => [
429
				'popover',
430
				'vector',
431
				[ 'ext.bootstrapComponents.button.fix', 'ext.bootstrapComponents.popover', 'ext.bootstrapComponents.popover.vector-fix', ],
432
			],
433
			'tooltip'         => [
434
				'tooltip',
435
				null,
436
				[ 'ext.bootstrapComponents.tooltip' ],
437
			],
438
			'tooltip_vector'  => [
439
				'tooltip',
440
				'vector',
441
				[ 'ext.bootstrapComponents.tooltip' ],
442
			],
443
		];
444
	}
445
446
	/**
447
	 * @return array
448
	 */
449
	public function whiteListProvider() {
450
		return [
451
			'true'     => [
452
				true, array_keys( $this->componentNameAndClassProvider() ),
453
			],
454
			'false'    => [
455
				false, [],
456
			],
457
			'normal' => [
458
				[ 'alert', 'card', 'modal' ],
459
				[ 'alert', 'card', 'modal', ],
460
			],
461
			'alias w/ corresponding component' => [
462
				[ 'alert', 'card', 'modal', 'panel' ],
463
				[ 'alert', 'card', 'modal', 'panel' ],
464
			],
465
			'alias w/o corresponding component' => [
466
				[ 'alert', 'modal', 'panel' ],
467
				[ 'alert', 'modal', 'panel', ],
468
			],
469
			'manual 2' => [
470
				[ 'collapse', 'jumbotron', 'well', 'foobar' ],
471
				[ 'collapse', 'jumbotron', 'well' ],
472
			],
473
		];
474
	}
475
}
476