Completed
Branch master (ecb46d)
by Tobias
01:39
created

testCanSuppressImageModals()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 9.2
c 0
b 0
f 0
cc 1
eloc 17
nc 1
nop 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A ParserOutputHelperTest::componentNameAndClassProvider() 0 8 2
1
<?php
2
3
namespace BootstrapComponents\Tests\Unit;
4
5
use BootstrapComponents\ComponentLibrary;
6
use BootstrapComponents\ParserOutputHelper;
7
use \MWException;
8
use \Parser;
9
use \PHPUnit_Framework_MockObject_MockObject;
10
use \PHPUnit_Framework_TestCase;
11
12
/**
13
 * @covers  \BootstrapComponents\ParserOutputHelper
14
 *
15
 * @ingroup Test
16
 *
17
 * @group extension-bootstrap-components
18
 * @group mediawiki-databaseless
19
 *
20
 * @license GNU GPL v3+
21
 *
22
 * @since   1.0
23
 * @author  Tobias Oetterer
24
 */
25
class ParserOutputHelperTest extends PHPUnit_Framework_TestCase {
26
	/**
27
	 * @var Parser
28
	 */
29
	private $parser;
30
31
	public function setUp() {
32
		parent::setUp();
33
34
		$this->parser = $this->getMockBuilder( 'Parser' )
35
			->disableOriginalConstructor()
36
			->getMock();
37
	}
38
39
	public function testCanConstruct() {
40
41
		$this->assertInstanceOf(
42
			'BootstrapComponents\\ParserOutputHelper',
43
			new ParserOutputHelper( $this->parser )
44
		);
45
	}
46
47
	public function testCanAddErrorTrackingCategory() {
48
49
		$parser = $this->getMockBuilder( 'Parser' )
50
			->disableOriginalConstructor()
51
			->getMock();
52
		$parser->expects( $this->once() )
53
			->method( 'getOutput' )
54
			->willReturn( false );
55
56
		/** @noinspection PhpParamsInspection */
57
		$instance = new ParserOutputHelper( $parser );
58
59
		$instance->addErrorTrackingCategory();
60
		$instance->addErrorTrackingCategory();
61
	}
62
63
	/**
64
	 * This is so lame to test. Only reason to do this to up test coverage.
65
	 */
66
	public function testCanAddModules() {
67
		$parserOutput = $this->getMockBuilder( 'ParserOutput' )
68
			->disableOriginalConstructor()
69
			->getMock();
70
		$parserOutput->expects( $this->exactly( 4 ) )
71
			->method( 'addModules' )
72
			->will( $this->returnArgument( 0 ) );
73
		$parser = $this->getMockBuilder( 'Parser' )
74
			->disableOriginalConstructor()
75
			->getMock();
76
		$parser->expects( $this->exactly( 4 ) )
77
			->method( 'getOutput' )
78
			->willReturn( $parserOutput );
79
80
		/** @noinspection PhpParamsInspection */
81
		$instance = new ParserOutputHelper( $parser );
82
83
		$instance->addModules( /** @scrutinizer ignore-type */ null );
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
84
85
		$instance->addModules( [] );
86
87
		/** @noinspection PhpParamsInspection */
88
		$instance->addModules( /** @scrutinizer ignore-type */ 'module0' );
0 ignored issues
show
Documentation introduced by
'module0' is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
89
90
		$instance->addModules( [ 'module1', 'module2' ] );
91
	}
92
93
	public function testCanAddTrackingCategory() {
94
		$parser = $this->getMockBuilder( 'Parser' )
95
			->disableOriginalConstructor()
96
			->getMock();
97
		$parser->expects( $this->once() )
98
			->method( 'getOutput' )
99
			->willReturn( false );
100
101
		/** @noinspection PhpParamsInspection */
102
		$instance = new ParserOutputHelper( $parser );
103
104
		$instance->addTrackingCategory();
105
		$instance->addTrackingCategory();
106
	}
107
108
	/**
109
	 * @param mixed $storedText
110
	 * @param string $expectedReturn
111
	 *
112
	 * @dataProvider contentForLaterInjectionProvider
113
	 */
114
	public function testCanGetContentForLaterInjection( $storedText, $expectedReturn ) {
115
		$instance = new ParserOutputHelper( $this->parser );
116
117
		$instance->injectLater( $storedText );
118
119
		$this->assertEquals(
120
			$expectedReturn,
121
			$instance->getContentForLaterInjection()
122
		);
123
	}
124
125
	public function testCanGetNameOfActiveSkin() {
126
		$instance = new ParserOutputHelper( $this->parser );
127
128
		$this->assertEquals(
129
			'vector',
130
			$instance->getNameOfActiveSkin()
131
		);
132
	}
133
134
	public function testCanLoadBootstrapModules() {
135
		$parserOutput = $this->getMockBuilder( 'ParserOutput' )
136
			->disableOriginalConstructor()
137
			->getMock();
138
		$parserOutput->expects( $this->once() )
139
			->method( 'addModuleStyles' )
140
			->will( $this->returnArgument( 0 ) );
141
		$parserOutput->expects( $this->once() )
142
			->method( 'addModuleScripts' )
143
			->will( $this->returnArgument( 0 ) );
144
		$parserOutput->expects( $this->once() )
145
			->method( 'addModules' )
146
			->will( $this->returnArgument( 0 ) );
147
		$parser = $this->getMockBuilder( 'Parser' )
148
			->disableOriginalConstructor()
149
			->getMock();
150
		$parser->expects( $this->once() )
151
			->method( 'getOutput' )
152
			->willReturn( $parserOutput );
153
154
		/** @noinspection PhpParamsInspection */
155
		$instance = new ParserOutputHelper( $parser );
156
157
		$instance->loadBootstrapModules();
158
	}
159
160
	/**
161
	 * @param string $messageText
162
	 * @param string $renderedMessage
163
	 *
164
	 * @dataProvider errorMessageProvider
165
	 */
166
	public function __testCanRenderErrorMessage( $messageText, $renderedMessage ) {
167
		/** @noinspection PhpParamsInspection */
168
		$instance = new ParserOutputHelper(
169
			$this->buildFullyEquippedParser( ( $renderedMessage != '~^$~' ) )
170
		);
171
172
		$this->assertRegExp(
173
			$renderedMessage,
174
			$instance->renderErrorMessage( $messageText )
175
		);
176
	}
177
178
	public function testVectorSkinInUse() {
179
		$instance = new ParserOutputHelper( $this->parser );
180
		$this->assertInternalType(
181
			'bool',
182
			$instance->vectorSkinInUse()
183
		);
184
	}
185
186
	/**
187
	 * @return array[]
188
	 *
189
	 * @throws \ConfigException
190
	 * @throws MWException
191
	 */
192
	public function componentNameAndClassProvider() {
193
		$cl = new ComponentLibrary();
194
		$provider = [];
195
		foreach ( $cl->getRegisteredComponents() as $componentName ) {
196
			$provider['open ' . $componentName] = [ $componentName, $cl->getClassFor( $componentName ) ];
197
		}
198
		return $provider;
199
	}
200
201
	/**
202
	 * @return array[]
203
	 */
204
	public function errorMessageProvider() {
205
		return [
206
			'null'       => [ null, '~^$~' ],
207
			'false'      => [ false, '~^$~' ],
208
			'none'       => [ '', '~^$~' ],
209
			'empty'      => [ '      ', '~^$~' ],
210
			'word'       => [ '__rndErrorMessageTextNotInMessageFiles', '~^<span class="error">[^_]+__rndErrorMessageTextNotInMessageFiles[^<]+</span>$~' ],
211
			'word space' => [ '  __rndErrorMessageTextNotInMessageFiles  ', '~^<span class="error">[^_]+__rndErrorMessageTextNotInMessageFiles[^<]+</span>$~' ],
212
		];
213
	}
214
215
	/**
216
	 * @return array
217
	 */
218
	public function contentForLaterInjectionProvider() {
219
		return [
220
			'none' => [ '', '' ],
221
			'false' => [ false, '' ],
222
			'null' => [ false, '' ],
223
			'string' => [ 'text', '<!-- injected by Extension:BootstrapComponents -->text<!-- /injected by Extension:BootstrapComponents -->' ],
224
		];
225
	}
226
227
	/**
228
	 * @param bool $expectError
229
	 *
230
	 * @return PHPUnit_Framework_MockObject_MockObject
231
	 */
232
	private function buildFullyEquippedParser( $expectError = true ) {
233
		$outputPropertyReturnString = 'rnd_string';
234
		$parser = $this->getMockBuilder( 'Parser' )
235
			->disableOriginalConstructor()
236
			->getMock();
237
		if ( $expectError ) {
238
			$parserOutput = $this->getMockBuilder( 'ParserOutput' )
239
				->disableOriginalConstructor()
240
				->getMock();
241
			$parserOutput->expects( $this->once() )
242
				->method( 'getProperty' )
243
				->with(
244
					$this->equalTo( 'defaultsort' )
245
				)
246
				->willReturn( $outputPropertyReturnString );
247
			$parserOutput->expects( $this->once() )
248
				->method( 'addCategory' )
249
				->with(
250
					$this->equalTo( 'Pages_with_bootstrap_component_errors' ),
251
					$this->equalTo( $outputPropertyReturnString )
252
				)
253
				->willReturn( $parserOutput );
254
			$parser->expects( $this->once() )
255
				->method( 'getOutput' )
256
				->willReturn( $parserOutput );
257
		}
258
259
		return $parser;
260
	}
261
}
262