Completed
Push — master ( 292636...4a8b61 )
by Tobias
09:54
created

testCanRenderErrorMessage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
dl 0
loc 11
rs 9.4285
c 1
b 1
f 0
cc 1
eloc 6
nc 1
nop 2
1
<?php
2
3
namespace BootstrapComponents\Tests\Unit;
4
5
use BootstrapComponents\ComponentLibrary;
6
use BootstrapComponents\ParserOutputHelper;
7
use BootstrapComponents\ParserRequest;
8
use \MWException;
9
use \Parser;
10
use \PHPUnit_Framework_MockObject_MockObject;
11
use \PHPUnit_Framework_TestCase;
12
13
/**
14
 * @covers  \BootstrapComponents\ParserOutputHelper
15
 *
16
 * @ingroup Test
17
 *
18
 * @group extension-bootstrap-components
19
 * @group mediawiki-databaseless
20
 *
21
 * @license GNU GPL v3+
22
 *
23
 * @since   1.0
24
 * @author  Tobias Oetterer
25
 */
26
class ParserOutputHelperTest extends PHPUnit_Framework_TestCase {
27
	/**
28
	 * @var Parser
29
	 */
30
	private $parser;
31
32
	public function setUp() {
33
		parent::setUp();
34
35
		$this->parser = $this->getMockBuilder( 'Parser' )
36
			->disableOriginalConstructor()
37
			->getMock();
38
	}
39
40
	public function testCanConstruct() {
41
42
		$this->assertInstanceOf(
43
			'BootstrapComponents\\ParserOutputHelper',
44
			new ParserOutputHelper( $this->parser )
45
		);
46
	}
47
48
	public function testCanAddErrorTrackingCategory() {
49
50
		$parser = $this->getMockBuilder( 'Parser' )
51
			->disableOriginalConstructor()
52
			->getMock();
53
		$parser->expects( $this->once() )
54
			->method( 'getOutput' )
55
			->willReturn( false );
56
57
		/** @noinspection PhpParamsInspection */
58
		$instance = new ParserOutputHelper( $parser );
59
60
		$instance->addErrorTrackingCategory();
61
		$instance->addErrorTrackingCategory();
62
	}
63
64
	/**
65
	 * This is so lame to test. Only reason to do this to up test coverage.
66
	 */
67
	public function testCanAddModules() {
68
		$parserOutput = $this->getMockBuilder( 'ParserOutput' )
69
			->disableOriginalConstructor()
70
			->getMock();
71
		$parserOutput->expects( $this->exactly( 4 ) )
72
			->method( 'addModules' )
73
			->will( $this->returnArgument( 0 ) );
74
		$parser = $this->getMockBuilder( 'Parser' )
75
			->disableOriginalConstructor()
76
			->getMock();
77
		$parser->expects( $this->exactly( 4 ) )
78
			->method( 'getOutput' )
79
			->willReturn( $parserOutput );
80
81
		/** @noinspection PhpParamsInspection */
82
		$instance = new ParserOutputHelper( $parser );
83
84
		$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...
85
86
		$instance->addModules( [] );
87
88
		/** @noinspection PhpParamsInspection */
89
		$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...
90
91
		$instance->addModules( [ 'module1', 'module2' ] );
92
	}
93
94
	public function testCanAddTrackingCategory() {
95
		$parser = $this->getMockBuilder( 'Parser' )
96
			->disableOriginalConstructor()
97
			->getMock();
98
		$parser->expects( $this->once() )
99
			->method( 'getOutput' )
100
			->willReturn( false );
101
102
		/** @noinspection PhpParamsInspection */
103
		$instance = new ParserOutputHelper( $parser );
104
105
		$instance->addTrackingCategory();
106
		$instance->addTrackingCategory();
107
	}
108
109
	/**
110
	 * @param mixed $storedText
111
	 * @param string $expectedReturn
112
	 *
113
	 * @dataProvider contentForLaterInjectionProvider
114
	 */
115
	public function testCanGetContentForLaterInjection( $storedText, $expectedReturn ) {
116
		$instance = new ParserOutputHelper( $this->parser );
117
118
		$instance->injectLater( $storedText );
119
120
		$this->assertEquals(
121
			$expectedReturn,
122
			$instance->getContentForLaterInjection()
123
		);
124
	}
125
126
	public function testCanGetNameOfActiveSkin() {
127
		$instance = new ParserOutputHelper( $this->parser );
128
129
		$this->assertEquals(
130
			'vector',
131
			$instance->getNameOfActiveSkin()
132
		);
133
	}
134
135
	public function testCanLoadBootstrapModules() {
136
		$parserOutput = $this->getMockBuilder( 'ParserOutput' )
137
			->disableOriginalConstructor()
138
			->getMock();
139
		$parserOutput->expects( $this->once() )
140
			->method( 'addModuleStyles' )
141
			->will( $this->returnArgument( 0 ) );
142
		$parserOutput->expects( $this->once() )
143
			->method( 'addModuleScripts' )
144
			->will( $this->returnArgument( 0 ) );
145
		$parserOutput->expects( $this->once() )
146
			->method( 'addModules' )
147
			->will( $this->returnArgument( 0 ) );
148
		$parser = $this->getMockBuilder( 'Parser' )
149
			->disableOriginalConstructor()
150
			->getMock();
151
		$parser->expects( $this->once() )
152
			->method( 'getOutput' )
153
			->willReturn( $parserOutput );
154
155
		/** @noinspection PhpParamsInspection */
156
		$instance = new ParserOutputHelper( $parser );
157
158
		$instance->loadBootstrapModules();
159
	}
160
161
	/**
162
	 * @param string $messageText
163
	 * @param string $renderedMessage
164
	 *
165
	 * @dataProvider errorMessageProvider
166
	 */
167
	public function testCanRenderErrorMessage( $messageText, $renderedMessage ) {
168
		/** @noinspection PhpParamsInspection */
169
		$instance = new ParserOutputHelper(
170
			$this->buildFullyEquippedParser( ( $renderedMessage != '~^$~' ) )
171
		);
172
173
		$this->assertRegExp(
174
			$renderedMessage,
175
			$instance->renderErrorMessage( $messageText )
176
		);
177
	}
178
179
	public function testVectorSkinInUse() {
180
		$instance = new ParserOutputHelper( $this->parser );
181
		$this->assertInternalType(
182
			'bool',
183
			$instance->vectorSkinInUse()
184
		);
185
	}
186
187
	/**
188
	 * @throws \ReflectionException
189
	 */
190
	public function testPrivateCanDetectSkinInUse() {
191
		$instance = new ParserOutputHelper( $this->parser );
192
193
		$reflection = new \ReflectionClass( ParserOutputHelper::class );
194
		$method = $reflection->getMethod( 'detectSkinInUse' );
195
		$method->setAccessible( true );
196
197
		// this is default
198
		$this->assertEquals(
199
			'vector',
200
			$method->invokeArgs( $instance, [ false ] )
201
		);
202
203
		// this was introduced due to issue #9
204
		$this->assertEquals(
205
			'vector',
206
			$method->invokeArgs( $instance, [ true ] )
207
		);
208
	}
209
210
	/**
211
	 * @return array[]
212
	 *
213
	 * @throws \ConfigException
214
	 * @throws MWException
215
	 */
216
	public function componentNameAndClassProvider() {
217
		$cl = new ComponentLibrary();
218
		$provider = [];
219
		foreach ( $cl->getRegisteredComponents() as $componentName ) {
220
			$provider['open ' . $componentName] = [ $componentName, $cl->getClassFor( $componentName ) ];
221
		}
222
		return $provider;
223
	}
224
225
	/**
226
	 * @return array[]
227
	 */
228
	public function errorMessageProvider() {
229
		return [
230
			'null'       => [ null, '~^$~' ],
231
			'false'      => [ false, '~^$~' ],
232
			'none'       => [ '', '~^$~' ],
233
			'empty'      => [ '      ', '~^$~' ],
234
			'word'       => [ '__rndErrorMessageTextNotInMessageFiles', '~^<span class="error">[^_]+__rndErrorMessageTextNotInMessageFiles[^<]+</span>$~' ],
235
			'word space' => [ '  __rndErrorMessageTextNotInMessageFiles  ', '~^<span class="error">[^_]+__rndErrorMessageTextNotInMessageFiles[^<]+</span>$~' ],
236
		];
237
	}
238
239
	/**
240
	 * @return array
241
	 */
242
	public function contentForLaterInjectionProvider() {
243
		return [
244
			'none' => [ '', '' ],
245
			'false' => [ false, '' ],
246
			'null' => [ false, '' ],
247
			'string' => [ 'text', '<!-- injected by Extension:BootstrapComponents -->text<!-- /injected by Extension:BootstrapComponents -->' ],
248
		];
249
	}
250
251
	/**
252
	 * @param bool $expectError
253
	 *
254
	 * @return PHPUnit_Framework_MockObject_MockObject
255
	 */
256
	private function buildFullyEquippedParser( $expectError = true ) {
257
		$outputPropertyReturnString = 'rnd_string';
258
		$parser = $this->getMockBuilder( 'Parser' )
259
			->disableOriginalConstructor()
260
			->getMock();
261
		if ( $expectError ) {
262
			$parserOutput = $this->getMockBuilder( 'ParserOutput' )
263
				->disableOriginalConstructor()
264
				->getMock();
265
			$parserOutput->expects( $this->once() )
266
				->method( 'getProperty' )
267
				->with(
268
					$this->equalTo( 'defaultsort' )
269
				)
270
				->willReturn( $outputPropertyReturnString );
271
			$parserOutput->expects( $this->once() )
272
				->method( 'addCategory' )
273
				->with(
274
					$this->equalTo( 'Pages_with_bootstrap_component_errors' ),
275
					$this->equalTo( $outputPropertyReturnString )
276
				)
277
				->willReturn( $parserOutput );
278
			$parser->expects( $this->once() )
279
				->method( 'getOutput' )
280
				->willReturn( $parserOutput );
281
		}
282
283
		return $parser;
284
	}
285
}
286