LingoParserTest::testGetInstance()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 9
rs 10
1
<?php
2
/**
3
 * This file is part of the MediaWiki extension Lingo.
4
 *
5
 * @copyright 2011 - 2018, Stephan Gambke
6
 * @license GPL-2.0-or-later
7
 *
8
 * The Lingo extension is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU General Public License as published by the Free
10
 * Software Foundation; either version 2 of the License, or (at your option) any
11
 * later version.
12
 *
13
 * The Lingo extension is distributed in the hope that it will be useful, but
14
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
16
 * details.
17
 *
18
 * You should have received a copy of the GNU General Public License along
19
 * with this program. If not, see <http://www.gnu.org/licenses/>.
20
 *
21
 * @author Stephan Gambke
22
 * @since 2.0
23
 * @file
24
 * @ingroup Lingo
25
 */
26
27
namespace Lingo\Tests\Unit;
28
29
use Lingo\LingoParser;
30
use PHPUnit\Framework\MockObject\MockObject;
0 ignored issues
show
Bug introduced by
The type PHPUnit\Framework\MockObject\MockObject was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
31
32
/**
33
 * @group extensions-lingo
34
 * @group extensions-lingo-unit
35
 * @group mediawiki-databaseless
36
 *
37
 * @coversDefaultClass \Lingo\LingoParser
38
 * @covers ::<private>
39
 * @covers ::<protected>
40
 *
41
 * @ingroup Lingo
42
 * @ingroup Test
43
 */
