Passed
Push — master ( ff1e99...d91bed )
by
unknown
31:19
created

LingoParserTest::getBackendMock()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

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