Completed
Push — master ( 552173...c8702c )
by Tobias
12:00 queued 43s
created

ModalTest   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

Changes 0
Metric Value
wmc 3
lcom 2
cbo 2
dl 0
loc 104
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A testCanConstruct() 0 11 1
B testCanRender() 0 33 1
B placeMeArgumentsProvider() 0 38 1
1
<?php
2
3
namespace BootstrapComponents\Tests\Unit\Components;
4
5
use BootstrapComponents\Components\Modal;
6
use BootstrapComponents\Tests\Unit\ComponentsTestBase;
7
use \MWException;
8
9
/**
10
 * @covers  \BootstrapComponents\Components\Modal
11
 *
12
 * @ingroup Test
13
 *
14
 * @group extension-bootstrap-components
15
 * @group mediawiki-databaseless
16
 *
17
 * @license GNU GPL v3+
18
 *
19
 * @since   1.0
20
 * @author  Tobias Oetterer
21
 */
22
class ModalTest extends ComponentsTestBase {
23
24
	private $input = 'Modal test text';
25
26
	/**
27
	 * @throws \MWException
28
	 */
29
	public function testCanConstruct() {
30
31
		$this->assertInstanceOf(
32
			'BootstrapComponents\\Components\\Modal',
33
			new Modal(
34
				$this->getComponentLibrary(),
35
				$this->getParserOutputHelper(),
36
				$this->getNestingController()
37
			)
38
		);
39
	}
40
41
	/**
42
	 * @param string $input
43
	 * @param array  $arguments
44
	 * @param string $expectedTriggerOutput
45
	 * @param string $expectedModalOutput
46
	 *
47
	 * @dataProvider placeMeArgumentsProvider
48
	 * @throws MWException
49
	 */
50
	public function testCanRender( $input, $arguments, $expectedTriggerOutput, $expectedModalOutput ) {
51
52
		$modalInjection = '';
53
		$parserOutputHelper = $this->getMockBuilder( 'BootstrapComponents\\ParserOutputHelper' )
54
			->disableOriginalConstructor()
55
			->getMock();
56
		$parserOutputHelper->expects( $this->any() )
57
			->method( 'injectLater' )
58
			->will( $this->returnCallback( function( $text ) use ( &$modalInjection ) {
59
				$modalInjection .= $text;
60
			} ) );
61
		$parserOutputHelper->expects( $this->any() )
62
			->method( 'renderErrorMessage' )
63
			->will( $this->returnArgument( 0 ) );
64
65
		/** @noinspection PhpParamsInspection */
66
		$instance = new Modal(
67
			$this->getComponentLibrary(),
68
			$parserOutputHelper,
69
			$this->getNestingController()
70
		);
71
72
		$parserRequest = $this->buildParserRequest( $input, $arguments );
73
74
		/** @noinspection PhpParamsInspection */
75
		$generatedOutput = $instance->parseComponent( $parserRequest );
76
77
		$this->assertEquals( $expectedTriggerOutput, $generatedOutput );
78
		$this->assertEquals(
79
			$expectedModalOutput,
80
			$modalInjection
81
		);
82
	}
83
84
	/**
85
	 * @return array
86
	 */
87
	public function placeMeArgumentsProvider() {
88
		return [
89
			'simple'              => [
90
				$this->input,
91
				[ 'text' => 'BUTTON' ],
92
				'<button type="button" class="modal-trigger btn btn-default" data-toggle="modal" data-target="#bsc_modal_NULL">BUTTON</button>',
93
				'<div class="modal fade" role="dialog" id="bsc_modal_NULL" aria-hidden="true"><div class="modal-dialog"><div class="modal-content"><div class="modal-header"><button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button></div>'
94
				. '<div class="modal-body">' . $this->input . '</div><div class="modal-footer"><button type="button" class="btn btn-default" data-dismiss="modal" aria-label="Close">Close</button></div></div></div></div>' . "\n",
95
			],
96
			'text missing'        => [
97
				$this->input,
98
				[ 'text' => '' ],
99
				'bootstrap-components-modal-text-missing',
100
				''
101
			],
102
			'image, size invalid' => [
103
				$this->input,
104
				[
105
					'text' => 'before<a href="/File:Serenity.png" class="image"><img alt="Serenity" src="/images/a/aa/Serenity.png" width="160" height="42"></a>after',
106
					'size' => 'none',
107
				],
108
				'<span class="modal-trigger" data-toggle="modal" data-target="#bsc_modal_NULL">before<img alt="Serenity" src="/images/a/aa/Serenity.png" width="160" height="42">after</span>',
109
				'<div class="modal fade" role="dialog" id="bsc_modal_NULL" aria-hidden="true"><div class="modal-dialog"><div class="modal-content"><div class="modal-header"><button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button></div>'
110
				. '<div class="modal-body">' . $this->input . '</div><div class="modal-footer"><button type="button" class="btn btn-default" data-dismiss="modal" aria-label="Close">Close</button></div></div></div></div>' . "\n",
111
			],
112
			'all attributes'      => [
113
				$this->input,
114
				[
115
					'text'    => '<a href="/File:Serenity.png" class="image"><img alt="Serenity" src="/images/a/aa/Serenity.png" width="160" height="42"></a>',
116
					'id'      => 'firefly0', 'size' => 'lg', 'class' => 'shiny', 'style' => 'float:right;background-color:black',
117
					'heading' => 'You can\'t take the sky from me!',
118
				],
119
				'<span class="modal-trigger" data-toggle="modal" data-target="#firefly0"><img alt="Serenity" src="/images/a/aa/Serenity.png" width="160" height="42"></span>',
120
				'<div class="modal fade shiny" style="float:right;background-color:black" role="dialog" id="firefly0" aria-hidden="true"><div class="modal-dialog modal-lg"><div class="modal-content"><div class="modal-header"><button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button><span class="modal-title">You can\'t take the sky from me!</span></div>'
121
				. '<div class="modal-body">' . $this->input . '</div><div class="modal-footer"><button type="button" class="btn btn-default" data-dismiss="modal" aria-label="Close">Close</button></div></div></div></div>' . "\n",
122
			],
123
		];
124
	}
125
}
126