44
class LingoParserTest extends \PHPUnit\Framework\TestCase {
0 ignored issues
show
Bug introduced by
The type PHPUnit\Framework\TestCase was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
45
46
	private static $defaultTestConfig = [
47
		'mwParserExpectsGetOutput' => null,
48
		'mwParserExpectsGetTitle' => null,
49
		'mwTitleExpectsGetNamespace' => null,
50
		'mwOutputExpectsGetText' => null,
51
52
		'mwParserProperties' => [],
53
54
		'namespace' => 0,
55
		'text' => null,
56
57
		'wgexLingoUseNamespaces' => [],
58
		'wgexLingoBackend' => 'Lingo\\BasicBackend',
59
	];
60
61
	/**
62
	 * @covers ::__construct
63
	 */
64
	public function testCanConstruct() {
65
		$this->assertInstanceOf(
66
			'\Lingo\LingoParser',
67
			new \Lingo\LingoParser()
68
		);
69
	}
70
71
	/**
72
	 * This will NOT test the execution path in LingoParser::getInstance where the singleton is actually created as that
73
	 * path is executed during the initialisation of MW. It will test however that the singleton is of the correct class
74
	 * and that once created subsequent calls to LingoParser::getInstance will return the same object.
75
	 *
76
	 * @covers ::getInstance
77
	 */
78
	public function testGetInstance() {
79
		$singleton = LingoParser::getInstance();
80
81
		$this->assertInstanceOf(
82
			'\Lingo\LingoParser',
83
			$singleton
84
		);
85
86
		$this->assertEquals( $singleton, LingoParser::getInstance() );
87
	}
88
89
	/**
90
	 * Tests
91
	 *
92
	 *
93
	 * @covers ::parse
94
	 * @dataProvider parseProvider
95
	 */
96
	public function testParse( $config ) {
97
		// Setup
98
		$config += self::$defaultTestConfig;
99
100
		$mwParser = $this->getParserMock( $config );
101
		$backend = $this->getBackendMock();
102
103
		$parser = new LingoParser();
104
		$parser->setBackend( $backend );
105
106
		$GLOBALS[ 'wgLingoPageName' ] = 'SomePage';
107
		$GLOBALS[ 'wgexLingoUseNamespaces' ] = $config[ 'wgexLingoUseNamespaces' ];
108
109
		// Run
110
		$ret = $parser->parse( $mwParser );
111
112
		// Check
113
		$this->assertTrue( $ret );
114
115
		// Teardown
116
	}
117
118
	/**
119
	 * @return array
120
	 */
121
	public function parseProvider() {
122
		return [
123
124
			// Lingo parser does not start parsing (i.e. accesses parser output) when __NOGLOSSARY__ is set
125
			[ [
126
				'mwParserExpectsGetOutput' => $this->never(),
127
				'mwParserProperties' => [ 'mDoubleUnderscores' => [ 'noglossary' => true ] ],
128
			] ],
129
130
			// Lingo parser does not start parsing (i.e. accesses parser output) when parsed Page is unknown
131
			[ [
132
				'mwParserExpectsGetOutput' => $this->never(),
133
				'mwTitle' => null
134
			] ],
135
136
			// Lingo parser does not start parsing (i.e. accesses parser output) when parsed Page is in explicitly forbidden namespace
137
			[ [
138
				'mwParserExpectsGetOutput' => $this->never(),
139
				'namespace' => 100,
140
				'wgexLingoUseNamespaces' => [ 100 => false ],
141
			] ],
142
143
			// Lingo parser starts parsing (i.e. accesses parser output) when parsed Page is in explicitly allowed namespace
144
			[ [
145
				'mwParserExpectsGetOutput' => $this->once(),
146
				'namespace' => 100,
147
				'wgexLingoUseNamespaces' => [ 100 => true ],
148
			] ],
149
150
			// Lingo parser starts parsing (i.e. accesses parser output) when parsed Page is not in explicitly forbidden namespace
151
			[ [
152
				'mwParserExpectsGetOutput' => $this->once(),
153
				'namespace' => 100,
154
				'wgexLingoUseNamespaces' => [ 101 => false ],
155
			] ],
156
157
			// Not a real test. Just make sure that it does not break right away.
158
			[ [
159
				'text' => 'foo',
160
			] ],
161
162
		];
163
	}
164
165
	/**
166
	 * @return MockObject
167
	 */
168
	protected function getParserMock( $config = [] ) {
169
		if ( array_key_exists( 'mwParser', $config ) ) {
170
			return $config[ 'mwParser' ];
171
		}
172
173
		$mwTitle = $this->getTitleMock( $config );
174
175
		$mwParserOutput = $this->getMockBuilder( '\ParserOutput' )
176
			->disableOriginalConstructor()
177
			->getMock();
178
179
		$mwParser = $this->getMockBuilder( '\Parser' )
180
			->disableOriginalConstructor()
181
			->getMock();
182
183
		$mwParserOutput->expects( $config[ 'mwOutputExpectsGetText' ] ?: $this->any() )
184
			->method( 'getText' )
185
			->willReturn( $config[ 'text' ] );
186
187
		$mwParser->expects( $config[ 'mwParserExpectsGetTitle' ] ?: $this->any() )
188
			->method( 'getTitle' )
189
			->willReturn( $mwTitle );
190
191
		$mwParser->expects( $config[ 'mwParserExpectsGetOutput' ] ?: $this->any() )
192
			->method( 'getOutput' )
193
			->willReturn( $mwParserOutput );
194
195
		foreach ( $config[ 'mwParserProperties' ] as $propName => $propValue ) {
196
			$mwParser->$propName = $propValue;
197
		}
198
199
		return $mwParser;
200
	}
201
202
	/**
203
	 * @param $config
204
	 *
205
	 * @return MockObject
206
	 */
207
	protected function getTitleMock( $config ) {
208
		if ( array_key_exists( 'mwTitle', $config ) ) {
209
			return $config[ 'mwTitle' ];
210
		}
211
212
		$mwTitle = $this->getMockBuilder( '\Title' )
213
			->disableOriginalConstructor()
214
			->getMock();
215
216
		$mwTitle->expects( $config[ 'mwTitleExpectsGetNamespace' ] ?: $this->any() )
217
			->method( 'getNamespace' )
218
			->willReturn( $config[ 'namespace' ] );
219
220
		return $mwTitle;
221
	}
222
223
	/**
224
	 * @return MockObject
225
	 */
226
	protected function getBackendMock() {
227
		$backend = $this->getMockBuilder( 'Lingo\BasicBackend' )
228
			->disableOriginalConstructor()
229
			->setMethods( [
230
				'getLatestRevisionFromTitle',
231
				'getApprovedRevisionFromTitle',
232
				'getTitleFromText',
233
			] )
234
			->getMock();
235
236
		$lingoPageTitle = $this->getMockBuilder( 'Title' )
237
			->getMock();
238
		$lingoPageTitle->expects( $this->any() )
239
			->method( 'getInterwiki' )
240
			->willReturn( '' );
241
		$lingoPageTitle->expects( $this->any() )
242
			->method( 'getArticleID' )
243
			->willReturn( 'Foom' );
244
245
		$backend->expects( $this->any() )
246
			->method( 'getTitleFromText' )
247
			->willReturn( $lingoPageTitle );
248
249
		return $backend;
250
	}
251
252
}
253