Test Setup Failed
Push — master ( 1f58dd...e46154 )
by
unknown
31:29
created

tests/phpunit/Unit/LingoParserTest.php (2 issues)

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
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
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
	 * Tests
90
	 *
91
	 *
92
	 * @covers ::parse
93
	 * @dataProvider parseProvider
94
	 */
95
	public function testParse( $config ) {
96
		// Setup
97
		$config += self::$defaultTestConfig;
98
99
		$mwParser = $this->getParserMock( $config );
100
		$backend = $this->getBackendMock();
101
102
		$parser = new LingoParser();
103
		$parser->setBackend( $backend );
104
105
		$GLOBALS[ 'wgLingoPageName' ] = 'SomePage';
106
		$GLOBALS[ 'wgexLingoUseNamespaces' ] = $config[ 'wgexLingoUseNamespaces' ];
107
108
		// Run
109
		$ret = $parser->parse( $mwParser );
110
111
		// Check
112
		$this->assertTrue( $ret );
113
114
		// Teardown
115
	}
116
117
	/**
118
	 * @return array
119
	 */
120
	public function parseProvider() {
121
		return [
122
			// trivial case where $wgParser being unset should at least not raise any exceptions
123
			[ [ 'mwParser' => null ] ],
124
125
			// Lingo parser does not start parsing (i.e. accesses parser output) when __NOGLOSSARY__ is set
126
			[ [
127
				'mwParserExpectsGetOutput' => $this->never(),
128
				'mwParserProperties' => [ 'mDoubleUnderscores' => [ 'noglossary' => true ] ],
129
			] ],
130
131
			// Lingo parser does not start parsing (i.e. accesses parser output) when parsed Page is unknown
132
			[ [
133
				'mwParserExpectsGetOutput' => $this->never(),
134
				'mwTitle' => null
135
			] ],
136
137
			// Lingo parser does not start parsing (i.e. accesses parser output) when parsed Page is in explicitly forbidden namespace
138
			[ [
139
				'mwParserExpectsGetOutput' => $this->never(),
140
				'namespace' => 100,
141
				'wgexLingoUseNamespaces' => [ 100 => false ],
142
			] ],
143
144
			// Lingo parser starts parsing (i.e. accesses parser output) when parsed Page is in explicitly allowed namespace
145
			[ [
146
				'mwParserExpectsGetOutput' => $this->once(),
147
				'namespace' => 100,
148
				'wgexLingoUseNamespaces' => [ 100 => true ],
149
			] ],
150
151
			// Lingo parser starts parsing (i.e. accesses parser output) when parsed Page is not in explicitly forbidden namespace
152
			[ [
153
				'mwParserExpectsGetOutput' => $this->once(),
154
				'namespace' => 100,
155
				'wgexLingoUseNamespaces' => [ 101 => false ],
156
			] ],
157
158
			// Not a real test. Just make sure that it does not break right away.
159
			[ [
160
				'text' => 'foo',
161
			] ],
162
163
		];
164
	}
165
166
	/**
167
	 * @return \PHPUnit_Framework_MockObject_MockObject
0 ignored issues
show
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...
168
	 */
169
	protected function getParserMock( $config = [] ) {
170
		if ( array_key_exists( 'mwParser', $config ) ) {
171
			return $config[ 'mwParser' ];
172
		}
173
174
		$mwTitle = $this->getTitleMock( $config );
175
176
		$mwParserOutput = $this->getMockBuilder( '\ParserOutput' )
177
			->disableOriginalConstructor()
178
			->getMock();
179
180
		$mwParser = $this->getMockBuilder( '\Parser' )
181
			->disableOriginalConstructor()
182
			->getMock();
183
184
		$mwParserOutput->expects( $config[ 'mwOutputExpectsGetText' ] ?: $this->any() )
185
			->method( 'getText' )
186
			->willReturn( $config[ 'text' ] );
187
188
		$mwParser->expects( $config[ 'mwParserExpectsGetTitle' ] ?: $this->any() )
189
			->method( 'getTitle' )
190
			->willReturn( $mwTitle );
191
192
		$mwParser->expects( $config[ 'mwParserExpectsGetOutput' ] ?: $this->any() )
193
			->method( 'getOutput' )
194
			->willReturn( $mwParserOutput );
195
196
		foreach ( $config[ 'mwParserProperties' ] as $propName => $propValue ) {
197
			$mwParser->$propName = $propValue;
198
		}
199
200
		return $mwParser;
201
	}
202
203
	/**
204
	 * @param $config
205
	 *
206
	 * @return \PHPUnit_Framework_MockObject_MockObject
207
	 */
208
	protected function getTitleMock( $config ) {
209
		if ( array_key_exists( 'mwTitle', $config ) ) {
210
			return $config[ 'mwTitle' ];
211
		}
212
213
		$mwTitle = $this->getMockBuilder( '\Title' )
214
			->disableOriginalConstructor()
215
			->getMock();
216
217
		$mwTitle->expects( $config[ 'mwTitleExpectsGetNamespace' ] ?: $this->any() )
218
			->method( 'getNamespace' )
219
			->willReturn( $config[ 'namespace' ] );
220
221
		return $mwTitle;
222
	}
223
224
	/**
225
	 * @return \PHPUnit_Framework_MockObject_MockObject
226
	 */
227
	protected function getBackendMock() {
228
		$backend = $this->getMockBuilder( 'Lingo\BasicBackend' )
229
			->disableOriginalConstructor()
230
			->setMethods( [
231
				'getLatestRevisionFromTitle',
232
				'getApprovedRevisionFromTitle',
233
				'getTitleFromText',
234
			] )
235
			->getMock();
236
237
		$lingoPageTitle = $this->getMockBuilder( 'Title' )
238
			->getMock();
239
		$lingoPageTitle->expects( $this->any() )
240
			->method( 'getInterwiki' )
241
			->willReturn( '' );
242
		$lingoPageTitle->expects( $this->any() )
243
			->method( 'getArticleID' )
244
			->willReturn( 'Foom' );
245
246
		$backend->expects( $this->any() )
247
			->method( 'getTitleFromText' )
248
			->willReturn( $lingoPageTitle );
249
250
		return $backend;
251
	}
252
253
}
254