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

CardTest::testCanRenderAccordionCard()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 36
rs 9.344
c 0
b 0
f 0
cc 1
nc 1
nop 3
1
<?php
2
3
namespace BootstrapComponents\Tests\Unit\Components;
4
5
use BootstrapComponents\Components\Accordion;
6
use BootstrapComponents\Components\Card;
7
use BootstrapComponents\NestingController;
8
use BootstrapComponents\Tests\Unit\ComponentsTestBase;
9
use \MWException;
10
11
/**
12
 * @covers  \BootstrapComponents\Components\Card
13
 *
14
 * @ingroup Test
15
 *
16
 * @group   extension-bootstrap-components
17
 * @group   mediawiki-databaseless
18
 *
19
 * @license GNU GPL v3+
20
 *
21
 * @since   4.0
22
 * @author  Tobias Oetterer
23
 */
24
class CardTest extends ComponentsTestBase {
25
26
	private $input = 'Card test text';
27
28
	/**
29
	 * @throws \MWException
30
	 */
31
	public function testCanConstruct() {
32
33
		$this->assertInstanceOf(
34
			Card::class,
35
			new Card(
36
				$this->getComponentLibrary(),
37
				$this->getParserOutputHelper(),
38
				$this->getNestingController()
39
			)
40
		);
41
	}
42
43
	/**
44
	 * @param string $input
45
	 * @param array  $arguments
46
	 * @param string $expectedOutput
47
	 *
48
	 * @throws MWException
49
	 *
50
	 * @dataProvider placeMeArgumentsProvider
51
	 */
52
	public function testCanRender( $input, $arguments, $expectedOutput ) {
53
		$instance = new Card(
54
			$this->getComponentLibrary(),
55
			$this->getParserOutputHelper(),
56
			$this->getNestingController()
57
		);
58
59
		$parserRequest = $this->buildParserRequest( $input, $arguments );
60
61
		/** @noinspection PhpParamsInspection */
62
		$generatedOutput = $instance->parseComponent( $parserRequest );
63
64
		$this->assertEquals( $expectedOutput, $generatedOutput );
65
	}
66
67
	/**
68
	 * @param string $input
69
	 * @param array  $arguments
70
	 * @param string $expectedOutput
71
	 *
72
	 * @throws MWException
73
	 *
74
	 * @dataProvider placeMeInsideAccordionArgumentsProvider
75
	 */
76
	public function testCanRenderAccordionCard( $input, $arguments, $expectedOutput ) {
77
		$accordion = $this->getMockBuilder( Accordion::class )
78
			->disableOriginalConstructor()
79
			->getMock();
80
		$accordion->expects( $this->any() )
81
			->method( 'getComponentName' )
82
			->willReturn( 'accordion' );
83
		$accordion->expects( $this->any() )
84
			->method( 'getId' )
85
			->willReturn( 'accordion0' );
86
		$nestingController = $this->getMockBuilder( NestingController::class )
87
			->disableOriginalConstructor()
88
			->getMock();
89
		$nestingController->expects( $this->any() )
90
			->method( 'generateUniqueId' )
91
			->will( $this->returnCallback( function( $componentName ) {
92
				return 'bsc_' . $componentName . '_NULL';
93
			} ) );
94
		$nestingController->expects( $this->any() )
95
			->method( 'getCurrentElement' )
96
			->willReturn( $accordion );
97
98
		/** @noinspection PhpParamsInspection */
99
		$instance = new Card(
100
			$this->getComponentLibrary(),
101
			$this->getParserOutputHelper(),
102
			$nestingController
103
		);
104
105
		$parserRequest = $this->buildParserRequest( $input, $arguments );
106
107
		/** @noinspection PhpParamsInspection */
108
		$generatedOutput = $instance->parseComponent( $parserRequest );
109
110
		$this->assertEquals( $expectedOutput, $generatedOutput );
111
	}
112
113
	/**
114
	 * @return array
115
	 */
116
	public function placeMeArgumentsProvider() {
117
		return [
118
			'simple'            => [
119
				$this->input,
120
				[],
121
				'<div class="card"><div id="bsc_card_NULL"><div class="card-body">' . $this->input . '</div></div></div>',
122
			],
123
			'text missing'      => [
124
				'',
125
				[ 'header' => 'watch this', 'footer' => 'watch what?', 'collapsible' => 'false', ],
126
				'<div class="card"><div class="card-header"><h4 class="card-title" style="margin-top:0;padding-top:0;">watch this</h4></div><div id="bsc_card_NULL"><div class="card-body"></div><div class="card-footer">watch what?</div></div></div>',
127
			],
128
			'heading alias' => [
129
				'',
130
				[ 'heading' => 'watch this', ],
131
				'<div class="card"><div class="card-header"><h4 class="card-title" style="margin-top:0;padding-top:0;">watch this</h4></div><div id="bsc_card_NULL"><div class="card-body"></div></div></div>',
132
			],
133
			'all attributes'    => [
134
				$this->input,
135
				[
136
					'class'       => 'dummy nice',
137
					'style'       => 'float:right;background-color:green',
138
					'id'          => 'badgers_bowler',
139
					'active'      => 'yes',
140
					'color'       => 'info',
141
					'collapsible' => '',
142
					'header'     => 'HEADING TEXT',
143
					'footer'      => 'FOOTER TEXT',
144
				],
145
				'<div class="card border-info dummy nice" style="float:right;background-color:green"><div class="card-header" data-toggle="collapse" href="#badgers_bowler"><h4 class="card-title" style="margin-top:0;padding-top:0;">HEADING TEXT</h4></div><div id="badgers_bowler" class="card-collapse collapse fade in"><div class="card-body text-info">' . $this->input . '</div><div class="card-footer">FOOTER TEXT</div></div></div>',
146
			],
147
			'collapsible false' => [
148
				$this->input,
149
				[ 'collapsible' => 'false', ],
150
				'<div class="card"><div id="bsc_card_NULL"><div class="card-body">' . $this->input . '</div></div></div>',
151
			],
152
			'background not white' => [
153
				$this->input,
154
				[ 'background' => 'danger', ],
155
				'<div class="card bg-danger text-white"><div id="bsc_card_NULL"><div class="card-body">' . $this->input . '</div></div></div>',
156
			],
157
			'background white' => [
158
				$this->input,
159
				[ 'background' => 'light', ],
160
				'<div class="card bg-light"><div id="bsc_card_NULL"><div class="card-body">' . $this->input . '</div></div></div>',
161
			],
162
		];
163
	}
164
165
	/**
166
	 * @return array
167
	 */
168
	public function placeMeInsideAccordionArgumentsProvider() {
169
		return [
170
			'simple'            => [
171
				$this->input,
172
				[],
173
				'<div class="card"><div class="card-header" data-parent="#accordion0" data-toggle="collapse" href="#bsc_card_NULL"><h4 class="card-title" style="margin-top:0;padding-top:0;">bsc_card_NULL</h4></div><div id="bsc_card_NULL" class="card-collapse collapse fade"><div class="card-body">' . $this->input . '</div></div></div>',
174
			],
175
			'text missing'      => [
176
				'',
177
				[ 'header' => 'watch this', 'footer' => 'watch what?', 'collapsible' => 'false', ],
178
				'<div class="card"><div class="card-header" data-parent="#accordion0" data-toggle="collapse" href="#bsc_card_NULL"><h4 class="card-title" style="margin-top:0;padding-top:0;">watch this</h4></div><div id="bsc_card_NULL" class="card-collapse collapse fade"><div class="card-body"></div><div class="card-footer">watch what?</div></div></div>',
179
			],
180
			'all attributes'    => [
181
				$this->input,
182
				[
183
					'class'       => 'dummy nice',
184
					'style'       => 'float:right;background-color:green',
185
					'id'          => 'badgers_bowler',
186
					'active'      => 'yes',
187
					'color'       => 'info',
188
					'collapsible' => '',
189
					'heading'     => 'HEADING TEXT',
190
					'footer'      => 'FOOTER TEXT',
191
				],
192
				'<div class="card border-info dummy nice" style="float:right;background-color:green"><div class="card-header" data-parent="#accordion0" data-toggle="collapse" href="#badgers_bowler"><h4 class="card-title" style="margin-top:0;padding-top:0;">HEADING TEXT</h4></div><div id="badgers_bowler" class="card-collapse collapse fade in"><div class="card-body text-info">' . $this->input . '</div><div class="card-footer">FOOTER TEXT</div></div></div>',
193
			],
194
			'collapsible false' => [
195
				$this->input,
196
				[ 'collapsible' => 'false', ],
197
				'<div class="card"><div class="card-header" data-parent="#accordion0" data-toggle="collapse" href="#bsc_card_NULL"><h4 class="card-title" style="margin-top:0;padding-top:0;">bsc_card_NULL</h4></div><div id="bsc_card_NULL" class="card-collapse collapse fade"><div class="card-body">' . $this->input . '</div></div></div>',
198
			],
199
		];
200
	}
201
}
202